|
|
|
@ -3,31 +3,27 @@ package main |
|
|
|
|
import ( |
|
|
|
|
"bytes" |
|
|
|
|
"crypto/sha256" |
|
|
|
|
"strconv" |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
|
const ( |
|
|
|
|
TOTAL_COINS = 21000000 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Block keeps block headers.
|
|
|
|
|
type Block struct { |
|
|
|
|
Timestamp int64 |
|
|
|
|
utxoPool []UTXOPool |
|
|
|
|
Transactions []*Transaction |
|
|
|
|
PrevBlockHash []byte |
|
|
|
|
Hash []byte |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//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{})
|
|
|
|
|
headers := bytes.Join([][]byte{b.PrevBlockHash, timestamp}, []byte{}) |
|
|
|
|
hash := sha256.Sum256(headers) |
|
|
|
|
// HashTransactions returns a hash of the transactions in the block
|
|
|
|
|
func (b *Block) HashTransactions() []byte { |
|
|
|
|
var txHashes [][]byte |
|
|
|
|
var txHash [32]byte |
|
|
|
|
|
|
|
|
|
b.Hash = hash[:] |
|
|
|
|
for _, tx := range b.Transactions { |
|
|
|
|
txHashes = append(txHashes, tx.ID) |
|
|
|
|
} |
|
|
|
|
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{})) |
|
|
|
|
return txHash[:] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewBlock creates and returns Block.
|
|
|
|
|