blockchain_go/block.go

35 lines
650 B
Go

package main
import (
"time"
)
// Block keeps block headers
type Block struct {
Timestamp int64
Data []byte
PrevBlockHash []byte
Hash []byte
Nonce int
}
// Prove obtains proof of work
func (b *Block) Prove() {
nonce, hash := Prove(b)
b.Hash = hash[:]
b.Nonce = nonce
}
// NewBlock creates and returns Block
func NewBlock(data string, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}, 0}
block.Prove()
return block
}
// NewGenesisBlock creates and returns genesis Block
func NewGenesisBlock() *Block {
return NewBlock("Genesis Block", []byte{})
}