dash/src/evo/providertx.h
Wladimir J. van der Laan eec81f7b33 Merge #15921: validation: Tidy up ValidationState interface
3004d5a12d09d94bfc4dee2a8e8f2291996a4aaf [validation] Remove fMissingInputs from AcceptToMemoryPool() (John Newbery)
c428622a5bb1e37b2e6ab2c52791ac05d9271238 [validation] Remove unused first_invalid parameter from ProcessNewBlockHeaders() (John Newbery)
7204c6434b944f6ad51b3c895837729d3aa56eea [validation] Remove useless ret parameter from Invalid() (John Newbery)
1a37de4b3174d19a6d8691ae07e92b32fdfaef11 [validation] Remove error() calls from Invalid() calls (John Newbery)
067981e49246822421a7bcc720491427e1dba8a3 [validation] Tidy Up ValidationResult class (John Newbery)
a27a2957ed9afbe5a96caa5f0f4cbec730d27460 [validation] Add CValidationState subclasses (John Newbery)

Pull request description:

  Carries out some remaining tidy-ups remaining after PR 15141:

  - split ValidationState into TxValidationState and BlockValidationState (commit from ajtowns)
  - various minor code style tidy-ups to the ValidationState class
  - remove the useless `ret` parameter from `ValidationState::Invalid()`
  - remove the now unused `first_invalid` parameter from `ProcessNewBlockHeaders()`
  - remove the `fMissingInputs` parameter from `AcceptToMemoryPool()`, and deal with missing inputs the same way as other errors by using the `TxValidationState` object.

  Tip for reviewers (thanks ryanofsky!): The first commit ("[validation] Add CValidationState subclasses" ) is huge and can be easier to start reviewing if you revert the rote, mechanical changes:

  Substitute the commit hash of commit "[validation] Add CValidationState subclasses" for <CommitHash> in the commands below.

  ```sh
  git checkout <CommitHash>
  git grep -l ValidationState | xargs sed -i 's/BlockValidationState\|TxValidationState/CValidationState/g'
  git grep -l ValidationResult | xargs sed -i 's/BlockValidationResult\|TxValidationResult/ValidationInvalidReason/g'
  git grep -l MaybePunish | xargs sed -i 's/MaybePunishNode\(ForBlock\|ForTx\)/MaybePunishNode/g'
  git diff HEAD^
  ```

  After that it's possible to easily see the mechanical changes with:

  ```sh
  git log -p -n1 -U0 --word-diff-regex=. <CommitHash>
  ```

ACKs for top commit:
  laanwj:
    ACK 3004d5a12d09d94bfc4dee2a8e8f2291996a4aaf
  amitiuttarwar:
    code review ACK 3004d5a12d09d94bfc4dee2a8e8f2291996a4aaf. Also built & ran tests locally.
  fjahr:
    Code review ACK 3004d5a12d09d94bfc4dee2a8e8f2291996a4aaf . Only nit style change and pure virtual destructor added since my last review.
  ryanofsky:
    Code review ACK 3004d5a12d09d94bfc4dee2a8e8f2291996a4aaf. Just whitespace change and pure virtual destructor added since last review.

Tree-SHA512: 511de1fb380a18bec1944ea82b513b6192df632ee08bb16344a2df3c40811a88f3872f04df24bc93a41643c96c48f376a04551840fd804a961490d6c702c3d36
2023-04-17 10:42:25 -05:00

340 lines
11 KiB
C++

// Copyright (c) 2018-2022 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_EVO_PROVIDERTX_H
#define BITCOIN_EVO_PROVIDERTX_H
#include <bls/bls.h>
#include <evo/specialtx.h>
#include <primitives/transaction.h>
#include <consensus/validation.h>
#include <evo/dmn_types.h>
#include <key_io.h>
#include <netaddress.h>
#include <pubkey.h>
#include <tinyformat.h>
#include <univalue.h>
#include <util/underlying.h>
class CBlockIndex;
class CCoinsViewCache;
class TxValidationState;
class CProRegTx
{
public:
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_REGISTER;
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
static constexpr uint16_t BASIC_BLS_VERSION = 2;
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
{
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
}
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
MnType nType{MnType::Regular};
uint16_t nMode{0}; // only 0 supported for now
COutPoint collateralOutpoint{uint256(), (uint32_t)-1}; // if hash is null, we refer to a ProRegTx output
CService addr;
uint160 platformNodeID{};
uint16_t platformP2PPort{0};
uint16_t platformHTTPPort{0};
CKeyID keyIDOwner;
CBLSPublicKey pubKeyOperator;
CKeyID keyIDVoting;
uint16_t nOperatorReward{0};
CScript scriptPayout;
uint256 inputsHash; // replay protection
std::vector<unsigned char> vchSig;
SERIALIZE_METHODS(CProRegTx, obj)
{
READWRITE(
obj.nVersion
);
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
// unknown version, bail out early
return;
}
READWRITE(
obj.nType,
obj.nMode,
obj.collateralOutpoint,
obj.addr,
obj.keyIDOwner,
CBLSPublicKeyVersionWrapper(const_cast<CBLSPublicKey&>(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)),
obj.keyIDVoting,
obj.nOperatorReward,
obj.scriptPayout,
obj.inputsHash
);
if (obj.nVersion == BASIC_BLS_VERSION && obj.nType == MnType::HighPerformance) {
READWRITE(
obj.platformNodeID,
obj.platformP2PPort,
obj.platformHTTPPort);
}
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
}
// When signing with the collateral key, we don't sign the hash but a generated message instead
// This is needed for HW wallet support which can only sign text messages as of now
std::string MakeSignString() const;
std::string ToString() const;
void ToJson(UniValue& obj) const
{
obj.clear();
obj.setObject();
obj.pushKV("version", nVersion);
obj.pushKV("type", ToUnderlying(nType));
obj.pushKV("collateralHash", collateralOutpoint.hash.ToString());
obj.pushKV("collateralIndex", (int)collateralOutpoint.n);
obj.pushKV("service", addr.ToString(false));
obj.pushKV("ownerAddress", EncodeDestination(PKHash(keyIDOwner)));
obj.pushKV("votingAddress", EncodeDestination(PKHash(keyIDVoting)));
CTxDestination dest;
if (ExtractDestination(scriptPayout, dest)) {
obj.pushKV("payoutAddress", EncodeDestination(dest));
}
obj.pushKV("pubKeyOperator", pubKeyOperator.ToString(nVersion == LEGACY_BLS_VERSION));
obj.pushKV("operatorReward", (double)nOperatorReward / 100);
if (nType == MnType::HighPerformance) {
obj.pushKV("platformNodeID", platformNodeID.ToString());
obj.pushKV("platformP2PPort", platformP2PPort);
obj.pushKV("platformHTTPPort", platformHTTPPort);
}
obj.pushKV("inputsHash", inputsHash.ToString());
}
maybe_error IsTriviallyValid(bool is_bls_legacy_scheme) const;
};
class CProUpServTx
{
public:
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_SERVICE;
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
static constexpr uint16_t BASIC_BLS_VERSION = 2;
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
{
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
}
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
MnType nType{MnType::Regular};
uint256 proTxHash;
CService addr;
uint160 platformNodeID{};
uint16_t platformP2PPort{0};
uint16_t platformHTTPPort{0};
CScript scriptOperatorPayout;
uint256 inputsHash; // replay protection
CBLSSignature sig;
SERIALIZE_METHODS(CProUpServTx, obj)
{
READWRITE(
obj.nVersion
);
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
// unknown version, bail out early
return;
}
if (obj.nVersion == BASIC_BLS_VERSION) {
READWRITE(
obj.nType);
}
READWRITE(
obj.proTxHash,
obj.addr,
obj.scriptOperatorPayout,
obj.inputsHash
);
if (obj.nVersion == BASIC_BLS_VERSION && obj.nType == MnType::HighPerformance) {
READWRITE(
obj.platformNodeID,
obj.platformP2PPort,
obj.platformHTTPPort);
}
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == LEGACY_BLS_VERSION), true)
);
}
}
std::string ToString() const;
void ToJson(UniValue& obj) const
{
obj.clear();
obj.setObject();
obj.pushKV("version", nVersion);
obj.pushKV("type", ToUnderlying(nType));
obj.pushKV("proTxHash", proTxHash.ToString());
obj.pushKV("service", addr.ToString(false));
CTxDestination dest;
if (ExtractDestination(scriptOperatorPayout, dest)) {
obj.pushKV("operatorPayoutAddress", EncodeDestination(dest));
}
if (nType == MnType::HighPerformance) {
obj.pushKV("platformNodeID", platformNodeID.ToString());
obj.pushKV("platformP2PPort", platformP2PPort);
obj.pushKV("platformHTTPPort", platformHTTPPort);
}
obj.pushKV("inputsHash", inputsHash.ToString());
}
maybe_error IsTriviallyValid(bool is_bls_legacy_scheme) const;
};
class CProUpRegTx
{
public:
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_REGISTRAR;
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
static constexpr uint16_t BASIC_BLS_VERSION = 2;
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
{
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
}
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
uint256 proTxHash;
uint16_t nMode{0}; // only 0 supported for now
CBLSPublicKey pubKeyOperator;
CKeyID keyIDVoting;
CScript scriptPayout;
uint256 inputsHash; // replay protection
std::vector<unsigned char> vchSig;
SERIALIZE_METHODS(CProUpRegTx, obj)
{
READWRITE(
obj.nVersion
);
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
// unknown version, bail out early
return;
}
READWRITE(
obj.proTxHash,
obj.nMode,
CBLSPublicKeyVersionWrapper(const_cast<CBLSPublicKey&>(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)),
obj.keyIDVoting,
obj.scriptPayout,
obj.inputsHash
);
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(
obj.vchSig
);
}
}
std::string ToString() const;
void ToJson(UniValue& obj) const
{
obj.clear();
obj.setObject();
obj.pushKV("version", nVersion);
obj.pushKV("proTxHash", proTxHash.ToString());
obj.pushKV("votingAddress", EncodeDestination(PKHash(keyIDVoting)));
CTxDestination dest;
if (ExtractDestination(scriptPayout, dest)) {
obj.pushKV("payoutAddress", EncodeDestination(dest));
}
obj.pushKV("pubKeyOperator", pubKeyOperator.ToString(nVersion == LEGACY_BLS_VERSION));
obj.pushKV("inputsHash", inputsHash.ToString());
}
maybe_error IsTriviallyValid(bool is_bls_legacy_scheme) const;
};
class CProUpRevTx
{
public:
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_REVOKE;
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
static constexpr uint16_t BASIC_BLS_VERSION = 2;
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
{
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
}
// these are just informational and do not have any effect on the revocation
enum {
REASON_NOT_SPECIFIED = 0,
REASON_TERMINATION_OF_SERVICE = 1,
REASON_COMPROMISED_KEYS = 2,
REASON_CHANGE_OF_KEYS = 3,
REASON_LAST = REASON_CHANGE_OF_KEYS
};
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
uint256 proTxHash;
uint16_t nReason{REASON_NOT_SPECIFIED};
uint256 inputsHash; // replay protection
CBLSSignature sig;
SERIALIZE_METHODS(CProUpRevTx, obj)
{
READWRITE(
obj.nVersion
);
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
// unknown version, bail out early
return;
}
READWRITE(
obj.proTxHash,
obj.nReason,
obj.inputsHash
);
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == LEGACY_BLS_VERSION), true)
);
}
}
std::string ToString() const;
void ToJson(UniValue& obj) const
{
obj.clear();
obj.setObject();
obj.pushKV("version", nVersion);
obj.pushKV("proTxHash", proTxHash.ToString());
obj.pushKV("reason", (int)nReason);
obj.pushKV("inputsHash", inputsHash.ToString());
}
maybe_error IsTriviallyValid(bool is_bls_legacy_scheme) const;
};
template <typename ProTx>
static maybe_error CheckInputsHash(const CTransaction& tx, const ProTx& proTx)
{
if (uint256 inputsHash = CalcTxInputsHash(tx); inputsHash != proTx.inputsHash) {
return {TxValidationResult::TX_CONSENSUS, "bad-protx-inputs-hash"};
}
return {};
}
#endif // BITCOIN_EVO_PROVIDERTX_H