1 // Copyright (c) 2012-2015 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.
5 #include "validation.h"
8 #include "script/script.h"
9 #include "script/standard.h"
11 #include "test/test_bitcoin.h"
15 #include <boost/foreach.hpp>
16 #include <boost/test/unit_test.hpp>
21 static std::vector
<unsigned char>
22 Serialize(const CScript
& s
)
24 std::vector
<unsigned char> sSerialized(s
.begin(), s
.end());
28 BOOST_FIXTURE_TEST_SUITE(sigopcount_tests
, BasicTestingSetup
)
30 BOOST_AUTO_TEST_CASE(GetSigOpCount
)
32 // Test CScript::GetSigOpCount()
34 BOOST_CHECK_EQUAL(s1
.GetSigOpCount(false), 0U);
35 BOOST_CHECK_EQUAL(s1
.GetSigOpCount(true), 0U);
38 s1
<< OP_1
<< ToByteVector(dummy
) << ToByteVector(dummy
) << OP_2
<< OP_CHECKMULTISIG
;
39 BOOST_CHECK_EQUAL(s1
.GetSigOpCount(true), 2U);
40 s1
<< OP_IF
<< OP_CHECKSIG
<< OP_ENDIF
;
41 BOOST_CHECK_EQUAL(s1
.GetSigOpCount(true), 3U);
42 BOOST_CHECK_EQUAL(s1
.GetSigOpCount(false), 21U);
44 CScript p2sh
= GetScriptForDestination(CScriptID(s1
));
46 scriptSig
<< OP_0
<< Serialize(s1
);
47 BOOST_CHECK_EQUAL(p2sh
.GetSigOpCount(scriptSig
), 3U);
49 std::vector
<CPubKey
> keys
;
50 for (int i
= 0; i
< 3; i
++)
54 keys
.push_back(k
.GetPubKey());
56 CScript s2
= GetScriptForMultisig(1, keys
);
57 BOOST_CHECK_EQUAL(s2
.GetSigOpCount(true), 3U);
58 BOOST_CHECK_EQUAL(s2
.GetSigOpCount(false), 20U);
60 p2sh
= GetScriptForDestination(CScriptID(s2
));
61 BOOST_CHECK_EQUAL(p2sh
.GetSigOpCount(true), 0U);
62 BOOST_CHECK_EQUAL(p2sh
.GetSigOpCount(false), 0U);
64 scriptSig2
<< OP_1
<< ToByteVector(dummy
) << ToByteVector(dummy
) << Serialize(s2
);
65 BOOST_CHECK_EQUAL(p2sh
.GetSigOpCount(scriptSig2
), 3U);
69 * Verifies script execution of the zeroth scriptPubKey of tx output and
70 * zeroth scriptSig and witness of tx input.
72 ScriptError
VerifyWithFlag(const CTransaction
& output
, const CMutableTransaction
& input
, int flags
)
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
), &error
);
77 BOOST_CHECK((ret
== true) == (error
== SCRIPT_ERR_OK
));
83 * Builds a creationTx from scriptPubKey and a spendingTx from scriptSig
84 * and witness such that spendingTx spends output zero of creationTx.
85 * Also inserts creationTx's output into the coins view.
87 void BuildTxs(CMutableTransaction
& spendingTx
, CCoinsViewCache
& coins
, CMutableTransaction
& creationTx
, const CScript
& scriptPubKey
, const CScript
& scriptSig
, const CScriptWitness
& witness
)
89 creationTx
.nVersion
= 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
;
97 spendingTx
.nVersion
= 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();
107 coins
.ModifyCoins(creationTx
.GetHash())->FromTx(creationTx
, 0);
110 BOOST_AUTO_TEST_CASE(GetTxSigOpCost
)
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
;
119 CCoinsView coinsDummy
;
120 CCoinsViewCache
coins(&coinsDummy
);
123 key
.MakeNewKey(true);
124 CPubKey pubkey
= key
.GetPubKey();
126 int flags
= SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
;
128 // Multisig script (legacy counting)
130 CScript scriptPubKey
= CScript() << 1 << ToByteVector(pubkey
) << ToByteVector(pubkey
) << 2 << OP_CHECKMULTISIGVERIFY
;
131 // Do not use a valid signature to avoid using wallet operations.
132 CScript scriptSig
= CScript() << OP_0
<< OP_0
;
134 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, CScriptWitness());
135 // Legacy counting only includes signature operations in scriptSigs and scriptPubKeys
136 // of a transaction and does not take the actual executed sig operations into account.
137 // spendingTx in itself does not contain a signature operation.
138 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 0);
139 // creationTx contains two signature operations in its scriptPubKey, but legacy counting
141 assert(GetTransactionSigOpCost(CTransaction(creationTx
), coins
, flags
) == MAX_PUBKEYS_PER_MULTISIG
* WITNESS_SCALE_FACTOR
);
142 // Sanity check: script verification fails because of an invalid signature.
143 assert(VerifyWithFlag(creationTx
, spendingTx
, flags
) == SCRIPT_ERR_CHECKMULTISIGVERIFY
);
146 // Multisig nested in P2SH
148 CScript redeemScript
= CScript() << 1 << ToByteVector(pubkey
) << ToByteVector(pubkey
) << 2 << OP_CHECKMULTISIGVERIFY
;
149 CScript scriptPubKey
= GetScriptForDestination(CScriptID(redeemScript
));
150 CScript scriptSig
= CScript() << OP_0
<< OP_0
<< ToByteVector(redeemScript
);
152 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, CScriptWitness());
153 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 2 * WITNESS_SCALE_FACTOR
);
154 assert(VerifyWithFlag(creationTx
, spendingTx
, flags
) == SCRIPT_ERR_CHECKMULTISIGVERIFY
);
157 // P2WPKH witness program
159 CScript p2pk
= CScript() << ToByteVector(pubkey
) << OP_CHECKSIG
;
160 CScript scriptPubKey
= GetScriptForWitness(p2pk
);
161 CScript scriptSig
= CScript();
162 CScriptWitness scriptWitness
;
163 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
164 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
167 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, scriptWitness
);
168 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 1);
169 // No signature operations if we don't verify the witness.
170 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
& ~SCRIPT_VERIFY_WITNESS
) == 0);
171 assert(VerifyWithFlag(creationTx
, spendingTx
, flags
) == SCRIPT_ERR_EQUALVERIFY
);
173 // The sig op cost for witness version != 0 is zero.
174 assert(scriptPubKey
[0] == 0x00);
175 scriptPubKey
[0] = 0x51;
176 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, scriptWitness
);
177 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 0);
178 scriptPubKey
[0] = 0x00;
179 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, scriptWitness
);
181 // The witness of a coinbase transaction is not taken into account.
182 spendingTx
.vin
[0].prevout
.SetNull();
183 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 0);
186 // P2WPKH nested in P2SH
188 CScript p2pk
= CScript() << ToByteVector(pubkey
) << OP_CHECKSIG
;
189 CScript scriptSig
= GetScriptForWitness(p2pk
);
190 CScript scriptPubKey
= GetScriptForDestination(CScriptID(scriptSig
));
191 scriptSig
= CScript() << ToByteVector(scriptSig
);
192 CScriptWitness scriptWitness
;
193 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
194 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
196 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, scriptWitness
);
197 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 1);
198 assert(VerifyWithFlag(creationTx
, spendingTx
, flags
) == SCRIPT_ERR_EQUALVERIFY
);
201 // P2WSH witness program
203 CScript witnessScript
= CScript() << 1 << ToByteVector(pubkey
) << ToByteVector(pubkey
) << 2 << OP_CHECKMULTISIGVERIFY
;
204 CScript scriptPubKey
= GetScriptForWitness(witnessScript
);
205 CScript scriptSig
= CScript();
206 CScriptWitness scriptWitness
;
207 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
208 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
209 scriptWitness
.stack
.push_back(vector
<unsigned char>(witnessScript
.begin(), witnessScript
.end()));
211 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, scriptWitness
);
212 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 2);
213 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
& ~SCRIPT_VERIFY_WITNESS
) == 0);
214 assert(VerifyWithFlag(creationTx
, spendingTx
, flags
) == SCRIPT_ERR_CHECKMULTISIGVERIFY
);
217 // P2WSH nested in P2SH
219 CScript witnessScript
= CScript() << 1 << ToByteVector(pubkey
) << ToByteVector(pubkey
) << 2 << OP_CHECKMULTISIGVERIFY
;
220 CScript redeemScript
= GetScriptForWitness(witnessScript
);
221 CScript scriptPubKey
= GetScriptForDestination(CScriptID(redeemScript
));
222 CScript scriptSig
= CScript() << ToByteVector(redeemScript
);
223 CScriptWitness scriptWitness
;
224 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
225 scriptWitness
.stack
.push_back(vector
<unsigned char>(0));
226 scriptWitness
.stack
.push_back(vector
<unsigned char>(witnessScript
.begin(), witnessScript
.end()));
228 BuildTxs(spendingTx
, coins
, creationTx
, scriptPubKey
, scriptSig
, scriptWitness
);
229 assert(GetTransactionSigOpCost(CTransaction(spendingTx
), coins
, flags
) == 2);
230 assert(VerifyWithFlag(creationTx
, spendingTx
, flags
) == SCRIPT_ERR_CHECKMULTISIGVERIFY
);
234 BOOST_AUTO_TEST_SUITE_END()