From cba69dda3da0e4fa39cff5ce4dc81d1242fe651b Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 11 Oct 2022 14:20:22 +1000 Subject: [PATCH 1/9] Move MANDATORY_SCRIPT_VERIFY_FLAGS from script/standard.h to policy/policy.h --- src/policy/policy.h | 16 ++++++++++++++-- src/script/standard.h | 10 ---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/policy/policy.h b/src/policy/policy.h index 9135cae91c1..657ee8b32df 100644 --- a/src/policy/policy.h +++ b/src/policy/policy.h @@ -69,10 +69,22 @@ static constexpr unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT_KVB{101}; * configurable as it doesn't materially change DoS parameters. */ static constexpr unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT{10000}; + + +/** + * Mandatory script verification flags that all new transactions must comply with for + * them to be valid. Failing one of these tests may trigger a DoS ban; + * see CheckInputScripts() for details. + * + * Note that this does not affect consensus validity; see GetBlockScriptFlags() + * for that. + */ +static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH; + /** * Standard script verification flags that standard transactions will comply - * with. However scripts violating these flags may still be present in valid - * blocks and we must accept those blocks. + * with. However we do not ban/disconnect nodes that forward txs violating + * these rules, for better forwards and backwards compatability. */ static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS{MANDATORY_SCRIPT_VERIFY_FLAGS | SCRIPT_VERIFY_DERSIG | diff --git a/src/script/standard.h b/src/script/standard.h index 18cf5c8c884..54065f6e302 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -38,16 +38,6 @@ public: */ static const unsigned int MAX_OP_RETURN_RELAY = 83; -/** - * Mandatory script verification flags that all new blocks must comply with for - * them to be valid. (but old blocks may not comply with) Currently just P2SH, - * but in the future other flags may be added. - * - * Failing one of these tests may trigger a DoS ban - see CheckInputScripts() for - * details. - */ -static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH; - enum class TxoutType { NONSTANDARD, // 'standard' transaction types: From b81ebff0d99c45c071b999796b8ae3f0f2517b22 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Tue, 8 Aug 2023 10:35:08 -0400 Subject: [PATCH 2/9] Remove ScriptHash from CScriptID constructor Replaces the constructor in CScriptID that converts a ScriptHash with a function ToScriptID that does the same. This prepares for a move of CScriptID to avoid a circular dependency. --- src/script/signingprovider.cpp | 2 +- src/script/standard.cpp | 6 +++++- src/script/standard.h | 3 +-- src/wallet/rpc/addresses.cpp | 3 +-- src/wallet/rpc/coins.cpp | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/script/signingprovider.cpp b/src/script/signingprovider.cpp index ef055573b97..fb5ae79c19f 100644 --- a/src/script/signingprovider.cpp +++ b/src/script/signingprovider.cpp @@ -205,7 +205,7 @@ CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& } if (auto script_hash = std::get_if(&dest)) { CScript script; - CScriptID script_id(*script_hash); + CScriptID script_id = ToScriptID(*script_hash); CTxDestination inner_dest; if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) { if (auto inner_witness_id = std::get_if(&inner_dest)) { diff --git a/src/script/standard.cpp b/src/script/standard.cpp index 7c4a05b6e69..e13784e0fc3 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -17,7 +17,6 @@ typedef std::vector valtype; CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {} -CScriptID::CScriptID(const ScriptHash& in) : BaseHash(static_cast(in)) {} ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in)) {} ScriptHash::ScriptHash(const CScriptID& in) : BaseHash(static_cast(in)) {} @@ -38,6 +37,11 @@ CKeyID ToKeyID(const WitnessV0KeyHash& key_hash) return CKeyID{static_cast(key_hash)}; } +CScriptID ToScriptID(const ScriptHash& script_hash) +{ + return CScriptID{static_cast(script_hash)}; +} + WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in) { CSHA256().Write(in.data(), in.size()).Finalize(begin()); diff --git a/src/script/standard.h b/src/script/standard.h index 54065f6e302..3e60ea453db 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -20,7 +20,6 @@ static const bool DEFAULT_ACCEPT_DATACARRIER = true; class CKeyID; class CScript; -struct ScriptHash; /** A reference to a CScript: the Hash160 of its serialization (see script.h) */ class CScriptID : public BaseHash @@ -29,7 +28,6 @@ public: CScriptID() : BaseHash() {} explicit CScriptID(const CScript& in); explicit CScriptID(const uint160& in) : BaseHash(in) {} - explicit CScriptID(const ScriptHash& in); }; /** @@ -80,6 +78,7 @@ struct ScriptHash : public BaseHash explicit ScriptHash(const CScript& script); explicit ScriptHash(const CScriptID& script); }; +CScriptID ToScriptID(const ScriptHash& script_hash); struct WitnessV0ScriptHash : public BaseHash { diff --git a/src/wallet/rpc/addresses.cpp b/src/wallet/rpc/addresses.cpp index a8ef0a5731f..06f396a6d7c 100644 --- a/src/wallet/rpc/addresses.cpp +++ b/src/wallet/rpc/addresses.cpp @@ -440,10 +440,9 @@ public: UniValue operator()(const ScriptHash& scripthash) const { - CScriptID scriptID(scripthash); UniValue obj(UniValue::VOBJ); CScript subscript; - if (provider && provider->GetCScript(scriptID, subscript)) { + if (provider && provider->GetCScript(ToScriptID(scripthash), subscript)) { ProcessSubScript(subscript, obj); } return obj; diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp index 22f0f0b83c5..521378e188c 100644 --- a/src/wallet/rpc/coins.cpp +++ b/src/wallet/rpc/coins.cpp @@ -672,7 +672,7 @@ RPCHelpMan listunspent() std::unique_ptr provider = pwallet->GetSolvingProvider(scriptPubKey); if (provider) { if (scriptPubKey.IsPayToScriptHash()) { - const CScriptID& hash = CScriptID(std::get(address)); + const CScriptID hash = ToScriptID(std::get(address)); CScript redeemScript; if (provider->GetCScript(hash, redeemScript)) { entry.pushKV("redeemScript", HexStr(redeemScript)); From 86ea8bed5473f400f7a93fcc455393a574a2f319 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Tue, 8 Aug 2023 10:39:01 -0400 Subject: [PATCH 3/9] Move CScriptID to script.{h/cpp} CScriptID should be next to CScript just as CKeyID is next to CPubKey --- src/compressor.cpp | 1 + src/interfaces/wallet.h | 1 + src/script/script.cpp | 3 +++ src/script/script.h | 11 +++++++++++ src/script/sign.cpp | 1 + src/script/standard.cpp | 2 -- src/script/standard.h | 9 --------- src/test/compress_tests.cpp | 1 + src/test/fuzz/integer.cpp | 1 + src/wallet/rpc/addresses.cpp | 1 + src/wallet/rpc/coins.cpp | 1 + src/wallet/rpc/spend.cpp | 1 + src/wallet/scriptpubkeyman.cpp | 1 + src/wallet/scriptpubkeyman.h | 1 + src/wallet/spend.cpp | 1 + src/wallet/walletdb.cpp | 1 + 16 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/compressor.cpp b/src/compressor.cpp index 32af8eab494..2dbd586bfac 100644 --- a/src/compressor.cpp +++ b/src/compressor.cpp @@ -6,6 +6,7 @@ #include #include +#include