2015-01-24 15:57:12 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2015-01-24 15:57:12 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_CONSENSUS_VALIDATION_H
|
|
|
|
#define BITCOIN_CONSENSUS_VALIDATION_H
|
|
|
|
|
|
|
|
#include <string>
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <version.h>
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <primitives/block.h>
|
2015-01-24 15:57:12 +01:00
|
|
|
|
|
|
|
/** "reject" message codes */
|
|
|
|
static const unsigned char REJECT_MALFORMED = 0x01;
|
|
|
|
static const unsigned char REJECT_INVALID = 0x10;
|
|
|
|
static const unsigned char REJECT_OBSOLETE = 0x11;
|
|
|
|
static const unsigned char REJECT_DUPLICATE = 0x12;
|
|
|
|
static const unsigned char REJECT_NONSTANDARD = 0x40;
|
2017-06-09 01:18:42 +02:00
|
|
|
// static const unsigned char REJECT_DUST = 0x41; // part of BIP 61
|
2015-01-24 15:57:12 +01:00
|
|
|
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
|
|
|
|
static const unsigned char REJECT_CHECKPOINT = 0x43;
|
|
|
|
|
2019-01-16 04:11:13 +01:00
|
|
|
/** A "reason" why something was invalid, suitable for determining whether the
|
|
|
|
* provider of the object should be banned/ignored/disconnected/etc.
|
|
|
|
* These are much more granular than the rejection codes, which may be more
|
|
|
|
* useful for some other use-cases.
|
|
|
|
*/
|
|
|
|
enum class ValidationInvalidReason {
|
|
|
|
// txn and blocks:
|
|
|
|
NONE, //!< not actually invalid
|
|
|
|
CONSENSUS, //!< invalid by consensus rules (excluding any below reasons)
|
|
|
|
/**
|
|
|
|
* Invalid by a change to consensus rules more recent than SegWit.
|
|
|
|
* Currently unused as there are no such consensus rule changes, and any download
|
|
|
|
* sources realistically need to support SegWit in order to provide useful data,
|
|
|
|
* so differentiating between always-invalid and invalid-by-pre-SegWit-soft-fork
|
|
|
|
* is uninteresting.
|
|
|
|
*/
|
|
|
|
RECENT_CONSENSUS_CHANGE,
|
|
|
|
// Only blocks (or headers):
|
|
|
|
CACHED_INVALID, //!< this object was cached as being invalid, but we don't know why
|
|
|
|
BLOCK_INVALID_HEADER, //!< invalid proof of work or time too old
|
|
|
|
BLOCK_MUTATED, //!< the block's data didn't match the data committed to by the PoW
|
|
|
|
BLOCK_MISSING_PREV, //!< We don't have the previous block the checked one is built on
|
|
|
|
BLOCK_INVALID_PREV, //!< A block this one builds on is invalid
|
|
|
|
BLOCK_TIME_FUTURE, //!< block timestamp was > 2 hours in the future (or our clock is bad)
|
|
|
|
BLOCK_CHECKPOINT, //!< the block failed to meet one of our checkpoints
|
|
|
|
// Only loose txn:
|
|
|
|
TX_NOT_STANDARD, //!< didn't meet our local policy rules
|
|
|
|
TX_MISSING_INPUTS, //!< a transaction was missing some of its inputs (or its inputs were spent at < coinbase maturity height)
|
|
|
|
/**
|
|
|
|
* Transaction might be missing a witness, have a witness prior to SegWit
|
|
|
|
* activation, or witness may have been malleated (which includes
|
|
|
|
* non-standard witnesses).
|
|
|
|
*/
|
|
|
|
TX_WITNESS_MUTATED,
|
|
|
|
/**
|
|
|
|
* Tx already in mempool or conflicts with a tx in the chain
|
|
|
|
* (if it conflicts with another tx in mempool, we use MEMPOOL_POLICY as it failed to reach the RBF threshold)
|
|
|
|
* TODO: Currently this is only used if the transaction already exists in the mempool or on chain,
|
|
|
|
* TODO: ATMP's fMissingInputs and a valid CValidationState being used to indicate missing inputs
|
|
|
|
*/
|
|
|
|
TX_CONFLICT,
|
|
|
|
TX_MEMPOOL_POLICY, //!< violated mempool's fee/size/descendant/RBF/etc limits
|
|
|
|
};
|
|
|
|
|
2015-01-24 15:57:12 +01:00
|
|
|
/** Capture information about block/transaction validation */
|
|
|
|
class CValidationState {
|
|
|
|
private:
|
|
|
|
enum mode_state {
|
2016-06-29 11:48:51 +02:00
|
|
|
MODE_VALID, //!< everything ok
|
|
|
|
MODE_INVALID, //!< network rule violation (DoS value may be set)
|
|
|
|
MODE_ERROR, //!< run-time error
|
2015-01-24 15:57:12 +01:00
|
|
|
} mode;
|
2019-01-16 04:11:13 +01:00
|
|
|
ValidationInvalidReason m_reason;
|
2015-01-24 15:57:12 +01:00
|
|
|
int nDoS;
|
|
|
|
std::string strRejectReason;
|
2015-03-17 02:36:43 +01:00
|
|
|
unsigned int chRejectCode;
|
2015-01-24 15:57:12 +01:00
|
|
|
bool corruptionPossible;
|
2015-08-06 09:47:01 +02:00
|
|
|
std::string strDebugMessage;
|
2015-01-24 15:57:12 +01:00
|
|
|
public:
|
2019-01-16 04:11:13 +01:00
|
|
|
CValidationState() : mode(MODE_VALID), m_reason(ValidationInvalidReason::NONE), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
|
|
|
|
bool DoS(int level, ValidationInvalidReason reasonIn, bool ret = false,
|
2015-08-06 09:47:01 +02:00
|
|
|
unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="",
|
|
|
|
bool corruptionIn=false,
|
|
|
|
const std::string &strDebugMessageIn="") {
|
2019-01-16 04:11:13 +01:00
|
|
|
m_reason = reasonIn;
|
2015-01-24 15:57:12 +01:00
|
|
|
chRejectCode = chRejectCodeIn;
|
|
|
|
strRejectReason = strRejectReasonIn;
|
|
|
|
corruptionPossible = corruptionIn;
|
2015-08-06 09:47:01 +02:00
|
|
|
strDebugMessage = strDebugMessageIn;
|
2019-01-16 04:11:13 +01:00
|
|
|
nDoS += level;
|
|
|
|
assert(nDoS == GetDoSForReason());
|
|
|
|
assert(corruptionPossible == (m_reason == ValidationInvalidReason::BLOCK_MUTATED || m_reason == ValidationInvalidReason::TX_WITNESS_MUTATED));
|
2015-01-24 15:57:12 +01:00
|
|
|
if (mode == MODE_ERROR)
|
|
|
|
return ret;
|
|
|
|
mode = MODE_INVALID;
|
|
|
|
return ret;
|
|
|
|
}
|
2019-01-16 04:11:13 +01:00
|
|
|
bool Invalid(ValidationInvalidReason _reason, bool ret = false,
|
2015-08-06 09:47:01 +02:00
|
|
|
unsigned int _chRejectCode=0, const std::string &_strRejectReason="",
|
|
|
|
const std::string &_strDebugMessage="") {
|
2019-01-16 04:11:13 +01:00
|
|
|
return DoS(0, _reason, ret, _chRejectCode, _strRejectReason, false, _strDebugMessage);
|
2015-01-24 15:57:12 +01:00
|
|
|
}
|
2015-05-31 15:36:44 +02:00
|
|
|
bool Error(const std::string& strRejectReasonIn) {
|
2015-01-24 15:57:12 +01:00
|
|
|
if (mode == MODE_VALID)
|
|
|
|
strRejectReason = strRejectReasonIn;
|
|
|
|
mode = MODE_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool IsValid() const {
|
|
|
|
return mode == MODE_VALID;
|
|
|
|
}
|
|
|
|
bool IsInvalid() const {
|
|
|
|
return mode == MODE_INVALID;
|
|
|
|
}
|
|
|
|
bool IsError() const {
|
|
|
|
return mode == MODE_ERROR;
|
|
|
|
}
|
|
|
|
bool CorruptionPossible() const {
|
2019-01-16 04:11:13 +01:00
|
|
|
assert(corruptionPossible == (m_reason == ValidationInvalidReason::BLOCK_MUTATED || m_reason == ValidationInvalidReason::TX_WITNESS_MUTATED));
|
2015-01-24 15:57:12 +01:00
|
|
|
return corruptionPossible;
|
|
|
|
}
|
2015-11-06 01:42:38 +01:00
|
|
|
void SetCorruptionPossible() {
|
|
|
|
corruptionPossible = true;
|
2019-01-16 04:11:13 +01:00
|
|
|
assert(corruptionPossible == (m_reason == ValidationInvalidReason::BLOCK_MUTATED || m_reason == ValidationInvalidReason::TX_WITNESS_MUTATED));
|
2015-11-06 01:42:38 +01:00
|
|
|
}
|
2018-04-16 18:52:03 +02:00
|
|
|
int GetDoS(void) const { return nDoS; }
|
2019-01-16 04:11:13 +01:00
|
|
|
int GetDoSForReason() const {
|
|
|
|
switch (m_reason) {
|
|
|
|
case ValidationInvalidReason::NONE:
|
|
|
|
return 0;
|
|
|
|
case ValidationInvalidReason::CONSENSUS:
|
|
|
|
case ValidationInvalidReason::BLOCK_MUTATED:
|
|
|
|
case ValidationInvalidReason::BLOCK_INVALID_HEADER:
|
|
|
|
case ValidationInvalidReason::BLOCK_CHECKPOINT:
|
|
|
|
case ValidationInvalidReason::BLOCK_INVALID_PREV:
|
|
|
|
return 100;
|
|
|
|
case ValidationInvalidReason::BLOCK_MISSING_PREV:
|
|
|
|
return 10;
|
|
|
|
case ValidationInvalidReason::CACHED_INVALID:
|
|
|
|
case ValidationInvalidReason::RECENT_CONSENSUS_CHANGE:
|
|
|
|
case ValidationInvalidReason::BLOCK_TIME_FUTURE:
|
|
|
|
case ValidationInvalidReason::TX_NOT_STANDARD:
|
|
|
|
case ValidationInvalidReason::TX_MISSING_INPUTS:
|
|
|
|
case ValidationInvalidReason::TX_WITNESS_MUTATED:
|
|
|
|
case ValidationInvalidReason::TX_CONFLICT:
|
|
|
|
case ValidationInvalidReason::TX_MEMPOOL_POLICY:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ValidationInvalidReason GetReason() const { return m_reason; }
|
2015-03-17 02:36:43 +01:00
|
|
|
unsigned int GetRejectCode() const { return chRejectCode; }
|
2015-01-24 15:57:12 +01:00
|
|
|
std::string GetRejectReason() const { return strRejectReason; }
|
2015-08-06 09:47:01 +02:00
|
|
|
std::string GetDebugMessage() const { return strDebugMessage; }
|
2015-01-24 15:57:12 +01:00
|
|
|
};
|
|
|
|
|
2017-09-14 16:59:09 +02:00
|
|
|
// These implement the weight = (stripped_size * 4) + witness_size formula,
|
|
|
|
// using only serialization with and without witness data. As witness_size
|
|
|
|
// is equal to total_size - stripped_size, this formula is identical to:
|
|
|
|
// weight = (stripped_size * 3) + total_size.
|
2017-06-17 02:18:42 +02:00
|
|
|
static inline int64_t GetTransactionWeight(const CTransaction& tx)
|
|
|
|
{
|
2018-06-22 20:27:18 +02:00
|
|
|
return ::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, PROTOCOL_VERSION);
|
2017-06-17 02:18:42 +02:00
|
|
|
}
|
|
|
|
static inline int64_t GetBlockWeight(const CBlock& block)
|
|
|
|
{
|
2018-06-22 20:27:18 +02:00
|
|
|
return ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, PROTOCOL_VERSION);
|
2017-06-17 02:18:42 +02:00
|
|
|
}
|
2018-03-21 10:54:17 +01:00
|
|
|
static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
|
2018-03-05 22:37:24 +01:00
|
|
|
{
|
|
|
|
// scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
|
2018-06-22 20:27:18 +02:00
|
|
|
return ::GetSerializeSize(txin, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txin, PROTOCOL_VERSION) + ::GetSerializeSize(txin.scriptWitness.stack, PROTOCOL_VERSION);
|
2018-03-05 22:37:24 +01:00
|
|
|
}
|
2017-06-17 02:18:42 +02:00
|
|
|
|
2015-01-24 15:57:12 +01:00
|
|
|
#endif // BITCOIN_CONSENSUS_VALIDATION_H
|