Skip to content

Commit 7c123c0

Browse files
miner: add package feerate vector to CBlockTemplate
- The package feerates are ordered by the sequence in which packages are selected for inclusion in the block template. - The commit also tests this new behaviour. Co-authored-by: willcl-ark <[email protected]>
1 parent 433412f commit 7c123c0

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/node/miner.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
421421
}
422422

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

425426
// Update transactions that depend on each of these
426427
nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);

src/node/miner.h

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <policy/policy.h>
1111
#include <primitives/block.h>
1212
#include <txmempool.h>
13+
#include <util/feefrac.h>
1314

1415
#include <memory>
1516
#include <optional>
@@ -39,6 +40,9 @@ struct CBlockTemplate
3940
std::vector<CAmount> vTxFees;
4041
std::vector<int64_t> vTxSigOpsCost;
4142
std::vector<unsigned char> vchCoinbaseCommitment;
43+
/* A vector of package fee rates, ordered by the sequence in which
44+
* packages are selected for inclusion in the block template.*/
45+
std::vector<FeeFrac> m_package_feerates;
4246
};
4347

4448
// Container for tracking updates to ancestor feerate as we include (parent)

src/test/miner_tests.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <txmempool.h>
1717
#include <uint256.h>
1818
#include <util/check.h>
19+
#include <util/feefrac.h>
1920
#include <util/strencodings.h>
2021
#include <util/time.h>
2122
#include <util/translation.h>
@@ -25,6 +26,7 @@
2526
#include <test/util/setup_common.h>
2627

2728
#include <memory>
29+
#include <vector>
2830

2931
#include <boost/test/unit_test.hpp>
3032

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

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

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

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

153+
// Test the inclusion of package feerates in the block template and ensure they are sequential.
154+
const auto block_package_feerates = BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options}.CreateNewBlock()->m_package_feerates;
155+
BOOST_CHECK(block_package_feerates.size() == 2);
156+
157+
// parent_tx and high_fee_tx are added to the block as a package.
158+
const auto combined_txs_fee = parent_tx.GetFee() + high_fee_tx.GetFee();
159+
const auto combined_txs_size = parent_tx.GetTxSize() + high_fee_tx.GetTxSize();
160+
FeeFrac package_feefrac{combined_txs_fee, combined_txs_size};
161+
// The package should be added first.
162+
BOOST_CHECK(block_package_feerates[0] == package_feefrac);
163+
164+
// The medium_fee_tx should be added next.
165+
FeeFrac medium_tx_feefrac{medium_fee_tx.GetFee(), medium_fee_tx.GetTxSize()};
166+
BOOST_CHECK(block_package_feerates[1] == medium_tx_feefrac);
167+
148168
// Test that a package below the block min tx fee doesn't get included
149169
tx.vin[0].prevout.hash = hashHighFeeTx;
150170
tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee

0 commit comments

Comments
 (0)