Skip to content

Commit

Permalink
Port unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Aug 5, 2024
1 parent 9bcb69c commit b7d4c81
Show file tree
Hide file tree
Showing 27 changed files with 209 additions and 185 deletions.
26 changes: 25 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ TEST_BINARY=test/test_qtum$(EXEEXT)
FUZZ_BINARY=test/fuzz/fuzz$(EXEEXT)

JSON_TEST_FILES = \
test/qtumtests/data/btc_ecrecover.json \
test/qtumtests/data/ecrecover.json \
test/qtumtests/data/sha256.json \
test/qtumtests/data/ripemd160.json \
test/qtumtests/data/identity.json \
test/qtumtests/data/modexp.json \
test/qtumtests/data/modexp_eip2565.json \
test/qtumtests/data/alt_bn128_G1_add.json \
test/qtumtests/data/alt_bn128_G1_mul.json \
test/qtumtests/data/alt_bn128_pairing_product.json \
test/qtumtests/data/blake2_compression.json \
test/data/script_tests.json \
test/data/bip341_wallet_vectors.json \
test/data/base58_encode_decode.json \
Expand Down Expand Up @@ -174,7 +185,20 @@ BITCOIN_TESTS =\
test/validation_tests.cpp \
test/validationinterface_tests.cpp \
test/versionbits_tests.cpp \
test/xoroshiro128plusplus_tests.cpp
test/xoroshiro128plusplus_tests.cpp \
test/qtumtests/test_utils.h \
test/qtumtests/precompiled_utils.h \
test/qtumtests/qtumtxconverter_tests.cpp \
test/qtumtests/bytecodeexec_tests.cpp \
test/qtumtests/condensingtransaction_tests.cpp \
test/qtumtests/dgp_tests.cpp \
test/qtumtests/constantinoplefork_tests.cpp \
test/qtumtests/btcecrecoverfork_tests.cpp \
test/qtumtests/delegations_tests.cpp \
test/qtumtests/istanbulfork_tests.cpp \
test/qtumtests/londonfork_tests.cpp \
test/qtumtests/evmone_tests.cpp \
test/qtumtests/shanghaifork_tests.cpp

if ENABLE_WALLET
BITCOIN_TESTS += \
Expand Down
6 changes: 3 additions & 3 deletions src/test/amount_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(GetFeeTest)
BOOST_CHECK(CFeeRate(CAmount(26), 789) == CFeeRate(32));
BOOST_CHECK(CFeeRate(CAmount(27), 789) == CFeeRate(34));
// Maximum size in bytes, should not crash
CFeeRate(MAX_MONEY, std::numeric_limits<uint32_t>::max()).GetFeePerK();
CFeeRate(MAX_MONEY * 0.85, std::numeric_limits<uint32_t>::max()).GetFeePerK();

// check multiplication operator
// check multiplying by zero
Expand Down Expand Up @@ -134,8 +134,8 @@ BOOST_AUTO_TEST_CASE(ToStringTest)
{
CFeeRate feeRate;
feeRate = CFeeRate(1);
BOOST_CHECK_EQUAL(feeRate.ToString(), "0.00000001 BTC/kvB");
BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::BTC_KVB), "0.00000001 BTC/kvB");
BOOST_CHECK_EQUAL(feeRate.ToString(), "0.00000001 QTUM/kvB");
BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::BTC_KVB), "0.00000001 QTUM/kvB");
BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::SAT_VB), "0.001 sat/vB");
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/blockmanager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
CBlock block3;
block3.nVersion = 3;

// They are 80 bytes header + 1 byte 0x00 for vtx length
constexpr int TEST_BLOCK_SIZE{81};
// They are 181 bytes header + 1 byte 0x00 for vtx length
constexpr int TEST_BLOCK_SIZE{182};

// Blockstore is empty
BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), 0);
Expand Down
14 changes: 7 additions & 7 deletions src/test/bloom_tests.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/test/coins_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ BOOST_AUTO_TEST_CASE(ccoins_serialization)
Coin cc1;
ss1 >> cc1;
BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
BOOST_CHECK_EQUAL(cc1.nHeight, 101999U);
BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("816115944e077fe7c803cfa57f29b36bf87c1d35"))))));

Expand All @@ -516,7 +516,7 @@ BOOST_AUTO_TEST_CASE(ccoins_serialization)
Coin cc2;
ss2 >> cc2;
BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
BOOST_CHECK_EQUAL(cc2.nHeight, 60445U);
BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"))))));

Expand Down
4 changes: 2 additions & 2 deletions src/test/compress_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// amounts 1 .. 10000
#define NUM_MULTIPLES_1BTC 10000

// amounts 50 .. 21000000
#define NUM_MULTIPLES_50BTC 420000
// amounts 50 .. 100000000
#define NUM_MULTIPLES_50BTC 2000000

BOOST_FIXTURE_TEST_SUITE(compress_tests, BasicTestingSetup)

Expand Down
2 changes: 1 addition & 1 deletion src/test/denialofservice_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)

const auto time_init{GetTime<std::chrono::seconds>()};
SetMockTime(time_init);
const auto time_later{time_init + 3 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s};
const auto time_later{time_init + 5 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s};
connman->Init(options);
std::vector<CNode *> vNodes;

Expand Down
12 changes: 5 additions & 7 deletions src/test/descriptor_tests.cpp

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/test/interfaces_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ BOOST_AUTO_TEST_CASE(findBlock)

bool cur_active{false}, next_active{false};
uint256 next_hash;
BOOST_CHECK_EQUAL(active.Height(), 100);
BOOST_CHECK(chain->findBlock(active[99]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active).hash(next_hash))));
BOOST_CHECK_EQUAL(active.Height(), 2000);
BOOST_CHECK(chain->findBlock(active[1999]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active).hash(next_hash))));
BOOST_CHECK(cur_active);
BOOST_CHECK(next_active);
BOOST_CHECK_EQUAL(next_hash, active[100]->GetBlockHash());
BOOST_CHECK_EQUAL(next_hash, active[2000]->GetBlockHash());
cur_active = next_active = false;
BOOST_CHECK(chain->findBlock(active[100]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active))));
BOOST_CHECK(chain->findBlock(active[2000]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active))));
BOOST_CHECK(cur_active);
BOOST_CHECK(!next_active);

Expand Down
2 changes: 1 addition & 1 deletion src/test/key_io_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(key_io_invalid)
std::string exp_base58string = test[0].get_str();

// must be invalid as public and as private key
for (const auto& chain : {ChainType::MAIN, ChainType::TESTNET, ChainType::SIGNET, ChainType::REGTEST}) {
for (const auto& chain : {ChainType::MAIN, ChainType::TESTNET, ChainType::SIGNET, ChainType::UNITTEST}) {
SelectParams(chain);
destination = DecodeDestination(exp_base58string);
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid pubkey in mainnet:" + strTest);
Expand Down
6 changes: 3 additions & 3 deletions src/test/logging_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
ResetLogger();
ArgsManager args;
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
const char* argv_test[] = {"bitcoind", "-loglevel=debug"};
const char* argv_test[] = {"qtumd", "-loglevel=debug"};
std::string err;
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));

Expand All @@ -237,7 +237,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
ResetLogger();
ArgsManager args;
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
const char* argv_test[] = {"bitcoind", "-loglevel=net:trace"};
const char* argv_test[] = {"qtumd", "-loglevel=net:trace"};
std::string err;
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));

Expand All @@ -256,7 +256,7 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
ResetLogger();
ArgsManager args;
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
const char* argv_test[] = {"qtumd", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
std::string err;
BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));

Expand Down
2 changes: 1 addition & 1 deletion src/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* See https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/boost_test/adv_scenarios/single_header_customizations/multiple_translation_units.html
*/
#define BOOST_TEST_MODULE Bitcoin Core Test Suite
#define BOOST_TEST_MODULE Qtum Core Test Suite

#include <boost/test/included/unit_test.hpp>

Expand Down
36 changes: 18 additions & 18 deletions src/test/mempool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,15 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
tx1.vout.resize(1);
tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
tx1.vout[0].nValue = 10 * COIN;
pool.addUnchecked(entry.Fee(10000LL).FromTx(tx1));
pool.addUnchecked(entry.Fee(100000LL).FromTx(tx1));

CMutableTransaction tx2 = CMutableTransaction();
tx2.vin.resize(1);
tx2.vin[0].scriptSig = CScript() << OP_2;
tx2.vout.resize(1);
tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
tx2.vout[0].nValue = 10 * COIN;
pool.addUnchecked(entry.Fee(5000LL).FromTx(tx2));
pool.addUnchecked(entry.Fee(50000LL).FromTx(tx2));

pool.TrimToSize(pool.DynamicMemoryUsage()); // should do nothing
BOOST_CHECK(pool.exists(GenTxid::Txid(tx1.GetHash())));
Expand All @@ -465,7 +465,7 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
tx3.vout.resize(1);
tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
tx3.vout[0].nValue = 10 * COIN;
pool.addUnchecked(entry.Fee(20000LL).FromTx(tx3));
pool.addUnchecked(entry.Fee(200000LL).FromTx(tx3));

pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); // tx3 should pay for tx2 (CPFP)
BOOST_CHECK(!pool.exists(GenTxid::Txid(tx1.GetHash())));
Expand All @@ -477,8 +477,8 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
BOOST_CHECK(!pool.exists(GenTxid::Txid(tx2.GetHash())));
BOOST_CHECK(!pool.exists(GenTxid::Txid(tx3.GetHash())));

CFeeRate maxFeeRateRemoved(25000, GetVirtualTransactionSize(CTransaction(tx3)) + GetVirtualTransactionSize(CTransaction(tx2)));
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 1000);
CFeeRate maxFeeRateRemoved(250000, GetVirtualTransactionSize(CTransaction(tx3)) + GetVirtualTransactionSize(CTransaction(tx2)));
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 10000);

CMutableTransaction tx4 = CMutableTransaction();
tx4.vin.resize(2);
Expand Down Expand Up @@ -528,10 +528,10 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
tx7.vout[1].nValue = 10 * COIN;

pool.addUnchecked(entry.Fee(7000LL).FromTx(tx4));
pool.addUnchecked(entry.Fee(1000LL).FromTx(tx5));
pool.addUnchecked(entry.Fee(1100LL).FromTx(tx6));
pool.addUnchecked(entry.Fee(9000LL).FromTx(tx7));
pool.addUnchecked(entry.Fee(70000LL).FromTx(tx4));
pool.addUnchecked(entry.Fee(10000LL).FromTx(tx5));
pool.addUnchecked(entry.Fee(11000LL).FromTx(tx6));
pool.addUnchecked(entry.Fee(90000LL).FromTx(tx7));

// we only require this to remove, at max, 2 txn, because it's not clear what we're really optimizing for aside from that
pool.TrimToSize(pool.DynamicMemoryUsage() - 1);
Expand All @@ -540,38 +540,38 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
BOOST_CHECK(!pool.exists(GenTxid::Txid(tx7.GetHash())));

if (!pool.exists(GenTxid::Txid(tx5.GetHash())))
pool.addUnchecked(entry.Fee(1000LL).FromTx(tx5));
pool.addUnchecked(entry.Fee(9000LL).FromTx(tx7));
pool.addUnchecked(entry.Fee(10000LL).FromTx(tx5));
pool.addUnchecked(entry.Fee(90000LL).FromTx(tx7));

pool.TrimToSize(pool.DynamicMemoryUsage() / 2); // should maximize mempool size by only removing 5/7
BOOST_CHECK(pool.exists(GenTxid::Txid(tx4.GetHash())));
BOOST_CHECK(!pool.exists(GenTxid::Txid(tx5.GetHash())));
BOOST_CHECK(pool.exists(GenTxid::Txid(tx6.GetHash())));
BOOST_CHECK(!pool.exists(GenTxid::Txid(tx7.GetHash())));

pool.addUnchecked(entry.Fee(1000LL).FromTx(tx5));
pool.addUnchecked(entry.Fee(9000LL).FromTx(tx7));
pool.addUnchecked(entry.Fee(10000LL).FromTx(tx5));
pool.addUnchecked(entry.Fee(90000LL).FromTx(tx7));

std::vector<CTransactionRef> vtx;
SetMockTime(42);
SetMockTime(42 + CTxMemPool::ROLLING_FEE_HALFLIFE);
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 1000);
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 10000);
// ... we should keep the same min fee until we get a block
pool.removeForBlock(vtx, 1);
SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE);
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 1000)/2.0));
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 10000)/2.0));
// ... then feerate should drop 1/2 each halflife

SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2);
BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 5 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 1000)/4.0));
BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 5 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 10000)/4.0));
// ... with a 1/2 halflife when mempool is < 1/2 its target size

SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2 + CTxMemPool::ROLLING_FEE_HALFLIFE/4);
BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 9 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 1000)/8.0));
BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 9 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 10000)/8.0));
// ... with a 1/4 halflife when mempool is < 1/4 its target size

SetMockTime(42 + 7*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2 + CTxMemPool::ROLLING_FEE_HALFLIFE/4);
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), 1000);
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), 10000);
// ... but feerate should never drop below 1000

SetMockTime(42 + 8*CTxMemPool::ROLLING_FEE_HALFLIFE + CTxMemPool::ROLLING_FEE_HALFLIFE/2 + CTxMemPool::ROLLING_FEE_HALFLIFE/4);
Expand Down
6 changes: 3 additions & 3 deletions src/test/net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
auto decoy_data = g_insecure_rand_ctx.randbytes<uint8_t>(InsecureRandRange(1000));
tester.SendPacket(/*content=*/decoy_data, /*aad=*/{}, /*ignore=*/true);
}
auto msg_data_1 = g_insecure_rand_ctx.randbytes<uint8_t>(InsecureRandRange(4000000));
auto msg_data_1 = g_insecure_rand_ctx.randbytes<uint8_t>(InsecureRandRange(2000000));
tester.SendMessage(uint8_t(28), msg_data_1);
for (unsigned d = 0; d < num_decoys_2; ++d) {
auto decoy_data = g_insecure_rand_ctx.randbytes<uint8_t>(InsecureRandRange(1000));
Expand Down Expand Up @@ -1535,8 +1535,8 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
tester.ReceiveGarbage();
tester.ReceiveVersion();
tester.CompareSessionIDs();
auto msg_data_1 = g_insecure_rand_ctx.randbytes<uint8_t>(4000000); // test that receiving 4M payload works
auto msg_data_2 = g_insecure_rand_ctx.randbytes<uint8_t>(4000000); // test that sending 4M payload works
auto msg_data_1 = g_insecure_rand_ctx.randbytes<uint8_t>(2000000); // test that receiving 2M payload works
auto msg_data_2 = g_insecure_rand_ctx.randbytes<uint8_t>(2000000); // test that sending 2M payload works
tester.SendMessage(uint8_t(InsecureRandRange(223) + 33), {}); // unknown short id
tester.SendMessage(uint8_t(2), msg_data_1); // "block" short id
tester.AddMessage("blocktxn", msg_data_2); // schedule blocktxn to be sent to us
Expand Down
Loading

0 comments on commit b7d4c81

Please sign in to comment.