Skip to content

Commit

Permalink
Introduce REJECT_INTERNAL codes for local AcceptToMempool errors
Browse files Browse the repository at this point in the history
Add status codes specific to AcceptToMempool procession of transactions.
These can never happen due to block validation, and must never be sent
over the P2P network. Add assertions where appropriate.

(cherry picked from commit bitcoin/bitcoin@dc58258)
  • Loading branch information
laanwj authored and str4d committed Aug 13, 2021
1 parent 36c70c0 commit aa97bd1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ bool AcceptToMemoryPool(
// is it already in the memory pool?
uint256 hash = tx.GetHash();
if (pool.exists(hash))
return false;
return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-in-mempool");

// Check for conflicts with in-memory transactions
for (unsigned int i = 0; i < tx.vin.size(); i++)
Expand All @@ -1787,7 +1787,7 @@ bool AcceptToMemoryPool(
if (pool.mapNextTx.count(outpoint))
{
// Disable replacement feature for now
return false;
return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict");
}
}
for (const JSDescription &joinsplit : tx.vJoinSplit) {
Expand All @@ -1813,7 +1813,7 @@ bool AcceptToMemoryPool(

// do we already have it?
if (view.HaveCoins(hash))
return false;
return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-known");

// do all inputs exist?
// Note that this does not check for the presence of actual outputs (see the next check for that),
Expand All @@ -1822,7 +1822,7 @@ bool AcceptToMemoryPool(
if (!view.HaveCoins(txin.prevout.hash)) {
if (pfMissingInputs)
*pfMissingInputs = true;
return false;
return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
}
}

Expand Down Expand Up @@ -1853,7 +1853,7 @@ bool AcceptToMemoryPool(

// Check for non-standard pay-to-script-hash in inputs
if (chainparams.RequireStandard() && !AreInputsStandard(tx, view, consensusBranchId))
return error("AcceptToMemoryPool: nonstandard transaction input");
return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");

// Check that the transaction doesn't have an excessive number of
// sigops, making it impossible to mine. Since the coinbase transaction
Expand Down Expand Up @@ -2415,7 +2415,7 @@ void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state
if (state.IsInvalid(nDoS)) {
std::map<uint256, NodeId>::iterator it = mapBlockSource.find(pindex->GetBlockHash());
if (it != mapBlockSource.end() && State(it->second)) {
assert(state.GetRejectCode() < 0x100);
assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), pindex->GetBlockHash()};
State(it->second)->rejects.push_back(reject);
if (nDoS > 0)
Expand Down Expand Up @@ -6779,8 +6779,9 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
LogPrint("mempool", "%s from peer=%d %s was not accepted into the memory pool: %s\n", tx.GetHash().ToString(),
pfrom->id, pfrom->cleanSubVer,
state.GetRejectReason());
pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
if (state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
if (nDoS > 0)
Misbehaving(pfrom->GetId(), nDoS);
}
Expand Down Expand Up @@ -6882,6 +6883,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
ProcessNewBlock(state, chainparams, pfrom, &block, forceProcessing, NULL);
int nDoS;
if (state.IsInvalid(nDoS)) {
assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
if (nDoS > 0) {
Expand Down
11 changes: 10 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,17 @@ extern CBlockTreeDB *pblocktree;
*/
int GetSpendHeight(const CCoinsViewCache& inputs);

/** local "reject" message codes for RPC which can not be triggered by p2p trasactions */
/** Reject codes greater or equal to this can be returned by AcceptToMemPool
* for transactions, to signal internal conditions. They cannot and should not
* be sent over the P2P network.
*/
static const unsigned int REJECT_INTERNAL = 0x100;
/** Too high fee. Can not be triggered by P2P transactions */
static const unsigned int REJECT_HIGHFEE = 0x100;
/** Transaction is already known (either in mempool or blockchain) */
static const unsigned int REJECT_ALREADY_KNOWN = 0x101;
/** Transaction conflicts with a transaction already known */
static const unsigned int REJECT_CONFLICT = 0x102;

uint64_t CalculateCurrentUsage();

Expand Down

0 comments on commit aa97bd1

Please sign in to comment.