12345678910111213141516171819202122232425262728293031323334353637 |
- package blockchain
- type BlockChain struct {
- Blocks []*Block
- }
- type Block struct {
- Hash []byte
- Data []byte
- PrevHash []byte
- Nonce int
- }
- func CreateBlock(data string, prevHash []byte) *Block {
- block := &Block{[]byte{}, []byte(data), prevHash, 0}
- pow := NewProof(block)
- nonce, hash := pow.Run()
- block.Hash = hash[:]
- block.Nonce = nonce
- return block
- }
- func (chain *BlockChain) AddBlock(data string) {
- prevBlock := chain.Blocks[len(chain.Blocks)-1]
- new := CreateBlock(data, prevBlock.Hash)
- chain.Blocks = append(chain.Blocks, new)
- }
- func Genesis() *Block {
- return CreateBlock("Genesis", []byte{})
- }
- func InitBlockChain() *BlockChain {
- return &BlockChain{[]*Block{Genesis()}}
- }
|