@@ -49,6 +49,9 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
49
49
c1.InitCoinsDB (
50
50
/* cache_size_bytes=*/ 1 << 23 , /* in_memory=*/ true , /* should_wipe=*/ false );
51
51
WITH_LOCK (::cs_main, c1.InitCoinsCache (1 << 23 ));
52
+ c1.LoadGenesisBlock ();
53
+ BlockValidationState val_state;
54
+ BOOST_CHECK (c1.ActivateBestChain (val_state, nullptr ));
52
55
53
56
BOOST_CHECK (!manager.IsSnapshotActive ());
54
57
BOOST_CHECK (WITH_LOCK (::cs_main, return !manager.IsSnapshotValidated ()));
@@ -58,7 +61,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
58
61
auto & active_chain = WITH_LOCK (manager.GetMutex (), return manager.ActiveChain ());
59
62
BOOST_CHECK_EQUAL (&active_chain, &c1.m_chain );
60
63
61
- BOOST_CHECK_EQUAL (WITH_LOCK (manager.GetMutex (), return manager.ActiveHeight ()), - 1 );
64
+ BOOST_CHECK_EQUAL (WITH_LOCK (manager.GetMutex (), return manager.ActiveHeight ()), 0 );
62
65
63
66
auto active_tip = WITH_LOCK (manager.GetMutex (), return manager.ActiveTip ());
64
67
auto exp_tip = c1.m_chain .Tip ();
@@ -68,7 +71,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
68
71
69
72
// Create a snapshot-based chainstate.
70
73
//
71
- const uint256 snapshot_blockhash = GetRandHash ();
74
+ const uint256 snapshot_blockhash = active_tip-> GetBlockHash ();
72
75
Chainstate& c2 = WITH_LOCK (::cs_main, return manager.ActivateExistingSnapshot (
73
76
&mempool, snapshot_blockhash));
74
77
chainstates.push_back (&c2);
@@ -78,8 +81,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
78
81
c2.InitCoinsDB (
79
82
/* cache_size_bytes=*/ 1 << 23 , /* in_memory=*/ true , /* should_wipe=*/ false );
80
83
WITH_LOCK (::cs_main, c2.InitCoinsCache (1 << 23 ));
81
- // Unlike c1, which doesn't have any blocks. Gets us different tip, height.
82
- c2.LoadGenesisBlock ();
84
+ c2.m_chain .SetTip (*active_tip);
83
85
BlockValidationState _;
84
86
BOOST_CHECK (c2.ActivateBestChain (_, nullptr ));
85
87
@@ -99,16 +101,14 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
99
101
auto exp_tip2 = c2.m_chain .Tip ();
100
102
BOOST_CHECK_EQUAL (active_tip2, exp_tip2);
101
103
102
- // Ensure that these pointers actually correspond to different
103
- // CCoinsViewCache instances.
104
- BOOST_CHECK (exp_tip != exp_tip2);
104
+ BOOST_CHECK_EQUAL (exp_tip, exp_tip2);
105
105
106
106
// Let scheduler events finish running to avoid accessing memory that is going to be unloaded
107
107
SyncWithValidationInterfaceQueue ();
108
108
}
109
109
110
110
// ! Test rebalancing the caches associated with each chainstate.
111
- BOOST_AUTO_TEST_CASE (chainstatemanager_rebalance_caches)
111
+ BOOST_FIXTURE_TEST_CASE (chainstatemanager_rebalance_caches, TestChain100Setup )
112
112
{
113
113
ChainstateManager& manager = *m_node.chainman ;
114
114
CTxMemPool& mempool = *m_node.mempool ;
@@ -121,16 +121,14 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
121
121
122
122
// Create a legacy (IBD) chainstate.
123
123
//
124
- Chainstate& c1 = WITH_LOCK (::cs_main, return manager.InitializeChainstate (&mempool) );
124
+ Chainstate& c1 = manager.ActiveChainstate ( );
125
125
chainstates.push_back (&c1);
126
126
c1.InitCoinsDB (
127
127
/* cache_size_bytes=*/ 1 << 23 , /* in_memory=*/ true , /* should_wipe=*/ false );
128
128
129
129
{
130
130
LOCK (::cs_main);
131
131
c1.InitCoinsCache (1 << 23 );
132
- BOOST_REQUIRE (c1.LoadGenesisBlock ());
133
- c1.CoinsTip ().SetBestBlock (InsecureRand256 ());
134
132
manager.MaybeRebalanceCaches ();
135
133
}
136
134
@@ -139,16 +137,15 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
139
137
140
138
// Create a snapshot-based chainstate.
141
139
//
142
- Chainstate& c2 = WITH_LOCK (cs_main, return manager.ActivateExistingSnapshot (&mempool, GetRandHash ()));
140
+ CBlockIndex* snapshot_base{WITH_LOCK (manager.GetMutex (), return manager.ActiveChain ()[manager.ActiveChain ().Height () / 2 ])};
141
+ Chainstate& c2 = WITH_LOCK (cs_main, return manager.ActivateExistingSnapshot (&mempool, *snapshot_base->phashBlock ));
143
142
chainstates.push_back (&c2);
144
143
c2.InitCoinsDB (
145
144
/* cache_size_bytes=*/ 1 << 23 , /* in_memory=*/ true , /* should_wipe=*/ false );
146
145
147
146
{
148
147
LOCK (::cs_main);
149
148
c2.InitCoinsCache (1 << 23 );
150
- BOOST_REQUIRE (c2.LoadGenesisBlock ());
151
- c2.CoinsTip ().SetBestBlock (InsecureRand256 ());
152
149
manager.MaybeRebalanceCaches ();
153
150
}
154
151
@@ -430,6 +427,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
430
427
const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
431
428
432
429
CBlockIndex* validated_tip{nullptr };
430
+ CBlockIndex* assumed_base{nullptr };
433
431
CBlockIndex* assumed_tip{WITH_LOCK (chainman.GetMutex (), return chainman.ActiveChain ().Tip ())};
434
432
435
433
auto reload_all_block_indexes = [&]() {
@@ -468,12 +466,20 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
468
466
validated_tip = index;
469
467
BOOST_CHECK (!index->IsAssumedValid ());
470
468
}
469
+ // Note the block after the last assumed valid block as the snapshot base
470
+ if (i == last_assumed_valid_idx) {
471
+ assumed_base = index;
472
+ BOOST_CHECK (!index->IsAssumedValid ());
473
+ }
471
474
}
472
475
473
476
BOOST_CHECK_EQUAL (expected_assumed_valid, num_assumed_valid);
474
477
475
478
Chainstate& cs2 = WITH_LOCK (::cs_main,
476
- return chainman.ActivateExistingSnapshot (&mempool, GetRandHash ()));
479
+ return chainman.ActivateExistingSnapshot (&mempool, *assumed_base->phashBlock ));
480
+
481
+ // Set tip of the fully validated chain to be the validated tip
482
+ cs1.m_chain .SetTip (*validated_tip);
477
483
478
484
reload_all_block_indexes ();
479
485
0 commit comments