Skip to content

Commit

Permalink
test: Fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lateminer committed Nov 5, 2023
1 parent 14f1fb9 commit 8ff2d42
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/blockencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ class PartiallyDownloadedBlock {
const ChainstateManager* chainman;
public:
CBlockHeader header;
std::vector<unsigned char> vchBlockSig;

// Can be overridden for testing
using CheckBlockFn = std::function<bool(const CBlock&, BlockValidationState&, const Consensus::Params&, Chainstate& chainstate, bool, bool, bool)>;
CheckBlockFn m_check_block_mock{nullptr};

std::vector<unsigned char> vchBlockSig;
explicit PartiallyDownloadedBlock(CTxMemPool* poolIn, ChainstateManager* _chainman) : pool(poolIn), chainman(_chainman) {}

// extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
Expand Down
14 changes: 7 additions & 7 deletions src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/test/blockencodings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class TestHeaderAndShortIDs {
uint64_t nonce;
std::vector<uint64_t> shorttxids;
std::vector<PrefilledTransaction> prefilledtxn;
std::vector<unsigned char> vchBlockSig;

explicit TestHeaderAndShortIDs(const CBlockHeaderAndShortTxIDs& orig) {
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
Expand All @@ -135,7 +136,7 @@ class TestHeaderAndShortIDs {
return base.GetShortID(txhash);
}

SERIALIZE_METHODS(TestHeaderAndShortIDs, obj) { READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<CBlockHeaderAndShortTxIDs::SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn); }
SERIALIZE_METHODS(TestHeaderAndShortIDs, obj) { READWRITE(obj.header, obj.nonce, obj.vchBlockSig, Using<VectorFormatter<CustomUintFormatter<CBlockHeaderAndShortTxIDs::SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn); }
};

BOOST_AUTO_TEST_CASE(NonCoinbasePreforwardRTTest)
Expand Down
2 changes: 1 addition & 1 deletion src/test/fs_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
33 changes: 17 additions & 16 deletions src/test/pow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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);
/*
Expand All @@ -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);
/*
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/test/validation_block_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> 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);

Expand Down

0 comments on commit 8ff2d42

Please sign in to comment.