2017-08-15 02:29:52 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Block keeps block headers
|
|
|
|
type Block struct {
|
2017-08-17 01:04:56 -05:00
|
|
|
Timestamp int64
|
|
|
|
Data []byte
|
|
|
|
PrevBlockHash []byte
|
|
|
|
Hash []byte
|
2017-08-21 05:50:41 -05:00
|
|
|
Nonce int
|
2017-08-15 02:29:52 -05:00
|
|
|
}
|
|
|
|
|
2017-08-21 05:50:41 -05:00
|
|
|
// Prove obtains proof of work
|
|
|
|
func (b *Block) Prove() {
|
|
|
|
nonce, hash := Prove(b)
|
2017-08-15 02:29:52 -05:00
|
|
|
|
2017-08-16 01:07:30 -05:00
|
|
|
b.Hash = hash[:]
|
2017-08-21 05:50:41 -05:00
|
|
|
b.Nonce = nonce
|
2017-08-15 02:29:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2017-08-15 02:29:52 -05:00
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGenesisBlock creates and returns genesis Block
|
|
|
|
func NewGenesisBlock() *Block {
|
2017-08-16 02:12:44 -05:00
|
|
|
return NewBlock("Genesis Block", []byte{})
|
2017-08-15 02:29:52 -05:00
|
|
|
}
|