blockchain_go/block.go

35 lines
650 B
Go
Raw Normal View History

package main
import (
"time"
)
// Block keeps block headers
type Block struct {
Timestamp int64
Data []byte
PrevBlockHash []byte
Hash []byte
2017-08-21 05:50:41 -05:00
Nonce int
}
2017-08-21 05:50:41 -05:00
// Prove obtains proof of work
func (b *Block) Prove() {
nonce, hash := Prove(b)
2017-08-16 01:07:30 -05:00
b.Hash = hash[:]
2017-08-21 05:50:41 -05:00
b.Nonce = nonce
}
// NewBlock creates and returns Block
2017-08-17 01:06:42 -05:00
func NewBlock(data string, prevBlockHash []byte) *Block {
2017-08-21 05:50:41 -05:00
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{})
}