Skip to content

Commit

Permalink
miner: add package feerate vector to CBlockTemplate
Browse files Browse the repository at this point in the history
- The package feerates are ordered by the sequence in which
  packages are selected for inclusion in the block template.

- Enable getting this vector through the mining interface.

- The commit also tests this new behaviour.

Co-authored-by: willcl-ark <[email protected]>
  • Loading branch information
ismaelsadeeq and willcl-ark committed Jan 7, 2025
1 parent 433412f commit a5ccca1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct NodeContext;
class BlockValidationState;
class CScript;

struct FeeFrac;

namespace interfaces {

//! Block template interface
Expand All @@ -41,6 +43,7 @@ class BlockTemplate

virtual CTransactionRef getCoinbaseTx() = 0;
virtual std::vector<unsigned char> getCoinbaseCommitment() = 0;
virtual std::vector<FeeFrac> getPackageFeerates() = 0;
virtual int getWitnessCommitmentIndex() = 0;

/**
Expand Down
6 changes: 6 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <uint256.h>
#include <univalue.h>
#include <util/check.h>
#include <util/feefrac.h>
#include <util/result.h>
#include <util/signalinterrupt.h>
#include <util/string.h>
Expand Down Expand Up @@ -906,6 +907,11 @@ class BlockTemplateImpl : public BlockTemplate
return m_block_template->vchCoinbaseCommitment;
}

std::vector<FeeFrac> getPackageFeerates() override
{
return m_block_template->m_package_feerates;
}

int getWitnessCommitmentIndex() override
{
return GetWitnessCommitmentIndex(m_block_template->block);
Expand Down
1 change: 1 addition & 0 deletions src/node/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
}

++nPackagesSelected;
pblocktemplate->m_package_feerates.emplace_back(packageFees, static_cast<int32_t>(packageSize));

// Update transactions that depend on each of these
nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);
Expand Down
4 changes: 4 additions & 0 deletions src/node/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <policy/policy.h>
#include <primitives/block.h>
#include <txmempool.h>
#include <util/feefrac.h>

#include <memory>
#include <optional>
Expand Down Expand Up @@ -39,6 +40,9 @@ struct CBlockTemplate
std::vector<CAmount> vTxFees;
std::vector<int64_t> vTxSigOpsCost;
std::vector<unsigned char> vchCoinbaseCommitment;
/* A vector of package fee rates, ordered by the sequence in which
* packages are selected for inclusion in the block template.*/
std::vector<FeeFrac> m_package_feerates;
};

// Container for tracking updates to ancestor feerate as we include (parent)
Expand Down
22 changes: 19 additions & 3 deletions src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <txmempool.h>
#include <uint256.h>
#include <util/check.h>
#include <util/feefrac.h>
#include <util/strencodings.h>
#include <util/time.h>
#include <util/translation.h>
Expand All @@ -25,6 +26,7 @@
#include <test/util/setup_common.h>

#include <memory>
#include <vector>

#include <boost/test/unit_test.hpp>

Expand Down Expand Up @@ -123,19 +125,22 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const
tx.vout[0].nValue = 5000000000LL - 1000;
// This tx has a low fee: 1000 satoshis
Txid hashParentTx = tx.GetHash(); // save this txid for later use
AddToMempool(tx_mempool, entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
const auto parent_tx{entry.Fee(1000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
AddToMempool(tx_mempool, parent_tx);

// This tx has a medium fee: 10000 satoshis
tx.vin[0].prevout.hash = txFirst[1]->GetHash();
tx.vout[0].nValue = 5000000000LL - 10000;
Txid hashMediumFeeTx = tx.GetHash();
AddToMempool(tx_mempool, entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx));
const auto medium_fee_tx{entry.Fee(10000).Time(Now<NodeSeconds>()).SpendsCoinbase(true).FromTx(tx)};
AddToMempool(tx_mempool, medium_fee_tx);

// This tx has a high fee, but depends on the first transaction
tx.vin[0].prevout.hash = hashParentTx;
tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee
Txid hashHighFeeTx = tx.GetHash();
AddToMempool(tx_mempool, entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
const auto high_fee_tx{entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx)};
AddToMempool(tx_mempool, high_fee_tx);

std::unique_ptr<BlockTemplate> block_template = mining->createNewBlock(options);
BOOST_REQUIRE(block_template);
Expand All @@ -145,6 +150,17 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const
BOOST_CHECK(block.vtx[2]->GetHash() == hashHighFeeTx);
BOOST_CHECK(block.vtx[3]->GetHash() == hashMediumFeeTx);

const auto block_package_feerates{block_template->getPackageFeerates()};
BOOST_CHECK(block_package_feerates.size() == 2);
// parent_tx and high_fee_tx are added to the block as a package.
const auto package_fee{parent_tx.GetFee() + high_fee_tx.GetFee()};
const auto package_size{parent_tx.GetTxSize() + high_fee_tx.GetTxSize()};
FeeFrac package_feefrac{package_fee, package_size};
BOOST_CHECK(block_package_feerates[0] == package_feefrac);

FeeFrac medium_tx_feefrac{medium_fee_tx.GetFee(), medium_fee_tx.GetTxSize()};
BOOST_CHECK(block_package_feerates[1] == medium_tx_feefrac);

// Test that a package below the block min tx fee doesn't get included
tx.vin[0].prevout.hash = hashHighFeeTx;
tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
Expand Down

0 comments on commit a5ccca1

Please sign in to comment.