Skip to content

Commit 5b77244

Browse files
committed
Merge pull request #6550
3b33ec8 Avoid duplicate CheckBlock checks (Pieter Wuille) 391dff1 Do not store Merkle branches in the wallet. (Pieter Wuille)
2 parents e04b2fa + 3b33ec8 commit 5b77244

12 files changed

Lines changed: 33 additions & 71 deletions

File tree

‎doc/release-notes.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ In this version, it is only enforced for peers that send protocol versions
105105
removed. It is recommended to update SPV clients to check for the `NODE_BLOOM`
106106
service bit for nodes that report versions newer than 70011.
107107

108+
Merkle branches removed from wallet
109+
-----------------------------------
110+
111+
Previously, every wallet transaction stored a Merkle branch to prove its
112+
presence in blocks. This wasn't being used for more than an expensive
113+
sanity check. Since 0.12, these are no longer stored. When loading a
114+
0.12 wallet into an older version, it will automatically rescan to avoid
115+
failed checks.
116+
108117
0.12.0 Change log
109118
=================
110119

‎src/chainparams.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
3131
genesis.nVersion = nVersion;
3232
genesis.vtx.push_back(txNew);
3333
genesis.hashPrevBlock.SetNull();
34-
genesis.hashMerkleRoot = genesis.BuildMerkleTree();
34+
genesis.hashMerkleRoot = genesis.ComputeMerkleRoot();
3535
return genesis;
3636
}
3737

‎src/core_memusage.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static inline size_t RecursiveDynamicUsage(const CMutableTransaction& tx) {
4848
}
4949

5050
static inline size_t RecursiveDynamicUsage(const CBlock& block) {
51-
size_t mem = memusage::DynamicUsage(block.vtx) + memusage::DynamicUsage(block.vMerkleTree);
51+
size_t mem = memusage::DynamicUsage(block.vtx);
5252
for (std::vector<CTransaction>::const_iterator it = block.vtx.begin(); it != block.vtx.end(); it++) {
5353
mem += RecursiveDynamicUsage(*it);
5454
}

‎src/main.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,9 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
25832583
{
25842584
// These are checks that are independent of context.
25852585

2586+
if (block.fChecked)
2587+
return true;
2588+
25862589
// Check that the header is valid (particularly PoW). This is mostly
25872590
// redundant with the call in AcceptBlockHeader.
25882591
if (!CheckBlockHeader(block, state, fCheckPOW))
@@ -2591,7 +2594,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
25912594
// Check the merkle root.
25922595
if (fCheckMerkleRoot) {
25932596
bool mutated;
2594-
uint256 hashMerkleRoot2 = block.BuildMerkleTree(&mutated);
2597+
uint256 hashMerkleRoot2 = block.ComputeMerkleRoot(&mutated);
25952598
if (block.hashMerkleRoot != hashMerkleRoot2)
25962599
return state.DoS(100, error("CheckBlock(): hashMerkleRoot mismatch"),
25972600
REJECT_INVALID, "bad-txnmrklroot", true);
@@ -2638,6 +2641,9 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
26382641
return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"),
26392642
REJECT_INVALID, "bad-blk-sigops", true);
26402643

2644+
if (fCheckPOW && fCheckMerkleRoot)
2645+
block.fChecked = true;
2646+
26412647
return true;
26422648
}
26432649

‎src/miner.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned
368368
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
369369

370370
pblock->vtx[0] = txCoinbase;
371-
pblock->hashMerkleRoot = pblock->BuildMerkleTree();
371+
pblock->hashMerkleRoot = pblock->ComputeMerkleRoot();
372372
}
373373

374374
//////////////////////////////////////////////////////////////////////////////

‎src/primitives/block.cpp‎

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ uint256 CBlockHeader::GetHash() const
1515
return SerializeHash(*this);
1616
}
1717

18-
uint256 CBlock::BuildMerkleTree(bool* fMutated) const
18+
uint256 CBlock::ComputeMerkleRoot(bool* fMutated) const
1919
{
2020
/* WARNING! If you're reading this because you're learning about crypto
2121
and/or designing a new system that will use merkle trees, keep in mind
@@ -52,7 +52,7 @@ uint256 CBlock::BuildMerkleTree(bool* fMutated) const
5252
known ways of changing the transactions without affecting the merkle
5353
root.
5454
*/
55-
vMerkleTree.clear();
55+
std::vector<uint256> vMerkleTree;
5656
vMerkleTree.reserve(vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
5757
for (std::vector<CTransaction>::const_iterator it(vtx.begin()); it != vtx.end(); ++it)
5858
vMerkleTree.push_back(it->GetHash());
@@ -78,37 +78,6 @@ uint256 CBlock::BuildMerkleTree(bool* fMutated) const
7878
return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
7979
}
8080

81-
std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
82-
{
83-
if (vMerkleTree.empty())
84-
BuildMerkleTree();
85-
std::vector<uint256> vMerkleBranch;
86-
int j = 0;
87-
for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
88-
{
89-
int i = std::min(nIndex^1, nSize-1);
90-
vMerkleBranch.push_back(vMerkleTree[j+i]);
91-
nIndex >>= 1;
92-
j += nSize;
93-
}
94-
return vMerkleBranch;
95-
}
96-
97-
uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
98-
{
99-
if (nIndex == -1)
100-
return uint256();
101-
for (std::vector<uint256>::const_iterator it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
102-
{
103-
if (nIndex & 1)
104-
hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
105-
else
106-
hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
107-
nIndex >>= 1;
108-
}
109-
return hash;
110-
}
111-
11281
std::string CBlock::ToString() const
11382
{
11483
std::stringstream s;
@@ -123,9 +92,5 @@ std::string CBlock::ToString() const
12392
{
12493
s << " " << vtx[i].ToString() << "\n";
12594
}
126-
s << " vMerkleTree: ";
127-
for (unsigned int i = 0; i < vMerkleTree.size(); i++)
128-
s << " " << vMerkleTree[i].ToString();
129-
s << "\n";
13095
return s.str();
13196
}

‎src/primitives/block.h‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class CBlock : public CBlockHeader
7878
std::vector<CTransaction> vtx;
7979

8080
// memory only
81-
mutable std::vector<uint256> vMerkleTree;
81+
mutable bool fChecked;
8282

8383
CBlock()
8484
{
@@ -103,7 +103,7 @@ class CBlock : public CBlockHeader
103103
{
104104
CBlockHeader::SetNull();
105105
vtx.clear();
106-
vMerkleTree.clear();
106+
fChecked = false;
107107
}
108108

109109
CBlockHeader GetBlockHeader() const
@@ -118,14 +118,12 @@ class CBlock : public CBlockHeader
118118
return block;
119119
}
120120

121-
// Build the in-memory merkle tree for this block and return the merkle root.
121+
// Build the merkle tree for this block and return the merkle root.
122122
// If non-NULL, *mutated is set to whether mutation was detected in the merkle
123123
// tree (a duplication of transactions in the block leading to an identical
124124
// merkle root).
125-
uint256 BuildMerkleTree(bool* mutated = NULL) const;
125+
uint256 ComputeMerkleRoot(bool* mutated = NULL) const;
126126

127-
std::vector<uint256> GetMerkleBranch(int nIndex) const;
128-
static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
129127
std::string ToString() const;
130128
};
131129

‎src/test/miner_tests.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
8787
pblock->vtx[0] = CTransaction(txCoinbase);
8888
if (txFirst.size() < 2)
8989
txFirst.push_back(new CTransaction(pblock->vtx[0]));
90-
pblock->hashMerkleRoot = pblock->BuildMerkleTree();
90+
pblock->hashMerkleRoot = pblock->ComputeMerkleRoot();
9191
pblock->nNonce = blockinfo[i].nonce;
9292
CValidationState state;
9393
BOOST_CHECK(ProcessNewBlock(state, NULL, pblock, true, NULL));

‎src/test/pmt_tests.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE(pmt_test1)
4848
}
4949

5050
// calculate actual merkle root and height
51-
uint256 merkleRoot1 = block.BuildMerkleTree();
51+
uint256 merkleRoot1 = block.ComputeMerkleRoot();
5252
std::vector<uint256> vTxid(nTx, uint256());
5353
for (unsigned int j=0; j<nTx; j++)
5454
vTxid[j] = block.vtx[j].GetHash();

‎src/wallet/wallet.cpp‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,8 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletD
702702
wtx.hashBlock = wtxIn.hashBlock;
703703
fUpdated = true;
704704
}
705-
if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
705+
if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex))
706706
{
707-
wtx.vMerkleBranch = wtxIn.vMerkleBranch;
708707
wtx.nIndex = wtxIn.nIndex;
709708
fUpdated = true;
710709
}
@@ -2812,15 +2811,11 @@ int CMerkleTx::SetMerkleBranch(const CBlock& block)
28122811
break;
28132812
if (nIndex == (int)block.vtx.size())
28142813
{
2815-
vMerkleBranch.clear();
28162814
nIndex = -1;
28172815
LogPrintf("ERROR: SetMerkleBranch(): couldn't find tx in block\n");
28182816
return 0;
28192817
}
28202818

2821-
// Fill in merkle branch
2822-
vMerkleBranch = block.GetMerkleBranch(nIndex);
2823-
28242819
// Is the tx in a block that's in the main chain
28252820
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
28262821
if (mi == mapBlockIndex.end())
@@ -2846,14 +2841,6 @@ int CMerkleTx::GetDepthInMainChainINTERNAL(const CBlockIndex* &pindexRet) const
28462841
if (!pindex || !chainActive.Contains(pindex))
28472842
return 0;
28482843

2849-
// Make sure the merkle branch connects to this block
2850-
if (!fMerkleVerified)
2851-
{
2852-
if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
2853-
return 0;
2854-
fMerkleVerified = true;
2855-
}
2856-
28572844
pindexRet = pindex;
28582845
return chainActive.Height() - pindex->nHeight + 1;
28592846
}

0 commit comments

Comments
 (0)