diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -6,6 +6,7 @@ #include #include +#include // For ChainstateActive() #include @@ -18,10 +19,15 @@ return it->peerid; } - // Reject invalid proof. - ProofValidationState state; - if (!proof.verify(state)) { - return NO_PEER; + { + // Reject invalid proof. + LOCK(cs_main); + const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); + + ProofValidationState state; + if (!proof.verify(state, coins)) { + return NO_PEER; + } } // We have no peer for this proof, time to create it. diff --git a/src/avalanche/test/peermanager_tests.cpp b/src/avalanche/test/peermanager_tests.cpp --- a/src/avalanche/test/peermanager_tests.cpp +++ b/src/avalanche/test/peermanager_tests.cpp @@ -12,7 +12,7 @@ using namespace avalanche; -BOOST_FIXTURE_TEST_SUITE(peermanager_tests, BasicTestingSetup) +BOOST_FIXTURE_TEST_SUITE(peermanager_tests, TestingSetup) BOOST_AUTO_TEST_CASE(select_peer_linear) { // No peers. diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include #include // For PeerLogicValidation @@ -69,13 +69,19 @@ const Config &config; CConnmanTest *m_connman; - AvalancheTestingSetup() : TestChain100Setup(), config(GetConfig()) { + CKey masterpriv; + + AvalancheTestingSetup() + : TestChain100Setup(), config(GetConfig()), masterpriv() { // Deterministic randomness for tests. auto connman = std::make_unique(config, 0x1337, 0x1337); m_connman = connman.get(); m_node.connman = std::move(connman); m_node.peer_logic = std::make_unique( m_connman, m_node.banman.get(), *m_node.scheduler); + + // The master private key we delegate to. + masterpriv.MakeNewKey(true); } ~AvalancheTestingSetup() { m_connman->ClearNodes(); } @@ -97,14 +103,26 @@ return node; } + size_t next_coinbase = 0; + Proof GetProof() { + size_t current_coinbase = next_coinbase++; + const CTransaction &coinbase = *m_coinbase_txns[current_coinbase]; + ProofBuilder pb(0, 0, masterpriv.GetPubKey()); + BOOST_CHECK(pb.addUTXO(COutPoint(coinbase.GetId(), 0), + coinbase.vout[0].nValue, current_coinbase + 1, + true, coinbaseKey)); + return pb.build(); + } + std::array ConnectNodes(Processor &p) { PeerManager &pm = AvalancheTest::getPeerManager(p); - Proof proof = buildRandomProof(100); + + Proof proof = GetProof(); std::array nodes; for (CNode *&n : nodes) { n = ConnectNode(NODE_AVALANCHE); - BOOST_CHECK(pm.addNode(n->GetId(), proof, CPubKey())); + BOOST_CHECK(pm.addNode(n->GetId(), proof, masterpriv.GetPubKey())); } return nodes; @@ -523,7 +541,7 @@ ConnectNode(NODE_NONE); auto avanode = ConnectNode(NODE_AVALANCHE); NodeId avanodeid = avanode->GetId(); - BOOST_CHECK(p.addNode(avanodeid, buildRandomProof(100), CPubKey())); + BOOST_CHECK(p.addNode(avanodeid, GetProof(), CPubKey())); // It returns the avalanche peer. BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), avanodeid); @@ -660,7 +678,7 @@ // Create a node that supports avalanche. auto avanode = ConnectNode(NODE_AVALANCHE); NodeId avanodeid = avanode->GetId(); - BOOST_CHECK(p.addNode(avanodeid, buildRandomProof(100), CPubKey())); + BOOST_CHECK(p.addNode(avanodeid, GetProof(), CPubKey())); // Expire requests after some time. auto queryTimeDuration = std::chrono::milliseconds(10); @@ -700,7 +718,7 @@ // Create enough nodes so that we run into the inflight request limit. PeerManager &pm = AvalancheTest::getPeerManager(p); - Proof proof = buildRandomProof(100); + Proof proof = GetProof(); std::array nodes; for (auto &n : nodes) { @@ -848,7 +866,7 @@ // Create a node that supports avalanche. auto avanode = ConnectNode(NODE_AVALANCHE); NodeId nodeid = avanode->GetId(); - BOOST_CHECK(p.addNode(nodeid, buildRandomProof(100), CPubKey())); + BOOST_CHECK(p.addNode(nodeid, GetProof(), CPubKey())); // There is no query in flight at the moment. BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), nodeid); diff --git a/src/avalanche/test/proof_tests.cpp b/src/avalanche/test/proof_tests.cpp --- a/src/avalanche/test/proof_tests.cpp +++ b/src/avalanche/test/proof_tests.cpp @@ -18,7 +18,7 @@ using namespace avalanche; -BOOST_FIXTURE_TEST_SUITE(proof_tests, BasicTestingSetup) +BOOST_FIXTURE_TEST_SUITE(proof_tests, TestingSetup) BOOST_AUTO_TEST_CASE(proof_random) { for (int i = 0; i < 1000; i++) { diff --git a/src/avalanche/test/util.cpp b/src/avalanche/test/util.cpp --- a/src/avalanche/test/util.cpp +++ b/src/avalanche/test/util.cpp @@ -8,6 +8,8 @@ #include #include #include +#include