PIVX Core  5.6.99
P2P Digital Currency
blockassembler.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Bitcoin Core developers
3 // Copyright (c) 2021 The PIVX Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_BLOCKASSEMBLER_H
8 #define PIVX_BLOCKASSEMBLER_H
9 
10 #include "primitives/block.h"
11 #include "txmempool.h"
12 
13 #include <stdint.h>
14 #include <memory>
15 #include "boost/multi_index_container.hpp"
16 #include "boost/multi_index/ordered_index.hpp"
17 
18 class CBlockIndex;
19 class CChainParams;
20 class CReserveKey;
21 class CStakeableOutput;
22 class CScript;
23 class CWallet;
24 
25 namespace Consensus { struct Params; };
26 
28 {
30  std::vector<CAmount> vTxFees;
31  std::vector<int64_t> vTxSigOps;
32 };
33 
34 // Container for tracking updates to ancestor feerate as we include (parent)
35 // transactions in a block
38  {
39  iter = entry;
40  nSizeWithAncestors = entry->GetSizeWithAncestors();
41  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
42  nSigOpCountWithAncestors = entry->GetSigOpCountWithAncestors();
43  }
44 
49 };
50 
57  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
58  {
59  return &(*a) < &(*b);
60  }
61 };
62 
66  {
67  return entry.iter;
68  }
69 };
70 
71 // This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
72 // except operating on CTxMemPoolModifiedEntry.
73 // TODO: refactor to avoid duplication of this logic.
76  {
77  double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
78  double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
79  if (f1 == f2) {
81  }
82  return f1 > f2;
83  }
84 };
85 
86 // A comparator that sorts transactions based on number of ancestors.
87 // This is sufficient to sort an ancestor package in an order that is valid
88 // to appear in a block.
90  bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
91  {
92  if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
93  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
94  return CTxMemPool::CompareIteratorByHash()(a, b);
95  }
96 };
97 
98 typedef boost::multi_index_container<
100  boost::multi_index::indexed_by<
101  boost::multi_index::ordered_unique<
104  >,
105  // sorted by modified ancestor fee rate
106  boost::multi_index::ordered_non_unique<
107  // Reuse same tag from CTxMemPool's similar index
108  boost::multi_index::tag<ancestor_score>,
109  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
111  >
112  >
114 
115 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
116 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
117 
119 {
121 
123  {
124  e.nModFeesWithAncestors -= iter->GetFee();
125  e.nSizeWithAncestors -= iter->GetTxSize();
126  e.nSigOpCountWithAncestors -= iter->GetSigOpCount();
127  }
128 
130 };
131 
132 
135 {
136 private:
137  // The constructed block template
138  std::unique_ptr<CBlockTemplate> pblocktemplate;
139  // A convenience pointer that always refers to the CBlock in pblocktemplate
140  CBlock* pblock{nullptr};
141 
142  // Configuration parameters for the block max size
143  unsigned int nBlockMaxSize{0};
144 
145  // Information on the current status of the block
146  uint64_t nBlockSize{0};
147  uint64_t nBlockTx{0};
148  unsigned int nBlockSigOps{0};
151 
152  // Chain context for the block
153  int nHeight{0};
155 
156  // Keep track of block space used for shield txes
157  unsigned int nSizeShielded{0};
158 
159  // Whether should print priority by default or not
160  const bool defaultPrintPriority{false};
161 
162 public:
165  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn,
166  CWallet* pwallet = nullptr,
167  bool fProofOfStake = false,
168  std::vector<CStakeableOutput>* availableCoins = nullptr,
169  bool fNoMempoolTx = false,
170  bool fTestValidity = true,
171  CBlockIndex* prevBlock = nullptr,
172  bool stopPoSOnNewBlock = true,
173  bool fIncludeQfc = true);
174 
175 private:
176  // utility functions
178  void resetBlock();
180  void AddToBlock(CTxMemPool::txiter iter);
181 
182  // Methods for how to add transactions to a block.
184  void addPackageTxs();
186  void appendSaplingTreeRoot();
187 
188  // helper functions for addPackageTxs()
192  bool TestPackage(uint64_t packageSize, unsigned int packageSigOps);
194  bool TestPackageFinality(const CTxMemPool::setEntries& package);
199  void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
202  void UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
203 
204 };
205 
207 bool SolveBlock(std::shared_ptr<CBlock>& pblock, int nHeight);
208 void IncrementExtraNonce(std::shared_ptr<CBlock>& pblock, int nHeight, unsigned int& nExtraNonce);
209 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
210 int32_t ComputeBlockVersion(const Consensus::Params& consensusParams, int nHeight);
211 
212 // Visible for testing purposes only
213 bool CreateCoinbaseTx(CBlock* pblock, const CScript& scriptPubKeyIn, CBlockIndex* pindexPrev);
214 CMutableTransaction CreateCoinbaseTx(const CScript& scriptPubKeyIn, CBlockIndex* pindexPrev);
215 
216 // Visible for testing purposes only
217 uint256 CalculateSaplingTreeRoot(CBlock* pblock, int nHeight, const CChainParams& chainparams);
218 
219 #endif // PIVX_BLOCKASSEMBLER_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
void IncrementExtraNonce(std::shared_ptr< CBlock > &pblock, int nHeight, unsigned int &nExtraNonce)
bool SolveBlock(std::shared_ptr< CBlock > &pblock, int nHeight)
Modify the nonce/extranonce in a block.
int32_t ComputeBlockVersion(const Consensus::Params &consensusParams, int nHeight)
bool CreateCoinbaseTx(CBlock *pblock, const CScript &scriptPubKeyIn, CBlockIndex *pindexPrev)
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
uint256 CalculateSaplingTreeRoot(CBlock *pblock, int nHeight, const CChainParams &chainparams)
boost::multi_index_container< CTxMemPoolModifiedEntry, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< modifiedentry_iter, CompareCTxMemPoolIter >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score >, boost::multi_index::identity< CTxMemPoolModifiedEntry >, CompareModifiedEntry > >> indexed_modified_transaction_set
Generate a new block.
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn, CWallet *pwallet=nullptr, bool fProofOfStake=false, std::vector< CStakeableOutput > *availableCoins=nullptr, bool fNoMempoolTx=false, bool fTestValidity=true, CBlockIndex *prevBlock=nullptr, bool stopPoSOnNewBlock=true, bool fIncludeQfc=true)
Construct a new block template with coinbase to scriptPubKeyIn.
CTxMemPool::setEntries inBlock
void addPackageTxs()
Add transactions based on feerate including unconfirmed ancestors.
bool TestPackageFinality(const CTxMemPool::setEntries &package)
Test if a set of transactions are all final.
unsigned int nBlockMaxSize
void SortForBlock(const CTxMemPool::setEntries &package, CTxMemPool::txiter entry, std::vector< CTxMemPool::txiter > &sortedEntries)
Sort the package in an order that is valid to appear in a block.
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
uint64_t nBlockSize
void appendSaplingTreeRoot()
Add the tip updated incremental merkle tree to the header.
BlockAssembler(const CChainParams &chainparams, const bool defaultPrintPriority)
unsigned int nSizeShielded
void resetBlock()
Clear the block's state and prepare for assembling a new block.
std::unique_ptr< CBlockTemplate > pblocktemplate
const CChainParams & chainparams
void UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded, indexed_modified_transaction_set &mapModifiedTx)
Add descendants of given transactions to mapModifiedTx with ancestor state updated assuming given tra...
unsigned int nBlockSigOps
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
Return true if given transaction from mapTx has already been evaluated, or if the transaction's cache...
const bool defaultPrintPriority
bool TestPackage(uint64_t packageSize, unsigned int packageSigOps)
Test if a new package would "fit" in the block.
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
Definition: block.h:80
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
CChainParams defines various tweakable parameters of a given instance of the PIVX system.
Definition: chainparams.h:43
A key allocated from the key pool.
Definition: wallet.h:1256
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:481
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:474
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:577
256-bit opaque blob.
Definition: uint256.h:138
std::vector< int64_t > vTxSigOps
std::vector< CAmount > vTxFees
A mutable version of CTransaction.
Definition: transaction.h:409
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
unsigned int nSigOpCountWithAncestors
uint64_t nSizeWithAncestors
CAmount nModFeesWithAncestors
CTxMemPool::txiter iter
Comparator for CTxMemPool::txiter objects.
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b) const
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Parameters that influence chain consensus.
Definition: params.h:171
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
CTxMemPool::txiter result_type
update_for_parent_inclusion(CTxMemPool::txiter it)
void operator()(CTxMemPoolModifiedEntry &e)