Skip to content

Commit

Permalink
multi: replace usage of BuildMerkleTreeStore with CalcMerkleRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Apr 23, 2019
1 parent 178084d commit c3ee68e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 20 deletions.
3 changes: 1 addition & 2 deletions blockchain/fullblocktests/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ func calcMerkleRoot(txns []*wire.MsgTx) chainhash.Hash {
for _, tx := range txns {
utilTxns = append(utilTxns, btcutil.NewTx(tx))
}
merkles := blockchain.BuildMerkleTreeStore(utilTxns, false)
return *merkles[len(merkles)-1]
return blockchain.CalcMerkleRoot(utilTxns, false)
}

// solveBlock attempts to find a nonce which makes the passed block header hash
Expand Down
3 changes: 1 addition & 2 deletions blockchain/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ func ValidateWitnessCommitment(blk *btcutil.Block) error {
// the extracted witnessCommitment is equal to:
// SHA256(witnessMerkleRoot || witnessNonce). Where witnessNonce is the
// coinbase transaction's only witness item.
witnessMerkleTree := BuildMerkleTreeStore(blk.Transactions(), true)
witnessMerkleRoot := witnessMerkleTree[len(witnessMerkleTree)-1]
witnessMerkleRoot := CalcMerkleRoot(blk.Transactions(), true)

var witnessPreimage [chainhash.HashSize * 2]byte
copy(witnessPreimage[:], witnessMerkleRoot[:])
Expand Down
7 changes: 3 additions & 4 deletions blockchain/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,11 @@ func checkBlockSanity(block *btcutil.Block, powLimit *big.Int, timeSource Median
// checks. Bitcoind builds the tree here and checks the merkle root
// after the following checks, but there is no reason not to check the
// merkle root matches here.
merkles := BuildMerkleTreeStore(block.Transactions(), false)
calculatedMerkleRoot := merkles[len(merkles)-1]
if !header.MerkleRoot.IsEqual(calculatedMerkleRoot) {
calcMerkleRoot := CalcMerkleRoot(block.Transactions(), false)
if !header.MerkleRoot.IsEqual(&calcMerkleRoot) {
str := fmt.Sprintf("block merkle root is invalid - block "+
"header indicates %v, but calculated value is %v",
header.MerkleRoot, calculatedMerkleRoot)
header.MerkleRoot, calcMerkleRoot)
return ruleError(ErrBadMerkleRoot, str)
}

Expand Down
3 changes: 1 addition & 2 deletions integration/rpctest/blockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,11 @@ func CreateBlock(prevBlock *btcutil.Block, inclusionTxs []*btcutil.Tx,
if inclusionTxs != nil {
blockTxns = append(blockTxns, inclusionTxs...)
}
merkles := blockchain.BuildMerkleTreeStore(blockTxns, false)
var block wire.MsgBlock
block.Header = wire.BlockHeader{
Version: blockVersion,
PrevBlock: *prevHash,
MerkleRoot: *merkles[len(merkles)-1],
MerkleRoot: blockchain.CalcMerkleRoot(blockTxns, false),
Timestamp: ts,
Bits: net.PowLimitBits,
}
Expand Down
11 changes: 4 additions & 7 deletions mining/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,7 @@ mempoolLoop:
// Next, obtain the merkle root of a tree which consists of the
// wtxid of all transactions in the block. The coinbase
// transaction will have a special wtxid of all zeroes.
witnessMerkleTree := blockchain.BuildMerkleTreeStore(blockTxns,
true)
witnessMerkleRoot := witnessMerkleTree[len(witnessMerkleTree)-1]
witnessMerkleRoot := blockchain.CalcMerkleRoot(blockTxns, true)

// The preimage to the witness commitment is:
// witnessRoot || coinbaseWitness
Expand Down Expand Up @@ -856,12 +854,11 @@ mempoolLoop:
}

// Create a new block ready to be solved.
merkles := blockchain.BuildMerkleTreeStore(blockTxns, false)
var msgBlock wire.MsgBlock
msgBlock.Header = wire.BlockHeader{
Version: nextBlockVersion,
PrevBlock: best.Hash,
MerkleRoot: *merkles[len(merkles)-1],
MerkleRoot: blockchain.CalcMerkleRoot(blockTxns, false),
Timestamp: ts,
Bits: reqDifficulty,
}
Expand Down Expand Up @@ -943,8 +940,8 @@ func (g *BlkTmplGenerator) UpdateExtraNonce(msgBlock *wire.MsgBlock, blockHeight

// Recalculate the merkle root with the updated extra nonce.
block := btcutil.NewBlock(msgBlock)
merkles := blockchain.BuildMerkleTreeStore(block.Transactions(), false)
msgBlock.Header.MerkleRoot = *merkles[len(merkles)-1]
merkleRoot := blockchain.CalcMerkleRoot(block.Transactions(), false)
msgBlock.Header.MerkleRoot = merkleRoot
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1631,8 +1631,8 @@ func (state *gbtWorkState) updateBlockTemplate(s *rpcServer, useCoinbaseValue bo

// Update the merkle root.
block := btcutil.NewBlock(template.Block)
merkles := blockchain.BuildMerkleTreeStore(block.Transactions(), false)
template.Block.Header.MerkleRoot = *merkles[len(merkles)-1]
merkleRoot := blockchain.CalcMerkleRoot(block.Transactions(), false)
template.Block.Header.MerkleRoot = merkleRoot
}

// Set locals for convenience.
Expand Down Expand Up @@ -4281,7 +4281,7 @@ func newRPCServer(config *rpcserverConfig) (*rpcServer, error) {
gbtWorkState: newGbtWorkState(config.TimeSource),
helpCacher: newHelpCacher(),
requestProcessShutdown: make(chan struct{}),
quit: make(chan int),
quit: make(chan int),
}
if cfg.RPCUser != "" && cfg.RPCPass != "" {
login := cfg.RPCUser + ":" + cfg.RPCPass
Expand Down

0 comments on commit c3ee68e

Please sign in to comment.