Skip to content

Commit

Permalink
refactor: get rid usage of global ::ChainstateActive() and ::ChainAct…
Browse files Browse the repository at this point in the history
…ive() from block_reward_reallocation_tests.cpp
  • Loading branch information
knst committed Oct 16, 2023
1 parent 15b7d87 commit 8937ca3
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions src/test/block_reward_reallocation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ BOOST_AUTO_TEST_SUITE(block_reward_reallocation_tests)
BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationSetup)
{
const auto& consensus_params = Params().GetConsensus();

CScript coinbasePubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;

BOOST_ASSERT(deterministicMNManager->IsDIP3Enforced(WITH_LOCK(cs_main, return ::ChainActive().Height())));
BOOST_ASSERT(deterministicMNManager->IsDIP3Enforced(WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Height())));

// Register one MN
CKey ownerKey;
Expand All @@ -157,56 +158,58 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
auto tx = CreateProRegTx(*m_node.mempool, utxos, 1, GenerateRandomAddress(), coinbaseKey, ownerKey, operatorKey);

CreateAndProcessBlock({tx}, coinbaseKey);
// Will be updated later
bool isMNRewardReallocated = false;

{
LOCK(cs_main);
deterministicMNManager->UpdatedBlockTip(::ChainActive().Tip());
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
deterministicMNManager->UpdatedBlockTip(tip);

BOOST_ASSERT(deterministicMNManager->GetListAtChainTip().HasMN(tx.GetHash()));

BOOST_CHECK_EQUAL(::ChainActive().Height(), 498);
BOOST_CHECK(::ChainActive().Height() < Params().GetConsensus().BRRHeight);
BOOST_CHECK_EQUAL(tip->nHeight, 498);
BOOST_CHECK(tip->nHeight < Params().GetConsensus().BRRHeight);
}

CreateAndProcessBlock({}, coinbaseKey);

{
LOCK(cs_main);
BOOST_CHECK_EQUAL(::ChainActive().Height(), 499);
deterministicMNManager->UpdatedBlockTip(::ChainActive().Tip());
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
BOOST_CHECK_EQUAL(tip->nHeight, 499);
deterministicMNManager->UpdatedBlockTip(tip);
BOOST_ASSERT(deterministicMNManager->GetListAtChainTip().HasMN(tx.GetHash()));
BOOST_CHECK(::ChainActive().Height() < Params().GetConsensus().BRRHeight);
BOOST_CHECK(tip->nHeight < Params().GetConsensus().BRRHeight);
// Creating blocks by different ways
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
}
for ([[maybe_unused]] auto _ : irange::range(1999)) {
CreateAndProcessBlock({}, coinbaseKey);
LOCK(cs_main);
deterministicMNManager->UpdatedBlockTip(::ChainActive().Tip());
deterministicMNManager->UpdatedBlockTip(m_node.chainman->ActiveChain().Tip());
}
BOOST_CHECK(::ChainActive().Height() < Params().GetConsensus().BRRHeight);
BOOST_CHECK(m_node.chainman->ActiveChain().Height() < Params().GetConsensus().BRRHeight);
CreateAndProcessBlock({}, coinbaseKey);

{
// Advance to ACTIVE at height = (BRRHeight - 1)
LOCK(cs_main);
BOOST_CHECK_EQUAL(::ChainActive().Height(), Params().GetConsensus().BRRHeight - 1);
deterministicMNManager->UpdatedBlockTip(::ChainActive().Tip());
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
BOOST_CHECK_EQUAL(tip->nHeight, Params().GetConsensus().BRRHeight - 1);
deterministicMNManager->UpdatedBlockTip(tip);
BOOST_ASSERT(deterministicMNManager->GetListAtChainTip().HasMN(tx.GetHash()));
BOOST_CHECK(::ChainActive().Height() + 1 == Params().GetConsensus().BRRHeight);
}

{
// Reward split should stay ~50/50 before the first superblock after activation.
// This applies even if reallocation was activated right at superblock height like it does here.
// next block should be signaling by default
LOCK(cs_main);
deterministicMNManager->UpdatedBlockTip(::ChainActive().Tip());
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
const bool isMNRewardReallocated{llmq::utils::IsMNRewardReallocationActive(tip)};
deterministicMNManager->UpdatedBlockTip(tip);
BOOST_ASSERT(deterministicMNManager->GetListAtChainTip().HasMN(tx.GetHash()));
const CAmount masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params, isMNRewardReallocated), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
const CAmount masternode_payment = GetMasternodePayment(tip->nHeight, GetBlockSubsidy(tip, consensus_params), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
}

Expand All @@ -216,8 +219,10 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS

{
LOCK(cs_main);
const CAmount masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params, isMNRewardReallocated), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
const bool isMNRewardReallocated{llmq::utils::IsMNRewardReallocationActive(tip)};
const CAmount masternode_payment = GetMasternodePayment(tip->nHeight, GetBlockSubsidy(tip, consensus_params), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx[0]->GetValueOut(), 13748571607);
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, 6874285801); // 0.4999999998
Expand All @@ -231,22 +236,26 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
CreateAndProcessBlock({}, coinbaseKey);
}
LOCK(cs_main);
const CAmount masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params, isMNRewardReallocated), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
const bool isMNRewardReallocated{llmq::utils::IsMNRewardReallocationActive(tip)};
const CAmount masternode_payment = GetMasternodePayment(tip->nHeight, GetBlockSubsidy(tip, consensus_params), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
}
}

{
// Reward split should reach ~60/40 after reallocation is done
LOCK(cs_main);
const CAmount masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params, isMNRewardReallocated), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
const bool isMNRewardReallocated{llmq::utils::IsMNRewardReallocationActive(tip)};
const CAmount masternode_payment = GetMasternodePayment(tip->nHeight, GetBlockSubsidy(tip, consensus_params), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx[0]->GetValueOut(), 10221599170);
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, 6132959502); // 0.6
}
BOOST_CHECK(!llmq::utils::IsMNRewardReallocationActive(::ChainActive().Tip()));
BOOST_CHECK(!llmq::utils::IsMNRewardReallocationActive(m_node.chainman->ActiveChain().Tip()));

// Reward split should stay ~60/40 after reallocation is done,
// check 10 next superblocks
Expand All @@ -255,9 +264,10 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
CreateAndProcessBlock({}, coinbaseKey);
}
LOCK(cs_main);
isMNRewardReallocated = llmq::utils::IsMNRewardReallocationActive(::ChainActive().Tip());
CAmount masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params, isMNRewardReallocated), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
const bool isMNRewardReallocated{llmq::utils::IsMNRewardReallocationActive(tip)};
CAmount masternode_payment = GetMasternodePayment(tip->nHeight, GetBlockSubsidy(tip, consensus_params), isMNRewardReallocated);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);

if (isMNRewardReallocated) {
const CAmount platform_payment = MasternodePayments::PlatformShare(masternode_payment);
Expand All @@ -268,15 +278,16 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[payment_index].nValue, masternode_payment);
}

BOOST_CHECK(llmq::utils::IsMNRewardReallocationActive(::ChainActive().Tip()));
BOOST_CHECK(llmq::utils::IsMNRewardReallocationActive(m_node.chainman->ActiveChain().Tip()));
{ // At this moment Masternode reward should be reallocated to platform
// Allocation of block subsidy is 60% MN, 20% miners and 20% treasury
LOCK(cs_main);
isMNRewardReallocated = llmq::utils::IsMNRewardReallocationActive(::ChainActive().Tip());
CAmount masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params, isMNRewardReallocated), isMNRewardReallocated);
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
const bool isMNRewardReallocated{llmq::utils::IsMNRewardReallocationActive(tip)};
CAmount masternode_payment = GetMasternodePayment(tip->nHeight, GetBlockSubsidy(tip, consensus_params), isMNRewardReallocated);
const CAmount platform_payment = MasternodePayments::PlatformShare(masternode_payment);
masternode_payment -= platform_payment;
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);

// At this height (3178) the block subsidy is 10546094382.
CAmount block_subsidy = CAmount(10546094382);
Expand Down

0 comments on commit 8937ca3

Please sign in to comment.