Skip to content

Commit

Permalink
Use Consensus::Params in ContextualCheckTransaction and variations in…
Browse files Browse the repository at this point in the history
…stead of Config

Summary:
This also reduce needless dependencies on the Config.

Depends on D4030

Test Plan:
  make check

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D4031
  • Loading branch information
deadalnix committed Sep 11, 2019
1 parent 35bb8e4 commit 8cd6262
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 37 deletions.
12 changes: 5 additions & 7 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

#include <amount.h>
#include <chain.h>
#include <chainparams.h>
#include <coins.h>
#include <config.h>
#include <consensus/activation.h>
#include <consensus/consensus.h>
#include <consensus/params.h>
#include <consensus/validation.h>
#include <primitives/transaction.h>
#include <script/script_flags.h>
Expand Down Expand Up @@ -38,9 +37,9 @@ static bool IsFinalTx(const CTransaction &tx, int nBlockHeight,
return true;
}

bool ContextualCheckTransaction(const Config &config, const CTransaction &tx,
CValidationState &state, int nHeight,
int64_t nLockTimeCutoff,
bool ContextualCheckTransaction(const Consensus::Params &params,
const CTransaction &tx, CValidationState &state,
int nHeight, int64_t nLockTimeCutoff,
int64_t nMedianTimePast) {
if (!IsFinalTx(tx, nHeight, nLockTimeCutoff)) {
// While this is only one transaction, we use txns in the error to
Expand All @@ -49,8 +48,7 @@ bool ContextualCheckTransaction(const Config &config, const CTransaction &tx,
"non-final transaction");
}

if (IsMagneticAnomalyEnabled(config.GetChainParams().GetConsensus(),
nHeight)) {
if (IsMagneticAnomalyEnabled(params, nHeight)) {
// Size limit
if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) <
MIN_TX_SIZE) {
Expand Down
8 changes: 4 additions & 4 deletions src/consensus/tx_verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
struct Amount;
class CBlockIndex;
class CCoinsViewCache;
class Config;
class CTransaction;
class CValidationState;

Expand All @@ -23,6 +22,7 @@ bool CheckRegularTransaction(const CTransaction &tx, CValidationState &state);
bool CheckCoinbase(const CTransaction &tx, CValidationState &state);

namespace Consensus {
struct Params;

/**
* Check whether all inputs of this transaction are valid (no double spends and
Expand All @@ -43,9 +43,9 @@ bool CheckTxInputs(const CTransaction &tx, CValidationState &state,
* simply characteristic that are suceptible to change over time such as feature
* activation/deactivation and CLTV.
*/
bool ContextualCheckTransaction(const Config &config, const CTransaction &tx,
CValidationState &state, int nHeight,
int64_t nLockTimeCutoff,
bool ContextualCheckTransaction(const Consensus::Params &params,
const CTransaction &tx, CValidationState &state,
int nHeight, int64_t nLockTimeCutoff,
int64_t nMedianTimePast);

/**
Expand Down
6 changes: 4 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ bool BlockAssembler::TestPackageTransactions(
uint64_t nPotentialBlockSize = nBlockSize;
for (CTxMemPool::txiter it : package) {
CValidationState state;
if (!ContextualCheckTransaction(*config, it->GetTx(), state, nHeight,
if (!ContextualCheckTransaction(config->GetChainParams().GetConsensus(),
it->GetTx(), state, nHeight,
nLockTimeCutoff, nMedianTimePast)) {
return false;
}
Expand Down Expand Up @@ -343,7 +344,8 @@ BlockAssembler::TestForBlock(CTxMemPool::txiter it) {
// Must check that lock times are still valid. This can be removed once MTP
// is always enforced as long as reorgs keep the mempool consistent.
CValidationState state;
if (!ContextualCheckTransaction(*config, it->GetTx(), state, nHeight,
if (!ContextualCheckTransaction(config->GetChainParams().GetConsensus(),
it->GetTx(), state, nHeight,
nLockTimeCutoff, nMedianTimePast)) {
return TestForBlockResult::TXCantFit;
}
Expand Down
16 changes: 9 additions & 7 deletions src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,13 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) {
txid,
entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));

const Consensus::Params &params = config.GetChainParams().GetConsensus();

{
// Locktime passes.
CValidationState state;
BOOST_CHECK(ContextualCheckTransactionForCurrentBlock(
config, CTransaction(tx), state, flags));
params, CTransaction(tx), state, flags));
}

// Sequence locks fail.
Expand All @@ -572,7 +574,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) {
// Locktime passes.
CValidationState state;
BOOST_CHECK(ContextualCheckTransactionForCurrentBlock(
config, CTransaction(tx), state, flags));
params, CTransaction(tx), state, flags));
}

// Sequence locks fail.
Expand Down Expand Up @@ -605,7 +607,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) {
// Locktime fails.
CValidationState state;
BOOST_CHECK(!ContextualCheckTransactionForCurrentBlock(
config, CTransaction(tx), state, flags));
params, CTransaction(tx), state, flags));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-txns-nonfinal");
}

Expand All @@ -617,7 +619,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) {
CValidationState state;
int64_t nMedianTimePast = chainActive.Tip()->GetMedianTimePast();
BOOST_CHECK(ContextualCheckTransaction(
config, CTransaction(tx), state, chainActive.Tip()->nHeight + 2,
params, CTransaction(tx), state, chainActive.Tip()->nHeight + 2,
nMedianTimePast, nMedianTimePast));
}

Expand All @@ -633,7 +635,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) {
// Locktime fails.
CValidationState state;
BOOST_CHECK(!ContextualCheckTransactionForCurrentBlock(
config, CTransaction(tx), state, flags));
params, CTransaction(tx), state, flags));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-txns-nonfinal");
}

Expand All @@ -645,7 +647,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) {
CValidationState state;
int64_t nMedianTimePast = chainActive.Tip()->GetMedianTimePast() + 1;
BOOST_CHECK(ContextualCheckTransaction(
config, CTransaction(tx), state, chainActive.Tip()->nHeight + 1,
params, CTransaction(tx), state, chainActive.Tip()->nHeight + 1,
nMedianTimePast, nMedianTimePast));
}

Expand All @@ -659,7 +661,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) {
// Locktime passes.
CValidationState state;
BOOST_CHECK(ContextualCheckTransactionForCurrentBlock(
config, CTransaction(tx), state, flags));
params, CTransaction(tx), state, flags));
}

// Sequence locks pass.
Expand Down
7 changes: 4 additions & 3 deletions src/test/transaction_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,17 +786,18 @@ BOOST_AUTO_TEST_CASE(test_IsStandard) {

BOOST_AUTO_TEST_CASE(txsize_activation_test) {
const Config &config = GetConfig();
const Consensus::Params &params = config.GetChainParams().GetConsensus();
const int32_t magneticAnomalyActivationHeight =
config.GetChainParams().GetConsensus().magneticAnomalyHeight;
params.magneticAnomalyHeight;

// A minimaly sized transction.
CTransaction minTx;
CValidationState state;

BOOST_CHECK(ContextualCheckTransaction(
config, minTx, state, magneticAnomalyActivationHeight - 1, 5678, 1234));
params, minTx, state, magneticAnomalyActivationHeight - 1, 5678, 1234));
BOOST_CHECK(!ContextualCheckTransaction(
config, minTx, state, magneticAnomalyActivationHeight, 5678, 1234));
params, minTx, state, magneticAnomalyActivationHeight, 5678, 1234));
BOOST_CHECK_EQUAL(state.GetRejectCode(), REJECT_INVALID);
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-txns-undersize");
}
Expand Down
4 changes: 2 additions & 2 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ void CTxMemPool::removeForReorg(const Config &config,
bool validLP = TestLockPointValidity(&lp);

CValidationState state;
if (!ContextualCheckTransactionForCurrentBlock(config, tx, state,
flags) ||
if (!ContextualCheckTransactionForCurrentBlock(
config.GetChainParams().GetConsensus(), tx, state, flags) ||
!CheckSequenceLocks(tx, flags, &lp, validLP)) {
// Note if CheckSequenceLocks fails the LockPoints may still be
// invalid. So it's critical that we remove the tx and not depend on
Expand Down
8 changes: 4 additions & 4 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ static bool AcceptToMemoryPoolWorker(
// be mined yet.
CValidationState ctxState;
if (!ContextualCheckTransactionForCurrentBlock(
config, tx, ctxState, STANDARD_LOCKTIME_VERIFY_FLAGS)) {
consensusParams, tx, ctxState, STANDARD_LOCKTIME_VERIFY_FLAGS)) {
// We copy the state from a dummy to ensure we don't increase the
// ban score of peer for transaction that could be valid in the future.
return state.DoS(
Expand Down Expand Up @@ -3645,7 +3645,7 @@ static bool ContextualCheckBlockHeader(const Config &config,
return true;
}

bool ContextualCheckTransactionForCurrentBlock(const Config &config,
bool ContextualCheckTransactionForCurrentBlock(const Consensus::Params &params,
const CTransaction &tx,
CValidationState &state,
int flags) {
Expand Down Expand Up @@ -3678,7 +3678,7 @@ bool ContextualCheckTransactionForCurrentBlock(const Config &config,
? nMedianTimePast
: GetAdjustedTime();

return ContextualCheckTransaction(config, tx, state, nBlockHeight,
return ContextualCheckTransaction(params, tx, state, nBlockHeight,
nLockTimeCutoff, nMedianTimePast);
}

Expand Down Expand Up @@ -3737,7 +3737,7 @@ static bool ContextualCheckBlock(const Config &config, const CBlock &block,
}
}

if (!ContextualCheckTransaction(config, tx, state, nHeight,
if (!ContextualCheckTransaction(consensusParams, tx, state, nHeight,
nLockTimeCutoff, nMedianTimePast)) {
// state set by ContextualCheckTransaction.
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ bool CheckBlock(
*
* See consensus/consensus.h for flag definitions.
*/
bool ContextualCheckTransactionForCurrentBlock(const Config &config,
bool ContextualCheckTransactionForCurrentBlock(const Consensus::Params &params,
const CTransaction &tx,
CValidationState &state,
int flags = -1);
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/finaltx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
bool CheckFinalTx(const CTransaction &tx, int flags = -1) {
auto &config = GetConfig();
CValidationState state;
return ContextualCheckTransactionForCurrentBlock(config, tx, state, flags);
return ContextualCheckTransactionForCurrentBlock(
config.GetChainParams().GetConsensus(), tx, state, flags);
}
15 changes: 9 additions & 6 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,9 @@ static UniValue getreceivedbyaddress(const Config &config,
const CWalletTx &wtx = pairWtx.second;

CValidationState state;
if (wtx.IsCoinBase() || !ContextualCheckTransactionForCurrentBlock(
config, *wtx.tx, state)) {
if (wtx.IsCoinBase() ||
!ContextualCheckTransactionForCurrentBlock(
config.GetChainParams().GetConsensus(), *wtx.tx, state)) {
continue;
}

Expand Down Expand Up @@ -881,8 +882,9 @@ static UniValue getreceivedbylabel(const Config &config,
for (const std::pair<const TxId, CWalletTx> &pairWtx : pwallet->mapWallet) {
const CWalletTx &wtx = pairWtx.second;
CValidationState state;
if (wtx.IsCoinBase() || !ContextualCheckTransactionForCurrentBlock(
config, *wtx.tx, state)) {
if (wtx.IsCoinBase() ||
!ContextualCheckTransactionForCurrentBlock(
config.GetChainParams().GetConsensus(), *wtx.tx, state)) {
continue;
}

Expand Down Expand Up @@ -1519,8 +1521,9 @@ static UniValue ListReceived(const Config &config, CWallet *const pwallet,
const CWalletTx &wtx = pairWtx.second;

CValidationState state;
if (wtx.IsCoinBase() || !ContextualCheckTransactionForCurrentBlock(
config, *wtx.tx, state)) {
if (wtx.IsCoinBase() ||
!ContextualCheckTransactionForCurrentBlock(
config.GetChainParams().GetConsensus(), *wtx.tx, state)) {
continue;
}

Expand Down

0 comments on commit 8cd6262

Please sign in to comment.