diff --git a/src/Makefile.test.include b/src/Makefile.test.include --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -54,6 +54,7 @@ GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h) BITCOIN_TEST_SUITE = \ + avalanche/test/util.cpp \ test/jsonutil.cpp \ test/main.cpp \ test/scriptflags.cpp \ diff --git a/src/avalanche/proof.h b/src/avalanche/proof.h --- a/src/avalanche/proof.h +++ b/src/avalanche/proof.h @@ -122,12 +122,6 @@ uint32_t getScore() const; bool verify() const; - - /** - * Builds a randomized (and therefore invalid) Proof. - * Useful for tests. - */ - static Proof makeRandom(uint32_t score); }; } // namespace avalanche diff --git a/src/avalanche/proof.cpp b/src/avalanche/proof.cpp --- a/src/avalanche/proof.cpp +++ b/src/avalanche/proof.cpp @@ -6,9 +6,7 @@ #include // For SaltedOutpointHasher #include -#include -#include #include namespace avalanche { @@ -71,11 +69,4 @@ return true; } -Proof Proof::makeRandom(uint32_t score) { - return Proof(0, std::numeric_limits::max(), CPubKey(), - {{{COutPoint(TxId(GetRandHash()), 0), - (int64_t(score) * COIN) / 100, 0, CPubKey()}, - {}}}); -} - } // namespace avalanche diff --git a/src/avalanche/proofbuilder.h b/src/avalanche/proofbuilder.h --- a/src/avalanche/proofbuilder.h +++ b/src/avalanche/proofbuilder.h @@ -8,6 +8,8 @@ #include #include +#include + namespace avalanche { class ProofBuilder { diff --git a/src/avalanche/test/CMakeLists.txt b/src/avalanche/test/CMakeLists.txt --- a/src/avalanche/test/CMakeLists.txt +++ b/src/avalanche/test/CMakeLists.txt @@ -10,6 +10,7 @@ add_boost_unit_tests_to_suite(avalanche test-avalanche fixture.cpp + util.cpp TESTS peermanager_tests.cpp diff --git a/src/avalanche/test/fixture.cpp b/src/avalanche/test/fixture.cpp --- a/src/avalanche/test/fixture.cpp +++ b/src/avalanche/test/fixture.cpp @@ -3,5 +3,4 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #define BOOST_TEST_MODULE Avalanche Test Suite - #include 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 @@ -4,6 +4,8 @@ #include +#include + #include #include @@ -157,12 +159,12 @@ const NodeId node0 = 42, node1 = 69, node2 = 37; // One peer, we always return it. - Proof proof0 = Proof::makeRandom(100); - pm.addNode(node0, Proof::makeRandom(100), CPubKey()); + Proof proof0 = buildRandomProof(100); + pm.addNode(node0, buildRandomProof(100), CPubKey()); BOOST_CHECK_EQUAL(pm.selectNode(), node0); // Two peers, verify ratio. - pm.addNode(node1, Proof::makeRandom(200), CPubKey()); + pm.addNode(node1, buildRandomProof(200), CPubKey()); std::unordered_map results = {}; for (int i = 0; i < 10000; i++) { @@ -174,7 +176,7 @@ BOOST_CHECK(abs(2 * results[0] - results[1]) < 500); // Three peers, verify ratio. - pm.addNode(node2, Proof::makeRandom(100), CPubKey()); + pm.addNode(node2, buildRandomProof(100), CPubKey()); results.clear(); for (int i = 0; i < 10000; i++) { @@ -194,7 +196,7 @@ // Add 4 peers. std::array peerids; for (int i = 0; i < 4; i++) { - peerids[i] = pm.getPeer(Proof::makeRandom(100)); + peerids[i] = pm.getPeer(buildRandomProof(100)); } BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); @@ -224,7 +226,7 @@ // Add 4 more peers. for (int i = 0; i < 4; i++) { - peerids[i + 4] = pm.getPeer(Proof::makeRandom(100)); + peerids[i + 4] = pm.getPeer(buildRandomProof(100)); } BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); @@ -264,7 +266,7 @@ // Add 4 peers. std::array peerids; for (int i = 0; i < 4; i++) { - peerids[i] = pm.getPeer(Proof::makeRandom(100)); + peerids[i] = pm.getPeer(buildRandomProof(100)); } // Remove all peers. @@ -289,7 +291,7 @@ PeerManager pm; // Create one peer. - Proof proof = Proof::makeRandom(100); + Proof proof = buildRandomProof(100); BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); // Add 4 nodes. @@ -326,7 +328,7 @@ } // Move a node from a peer to another. - Proof altproof = Proof::makeRandom(0); + Proof altproof = buildRandomProof(0); BOOST_CHECK(pm.addNode(3, altproof, CPubKey())); for (int i = 0; i < 100; i++) { 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,6 +5,7 @@ #include #include +#include #include #include // For PeerLogicValidation #include @@ -210,7 +211,7 @@ PeerLogicValidation &peerLogic, CConnmanTest *connman) { PeerManager &pm = AvalancheTest::getPeerManager(p); - Proof proof = Proof::makeRandom(100); + Proof proof = buildRandomProof(100); std::array nodes; for (CNode *&n : nodes) { @@ -530,7 +531,7 @@ auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic, connman.get()); NodeId avanodeid = avanode->GetId(); - BOOST_CHECK(p.addNode(avanodeid, Proof::makeRandom(100), CPubKey())); + BOOST_CHECK(p.addNode(avanodeid, buildRandomProof(100), CPubKey())); // It returns the avalanche peer. BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), avanodeid); @@ -677,7 +678,7 @@ auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic, connman.get()); NodeId avanodeid = avanode->GetId(); - BOOST_CHECK(p.addNode(avanodeid, Proof::makeRandom(100), CPubKey())); + BOOST_CHECK(p.addNode(avanodeid, buildRandomProof(100), CPubKey())); // Expire requests after some time. auto queryTimeDuration = std::chrono::milliseconds(10); @@ -725,7 +726,7 @@ // Create enough nodes so that we run into the inflight request limit. PeerManager &pm = AvalancheTest::getPeerManager(p); - Proof proof = Proof::makeRandom(100); + Proof proof = buildRandomProof(100); std::array nodes; for (auto &n : nodes) { @@ -891,7 +892,7 @@ auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic, connman.get()); NodeId nodeid = avanode->GetId(); - BOOST_CHECK(p.addNode(nodeid, Proof::makeRandom(100), CPubKey())); + BOOST_CHECK(p.addNode(nodeid, buildRandomProof(100), 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 @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -16,6 +17,14 @@ BOOST_FIXTURE_TEST_SUITE(proof_tests, BasicTestingSetup) +BOOST_AUTO_TEST_CASE(proof_random) { + for (int i = 0; i < 1000; i++) { + const uint32_t score = InsecureRand32(); + const Proof p = buildRandomProof(score); + BOOST_CHECK_EQUAL(p.getScore(), score); + } +} + BOOST_AUTO_TEST_CASE(proofbuilder) { CKey key; @@ -297,12 +306,4 @@ } } -BOOST_AUTO_TEST_CASE(proof_random) { - for (int i = 0; i < 1000; i++) { - const uint32_t score = InsecureRand32(); - const Proof p = Proof::makeRandom(score); - BOOST_CHECK_EQUAL(p.getScore(), score); - } -} - BOOST_AUTO_TEST_SUITE_END() diff --git a/src/avalanche/test/util.h b/src/avalanche/test/util.h new file mode 100644 --- /dev/null +++ b/src/avalanche/test/util.h @@ -0,0 +1,18 @@ +// Copyright (c) 2020 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_AVALANCHE_TEST_UTIL_H +#define BITCOIN_AVALANCHE_TEST_UTIL_H + +#include + +#include + +namespace avalanche { + +Proof buildRandomProof(uint32_t score); + +} // namespace avalanche + +#endif // BITCOIN_AVALANCHE_TEST_UTIL_H diff --git a/src/avalanche/test/util.cpp b/src/avalanche/test/util.cpp new file mode 100644 --- /dev/null +++ b/src/avalanche/test/util.cpp @@ -0,0 +1,22 @@ +// Copyright (c) 2020 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include +#include +#include +#include + +#include + +namespace avalanche { + +Proof buildRandomProof(uint32_t score) { + return Proof(0, std::numeric_limits::max(), CPubKey(), + {{{COutPoint(TxId(GetRandHash()), 0), + (int64_t(score) * COIN) / 100, 0, CPubKey()}, + {}}}); +} + +} // namespace avalanche