|
|
|
@ -7,7 +7,12 @@ import ( |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Block keeps block headers
|
|
|
|
|
// Constants
|
|
|
|
|
const ( |
|
|
|
|
TOTAL_COINS = 21000000 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Block keeps block headers.
|
|
|
|
|
type Block struct { |
|
|
|
|
Timestamp int64 |
|
|
|
|
utxoPool []UTXOPool |
|
|
|
@ -15,7 +20,7 @@ type Block struct { |
|
|
|
|
Hash []byte |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//SetHash calculates and sets block hash
|
|
|
|
|
//SetHash calculates and sets block hash.
|
|
|
|
|
func (b *Block) SetHash() { |
|
|
|
|
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10)) |
|
|
|
|
// headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
|
|
|
|
@ -25,7 +30,7 @@ func (b *Block) SetHash() { |
|
|
|
|
b.Hash = hash[:] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewBlock creates and returns Block
|
|
|
|
|
// NewBlock creates and returns Block.
|
|
|
|
|
func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block { |
|
|
|
|
|
|
|
|
|
block := &Block{time.Now().Unix(), utxoPool, prevBlockHash, []byte{}} |
|
|
|
@ -33,7 +38,10 @@ func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block { |
|
|
|
|
return block |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewGenesisBlock creates and returns genesis Block
|
|
|
|
|
// NewGenesisBlock creates and returns genesis Block.
|
|
|
|
|
func NewGenesisBlock() *Block { |
|
|
|
|
return NewBlock([]UTXOPool{}, []byte{}) |
|
|
|
|
genesisUTXOPool := UTXOPool{} |
|
|
|
|
genesisUTXOPool.utxos["genesis"] = TOTAL_COINS |
|
|
|
|
|
|
|
|
|
return NewBlock(genesisUTXOPool, []byte{}) |
|
|
|
|
} |
|
|
|
|