Use Merkle root hash in proof-of-work
This commit is contained in:
parent
668d209f5e
commit
8cafc0ef1e
10
block.go
10
block.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
@ -36,15 +35,14 @@ func NewGenesisBlock(coinbase *Transaction) *Block {
|
||||||
|
|
||||||
// HashTransactions returns a hash of the transactions in the block
|
// HashTransactions returns a hash of the transactions in the block
|
||||||
func (b *Block) HashTransactions() []byte {
|
func (b *Block) HashTransactions() []byte {
|
||||||
var txHashes [][]byte
|
var transactions [][]byte
|
||||||
var txHash [32]byte
|
|
||||||
|
|
||||||
for _, tx := range b.Transactions {
|
for _, tx := range b.Transactions {
|
||||||
txHashes = append(txHashes, tx.Hash())
|
transactions = append(transactions, tx.Serialize())
|
||||||
}
|
}
|
||||||
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
|
mTree := NewMerkleTree(transactions)
|
||||||
|
|
||||||
return txHash[:]
|
return mTree.RootNode.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize serializes the block
|
// Serialize serializes the block
|
||||||
|
|
Loading…
Reference in New Issue