Rewrite reindex test to check beyond the max witness cache size

This commit is contained in:
Jack Grigg
2016-12-13 22:53:29 +13:00
parent 0752d1f8c4
commit 78f4e0ef37

View File

@@ -736,77 +736,77 @@ TEST(wallet_tests, CachedWitnessesDecrementFirst) {
TEST(wallet_tests, CachedWitnessesCleanIndex) { TEST(wallet_tests, CachedWitnessesCleanIndex) {
TestWallet wallet; TestWallet wallet;
CBlock block1; std::vector<CBlock> blocks;
CBlock block2; std::vector<CBlockIndex> indices;
CBlock block3; std::vector<JSOutPoint> notes;
CBlockIndex index1(block1); std::vector<uint256> anchors;
CBlockIndex index2(block2);
CBlockIndex index3(block3);
ZCIncrementalMerkleTree tree; ZCIncrementalMerkleTree tree;
ZCIncrementalMerkleTree riTree = tree;
std::vector<boost::optional<ZCIncrementalWitness>> witnesses;
auto sk = libzcash::SpendingKey::random(); auto sk = libzcash::SpendingKey::random();
wallet.AddSpendingKey(sk); wallet.AddSpendingKey(sk);
{ // Generate a chain
// First block (case tested in _empty_chain) size_t numBlocks = WITNESS_CACHE_SIZE + 10;
index1.nHeight = 1; blocks.resize(numBlocks);
CreateValidBlock(wallet, sk, index1, block1, tree); indices.resize(numBlocks);
for (size_t i = 0; i < numBlocks; i++) {
indices[i].nHeight = i;
auto old = tree.root();
auto jsoutpt = CreateValidBlock(wallet, sk, indices[i], blocks[i], tree);
EXPECT_NE(old, tree.root());
notes.push_back(jsoutpt);
witnesses.clear();
uint256 anchor;
wallet.GetNoteWitnesses(notes, witnesses, anchor);
for (size_t j = 0; j <= i; j++) {
EXPECT_TRUE((bool) witnesses[j]);
}
anchors.push_back(anchor);
} }
{ // Now pretend we are reindexing: the chain is cleared, and each block is
// Second block (case tested in _chain_tip) // used to increment witnesses again.
index2.nHeight = 2; for (size_t i = 0; i < numBlocks; i++) {
CreateValidBlock(wallet, sk, index2, block2, tree); ZCIncrementalMerkleTree riPrevTree {riTree};
} wallet.IncrementNoteWitnesses(&(indices[i]), &(blocks[i]), riTree);
{
// Third block
index3.nHeight = 3;
auto jsoutpt = CreateValidBlock(wallet, sk, index3, block3, tree);
std::vector<JSOutPoint> notes {jsoutpt};
std::vector<boost::optional<ZCIncrementalWitness>> witnesses;
uint256 anchor3;
wallet.GetNoteWitnesses(notes, witnesses, anchor3);
// Now pretend we are reindexing: the chain is cleared, and each block is
// used to increment witnesses again.
wallet.IncrementNoteWitnesses(&index1, &block1, tree);
uint256 anchor3a;
witnesses.clear(); witnesses.clear();
wallet.GetNoteWitnesses(notes, witnesses, anchor3a); uint256 anchor;
EXPECT_TRUE((bool) witnesses[0]); wallet.GetNoteWitnesses(notes, witnesses, anchor);
// Should equal third anchor because witness cache unaffected for (size_t j = 0; j < numBlocks; j++) {
EXPECT_EQ(anchor3, anchor3a); EXPECT_TRUE((bool) witnesses[j]);
}
// Should equal final anchor because witness cache unaffected
EXPECT_EQ(anchors.back(), anchor);
wallet.IncrementNoteWitnesses(&index2, &block2, tree); if ((i == 5) || (i == 50)) {
uint256 anchor3b; // Pretend a reorg happened that was recorded in the block files
witnesses.clear(); {
wallet.GetNoteWitnesses(notes, witnesses, anchor3b); wallet.DecrementNoteWitnesses(&(indices[i]));
EXPECT_TRUE((bool) witnesses[0]); witnesses.clear();
EXPECT_EQ(anchor3, anchor3b); uint256 anchor;
wallet.GetNoteWitnesses(notes, witnesses, anchor);
for (size_t j = 0; j < numBlocks; j++) {
EXPECT_TRUE((bool) witnesses[j]);
}
// Should equal final anchor because witness cache unaffected
EXPECT_EQ(anchors.back(), anchor);
}
// Pretend a reorg happened that was recorded in the block files {
wallet.DecrementNoteWitnesses(&index2); wallet.IncrementNoteWitnesses(&(indices[i]), &(blocks[i]), riPrevTree);
uint256 anchor3c; witnesses.clear();
witnesses.clear(); uint256 anchor;
wallet.GetNoteWitnesses(notes, witnesses, anchor3c); wallet.GetNoteWitnesses(notes, witnesses, anchor);
EXPECT_TRUE((bool) witnesses[0]); for (size_t j = 0; j < numBlocks; j++) {
EXPECT_EQ(anchor3, anchor3c); EXPECT_TRUE((bool) witnesses[j]);
}
wallet.IncrementNoteWitnesses(&index2, &block2, tree); // Should equal final anchor because witness cache unaffected
uint256 anchor3d; EXPECT_EQ(anchors.back(), anchor);
witnesses.clear(); }
wallet.GetNoteWitnesses(notes, witnesses, anchor3d); }
EXPECT_TRUE((bool) witnesses[0]);
EXPECT_EQ(anchor3, anchor3d);
wallet.IncrementNoteWitnesses(&index3, &block3, tree);
uint256 anchor3e;
witnesses.clear();
wallet.GetNoteWitnesses(notes, witnesses, anchor3e);
EXPECT_TRUE((bool) witnesses[0]);
EXPECT_EQ(anchor3, anchor3e);
} }
} }