Skip to content

Commit

Permalink
Ensure correct use of Params()
Browse files Browse the repository at this point in the history
  • Loading branch information
FornaxA committed Mar 31, 2020
1 parent 31bc2ef commit d425550
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "tx_verify.h"

#include "chainparams.h"
#include "consensus.h"
#include "primitives/transaction.h"
#include "script/interpreter.h"
Expand Down Expand Up @@ -215,7 +216,7 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state, const boo
return true;
}

bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee)
bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee, const Consensus::Params& params)
{
// are the actual inputs available?
if (!inputs.HaveInputs(tx)) {
Expand All @@ -230,22 +231,22 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c
assert(!coin.IsSpent());

// If prev is coinbase, coinstake or group authority confirguration, check that it's matured
if (coin.IsCoinStake() && nSpendHeight - coin.nHeight < (nSpendHeight <= 100 ? (int)10 : Params().nCoinbaseMaturity)) {
if (coin.IsCoinStake() && nSpendHeight - coin.nHeight < (nSpendHeight <= 100 ? (int)10 : params.nCoinbaseMaturity)) {
return state.Invalid(false,
REJECT_INVALID, "bad-txns-premature-spend-of-coinstake",
strprintf("tried to spend coinstake at depth %d", nSpendHeight - coin.nHeight));
}

if (coin.IsCoinBase() && nSpendHeight - coin.nHeight < (nSpendHeight <= 100 ? (int)10 : Params().nCoinbaseMaturity)) {
if (coin.IsCoinBase() && nSpendHeight - coin.nHeight < (nSpendHeight <= 100 ? (int)10 : params.nCoinbaseMaturity)) {
return state.Invalid(false,
REJECT_INVALID, "bad-txns-premature-spend-of-coinbase",
strprintf("tried to spend coinbase at depth %d", nSpendHeight - coin.nHeight));
}

if (IsOutputGroupedAuthority(coin.out)) {
if (nSpendHeight - coin.nHeight < Params().nOpGroupNewRequiredConfirmations) {
if (nSpendHeight - coin.nHeight < params.nOpGroupNewRequiredConfirmations) {
return state.Invalid(
error("CheckInputs() : tried to use a token authority before it reached maturity (%d confirmations)", Params().nOpGroupNewRequiredConfirmations),
error("CheckInputs() : tried to use a token authority before it reached maturity (%d confirmations)", params.nOpGroupNewRequiredConfirmations),
REJECT_INVALID, "bad-txns-premature-use-of-token-authority");
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/consensus/tx_verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BITCOIN_CONSENSUS_TX_VERIFY_H

#include "amount.h"
#include "consensus/params.h"

#include <stdint.h>
#include <vector>
Expand All @@ -27,7 +28,7 @@ namespace Consensus {
* @param[out] txfee Set to the transaction fee if successful.
* Preconditions: tx.IsCoinBase() is false.
*/
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee);
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee, const Consensus::Params& params);
} // namespace Consensus

/** Auxiliary functions for transaction validation (ideally should not be exposed) */
Expand Down
2 changes: 1 addition & 1 deletion src/qt/transactiondesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, TransactionReco

if (wtx.IsCoinBase() || wtx.IsCoinStake())
{
int numBlocksToMaturity = Consensus::Params().nCoinbaseMaturity;
int numBlocksToMaturity = Params().GetConsensus().nCoinbaseMaturity;
strHTML += "<br>" + tr("Generated coins must mature %1 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \"not accepted\" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.").arg(QString::number(numBlocksToMaturity)) + "<br>";
}

Expand Down
4 changes: 2 additions & 2 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMem
continue;
const Coin &coin = pcoins->AccessCoin(txin.prevout);
if (nCheckFrequency != 0) assert(!coin.IsSpent());
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < Consensus::Params().nCoinbaseMaturity)) {
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < Params().GetConsensus().nCoinbaseMaturity)) {
txToRemove.insert(it);
break;
}
Expand Down Expand Up @@ -1010,7 +1010,7 @@ static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& m
{
CValidationState state;
CAmount txfee = 0;
bool fCheckResult = tx.IsCoinBase() || Consensus::CheckTxInputs(tx, state, mempoolDuplicate, spendheight, txfee);
bool fCheckResult = tx.IsCoinBase() || Consensus::CheckTxInputs(tx, state, mempoolDuplicate, spendheight, txfee, Params().GetConsensus());
assert(fCheckResult);
UpdateCoins(tx, mempoolDuplicate, 1000000);
}
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
} // end LOCK(pool.cs)

CAmount nFees = 0;
if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees)) {
if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees, chainparams.GetConsensus())) {
return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
}

Expand Down Expand Up @@ -2111,7 +2111,7 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
} else if (!tx->IsCoinBase())
{
CAmount txfee = 0;
if (!Consensus::CheckTxInputs(*tx, state, view, pindex->nHeight, txfee)) {
if (!Consensus::CheckTxInputs(*tx, state, view, pindex->nHeight, txfee, Params().GetConsensus())) {
return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx->GetHash().ToString(), FormatStateMessage(state));
}
nFees += txfee;
Expand Down
5 changes: 3 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base58.h"
#include "checkpoints.h"
#include "chain.h"
#include "chainparams.h"
#include "wallet/coincontrol.h"
#include "consensus/consensus.h"
#include "consensus/validation.h"
Expand Down Expand Up @@ -6074,8 +6075,8 @@ int CMerkleTx::GetBlocksToMaturity() const
int depth = GetDepthInMainChain();
int minBlocksToMaturity = 0;
if (IsAnyOutputGroupedAuthority((CTransaction(*this))))
minBlocksToMaturity = std::max(0, (Consensus::Params().nOpGroupNewRequiredConfirmations + 1) - depth);
return std::max(minBlocksToMaturity, (Consensus::Params().nCoinbaseMaturity + 1) - depth);
minBlocksToMaturity = std::max(0, (Params().GetConsensus().nOpGroupNewRequiredConfirmations + 1) - depth);
return std::max(minBlocksToMaturity, (Params().GetConsensus().nCoinbaseMaturity + 1) - depth);
}


Expand Down

0 comments on commit d425550

Please sign in to comment.