Bitcoin Core  22.99.0
P2P Digital Currency
sigopcount_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 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 <consensus/consensus.h>
6 #include <consensus/tx_verify.h>
7 #include <key.h>
8 #include <pubkey.h>
9 #include <script/script.h>
10 #include <script/standard.h>
11 #include <test/util/setup_common.h>
12 #include <uint256.h>
13 
14 #include <vector>
15 
16 #include <boost/test/unit_test.hpp>
17 
18 // Helpers:
19 static std::vector<unsigned char>
20 Serialize(const CScript& s)
21 {
22  std::vector<unsigned char> sSerialized(s.begin(), s.end());
23  return sSerialized;
24 }
25 
27 
28 BOOST_AUTO_TEST_CASE(GetSigOpCount)
29 {
30  // Test CScript::GetSigOpCount()
31  CScript s1;
32  BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U);
33  BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U);
34 
35  uint160 dummy;
36  s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG;
37  BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U);
38  s1 << OP_IF << OP_CHECKSIG << OP_ENDIF;
39  BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U);
40  BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U);
41 
43  CScript scriptSig;
44  scriptSig << OP_0 << Serialize(s1);
45  BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U);
46 
47  std::vector<CPubKey> keys;
48  for (int i = 0; i < 3; i++)
49  {
50  CKey k;
51  k.MakeNewKey(true);
52  keys.push_back(k.GetPubKey());
53  }
54  CScript s2 = GetScriptForMultisig(1, keys);
55  BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U);
56  BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U);
57 
59  BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U);
60  BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U);
61  CScript scriptSig2;
62  scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2);
63  BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U);
64 }
65 
70 static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTransaction& input, uint32_t flags)
71 {
73  CTransaction inputi(input);
74  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);
75  BOOST_CHECK((ret == true) == (error == SCRIPT_ERR_OK));
76 
77  return error;
78 }
79 
85 static void BuildTxs(CMutableTransaction& spendingTx, CCoinsViewCache& coins, CMutableTransaction& creationTx, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
86 {
87  creationTx.nVersion = 1;
88  creationTx.vin.resize(1);
89  creationTx.vin[0].prevout.SetNull();
90  creationTx.vin[0].scriptSig = CScript();
91  creationTx.vout.resize(1);
92  creationTx.vout[0].nValue = 1;
93  creationTx.vout[0].scriptPubKey = scriptPubKey;
94 
95  spendingTx.nVersion = 1;
96  spendingTx.vin.resize(1);
97  spendingTx.vin[0].prevout.hash = creationTx.GetHash();
98  spendingTx.vin[0].prevout.n = 0;
99  spendingTx.vin[0].scriptSig = scriptSig;
100  spendingTx.vin[0].scriptWitness = witness;
101  spendingTx.vout.resize(1);
102  spendingTx.vout[0].nValue = 1;
103  spendingTx.vout[0].scriptPubKey = CScript();
104 
105  AddCoins(coins, CTransaction(creationTx), 0);
106 }
107 
108 BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
109 {
110  // Transaction creates outputs
111  CMutableTransaction creationTx;
112  // Transaction that spends outputs and whose
113  // sig op cost is going to be tested
114  CMutableTransaction spendingTx;
115 
116  // Create utxo set
117  CCoinsView coinsDummy;
118  CCoinsViewCache coins(&coinsDummy);
119  // Create key
120  CKey key;
121  key.MakeNewKey(true);
122  CPubKey pubkey = key.GetPubKey();
123  // Default flags
125 
126  // Multisig script (legacy counting)
127  {
128  CScript scriptPubKey = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
129  // Do not use a valid signature to avoid using wallet operations.
130  CScript scriptSig = CScript() << OP_0 << OP_0;
131 
132  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
133  // Legacy counting only includes signature operations in scriptSigs and scriptPubKeys
134  // of a transaction and does not take the actual executed sig operations into account.
135  // spendingTx in itself does not contain a signature operation.
136  assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
137  // creationTx contains two signature operations in its scriptPubKey, but legacy counting
138  // is not accurate.
140  // Sanity check: script verification fails because of an invalid signature.
142  }
143 
144  // Multisig nested in P2SH
145  {
146  CScript redeemScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
147  CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
148  CScript scriptSig = CScript() << OP_0 << OP_0 << ToByteVector(redeemScript);
149 
150  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
153  }
154 
155  // P2WPKH witness program
156  {
157  CScript scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkey));
158  CScript scriptSig = CScript();
159  CScriptWitness scriptWitness;
160  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
161  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
162 
163 
164  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
165  assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
166  // No signature operations if we don't verify the witness.
168  assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
169 
170  // The sig op cost for witness version != 0 is zero.
171  assert(scriptPubKey[0] == 0x00);
172  scriptPubKey[0] = 0x51;
173  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
174  assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
175  scriptPubKey[0] = 0x00;
176  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
177 
178  // The witness of a coinbase transaction is not taken into account.
179  spendingTx.vin[0].prevout.SetNull();
180  assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
181  }
182 
183  // P2WPKH nested in P2SH
184  {
185  CScript scriptSig = GetScriptForDestination(WitnessV0KeyHash(pubkey));
186  CScript scriptPubKey = GetScriptForDestination(ScriptHash(scriptSig));
187  scriptSig = CScript() << ToByteVector(scriptSig);
188  CScriptWitness scriptWitness;
189  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
190  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
191 
192  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
193  assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
194  assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
195  }
196 
197  // P2WSH witness program
198  {
199  CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
200  CScript scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
201  CScript scriptSig = CScript();
202  CScriptWitness scriptWitness;
203  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
204  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
205  scriptWitness.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
206 
207  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
208  assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
211  }
212 
213  // P2WSH nested in P2SH
214  {
215  CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
216  CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript));
217  CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
218  CScript scriptSig = CScript() << ToByteVector(redeemScript);
219  CScriptWitness scriptWitness;
220  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
221  scriptWitness.stack.push_back(std::vector<unsigned char>(0));
222  scriptWitness.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
223 
224  BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
225  assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
227  }
228 }
229 
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:270
assert
assert(!tx.IsCoinBase())
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(GetSigOpCount)
Definition: sigopcount_tests.cpp:28
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
Serialize
static std::vector< unsigned char > Serialize(const CScript &s)
Definition: sigopcount_tests.cpp:20
flags
int flags
Definition: bitcoin-tx.cpp:513
SCRIPT_ERR_OK
@ SCRIPT_ERR_OK
Definition: script_error.h:13
setup_common.h
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:351
CMutableTransaction::nVersion
int32_t nVersion
Definition: transaction.h:348
uint256.h
OP_CHECKMULTISIG
@ OP_CHECKMULTISIG
Definition: script.h:185
GenericTransactionSignatureChecker
Definition: interpreter.h:277
SCRIPT_ERR_CHECKMULTISIGVERIFY
@ SCRIPT_ERR_CHECKMULTISIGVERIFY
Definition: script_error.h:29
pubkey.h
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
MAX_PUBKEYS_PER_MULTISIG
static const int MAX_PUBKEYS_PER_MULTISIG
Definition: script.h:30
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
VerifyScript
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
Definition: interpreter.cpp:1969
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
ScriptError
enum ScriptError_t ScriptError
WitnessV0KeyHash
Definition: standard.h:109
CScriptWitness
Definition: script.h:557
CCoinsView
Abstract view on the open txout dataset.
Definition: coins.h:157
AddCoins
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:108
OP_1
@ OP_1
Definition: script.h:76
prevector::end
iterator end()
Definition: prevector.h:292
OP_0
@ OP_0
Definition: script.h:69
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
OP_IF
@ OP_IF
Definition: script.h:97
tx_verify.h
GetScriptForMultisig
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:361
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:271
SCRIPT_VERIFY_WITNESS
@ SCRIPT_VERIFY_WITNESS
Definition: interpreter.h:105
consensus.h
SCRIPT_ERR_EQUALVERIFY
@ SCRIPT_ERR_EQUALVERIFY
Definition: script_error.h:28
BuildTxs
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...
Definition: sigopcount_tests.cpp:85
standard.h
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
OP_2
@ OP_2
Definition: script.h:78
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
MissingDataBehavior::ASSERT_FAIL
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
script.h
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
key.h
uint160
160-bit opaque blob.
Definition: uint256.h:113
CPubKey
An encapsulated public key.
Definition: pubkey.h:32
CKey
An encapsulated private key.
Definition: key.h:26
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
CScript::GetSigOpCount
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:153
SCRIPT_VERIFY_P2SH
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:46
VerifyWithFlag
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...
Definition: sigopcount_tests.cpp:70
prevector::begin
iterator begin()
Definition: prevector.h:290
CMutableTransaction::GetHash
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:62
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
WitnessV0ScriptHash
Definition: standard.h:102
GetTransactionSigOpCost
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, uint32_t flags)
Compute total signature operation cost of a transaction.
Definition: tx_verify.cpp:147
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:183
WITNESS_SCALE_FACTOR
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
ScriptHash
Definition: standard.h:89
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
OP_ENDIF
@ OP_ENDIF
Definition: script.h:102
CScriptWitness::stack
std::vector< std::vector< unsigned char > > stack
Definition: script.h:561
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
OP_CHECKMULTISIGVERIFY
@ OP_CHECKMULTISIGVERIFY
Definition: script.h:186