Bitcoin Core 28.99.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages Concepts
sigopcount_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <addresstype.h>
6#include <coins.h>
9#include <key.h>
10#include <pubkey.h>
11#include <script/interpreter.h>
12#include <script/script.h>
13#include <script/solver.h>
15#include <uint256.h>
16
17#include <vector>
18
19#include <boost/test/unit_test.hpp>
20
21// Helpers:
22static std::vector<unsigned char>
24{
25 std::vector<unsigned char> sSerialized(s.begin(), s.end());
26 return sSerialized;
27}
28
30
32{
33 // Test CScript::GetSigOpCount()
34 CScript s1;
35 BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U);
36 BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U);
37
38 uint160 dummy;
39 s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG;
40 BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U);
41 s1 << OP_IF << OP_CHECKSIG << OP_ENDIF;
42 BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U);
43 BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U);
44
46 CScript scriptSig;
47 scriptSig << OP_0 << Serialize(s1);
48 BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U);
49
50 std::vector<CPubKey> keys;
51 for (int i = 0; i < 3; i++)
52 {
54 keys.push_back(k.GetPubKey());
55 }
56 CScript s2 = GetScriptForMultisig(1, keys);
57 BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U);
58 BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U);
59
61 BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U);
62 BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U);
63 CScript scriptSig2;
64 scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2);
65 BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U);
66}
67
72static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTransaction& input, uint32_t flags)
73{
74 ScriptError error;
75 CTransaction inputi(input);
76 bool ret = VerifyScript(inputi.vin[0].scriptSig, output.vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &error);
77 BOOST_CHECK((ret == true) == (error == SCRIPT_ERR_OK));
78
79 return error;
80}
81
87static void BuildTxs(CMutableTransaction& spendingTx, CCoinsViewCache& coins, CMutableTransaction& creationTx, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
88{
89 creationTx.version = 1;
90 creationTx.vin.resize(1);
91 creationTx.vin[0].prevout.SetNull();
92 creationTx.vin[0].scriptSig = CScript();
93 creationTx.vout.resize(1);
94 creationTx.vout[0].nValue = 1;
95 creationTx.vout[0].scriptPubKey = scriptPubKey;
96
97 spendingTx.version = 1;
98 spendingTx.vin.resize(1);
99 spendingTx.vin[0].prevout.hash = creationTx.GetHash();
100 spendingTx.vin[0].prevout.n = 0;
101 spendingTx.vin[0].scriptSig = scriptSig;
102 spendingTx.vin[0].scriptWitness = witness;
103 spendingTx.vout.resize(1);
104 spendingTx.vout[0].nValue = 1;
105 spendingTx.vout[0].scriptPubKey = CScript();
106
107 AddCoins(coins, CTransaction(creationTx), 0);
108}
109
110BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
111{
112 // Transaction creates outputs
113 CMutableTransaction creationTx;
114 // Transaction that spends outputs and whose
115 // sig op cost is going to be tested
116 CMutableTransaction spendingTx;
117
118 // Create utxo set
119 CCoinsView coinsDummy;
120 CCoinsViewCache coins(&coinsDummy);
121 // Create key
122 CKey key = GenerateRandomKey();
123 CPubKey pubkey = key.GetPubKey();
124 // Default flags
126
127 // Multisig script (legacy counting)
128 {
129 CScript scriptPubKey = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
130 // Do not use a valid signature to avoid using wallet operations.
131 CScript scriptSig = CScript() << OP_0 << OP_0;
132
133 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
134 // Legacy counting only includes signature operations in scriptSigs and scriptPubKeys
135 // of a transaction and does not take the actual executed sig operations into account.
136 // spendingTx in itself does not contain a signature operation.
137 assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
138 // creationTx contains two signature operations in its scriptPubKey, but legacy counting
139 // is not accurate.
141 // Sanity check: script verification fails because of an invalid signature.
143 }
144
145 // Multisig nested in P2SH
146 {
147 CScript redeemScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
148 CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
149 CScript scriptSig = CScript() << OP_0 << OP_0 << ToByteVector(redeemScript);
150
151 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
154 }
155
156 // P2WPKH witness program
157 {
158 CScript scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkey));
159 CScript scriptSig = CScript();
160 CScriptWitness scriptWitness;
161 scriptWitness.stack.emplace_back(0);
162 scriptWitness.stack.emplace_back(0);
163
164
165 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
166 assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
167 // No signature operations if we don't verify the witness.
169 assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
170
171 // The sig op cost for witness version != 0 is zero.
172 assert(scriptPubKey[0] == 0x00);
173 scriptPubKey[0] = 0x51;
174 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
175 assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
176 scriptPubKey[0] = 0x00;
177 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
178
179 // The witness of a coinbase transaction is not taken into account.
180 spendingTx.vin[0].prevout.SetNull();
181 assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
182 }
183
184 // P2WPKH nested in P2SH
185 {
187 CScript scriptPubKey = GetScriptForDestination(ScriptHash(scriptSig));
188 scriptSig = CScript() << ToByteVector(scriptSig);
189 CScriptWitness scriptWitness;
190 scriptWitness.stack.emplace_back(0);
191 scriptWitness.stack.emplace_back(0);
192
193 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
194 assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
195 assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
196 }
197
198 // P2WSH witness program
199 {
200 CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
201 CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
202 CScript scriptSig = CScript();
203 CScriptWitness scriptWitness;
204 scriptWitness.stack.emplace_back(0);
205 scriptWitness.stack.emplace_back(0);
206 scriptWitness.stack.emplace_back(witnessScript.begin(), witnessScript.end());
207
208 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
209 assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
212 }
213
214 // P2WSH nested in P2SH
215 {
216 CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
217 CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
218 CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
219 CScript scriptSig = CScript() << ToByteVector(redeemScript);
220 CScriptWitness scriptWitness;
221 scriptWitness.stack.emplace_back(0);
222 scriptWitness.stack.emplace_back(0);
223 scriptWitness.stack.emplace_back(witnessScript.begin(), witnessScript.end());
224
225 BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
226 assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
228 }
229}
230
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
int ret
int flags
Definition: bitcoin-tx.cpp:536
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
Abstract view on the open txout dataset.
Definition: coins.h:310
An encapsulated private key.
Definition: key.h:35
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:159
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
const std::vector< CTxOut > vout
Definition: transaction.h:307
const std::vector< CTxIn > vin
Definition: transaction.h:306
iterator begin()
Definition: prevector.h:302
iterator end()
Definition: prevector.h:304
160-bit opaque blob.
Definition: uint256.h:178
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
Definition: coins.cpp:119
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:49
@ SCRIPT_VERIFY_WITNESS
Definition: interpreter.h:108
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ OP_2
Definition: script.h:85
@ OP_CHECKMULTISIG
Definition: script.h:192
@ OP_IF
Definition: script.h:104
@ OP_CHECKSIG
Definition: script.h:190
@ OP_ENDIF
Definition: script.h:109
@ OP_1
Definition: script.h:83
@ OP_CHECKMULTISIGVERIFY
Definition: script.h:193
@ OP_0
Definition: script.h:76
static const int MAX_PUBKEYS_PER_MULTISIG
Definition: script.h:34
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:67
enum ScriptError_t ScriptError
@ SCRIPT_ERR_EQUALVERIFY
Definition: script_error.h:28
@ SCRIPT_ERR_OK
Definition: script_error.h:13
@ SCRIPT_ERR_CHECKMULTISIGVERIFY
Definition: script_error.h:29
static ScriptError VerifyWithFlag(const CTransaction &output, const CMutableTransaction &input, uint32_t flags)
Verifies script execution of the zeroth scriptPubKey of tx output and zeroth scriptSig and witness of...
static void BuildTxs(CMutableTransaction &spendingTx, CCoinsViewCache &coins, CMutableTransaction &creationTx, const CScript &scriptPubKey, const CScript &scriptSig, const CScriptWitness &witness)
Builds a creationTx from scriptPubKey and a spendingTx from scriptSig and witness such that spendingT...
static std::vector< unsigned char > Serialize(const CScript &s)
BOOST_AUTO_TEST_CASE(GetSigOpCount)
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: solver.cpp:218
Basic testing setup.
Definition: setup_common.h:63
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
std::vector< CTxIn > vin
Definition: transaction.h:379
std::vector< std::vector< unsigned char > > stack
Definition: script.h:588
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, uint32_t flags)
Compute total signature operation cost of a transaction.
Definition: tx_verify.cpp:143
assert(!tx.IsCoinBase())