33 #include <boost/thread.hpp>
45 int64_t nOldTime = pblock->
nTime;
48 if (nOldTime < nNewTime)
49 pblock->
nTime = nNewTime;
55 return nNewTime - nOldTime;
61 txCoinbase.
vout.emplace_back();
62 txCoinbase.
vout[0].SetEmpty();
63 if (pScriptPubKey) txCoinbase.
vout[0].scriptPubKey = *pScriptPubKey;
64 txCoinbase.
vin.emplace_back();
70 std::vector<CStakeableOutput>* availableCoins,
bool stopPoSOnNewBlock)
72 boost::this_thread::interruption_point();
81 int64_t nTxNewTime = 0;
101 return error(
"Unable to sign coinstake with input %s-%d", stakeIn.
hash.
ToString(), stakeIn.
n);
104 pblock->
vtx.emplace_back(MakeTransactionRef(txCoinbase));
105 pblock->
vtx.emplace_back(MakeTransactionRef(txCoinStake));
106 pblock->
nTime = nTxNewTime;
113 const int nHeight = pindexPrev->
nHeight + 1;
123 if (txCoinbase.
vout.size() == 1) {
132 pblock->
vtx.emplace_back(MakeTransactionRef(
CreateCoinbaseTx(scriptPubKeyIn, pindexPrev)));
137 : chainparams(_chainparams), defaultPrintPriority(_defaultPrintPriority)
161 std::vector<CStakeableOutput>* availableCoins,
165 bool stopPoSOnNewBlock,
185 if (
Params().IsRegTestNet()) {
199 for (
const auto& p :
Params().GetConsensus().llmqs) {
217 if (!fProofOfStake) {
221 pblock->
vtx[0] = MakeTransactionRef(txCoinbase);
242 LogPrintf(
"%s: Signing new block with UTXO key failed \n", __func__);
249 if (prevBlock ==
nullptr &&
chainActive.
Tip() != pindexPrev)
return nullptr;
254 throw std::runtime_error(
264 for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
267 testSet.erase(iit++);
279 if (
nBlockSigOps + packageSigOps >= MAX_BLOCK_SIGOPS_CURRENT)
297 pblock->
vtx.emplace_back(iter->GetSharedTx());
303 nFees += iter->GetFee();
307 if (fPrintPriority) {
308 LogPrintf(
"feerate %s txid %s\n",
310 iter->GetTx().GetHash().ToString());
322 if (alreadyAdded.count(desc))
324 modtxiter mit = mapModifiedTx.find(desc);
325 if (mit == mapModifiedTx.end()) {
330 mapModifiedTx.insert(modEntry);
350 if (mapModifiedTx.count(it) ||
inBlock.count(it) || failedTx.count(it))
361 sortedEntries.clear();
362 sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
401 bool fUsingModified =
false;
407 fUsingModified =
true;
417 fUsingModified =
true;
429 uint64_t packageSize = iter->GetSizeWithAncestors();
430 CAmount packageFees = iter->GetModFeesWithAncestors();
431 unsigned int packageSigOps = iter->GetSigOpCountWithAncestors();
432 if (fUsingModified) {
433 packageSize = modit->nSizeWithAncestors;
434 packageFees = modit->nModFeesWithAncestors;
435 packageSigOps = modit->nSigOpCountWithAncestors;
444 if (fUsingModified) {
449 failedTx.insert(iter);
455 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
460 ancestors.insert(iter);
464 if (fUsingModified) {
466 failedTx.insert(iter);
472 std::vector<CTxMemPool::txiter> sortedEntries;
475 for (
size_t i = 0; i < sortedEntries.size(); ++i) {
477 if (iterSortedEntries->IsShielded()) {
483 if (
nSizeShielded + iterSortedEntries->GetTxSize() > MAX_BLOCK_SHIELDED_TXES_SIZE) {
491 mapModifiedTx.erase(iterSortedEntries);
512 for (
const auto &tx : pblock->
vtx) {
513 if (tx->IsShieldedTx()) {
519 return sapling_tree.
root();
524 bool SolveBlock(std::shared_ptr<CBlock>& pblock,
int nHeight)
526 unsigned int extraNonce = 0;
528 while (pblock->nNonce < std::numeric_limits<uint32_t>::max() &&
532 return pblock->nNonce != std::numeric_limits<uint32_t>::max();
539 if (hashPrevBlock != pblock->hashPrevBlock) {
541 hashPrevBlock = pblock->hashPrevBlock;
546 assert(txCoinbase.
vin[0].scriptSig.size() <= 100);
548 pblock->vtx[0] = MakeTransactionRef(txCoinbase);
int64_t CAmount
Amount in PIV (Can be negative)
void IncrementExtraNonce(std::shared_ptr< CBlock > &pblock, int nHeight, unsigned int &nExtraNonce)
int32_t ComputeBlockVersion(const Consensus::Params &consensus, int nHeight)
CMutableTransaction CreateCoinbaseTx(const CScript &scriptPubKeyIn, CBlockIndex *pindexPrev)
bool SolveBlock(std::shared_ptr< CBlock > &pblock, int nHeight)
Modify the nonce/extranonce in a block.
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
uint256 CalculateSaplingTreeRoot(CBlock *pblock, int nHeight, const CChainParams &chainparams)
bool SolveProofOfStake(CBlock *pblock, CBlockIndex *pindexPrev, CWallet *pwallet, std::vector< CStakeableOutput > *availableCoins, bool stopPoSOnNewBlock)
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
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
bool SignBlock(CBlock &block, const CKeyStore &keystore)
const CChainParams & Params()
Return the currently selected parameters.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
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.
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.
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint256 GetBlockHash() const
int64_t GetMedianTimePast() const
int nHeight
height of the entry in the chain. The genesis block has height 0
CBlockIndex * Tip(bool fProofOfStake=false) const
Returns the index entry for the tip of this chain, or nullptr if none.
CChainParams defines various tweakable parameters of a given instance of the PIVX system.
const Consensus::Params & GetConsensus() const
Fee rate in PIV per kilobyte: CAmount / kB.
std::string ToString() const
CAmount GetFee(size_t size) const
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
bool IsSporkActive(SporkId nSporkID)
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents=true) const
Try to calculate all in-mempool ancestors of entry.
std::set< txiter, CompareIteratorByHash > setEntries
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
indexed_transaction_set mapTx
void CalculateDescendants(txiter it, setEntries &setDescendants)
Populate setDescendants with all in-mempool descendants of hash.
Capture information about block/transaction validation.
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
A shielded output to a transaction.
uint256 cmu
The u-coordinate of the note commitment for the output note.
std::string ToString() const
std::string GetHex() const
bool CreateCoinStake(const CBlockIndex *pindexPrev, unsigned int nBits, CMutableTransaction &txNew, int64_t &nTxNewTime, std::vector< CStakeableOutput > *availableCoins, bool stopOnNewBlock=true) const
bool SignCoinStake(CMutableTransaction &txNew) const
void BlockUntilSyncedToCurrentChain()
Blocks until the wallet state is up-to-date to /at least/ the current chain at the time this function...
#define LogPrint(category,...)
void FillBlockPayee(CMutableTransaction &txCoinbase, CMutableTransaction &txCoinstake, const CBlockIndex *pindexPrev, bool fProofOfStake)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
std::unique_ptr< CQuorumBlockProcessor > quorumBlockProcessor
RecursiveMutex cs_main
Global state.
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock)
CSporkManager sporkManager
@ SPORK_20_SAPLING_MAINTENANCE
A mutable version of CTransaction.
std::vector< CTxOut > vout
unsigned int nSigOpCountWithAncestors
uint64_t nSizeWithAncestors
CAmount nModFeesWithAncestors
Parameters that influence chain consensus.
bool NetworkUpgradeActive(int nHeight, Consensus::UpgradeIndex idx) const
Returns true if the given network upgrade is active as of the given block height.
bool fPowAllowMinDifficultyBlocks
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
bool error(const char *fmt, const Args &... args)
int64_t GetAdjustedTime()
std::shared_ptr< const CTransaction > CTransactionRef
bool IsFinalTx(const CTransactionRef &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Count ECDSA signature operations the old-fashioned (pre-0.6) way.
const uint256 UINT256_ZERO
constant uint256 instances
bool NetworkUpgradeActive(int nHeight, const Consensus::Params ¶ms, Consensus::UpgradeIndex idx)
Returns true if the given network upgrade is active as of the given block height.
std::string FormatStateMessage(const CValidationState &state)
Convert CValidationState to a human-readable message for logging.
CTxMemPool mempool(::minRelayTxFee)
CFeeRate minRelayTxFee
Fees smaller than this (in upiv) are considered zero fee (for relaying, mining and transaction creati...
CScript COINBASE_FLAGS
Constant stuff for coinbase transactions we create:
std::unique_ptr< CCoinsViewCache > pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
CAmount GetBlockValue(int nHeight)
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
bool TestBlockValidity(CValidationState &state, const CBlock &block, CBlockIndex *const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckBlockSig)
Check a block is completely valid from start to finish (only works on top of our current best block,...