blockchain

command module
v0.0.0-...-bc5e9de Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 5 Imported by: 0

README

blockchain

trying to build a blockchain from scratch in Golang, maybe

Documentation

Overview

package main

import (

"crypto/sha256"
"fmt"

)

type Block struct {
	Hash     string `json:"hash"`
	PrevHash string `json:"prev_hash"`
	Data     string `json:"data"`
}

var Blockchain = []Block{Block{Hash: "genesis hash", PrevHash: "", Data: "first data block"}}

func main() {
	// simple blockchain implementation

	// 1. create genesis block
	// 2. create new block
	// 3. add new block to blockchain
	// 4. print blockchain
	// 5. validate blockchain
	// 6. change data in block
	// 7. validate blockchain

	AddingBlock("second data block")
	fmt.Printf("blockchain is valid: %v\n", Validate())
	AddingBlock("third data block")
	fmt.Printf("blockchain is valid: %v\n", Validate())
	AddingBlock("fourth data block")
	fmt.Printf("blockchain is valid: %v\n", Validate())

	fmt.Printf("blockchain: %v\n", Blockchain)
}

func AddingBlock(data string) {
	hashes := sha256.New()
	hashes.Write([]byte(Blockchain[len(Blockchain)-1].Hash + ":%:" + data))
	hash := hashes.Sum(nil)
	Blockchain = append(Blockchain, Block{Hash: string(hash), PrevHash: Blockchain[len(Blockchain)-1].Hash, Data: data})
}

func Validate() bool {
	for i := 1; i < len(Blockchain); i++ {
		if Blockchain[i].PrevHash != Blockchain[i-1].Hash {
			return false
		}
	}
	return true
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL