22 lines
514 B
Go
22 lines
514 B
Go
package main
|
|
|
|
type Blockchain struct {
|
|
blocks []*Block
|
|
}
|
|
|
|
func NewBlockchain() *Blockchain {
|
|
return &Blockchain{[]*Block{NewGenesisBlock()}}
|
|
}
|
|
|
|
// NewGenesisBlock creates a new Genesis Block to start the blockchain
|
|
func NewGenesisBlock() *Block {
|
|
return NewBlock("Genesis Block", []byte{})
|
|
}
|
|
|
|
// AddBlock adds a block to the blockchain
|
|
func (bc *Blockchain) AddBlock(data string) {
|
|
prevBlock := bc.blocks[len(bc.blocks)-1]
|
|
newBlock := NewBlock(data, prevBlock.Hash)
|
|
bc.blocks = append(bc.blocks, newBlock)
|
|
}
|