From 8ff2d42231bf7800d838dc4e6dcdc32673ff2136 Mon Sep 17 00:00:00 2001 From: lateminer <9951982+lateminer@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:49:32 +0100 Subject: [PATCH] test: Fix some unit tests --- src/blockencodings.h | 2 +- src/test/addrman_tests.cpp | 14 ++++++------ src/test/blockencodings_tests.cpp | 3 ++- src/test/fs_tests.cpp | 2 +- src/test/pow_tests.cpp | 33 +++++++++++++++-------------- src/test/util/setup_common.cpp | 11 +++++----- src/test/validation_block_tests.cpp | 3 ++- 7 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/blockencodings.h b/src/blockencodings.h index 832a9fc300..d5fe1e74a5 100644 --- a/src/blockencodings.h +++ b/src/blockencodings.h @@ -138,12 +138,12 @@ class PartiallyDownloadedBlock { const ChainstateManager* chainman; public: CBlockHeader header; + std::vector vchBlockSig; // Can be overridden for testing using CheckBlockFn = std::function; CheckBlockFn m_check_block_mock{nullptr}; - std::vector vchBlockSig; explicit PartiallyDownloadedBlock(CTxMemPool* poolIn, ChainstateManager* _chainman) : pool(poolIn), chainman(_chainman) {} // extra_txn is a list of extra transactions to look at, in form diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index e319926d70..12b36844e6 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -117,11 +117,11 @@ BOOST_AUTO_TEST_CASE(addrman_ports) BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source)); BOOST_CHECK_EQUAL(addrman->Size(), 1U); - CService addr1_port = ResolveService("250.1.1.1", 8334); + CService addr1_port = ResolveService("250.1.1.1", 15717); BOOST_CHECK(addrman->Add({CAddress(addr1_port, NODE_NONE)}, source)); BOOST_CHECK_EQUAL(addrman->Size(), 2U); auto addr_ret2 = addrman->Select().first; - BOOST_CHECK(addr_ret2.ToStringAddrPort() == "250.1.1.1:15714" || addr_ret2.ToStringAddrPort() == "250.1.1.1:8334"); + BOOST_CHECK(addr_ret2.ToStringAddrPort() == "250.1.1.1:15714" || addr_ret2.ToStringAddrPort() == "250.1.1.1:15717"); // Test: Add same IP but diff port to tried table; this converts the entry with // the specified port to tried, but not the other. @@ -140,13 +140,13 @@ BOOST_AUTO_TEST_CASE(addrman_select) CNetAddr source = ResolveIP("252.2.2.2"); // Test: Select from new with 1 addr in new. - CService addr1 = ResolveService("250.1.1.1", 8333); + CService addr1 = ResolveService("250.1.1.1", 15714); BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source)); BOOST_CHECK_EQUAL(addrman->Size(), 1U); bool newOnly = true; auto addr_ret1 = addrman->Select(newOnly).first; - BOOST_CHECK_EQUAL(addr_ret1.ToStringAddrPort(), "250.1.1.1:8333"); + BOOST_CHECK_EQUAL(addr_ret1.ToStringAddrPort(), "250.1.1.1:15714"); // Test: move addr to tried, select from new expected nothing returned. BOOST_CHECK(addrman->Good(CAddress(addr1, NODE_NONE))); @@ -970,7 +970,7 @@ BOOST_AUTO_TEST_CASE(addrman_update_address) BOOST_CHECK_EQUAL(addrman->Size(), 1U); // Updating an addrman entry with a different port doesn't change it - CAddress addr_diff_port{CAddress(ResolveService("250.1.1.1", 8334), NODE_NONE)}; + CAddress addr_diff_port{CAddress(ResolveService("250.1.1.1", 15717), NODE_NONE)}; addr_diff_port.nTime = start_time; addrman->Connected(addr_diff_port); addrman->SetServices(addr_diff_port, NODE_NETWORK_LIMITED); @@ -1000,10 +1000,10 @@ BOOST_AUTO_TEST_CASE(addrman_size) BOOST_CHECK_EQUAL(addrman->Size(/*net=*/NET_IPV4, /*in_new=*/false), 0U); // add two ipv4 addresses, one to tried and new - const CAddress addr1{ResolveService("250.1.1.1", 8333), NODE_NONE}; + const CAddress addr1{ResolveService("250.1.1.1", 15714), NODE_NONE}; BOOST_CHECK(addrman->Add({addr1}, source)); BOOST_CHECK(addrman->Good(addr1)); - const CAddress addr2{ResolveService("250.1.1.2", 8333), NODE_NONE}; + const CAddress addr2{ResolveService("250.1.1.2", 15714), NODE_NONE}; BOOST_CHECK(addrman->Add({addr2}, source)); BOOST_CHECK_EQUAL(addrman->Size(/*net=*/std::nullopt, /*in_new=*/std::nullopt), 2U); diff --git a/src/test/blockencodings_tests.cpp b/src/test/blockencodings_tests.cpp index f32e044011..4c7ea03955 100644 --- a/src/test/blockencodings_tests.cpp +++ b/src/test/blockencodings_tests.cpp @@ -118,6 +118,7 @@ class TestHeaderAndShortIDs { uint64_t nonce; std::vector shorttxids; std::vector prefilledtxn; + std::vector vchBlockSig; explicit TestHeaderAndShortIDs(const CBlockHeaderAndShortTxIDs& orig) { CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); @@ -135,7 +136,7 @@ class TestHeaderAndShortIDs { return base.GetShortID(txhash); } - SERIALIZE_METHODS(TestHeaderAndShortIDs, obj) { READWRITE(obj.header, obj.nonce, Using>>(obj.shorttxids), obj.prefilledtxn); } + SERIALIZE_METHODS(TestHeaderAndShortIDs, obj) { READWRITE(obj.header, obj.nonce, obj.vchBlockSig, Using>>(obj.shorttxids), obj.prefilledtxn); } }; BOOST_AUTO_TEST_CASE(NonCoinbasePreforwardRTTest) diff --git a/src/test/fs_tests.cpp b/src/test/fs_tests.cpp index 080a03da0b..ad6df6fb72 100644 --- a/src/test/fs_tests.cpp +++ b/src/test/fs_tests.cpp @@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream) std::ifstream file{tmpfile1}; std::string input_buffer; file >> input_buffer; - BOOST_CHECK_EQUAL(input_buffer, "bitcointests"); + BOOST_CHECK_EQUAL(input_buffer, "blackcointests"); } { std::ofstream file{tmpfile2, std::ios_base::out | std::ios_base::trunc}; diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp index 94ed02faa4..47ae581dbd 100644 --- a/src/test/pow_tests.cpp +++ b/src/test/pow_tests.cpp @@ -16,17 +16,17 @@ BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(get_next_work) { const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); - int64_t nLastRetargetTime = 1261130161; // Block #30240 + int64_t nLastRetargetTime = 1395090794; // Block #30240 CBlockIndex pindexLast; pindexLast.nHeight = 32255; - pindexLast.nTime = 1262152739; // Block #32255 - pindexLast.nBits = 0x1d00ffff; + pindexLast.nTime = 1395223285; // Block #32255 + pindexLast.nBits = 0x1d028699; // Here (and below): expected_nbits is calculated in // CalculateNextWorkRequired(); redoing the calculation here would be just // reimplementing the same code that is written in pow.cpp. Rather than // copy that code, we just hardcode the expected result. - unsigned int expected_nbits = 0x1d00d86aU; + unsigned int expected_nbits = 0x1e029263; // Blackcoin BOOST_CHECK_EQUAL(CalculateNextTargetRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus(), false), expected_nbits); // BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits)); @@ -36,12 +36,12 @@ BOOST_AUTO_TEST_CASE(get_next_work) BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) { const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); - int64_t nLastRetargetTime = 1231006505; // Block #0 + int64_t nLastRetargetTime = 1393221600; // Block #0 CBlockIndex pindexLast; pindexLast.nHeight = 2015; - pindexLast.nTime = 1233061996; // Block #2015 - pindexLast.nBits = 0x1d00ffff; - unsigned int expected_nbits = 0x1d00ffffU; + pindexLast.nTime = 1393345424; // Block #2015 + pindexLast.nBits = 0x1c06a7a5; + unsigned int expected_nbits = 0x1d06559e; // Blackcoin BOOST_CHECK_EQUAL(CalculateNextTargetRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus(), false), expected_nbits); // BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits)); @@ -51,12 +51,12 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) { const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); - int64_t nLastRetargetTime = 1279008237; // Block #66528 + int64_t nLastRetargetTime = 1397272487; // Block #66528 CBlockIndex pindexLast; pindexLast.nHeight = 68543; - pindexLast.nTime = 1279297671; // Block #68543 - pindexLast.nBits = 0x1c05a3f4; - unsigned int expected_nbits = 0x1c0168fdU; + pindexLast.nTime = 1397374088; // Block #68543 + pindexLast.nBits = 0x1d055260; + unsigned int expected_nbits = 0x1e0428e3; // Blackcoin BOOST_CHECK_EQUAL(CalculateNextTargetRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus(), false), expected_nbits); /* @@ -74,9 +74,9 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time CBlockIndex pindexLast; pindexLast.nHeight = 46367; - pindexLast.nTime = 1269211443; // Block #46367 - pindexLast.nBits = 0x1c387f6f; - unsigned int expected_nbits = 0x1d00e1fdU; + pindexLast.nTime = 1396132439; // Block #46367 + pindexLast.nBits = 0x1d045f46; + unsigned int expected_nbits = 0x1e0fffff; // Blackcoin BOOST_CHECK_EQUAL(CalculateNextTargetRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus(), false), expected_nbits); /* @@ -180,7 +180,8 @@ void sanity_check_chainparams(const ArgsManager& args, std::string chainName) pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over); BOOST_CHECK(!neg && pow_compact != 0); BOOST_CHECK(!over); - BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact); + // Blackcoin + // BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact); // check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired() if (!consensus.fPowNoRetargeting) { diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 19d1833c45..dbfe983a1b 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -277,14 +277,14 @@ TestChain100Setup::TestChain100Setup( {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}; coinbaseKey.Set(vchKey.begin(), vchKey.end(), true); - // Generate a 100-block chain: - this->mineBlocks(Params().GetConsensus().nCoinbaseMaturity); - + // Blackcoin + // Generate a 500-block chain: + this->mineBlocks(500); { LOCK(::cs_main); assert( m_node.chainman->ActiveChain().Tip()->GetBlockHash().ToString() == - "571d80a9967ae599cec0448b0b0ba1cfb606f584d8069bd7166b86854ba7a191"); + "0000724595fb3b9609d441cbfb9577615c292abf07d996d3edabc48de843642d"); } } @@ -310,7 +310,8 @@ CBlock TestChain100Setup::CreateBlock( for (const CMutableTransaction& tx : txns) { block.vtx.push_back(MakeTransactionRef(tx)); } - RegenerateCommitments(block, *Assert(m_node.chainman)); + // Blackcoin + // RegenerateCommitments(block, *Assert(m_node.chainman)); while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce; diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp index 3526548f67..d0fe57351a 100644 --- a/src/test/validation_block_tests.cpp +++ b/src/test/validation_block_tests.cpp @@ -90,7 +90,8 @@ std::shared_ptr MinerTestingSetup::Block(const uint256& prev_hash) std::shared_ptr MinerTestingSetup::FinalizeBlock(std::shared_ptr pblock) { const CBlockIndex* prev_block{WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(pblock->hashPrevBlock))}; - m_node.chainman->GenerateCoinbaseCommitment(*pblock, prev_block); + // Blackcoin + // m_node.chainman->GenerateCoinbaseCommitment(*pblock, prev_block); pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);