Fabcoin Core  0.16.2
P2P Digital Currency
miner.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef FABCOIN_MINER_H
7 #define FABCOIN_MINER_H
8 
9 #include <primitives/block.h>
10 #include <libgpusolver/gpuconfig.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 #include <validation.h> // fasc
19 
20 class CBlockIndex;
21 class CChainParams;
22 
23 class CScript;
24 
25 namespace Consensus { struct Params; };
26 
27 static const bool DEFAULT_GENERATE = false;
28 static const int DEFAULT_GENERATE_THREADS = 1;
29 
30 static const bool DEFAULT_PRINTPRIORITY = false;
31 
32 //Will not add any more contracts when GetAdjustedTime() >= nTimeLimit-BYTECODE_TIME_BUFFER
33 //This does not affect non-contract transactions
34 static const int32_t BYTECODE_TIME_BUFFER = 6;
35 //How much time to spend trying to process transactions when using the generate RPC call
36 static const int32_t POW_MINER_MAX_TIME = 60;
37 
39 {
41  std::vector<CAmount> vTxFees;
42  std::vector<int64_t> vTxSigOpsCost;
43  std::vector<unsigned char> vchCoinbaseCommitment;
44 };
45 
46 // Container for tracking updates to ancestor feerate as we include (parent)
47 // transactions in a block
50  {
51  iter = entry;
52  nSizeWithAncestors = entry->GetSizeWithAncestors();
53  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
54  nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
55  }
56 
61 };
62 
69  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
70  {
71  return &(*a) < &(*b);
72  }
73 };
74 
77  result_type operator() (const CTxMemPoolModifiedEntry &entry) const
78  {
79  return entry.iter;
80  }
81 };
82 
83 // This related to the calculation in CompareTxMemPoolEntryByAncestorFeeOrGasPrice,
84 // except operating on CTxMemPoolModifiedEntry.
85 // TODO: refactor to avoid duplication of this logic.
88  {
89  bool fAHasCreateOrCall = a.iter->GetTx().HasCreateOrCall();
90  bool fBHasCreateOrCall = b.iter->GetTx().HasCreateOrCall();
91 
92  // If either of the two entries that we are comparing has a contract scriptPubKey, the comparison here takes precedence
93  if(fAHasCreateOrCall || fBHasCreateOrCall) {
94 
95  // Prioritze non-contract txs
96  if(fAHasCreateOrCall != fBHasCreateOrCall) {
97  return fAHasCreateOrCall ? false : true;
98  }
99 
100  // Prioritize the contract txs that have the least number of ancestors
101  // The reason for this is that otherwise it is possible to send one tx with a
102  // high gas limit but a low gas price which has a child with a low gas limit but a high gas price
103  // Without this condition that transaction chain would get priority in being included into the block.
104  // The two next checks are to see if all our ancestors have been added.
105  if(a.nSizeWithAncestors == a.iter->GetTxSize() && b.nSizeWithAncestors != b.iter->GetTxSize()) {
106  return true;
107  }
108 
109  if(b.nSizeWithAncestors == b.iter->GetTxSize() && a.nSizeWithAncestors != a.iter->GetTxSize()) {
110  return false;
111  }
112 
113  // Otherwise, prioritize the contract tx with the highest (minimum among its outputs) gas price
114  // The reason for using the gas price of the output that sets the minimum gas price is that
115  // otherwise it may be possible to game the prioritization by setting a large gas price in one output
116  // that does no execution, while the real execution has a very low gas price
117  if(a.iter->GetMinGasPrice() != b.iter->GetMinGasPrice()) {
118  return a.iter->GetMinGasPrice() > b.iter->GetMinGasPrice();
119  }
120 
121  // Otherwise, prioritize the tx with the min size
122  if(a.iter->GetTxSize() != b.iter->GetTxSize()) {
123  return a.iter->GetTxSize() < b.iter->GetTxSize();
124  }
125 
126  // If the txs are identical in their minimum gas prices and tx size
127  // order based on the tx hash for consistency.
129  }
130 
131  double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
132  double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
133  if (f1 == f2) {
135  }
136  return f1 > f2;
137  }
138 };
139 
140 // A comparator that sorts transactions based on number of ancestors.
141 // This is sufficient to sort an ancestor package in an order that is valid
142 // to appear in a block.
145  {
146  if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
147  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
149  }
150 };
151 
152 typedef boost::multi_index_container<
154  boost::multi_index::indexed_by<
155  boost::multi_index::ordered_unique<
158  >,
159  // sorted by modified ancestor fee rate or gas price
160  boost::multi_index::ordered_non_unique<
161  // Reuse same tag from CTxMemPool's similar index
162  boost::multi_index::tag<ancestor_score_or_gas_price>,
163  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
165  >
166  >
168 
169 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
170 typedef indexed_modified_transaction_set::index<ancestor_score_or_gas_price>::type::iterator modtxscoreiter;
171 
173 {
175 
176  void operator() (CTxMemPoolModifiedEntry &e)
177  {
178  e.nModFeesWithAncestors -= iter->GetFee();
179  e.nSizeWithAncestors -= iter->GetTxSize();
180  e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
181  }
182 
184 };
185 
188 {
189 private:
190  // The constructed block template
191  std::unique_ptr<CBlockTemplate> pblocktemplate;
192  // A convenience pointer that always refers to the CBlock in pblocktemplate
194 
195  // Configuration parameters for the block size
197  unsigned int nBlockMaxWeight;
199 
200  // Information on the current status of the block
201  uint64_t nBlockWeight;
202  uint64_t nBlockTx;
206 
207  // Chain context for the block
208  int nHeight;
211 
212 public:
213  struct Options {
214  Options();
218  };
219 
220  BlockAssembler(const CChainParams& params);
221  BlockAssembler(const CChainParams& params, const Options& options);
222 
224  //??? std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true);
227  uint64_t minGasPrice = 1;
230  uint64_t txGasLimit;
232 
233  // The original constructed reward tx (either coinbase or coinstake) without gas refund adjustments
235 
236  //When GetAdjustedTime() exceeds this, no more transactions will attempt to be added
237  int32_t nTimeLimit;
238 
240  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true, int64_t* pTotalFees = 0, int32_t nTime=0, int32_t nTimeLimit=0);
241 
242 private:
243  // utility functions
245  void resetBlock();
247  void AddToBlock(CTxMemPool::txiter iter);
248 
249  bool AttemptToAddContractToBlock(CTxMemPool::txiter iter, uint64_t minGasPrice);
250 
251  // Methods for how to add transactions to a block.
255  void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated, uint64_t minGasPrice);
256 
258  void RebuildRefundTransaction();
259 
260  // helper functions for addPackageTxs()
262  void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
264  bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
269  bool TestPackageTransactions(const CTxMemPool::setEntries& package);
272  bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);
274  void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
278  int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
279 };
280 
282 void GenerateFabcoins(bool fGenerate, int nThreads, const CChainParams& chainparams);
283 void GenerateFabcoins(bool fGenerate, int nThreads, const CChainParams& chainparams, GPUConfig conf);
284 
286 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
287 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
288 
289 void Scan_nNonce_nSolution(CBlock *pblock, unsigned int n, unsigned int k);
290 void creategenesisblock ( uint32_t nTime, uint32_t nBits );
291 
292 
293 #endif // FABCOIN_MINER_H
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:169
#define f1(l, r, km, kr)
Definition: cast.cpp:17
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:68
int32_t nTimeLimit
Definition: miner.h:237
CFeeRate blockMinFeeRate
Definition: miner.h:217
Definition: miner.h:48
unsigned int nBlockMaxWeight
Definition: miner.h:197
bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b) const
Definition: miner.h:87
Definition: block.h:155
uint64_t hardBlockGasLimit
Definition: miner.h:228
uint64_t nBlockTx
Definition: miner.h:202
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:144
CMutableTransaction originalRewardTx
Definition: miner.h:234
int64_t nLockTimeCutoff
Definition: miner.h:209
CTxMemPool::setEntries inBlock
Definition: miner.h:205
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:191
void GenerateFabcoins(bool fGenerate, int nThreads, const CChainParams &chainparams)
Run the miner threads.
CBlock * pblock
Definition: miner.h:193
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:534
#define f2(l, r, km, kr)
Definition: cast.cpp:21
CChainParams defines various tweakable parameters of a given instance of the Fabcoin system...
Definition: chainparams.h:47
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:69
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:42
#define a(i)
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_or_gas_price >, boost::multi_index::identity< CTxMemPoolModifiedEntry >, CompareModifiedEntry > >> indexed_modified_transaction_set
Definition: miner.h:167
void Scan_nNonce_nSolution(CBlock *pblock, unsigned int n, unsigned int k)
CTxMemPool::txiter result_type
Definition: miner.h:76
const CChainParams & chainparams
Definition: miner.h:210
CAmount nFees
Definition: miner.h:204
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:174
size_t nBlockMaxWeight
Definition: miner.h:215
Generate a new block, without valid proof-of-work.
Definition: miner.h:187
std::vector< CAmount > vTxFees
Definition: miner.h:41
Parameters that influence chain consensus.
Definition: params.h:39
CTxMemPool::txiter iter
Definition: miner.h:183
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:65
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:526
#define b(i, j)
uint64_t softBlockGasLimit
Definition: miner.h:229
uint64_t nSizeWithAncestors
Definition: miner.h:58
ByteCodeExecResult bceResult
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.h:226
uint64_t nBlockSigOpsCost
Definition: miner.h:203
CAmount nModFeesWithAncestors
Definition: miner.h:59
void creategenesisblock(uint32_t nTime, uint32_t nBits)
CFeeRate blockMinFeeRate
Definition: miner.h:198
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:177
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
int nHeight
Definition: miner.h:208
int64_t nSigOpCostWithAncestors
Definition: miner.h:60
uint64_t nBlockWeight
Definition: miner.h:201
Fee rate in liu per kilobyte: CAmount / kB.
Definition: feerate.h:20
bool fIncludeWitness
Definition: miner.h:196
#define e(i)
Definition: sha.cpp:733
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:49
A mutable version of CTransaction.
Definition: transaction.h:390
CBlock block
Definition: miner.h:40
CTxMemPool::txiter iter
Definition: miner.h:57
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:715
uint64_t txGasLimit
Definition: miner.h:230
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:43
indexed_modified_transaction_set::index< ancestor_score_or_gas_price >::type::iterator modtxscoreiter
Definition: miner.h:170
Definition: miner.h:86
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:35