Skip to content

Commit

Permalink
Miner support for signed blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Dec 19, 2018
1 parent 0e47322 commit 0d7e0f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
27 changes: 23 additions & 4 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <utilmoneystr.h>
#include <validationinterface.h>

// ELEMENTS
#include <block_proof.h> // ResetProof, ResetChallenge

#include <algorithm>
#include <queue>
#include <utility>
Expand Down Expand Up @@ -96,8 +99,9 @@ void BlockAssembler::resetBlock()
nFees = 0;
}

std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx)
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx, int min_tx_age)
{
assert(min_tx_age >= 0);
int64_t nTimeStart = GetTimeMicros();

resetBlock();
Expand Down Expand Up @@ -142,9 +146,18 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
// transaction (which in most cases can be a no-op).
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus()) && fMineWitnessTx;

if (g_signed_blocks) {
// Pad block weight by block proof fields (including upper-bound of signature)
nBlockWeight += chainparams.GetConsensus().signblockscript.size() * WITNESS_SCALE_FACTOR;
nBlockWeight += chainparams.GetConsensus().max_block_signature_size * WITNESS_SCALE_FACTOR;
// Reset block proof
ResetProof(*pblock);
ResetChallenge(*pblock, *pindexPrev, chainparams.GetConsensus());
}

int nPackagesSelected = 0;
int nDescendantsUpdated = 0;
addPackageTxs(nPackagesSelected, nDescendantsUpdated);
addPackageTxs(nPackagesSelected, nDescendantsUpdated, min_tx_age);

int64_t nTime1 = GetTimeMicros();

Expand All @@ -168,10 +181,10 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
// Fill in header
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
pblock->nBits = g_signed_blocks ? 0 : GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
if (g_con_blockheightinheader) {
pblock->block_height = nHeight;
}
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
pblock->nNonce = 0;
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);

Expand Down Expand Up @@ -306,7 +319,7 @@ void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::ve
// Each time through the loop, we compare the best transaction in
// mapModifiedTxs with the next transaction in the mempool to decide what
// transaction package to work on next.
void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated, int min_tx_age)
{
// mapModifiedTx will store sorted packages after they are modified
// because some of their txs are already in the block
Expand Down Expand Up @@ -362,6 +375,12 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
}
}

// Skip transactions that are under X seconds in mempool
// min_tx_age value of 0 is considered "inactive", in case of mocktime
if (min_tx_age > 0 && iter->GetTime() > GetTime() - min_tx_age) {
continue;
}

// We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
// contain anything that is inBlock.
assert(!inBlock.count(iter));
Expand Down
6 changes: 3 additions & 3 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ class BlockAssembler
explicit BlockAssembler(const CChainParams& params);
BlockAssembler(const CChainParams& params, const Options& options);

/** Construct a new block template with coinbase to scriptPubKeyIn */
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true);
/** Construct a new block template with coinbase to scriptPubKeyIn. min_tx_age is in seconds */
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true, int min_tx_age=0);

private:
// utility functions
Expand All @@ -170,7 +170,7 @@ class BlockAssembler
/** Add transactions based on feerate including unconfirmed ancestors
* Increments nPackagesSelected / nDescendantsUpdated with corresponding
* statistics from the package selection (for logging statistics). */
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated, int required_wait=0) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);

// helper functions for addPackageTxs()
/** Remove confirmed (inBlock) entries from given set */
Expand Down

0 comments on commit 0d7e0f9

Please sign in to comment.