| 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering) |
| 172 | { |
| 173 | // build a large-ish chain that's likely to have some forks |
| 174 | std::vector<std::shared_ptr<const CBlock>> blocks; |
| 175 | while (blocks.size() < 50) { |
| 176 | blocks.clear(); |
| 177 | BuildChain(Params().GenesisBlock().GetHash(), 100, 15, 10, 500, blocks); |
| 178 | } |
| 179 | |
| 180 | bool ignored; |
| 181 | // Connect the genesis block and drain any outstanding events |
| 182 | BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(std::make_shared<CBlock>(Params().GenesisBlock()), true, true, &ignored)); |
| 183 | m_node.validation_signals->SyncWithValidationInterfaceQueue(); |
| 184 | |
| 185 | // subscribe to events (this subscriber will validate event ordering) |
| 186 | const CBlockIndex* initial_tip = nullptr; |
| 187 | { |
| 188 | LOCK(cs_main); |
| 189 | initial_tip = m_node.chainman->ActiveChain().Tip(); |
| 190 | } |
| 191 | auto sub = std::make_shared<TestSubscriber>(initial_tip->GetBlockHash()); |
| 192 | m_node.validation_signals->RegisterSharedValidationInterface(sub); |
| 193 | |
| 194 | // create a bunch of threads that repeatedly process a block generated above at random |
| 195 | // this will create parallelism and randomness inside validation - the ValidationInterface |
| 196 | // will subscribe to events generated during block validation and assert on ordering invariance |
| 197 | std::vector<std::thread> threads; |
| 198 | threads.reserve(10); |
| 199 | for (int i = 0; i < 10; i++) { |
| 200 | threads.emplace_back([&]() { |
| 201 | bool ignored; |
| 202 | FastRandomContext insecure; |
| 203 | for (int i = 0; i < 1000; i++) { |
| 204 | const auto& block = blocks[insecure.randrange(blocks.size() - 1)]; |
| 205 | Assert(m_node.chainman)->ProcessNewBlock(block, true, true, &ignored); |
| 206 | } |
| 207 | |
| 208 | // to make sure that eventually we process the full chain - do it here |
| 209 | for (const auto& block : blocks) { |
| 210 | if (block->vtx.size() == 1) { |
| 211 | bool processed = Assert(m_node.chainman)->ProcessNewBlock(block, true, true, &ignored); |
| 212 | assert(processed); |
| 213 | } |
| 214 | } |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | for (auto& t : threads) { |
| 219 | t.join(); |
| 220 | } |
| 221 | m_node.validation_signals->SyncWithValidationInterfaceQueue(); |
| 222 | |
| 223 | m_node.validation_signals->UnregisterSharedValidationInterface(sub); |
| 224 | |
| 225 | LOCK(cs_main); |
| 226 | BOOST_CHECK_EQUAL(sub->m_expected_tip, m_node.chainman->ActiveChain().Tip()->GetBlockHash()); |
| 227 | } |
| 228 |
nothing calls this directly
no test coverage detected