Auto merge of #4661 - therealyingtong:history-node-garbage-test, r=daira

Add test for garbage memory in history nodes

Tests that garbage memory in history nodes smaller than `NODE_SERIALIZED_LENGTH` have no effect when computing the `HistoryRoot` hash.
This commit is contained in:
Homu 2020-08-13 14:54:40 +00:00
commit 95a27e9fc9
1 changed files with 34 additions and 0 deletions

View File

@ -169,3 +169,37 @@ TEST(History, EpochBoundaries) {
EXPECT_EQ(view.GetHistoryLength(2), 0);
}
TEST(History, GarbageMemoryHash) {
const auto consensusBranchId = NetworkUpgradeInfo[Consensus::UPGRADE_HEARTWOOD].nBranchId;
FakeCoinsViewDB fakeDB;
CCoinsViewCache view(&fakeDB);
// Hash two history nodes
HistoryNode node0 = getLeafN(1);
HistoryNode node1 = getLeafN(2);
view.PushHistoryNode(consensusBranchId, node0);
view.PushHistoryNode(consensusBranchId, node1);
uint256 historyRoot = view.GetHistoryRoot(consensusBranchId);
// Change garbage memory and re-hash nodes
FakeCoinsViewDB fakeDBGarbage;
CCoinsViewCache viewGarbage(&fakeDBGarbage);
HistoryNode node0Garbage = getLeafN(1);
HistoryNode node1Garbage = getLeafN(2);
node0Garbage.bytes[NODE_SERIALIZED_LENGTH - 1] = node0.bytes[NODE_SERIALIZED_LENGTH - 1] ^ 1;
node1Garbage.bytes[NODE_SERIALIZED_LENGTH - 1] = node1.bytes[NODE_SERIALIZED_LENGTH - 1] ^ 1;
viewGarbage.PushHistoryNode(consensusBranchId, node0Garbage);
viewGarbage.PushHistoryNode(consensusBranchId, node1Garbage);
uint256 historyRootGarbage = viewGarbage.GetHistoryRoot(consensusBranchId);
// Check history root and garbage history root are equal
EXPECT_EQ(historyRoot, historyRootGarbage);
}