1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| package main
import ( "crypto/sha256" "encoding/json" "fmt" )
type Block struct { Index int Timestamp string Data string PreviousHash string Hash string }
func (b *Block) CalculateHash() string { s := fmt.Sprintf("%d%s%s%s", b.Index, b.PreviousHash, b.Timestamp, b.Data) sum := sha256.Sum256([]byte(s)) return fmt.Sprintf("%x", sum) }
type BlockChain struct { Chain []*Block }
func NewBlockChain() *BlockChain { bc := &BlockChain{} bc.Chain = append(bc.Chain, bc.CreateGenesisBlock()) return bc }
func (bc *BlockChain) CreateGenesisBlock() *Block { b := &Block{ Index: 0, Timestamp: "2018-01-01", Data: "Genesis Block", PreviousHash: "0", } b.Hash = b.CalculateHash() return b }
func (bc *BlockChain) GetLatestBlock() *Block { return bc.Chain[len(bc.Chain)-1] }
func (bc *BlockChain) AddBlock(newBlock *Block) { newBlock.Index = bc.GetLatestBlock().Index + 1 newBlock.PreviousHash = bc.GetLatestBlock().Hash newBlock.Hash = newBlock.CalculateHash() bc.Chain = append(bc.Chain, newBlock) }
func (bc *BlockChain) IsChainValid() bool { for i := 1; i < len(bc.Chain); i++ { currentBlock := bc.Chain[i] previousBlock := bc.Chain[i-1]
if currentBlock.Hash != currentBlock.CalculateHash() { return false }
if currentBlock.PreviousHash != previousBlock.Hash { return false } } return true }
func main() { bc := NewBlockChain() bc.AddBlock(&Block{Timestamp: "2018-02-01", Data: `{"amout":10}`}) bc.AddBlock(&Block{Timestamp: "2018-03-01", Data: `{"amout":20}`}) bc.AddBlock(&Block{Timestamp: "2018-04-01", Data: `{"amout":30}`})
blockchain, _ := json.MarshalIndent(bc, "", "\t") fmt.Println(string(blockchain))
fmt.Println("is BlockChain valid ", bc.IsChainValid()) }
|