Implement blockchain as a storage of blocks

This commit is contained in:
Ivan Kuznetsov 2017-08-15 14:28:18 +07:00
parent 822bde77c9
commit 23982003a4
1 changed files with 28 additions and 8 deletions

36
main.go
View File

@ -44,12 +44,32 @@ func NewGenesisBlock() *Block {
return NewBlock("Genesis Block", []byte("0"))
}
func main() {
gb := NewGenesisBlock()
b1 := NewBlock("Send 1 BTC to Ivan", gb.hash)
fmt.Printf("%s\n", gb.Data)
fmt.Printf("%x\n", gb.hash)
fmt.Printf("%s\n", b1.Data)
fmt.Printf("%x\n", b1.hash)
// Blockchain keeps a sequence of Blocks
type Blockchain struct {
blocks []*Block
}
// AddBlock saves provided data as a block in the blockchain
func (bc *Blockchain) AddBlock(data string) {
prevBlock := bc.blocks[len(bc.blocks)-1]
newBlock := &Block{time.Now().Unix(), []byte(data), prevBlock.hash, []byte("")}
newBlock.SetHash()
bc.blocks = append(bc.blocks, newBlock)
}
// NewBlockchain creates a new Blockchain with genesis Block
func NewBlockchain() *Blockchain {
return &Blockchain{[]*Block{NewGenesisBlock()}}
}
func main() {
bc := NewBlockchain()
bc.AddBlock("Send 1 BTC to Ivan")
bc.AddBlock("Send 2 more BTC to Ivan")
for _, block := range bc.blocks {
fmt.Printf("%s\n", block.Data)
fmt.Printf("%x\n", block.hash)
}
}