Branch data Line data Source code
1 : : // Copyright (c) 2012-present 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/tx_verify.h>
6 : : #include <key.h>
7 : : #include <policy/policy.h>
8 : : #include <policy/settings.h>
9 : : #include <script/script.h>
10 : : #include <script/script_error.h>
11 : : #include <script/sign.h>
12 : : #include <script/signingprovider.h>
13 : : #include <test/util/setup_common.h>
14 : : #include <test/util/transaction_utils.h>
15 : : #include <validation.h>
16 : :
17 : : #include <vector>
18 : :
19 : : #include <boost/test/unit_test.hpp>
20 : :
21 : : // Helpers:
22 : 8 : static bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, std::string& reason)
23 : : {
24 : 8 : return IsStandardTx(tx, std::nullopt, permit_bare_multisig, CFeeRate{DUST_RELAY_TX_FEE}, reason);
25 : : }
26 : :
27 : 2 : static bool IsStandardTx(const CTransaction& tx, std::string& reason)
28 : : {
29 [ + - - + ]: 4 : return IsStandardTx(tx, std::nullopt, /*permit_bare_multisig=*/true, CFeeRate{DUST_RELAY_TX_FEE}, reason) &&
30 : 2 : IsStandardTx(tx, std::nullopt, /*permit_bare_multisig=*/false, CFeeRate{DUST_RELAY_TX_FEE}, reason);
31 : : }
32 : :
33 : 4 : static std::vector<unsigned char> Serialize(const CScript& s)
34 : : {
35 [ + - ]: 8 : std::vector<unsigned char> sSerialized(s.begin(), s.end());
36 : 4 : return sSerialized;
37 : : }
38 : :
39 : 4 : static bool Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict, ScriptError& err)
40 : : {
41 : : // Create dummy to/from transactions:
42 : 4 : CMutableTransaction txFrom;
43 : 4 : txFrom.vout = {
44 : 4 : CTxOut{-1, scriptPubKey},
45 [ + - + + : 12 : };
- - ]
46 : :
47 [ + - ]: 4 : CMutableTransaction txTo;
48 : 4 : txTo.vin = {
49 [ + - ]: 4 : CTxIn{COutPoint{txFrom.GetHash(), 0}, scriptSig},
50 [ + - + + : 12 : };
- - ]
51 : 4 : txTo.vout = {
52 : 0 : CTxOut{1, CScript{}},
53 [ + - + + : 12 : };
- - ]
54 : :
55 [ + + + - ]: 4 : return VerifyScript(scriptSig, scriptPubKey, nullptr, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err);
56 [ + - + - : 20 : }
+ - ]
57 : :
58 : :
59 : : BOOST_FIXTURE_TEST_SUITE(script_p2sh_tests, BasicTestingSetup)
60 : :
61 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(sign)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
62 : : {
63 : : // Pay-to-script-hash looks like this:
64 : : // scriptSig: <sig> <sig...> <serialized_script>
65 : : // scriptPubKey: HASH160 <hash> EQUAL
66 : :
67 : : // Test SignSignature() (and therefore the version of Solver() that signs transactions)
68 : 1 : FillableSigningProvider keystore;
69 : 4 : CKey key[4];
70 [ + + ]: 5 : for (int i = 0; i < 4; i++)
71 : : {
72 [ + - ]: 4 : key[i].MakeNewKey(true);
73 [ + - + - : 8 : BOOST_CHECK(keystore.AddKey(key[i]));
+ - ]
74 : : }
75 : :
76 : : // 8 Scripts: checking all combinations of
77 : : // different keys, straight/P2SH, pubkey/pubkeyhash
78 : 4 : CScript standardScripts[4];
79 [ + - + - : 2 : standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
+ - ]
80 [ + - + - : 2 : standardScripts[1] = GetScriptForDestination(PKHash(key[1].GetPubKey()));
+ - ]
81 [ + - + - : 2 : standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
+ - ]
82 [ + - + - : 2 : standardScripts[3] = GetScriptForDestination(PKHash(key[2].GetPubKey()));
+ - ]
83 : 4 : CScript evalScripts[4];
84 [ + + ]: 5 : for (int i = 0; i < 4; i++)
85 : : {
86 [ + - + - : 8 : BOOST_CHECK(keystore.AddCScript(standardScripts[i]));
+ - + - ]
87 [ + - + - ]: 4 : evalScripts[i] = GetScriptForDestination(ScriptHash(standardScripts[i]));
88 : : }
89 : :
90 [ + - ]: 1 : CMutableTransaction txFrom; // Funding transaction:
91 : 1 : std::string reason;
92 : 1 : txFrom.vout = {
93 : 0 : CTxOut{COIN, evalScripts[0]},
94 : 1 : CTxOut{COIN, evalScripts[1]},
95 : 1 : CTxOut{COIN, evalScripts[2]},
96 : 1 : CTxOut{COIN, evalScripts[3]},
97 : 1 : CTxOut{COIN, standardScripts[0]},
98 : 1 : CTxOut{COIN, standardScripts[1]},
99 : 1 : CTxOut{COIN, standardScripts[2]},
100 : 1 : CTxOut{COIN, standardScripts[3]},
101 [ - + + + : 9 : };
- - ]
102 [ + - + - : 3 : BOOST_CHECK(IsStandardTx(CTransaction(txFrom), reason));
+ - + - ]
103 : :
104 [ + - + + : 25 : CMutableTransaction txTo[8]; // Spending transactions
- - ]
105 [ + + ]: 9 : for (uint32_t i = 0; i < 8; i++)
106 : : {
107 : 8 : txTo[i].vin = {
108 [ + - ]: 8 : CTxIn{COutPoint{txFrom.GetHash(), i}},
109 [ - + + + : 16 : };
- - ]
110 : 8 : txTo[i].vout = {
111 : 8 : CTxOut{1, CScript{}},
112 [ - + + + : 16 : };
- - ]
113 : : }
114 [ + + ]: 9 : for (int i = 0; i < 8; i++)
115 : : {
116 : 8 : SignatureData empty;
117 [ + - + - : 24 : BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), txTo[i], 0, SIGHASH_ALL, empty), strprintf("SignSignature %d", i));
+ - + - +
- ]
118 : 8 : }
119 : : // All of the above should be OK, and the txTos have valid signatures
120 : : // Check to make sure signature verification fails if we use the wrong ScriptSig:
121 [ + - ]: 1 : SignatureCache signature_cache{DEFAULT_SIGNATURE_CACHE_BYTES};
122 [ + + ]: 9 : for (int i = 0; i < 8; i++) {
123 [ + - ]: 8 : PrecomputedTransactionData txdata(txTo[i]);
124 [ + + ]: 72 : for (int j = 0; j < 8; j++)
125 : : {
126 : 64 : CScript sigSave = txTo[i].vin[0].scriptSig;
127 : 64 : txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
128 [ + - + - ]: 64 : bool sigOK = !CScriptCheck(txFrom.vout[txTo[i].vin[0].prevout.n], CTransaction(txTo[i]), signature_cache, 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, false, &txdata)().has_value();
129 [ + + ]: 64 : if (i == j)
130 [ + - + - : 16 : BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
+ - ]
131 : : else
132 [ + - + - : 112 : BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
+ - ]
133 : 64 : txTo[i].vin[0].scriptSig = sigSave;
134 : 64 : }
135 : 8 : }
136 [ + - + - : 43 : }
+ - + - +
- + - + -
+ - + - +
- + + + +
+ + + + -
- - - - -
- - - - ]
137 : :
138 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(norecurse)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
139 : : {
140 : 1 : ScriptError err;
141 : : // Make sure only the outer pay-to-script-hash does the
142 : : // extra-validation thing:
143 : 1 : CScript invalidAsScript;
144 [ + - + - ]: 1 : invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE;
145 : :
146 [ + - + - ]: 1 : CScript p2sh = GetScriptForDestination(ScriptHash(invalidAsScript));
147 : :
148 : 1 : CScript scriptSig;
149 [ + - ]: 2 : scriptSig << Serialize(invalidAsScript);
150 : :
151 : : // Should not verify, because it will try to execute OP_INVALIDOPCODE
152 [ + - + - : 2 : BOOST_CHECK(!Verify(scriptSig, p2sh, true, err));
+ - + - ]
153 [ + - + - : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_BAD_OPCODE, ScriptErrorString(err));
+ - ]
154 : :
155 : : // Try to recur, and verification should succeed because
156 : : // the inner HASH160 <> EQUAL should only check the hash:
157 [ + - + - ]: 1 : CScript p2sh2 = GetScriptForDestination(ScriptHash(p2sh));
158 : 1 : CScript scriptSig2;
159 [ + - + - ]: 3 : scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh);
160 : :
161 [ + - + - : 2 : BOOST_CHECK(Verify(scriptSig2, p2sh2, true, err));
+ - + - ]
162 [ + - + - : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
+ - ]
163 : 1 : }
164 : :
165 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(set)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
166 : : {
167 : : // Test the CScript::Set* methods
168 : 1 : FillableSigningProvider keystore;
169 : 4 : CKey key[4];
170 : 1 : std::vector<CPubKey> keys;
171 [ + - ]: 1 : keys.reserve(4);
172 [ + + ]: 5 : for (int i = 0; i < 4; i++)
173 : : {
174 [ + - ]: 4 : key[i].MakeNewKey(true);
175 [ + - + - : 8 : BOOST_CHECK(keystore.AddKey(key[i]));
+ - + - ]
176 [ + - ]: 8 : keys.push_back(key[i].GetPubKey());
177 : : }
178 : :
179 : 4 : CScript inner[4];
180 [ + - + - : 2 : inner[0] = GetScriptForDestination(PKHash(key[0].GetPubKey()));
+ - ]
181 [ + - + - : 2 : inner[1] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
+ - ]
182 [ + - + - : 2 : inner[2] = GetScriptForMultisig(1, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
+ - ]
183 [ + - + - ]: 2 : inner[3] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+3));
184 : :
185 : 5 : CScript outer[4];
186 [ + + ]: 5 : for (int i = 0; i < 4; i++)
187 : : {
188 [ + - + - ]: 4 : outer[i] = GetScriptForDestination(ScriptHash(inner[i]));
189 [ + - + - : 8 : BOOST_CHECK(keystore.AddCScript(inner[i]));
+ - ]
190 : : }
191 : :
192 [ + - ]: 1 : CMutableTransaction txFrom; // Funding transaction:
193 : 1 : std::string reason;
194 : 1 : txFrom.vout = {
195 : 0 : CTxOut{CENT, outer[0]},
196 : 1 : CTxOut{CENT, outer[1]},
197 : 1 : CTxOut{CENT, outer[2]},
198 : 1 : CTxOut{CENT, outer[3]},
199 [ + - + + : 6 : };
- - ]
200 [ + - + - : 3 : BOOST_CHECK(IsStandardTx(CTransaction(txFrom), reason));
+ - + - ]
201 : :
202 [ + - + + : 13 : CMutableTransaction txTo[4]; // Spending transactions
- - ]
203 [ + + ]: 5 : for (uint32_t i = 0; i < 4; i++)
204 : : {
205 : 4 : txTo[i].vin = {
206 [ + - ]: 4 : CTxIn{COutPoint{txFrom.GetHash(), i}},
207 [ + - + + : 12 : };
- - ]
208 : 4 : txTo[i].vout = {
209 : 8 : CTxOut{1 * CENT, inner[i]},
210 [ + - + + : 12 : };
- - ]
211 : : }
212 [ + + ]: 5 : for (int i = 0; i < 4; i++)
213 : : {
214 : 4 : SignatureData empty;
215 [ + - + - : 12 : BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), txTo[i], 0, SIGHASH_ALL, empty), strprintf("SignSignature %d", i));
+ - + - +
- ]
216 [ + - + - : 12 : BOOST_CHECK_MESSAGE(IsStandardTx(CTransaction(txTo[i]), /*permit_bare_multisig=*/true, reason), strprintf("txTo[%d].IsStandard", i));
+ - + - +
- ]
217 [ + - + - ]: 4 : bool no_pbms_is_std = IsStandardTx(CTransaction(txTo[i]), /*permit_bare_multisig=*/false, reason);
218 [ + - + - : 8 : BOOST_CHECK_MESSAGE((i == 0 ? no_pbms_is_std : !no_pbms_is_std), strprintf("txTo[%d].IsStandard(permbaremulti=false)", i));
+ + + - ]
219 : 4 : }
220 [ + - + - : 31 : }
+ - + - +
- + - + +
+ + + + +
+ - - - -
- - - - -
- ]
221 : :
222 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(is)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
223 : : {
224 : : // Test CScript::IsPayToScriptHash()
225 : 1 : uint160 dummy;
226 : 1 : CScript p2sh;
227 [ + - + - : 2 : p2sh << OP_HASH160 << ToByteVector(dummy) << OP_EQUAL;
+ - ]
228 [ + - + - : 2 : BOOST_CHECK(p2sh.IsPayToScriptHash());
+ - + - ]
229 : :
230 [ + - ]: 1 : std::vector<unsigned char> direct = {OP_HASH160, 20};
231 [ + - ]: 1 : direct.insert(direct.end(), 20, 0);
232 [ + - ]: 1 : direct.push_back(OP_EQUAL);
233 [ + - + - : 2 : BOOST_CHECK(CScript(direct.begin(), direct.end()).IsPayToScriptHash());
+ - + - ]
234 : :
235 : : // Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes:
236 [ + - ]: 1 : std::vector<unsigned char> pushdata1 = {OP_HASH160, OP_PUSHDATA1, 20};
237 [ + - ]: 1 : pushdata1.insert(pushdata1.end(), 20, 0);
238 [ + - ]: 1 : pushdata1.push_back(OP_EQUAL);
239 [ + - + - : 2 : BOOST_CHECK(!CScript(pushdata1.begin(), pushdata1.end()).IsPayToScriptHash());
+ - + - ]
240 [ + - ]: 1 : std::vector<unsigned char> pushdata2 = {OP_HASH160, OP_PUSHDATA2, 20, 0};
241 [ + - ]: 1 : pushdata2.insert(pushdata2.end(), 20, 0);
242 [ + - ]: 1 : pushdata2.push_back(OP_EQUAL);
243 [ + - + - : 2 : BOOST_CHECK(!CScript(pushdata2.begin(), pushdata2.end()).IsPayToScriptHash());
+ - + - ]
244 [ + - ]: 1 : std::vector<unsigned char> pushdata4 = {OP_HASH160, OP_PUSHDATA4, 20, 0, 0, 0};
245 [ + - ]: 1 : pushdata4.insert(pushdata4.end(), 20, 0);
246 [ + - ]: 1 : pushdata4.push_back(OP_EQUAL);
247 [ + - + - : 2 : BOOST_CHECK(!CScript(pushdata4.begin(), pushdata4.end()).IsPayToScriptHash());
+ - + - ]
248 : :
249 : 1 : CScript not_p2sh;
250 [ + - + - : 2 : BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+ - ]
251 : :
252 [ + - + - : 4 : not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << ToByteVector(dummy) << OP_EQUAL;
+ - + - ]
253 [ + - + - : 2 : BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+ - ]
254 : :
255 [ + - + - : 2 : not_p2sh.clear(); not_p2sh << OP_NOP << ToByteVector(dummy) << OP_EQUAL;
+ - ]
256 [ + - + - : 2 : BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+ - ]
257 : :
258 [ + - + - : 2 : not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << OP_CHECKSIG;
+ - ]
259 [ + - + - : 2 : BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+ - ]
260 : 1 : }
261 : :
262 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(switchover)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
263 : : {
264 : : // Test switch over code
265 : 1 : CScript notValid;
266 : 1 : ScriptError err;
267 [ + - + - : 1 : notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
+ - ]
268 : 1 : CScript scriptSig;
269 [ + - ]: 2 : scriptSig << Serialize(notValid);
270 : :
271 [ + - + - ]: 1 : CScript fund = GetScriptForDestination(ScriptHash(notValid));
272 : :
273 : :
274 : : // Validation should succeed under old rules (hash is correct):
275 [ + - + - : 2 : BOOST_CHECK(Verify(scriptSig, fund, false, err));
+ - + - ]
276 [ + - + - : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
+ - ]
277 : : // Fail under new:
278 [ + - + - : 2 : BOOST_CHECK(!Verify(scriptSig, fund, true, err));
+ - + - ]
279 [ + - + - : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EQUALVERIFY, ScriptErrorString(err));
+ - ]
280 : 1 : }
281 : :
282 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(ValidateInputsStandardness)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
283 : : {
284 : 1 : CCoinsViewCache coins{&CoinsViewEmpty::Get()};
285 : 1 : FillableSigningProvider keystore;
286 : 6 : CKey key[6];
287 [ + + ]: 7 : for (int i = 0; i < 6; i++)
288 : : {
289 [ + - ]: 6 : key[i].MakeNewKey(true);
290 [ + - + - : 12 : BOOST_CHECK(keystore.AddKey(key[i]));
+ - ]
291 : : }
292 : 1 : std::vector<CPubKey> keys;
293 [ + - ]: 1 : keys.reserve(3);
294 [ + + ]: 4 : for (int i = 0; i < 3; i++)
295 [ + - ]: 6 : keys.push_back(key[i].GetPubKey());
296 : :
297 : : // First three are standard:
298 [ + - + - : 1 : CScript pay1 = GetScriptForDestination(PKHash(key[0].GetPubKey()));
+ - ]
299 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(pay1));
+ - + - ]
300 [ + - ]: 1 : CScript pay1of3 = GetScriptForMultisig(1, keys);
301 : :
302 : : // vout[3] is complicated 1-of-3 AND 2-of-3
303 : : // ... that is OK if wrapped in P2SH:
304 : 1 : CScript oneAndTwo;
305 [ + - + - : 4 : oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey());
+ - + - +
- + - +
- ]
306 [ + - + - ]: 1 : oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY;
307 [ + - + - : 4 : oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) << ToByteVector(key[4].GetPubKey()) << ToByteVector(key[5].GetPubKey());
+ - + - +
- + - +
- ]
308 [ + - + - ]: 1 : oneAndTwo << OP_3 << OP_CHECKMULTISIG;
309 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(oneAndTwo));
+ - + - ]
310 : :
311 : : // vout[4] is max sigops:
312 [ + - ]: 1 : CScript fifteenSigops; fifteenSigops << OP_1;
313 [ + + ]: 16 : for (unsigned i = 0; i < MAX_P2SH_SIGOPS; i++)
314 [ + - + - ]: 30 : fifteenSigops << ToByteVector(key[i%3].GetPubKey());
315 [ + - + - ]: 1 : fifteenSigops << OP_15 << OP_CHECKMULTISIG;
316 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(fifteenSigops));
+ - + - ]
317 : :
318 : : // vout[5/6] are non-standard because they exceed MAX_P2SH_SIGOPS
319 [ + - + - ]: 1 : CScript sixteenSigops; sixteenSigops << OP_16 << OP_CHECKMULTISIG;
320 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(sixteenSigops));
+ - + - ]
321 [ + - ]: 1 : CScript twentySigops; twentySigops << OP_CHECKMULTISIG;
322 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(twentySigops));
+ - ]
323 : :
324 : : // vout[7] is non-standard because it lacks sigops
325 : 1 : CScript no_sigops;
326 : :
327 : : // vout [8] is non-standard because it contains OP_RETURN in its redeemScript.
328 : 1 : static const unsigned char op_return[] = {OP_RETURN};
329 : 1 : const auto op_return_script = CScript(op_return, op_return + sizeof(op_return));
330 : :
331 : : // vout[9] is non-standard because its witness is unknown
332 : 1 : CScript witnessUnknown;
333 [ + - + - ]: 2 : witnessUnknown << OP_16 << ToByteVector(uint256::ONE);
334 : :
335 [ + - ]: 1 : CMutableTransaction txFrom;
336 : 1 : txFrom.vout = {
337 [ + - + - ]: 2 : /*[0]=*/ CTxOut{1000, GetScriptForDestination(ScriptHash(pay1))}, // P2SH (OP_CHECKSIG)
338 : 2 : /*[1]=*/ CTxOut{2000, pay1}, // ordinary OP_CHECKSIG
339 : 2 : /*[2]=*/ CTxOut{3000, pay1of3}, // ordinary OP_CHECKMULTISIG
340 [ + - + - ]: 2 : /*[3]=*/ CTxOut{4000, GetScriptForDestination(ScriptHash(oneAndTwo))},
341 [ + - + - ]: 2 : /*[4]=*/ CTxOut{5000, GetScriptForDestination(ScriptHash(fifteenSigops))},
342 [ + - + - ]: 2 : /*[5]=*/ CTxOut{5000, GetScriptForDestination(ScriptHash(sixteenSigops))},
343 [ + - + - ]: 2 : /*[6]=*/ CTxOut{3000, GetScriptForDestination(ScriptHash(twentySigops))},
344 : 2 : /*[7]=*/ CTxOut{1000, no_sigops},
345 [ + - + - ]: 2 : /*[8]=*/ CTxOut{1000, GetScriptForDestination(ScriptHash(op_return_script))},
346 : 2 : /*[9]=*/ CTxOut{1000, witnessUnknown},
347 [ - + + + : 11 : };
- - ]
348 : :
349 [ + - + - ]: 1 : AddCoins(coins, CTransaction(txFrom), /*nHeight=*/0);
350 : :
351 : 1 : {
352 [ + - ]: 1 : CMutableTransaction txTo;
353 : 1 : txTo.vout = {
354 [ + - + - : 2 : CTxOut{-1, GetScriptForDestination(PKHash(key[1].GetPubKey()))},
+ - ]
355 [ - + + + : 2 : };
- - ]
356 : :
357 : 1 : txTo.vin = {
358 [ + - ]: 1 : CTxIn{COutPoint{txFrom.GetHash(), 0}},
359 [ + - ]: 2 : CTxIn{COutPoint{txFrom.GetHash(), 1}},
360 [ + - ]: 2 : CTxIn{COutPoint{txFrom.GetHash(), 2}},
361 [ + - ]: 2 : CTxIn{COutPoint{txFrom.GetHash(), 3}},
362 [ + - ]: 2 : CTxIn{COutPoint{txFrom.GetHash(), 4}},
363 [ - + + + : 6 : };
- - ]
364 : 1 : SignatureData empty;
365 [ + - + - : 3 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, /*nIn=*/0, SIGHASH_ALL, empty));
+ - + - ]
366 : 1 : SignatureData empty_b;
367 [ + - + - : 3 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, /*nIn=*/1, SIGHASH_ALL, empty_b));
+ - + - ]
368 : 1 : SignatureData empty_c;
369 [ + - + - : 3 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, /*nIn=*/2, SIGHASH_ALL, empty_c));
+ - + - +
- ]
370 : : // SignSignature doesn't know how to sign these. We're
371 : : // not testing validating signatures, so just create
372 : : // dummy signatures that DO include the correct P2SH scripts:
373 [ + - + - : 3 : txTo.vin[3].scriptSig << OP_11 << OP_11 << std::vector<unsigned char>(oneAndTwo.begin(), oneAndTwo.end());
+ - + - +
- ]
374 [ + - + - : 3 : txTo.vin[4].scriptSig << std::vector<unsigned char>(fifteenSigops.begin(), fifteenSigops.end());
+ - ]
375 : :
376 [ + - + - : 4 : BOOST_CHECK(::ValidateInputsStandardness(CTransaction(txTo), coins).IsValid());
+ - + - +
- ]
377 : : // 22 P2SH sigops for all inputs (1 for vin[0], 6 for vin[3], 15 for vin[4]
378 [ + - + - : 1 : BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txTo), coins), 22U);
+ - + - ]
379 : 1 : }
380 : :
381 : 1 : {
382 [ + - ]: 1 : CMutableTransaction coinbase_tx_mut;
383 : 1 : coinbase_tx_mut.vin = {
384 [ + - ]: 1 : CTxIn{COutPoint{}},
385 [ - + + + : 2 : };
- - ]
386 [ + - ]: 1 : CTransaction coinbase_tx{coinbase_tx_mut};
387 [ + - + - : 2 : BOOST_CHECK(coinbase_tx.IsCoinBase());
+ - ]
388 [ + - + - : 1 : BOOST_CHECK_EQUAL(GetP2SHSigOpCount(coinbase_tx, coins), 0U);
+ - ]
389 : 1 : }
390 : :
391 : : // TxoutType::SCRIPTHASH
392 : 1 : {
393 [ + - ]: 1 : CMutableTransaction txToNonStd1;
394 : 1 : txToNonStd1.vout = {
395 [ + - + - : 2 : CTxOut{1000, GetScriptForDestination(PKHash(key[1].GetPubKey()))},
+ - ]
396 [ - + + + : 2 : };
- - ]
397 : 1 : txToNonStd1.vin = {
398 [ + - - + : 3 : CTxIn{COutPoint{txFrom.GetHash(), 5}, CScript{} << std::vector<unsigned char>(sixteenSigops.begin(), sixteenSigops.end())},
+ - ]
399 [ - + + + : 2 : };
- - ]
400 : :
401 [ + - + - ]: 1 : const auto txToNonStd1_res = ::ValidateInputsStandardness(CTransaction(txToNonStd1), coins);
402 [ + - + - : 2 : BOOST_CHECK(txToNonStd1_res.IsInvalid());
+ - ]
403 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd1_res.GetRejectReason(), "bad-txns-nonstandard-inputs");
+ - ]
404 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd1_res.GetDebugMessage(), "p2sh redeemscript sigops exceed limit (input 0: 16 > 15)");
+ - ]
405 : :
406 [ + - + - : 2 : BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd1), coins), 16U);
+ - + - ]
407 : 1 : }
408 : :
409 : 1 : {
410 [ + - ]: 1 : CMutableTransaction txToNonStd2;
411 : 1 : txToNonStd2.vout = {
412 [ + - + - : 2 : CTxOut{1000, GetScriptForDestination(PKHash(key[1].GetPubKey()))},
+ - ]
413 [ - + + + : 2 : };
- - ]
414 : 1 : txToNonStd2.vin = {
415 [ + - - + : 3 : CTxIn{COutPoint{txFrom.GetHash(), 6}, CScript{} << std::vector<unsigned char>(twentySigops.begin(), twentySigops.end())},
+ - ]
416 [ - + + + : 2 : };
- - ]
417 : :
418 [ + - + - ]: 1 : const auto txToNonStd2_res = ::ValidateInputsStandardness(CTransaction(txToNonStd2), coins);
419 [ + - + - : 2 : BOOST_CHECK(txToNonStd2_res.IsInvalid());
+ - ]
420 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd2_res.GetRejectReason(), "bad-txns-nonstandard-inputs");
+ - ]
421 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd2_res.GetDebugMessage(), "p2sh redeemscript sigops exceed limit (input 0: 20 > 15)");
+ - ]
422 [ + - + - : 2 : BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd2), coins), 20U);
+ - + - ]
423 : 1 : }
424 : :
425 : 1 : {
426 [ + - ]: 1 : CMutableTransaction txToNonStd2_no_scriptSig;
427 : 1 : txToNonStd2_no_scriptSig.vout = {
428 [ + - + - : 2 : CTxOut{1000, GetScriptForDestination(PKHash(key[1].GetPubKey()))},
+ - ]
429 [ - + + + : 2 : };
- - ]
430 : 1 : txToNonStd2_no_scriptSig.vin = {
431 [ + - ]: 1 : CTxIn{COutPoint{txFrom.GetHash(), 6}},
432 [ - + + + : 2 : };
- - ]
433 : :
434 [ + - + - ]: 1 : const auto txToNonStd2_no_scriptSig_res = ::ValidateInputsStandardness(CTransaction(txToNonStd2_no_scriptSig), coins);
435 [ + - + - : 2 : BOOST_CHECK(txToNonStd2_no_scriptSig_res.IsInvalid());
+ - ]
436 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd2_no_scriptSig_res.GetRejectReason(), "bad-txns-nonstandard-inputs");
+ - ]
437 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd2_no_scriptSig_res.GetDebugMessage(), "input 0 P2SH redeemscript missing");
+ - ]
438 [ + - + - : 2 : BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd2_no_scriptSig), coins), 0U);
+ - + - ]
439 : 1 : }
440 : :
441 : : // TxoutType::NONSTANDARD
442 : 1 : {
443 [ + - ]: 1 : CMutableTransaction txToNonStd3;
444 : 1 : txToNonStd3.vout = {
445 [ + - + - : 2 : CTxOut{1000, GetScriptForDestination(PKHash(key[1].GetPubKey()))},
+ - ]
446 [ - + + + : 2 : };
- - ]
447 : 1 : txToNonStd3.vin = {
448 [ + - ]: 1 : CTxIn{COutPoint{txFrom.GetHash(), 7}},
449 [ - + + + : 2 : };
- - ]
450 : :
451 [ + - + - ]: 1 : const auto txToNonStd3_res = ::ValidateInputsStandardness(CTransaction(txToNonStd3), coins);
452 [ + - + - : 2 : BOOST_CHECK(txToNonStd3_res.IsInvalid());
+ - ]
453 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd3_res.GetRejectReason(), "bad-txns-nonstandard-inputs");
+ - ]
454 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd3_res.GetDebugMessage(), "input 0 script unknown");
+ - ]
455 : 1 : }
456 : :
457 : : // TxoutType::INCORRECT_SCRIPTSIG
458 : 1 : {
459 [ + - ]: 1 : CMutableTransaction txToNonStd4;
460 : 1 : txToNonStd4.vout = {
461 [ + - + - : 2 : CTxOut{1000, GetScriptForDestination(PKHash(key[1].GetPubKey()))},
+ - ]
462 [ - + + + : 2 : };
- - ]
463 : 1 : txToNonStd4.vin = {
464 [ + - ]: 1 : CTxIn{COutPoint{txFrom.GetHash(), 8}, op_return_script},
465 [ - + + + : 2 : };
- - ]
466 : :
467 [ + - + - ]: 1 : const auto txToNonStd4_res = ::ValidateInputsStandardness(CTransaction(txToNonStd4), coins);
468 [ + - + - : 2 : BOOST_CHECK(txToNonStd4_res.IsInvalid());
+ - ]
469 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd4_res.GetRejectReason(), "bad-txns-nonstandard-inputs");
+ - ]
470 [ + - - + : 2 : BOOST_CHECK_EQUAL(txToNonStd4_res.GetDebugMessage(), "p2sh scriptsig malformed (input 0: OP_RETURN was encountered)");
+ - ]
471 : 1 : }
472 : :
473 : : // TxoutType::WITNESS_UNKNOWN
474 : 1 : {
475 [ + - ]: 1 : CMutableTransaction txWitnessUnknown;
476 : 1 : txWitnessUnknown.vout = {
477 [ + - + - : 2 : CTxOut{1000, GetScriptForDestination(PKHash(key[1].GetPubKey()))},
+ - ]
478 [ - + + + : 2 : };
- - ]
479 : 1 : txWitnessUnknown.vin = {
480 [ + - ]: 1 : CTxIn{COutPoint{txFrom.GetHash(), 9}},
481 [ - + + + : 2 : };
- - ]
482 [ + - + - ]: 1 : const auto txWitnessUnknown_res = ::ValidateInputsStandardness(CTransaction(txWitnessUnknown), coins);
483 [ + - + - : 2 : BOOST_CHECK(txWitnessUnknown_res.IsInvalid());
+ - ]
484 [ + - - + : 2 : BOOST_CHECK_EQUAL(txWitnessUnknown_res.GetRejectReason(), "bad-txns-nonstandard-inputs");
+ - ]
485 [ + - - + : 2 : BOOST_CHECK_EQUAL(txWitnessUnknown_res.GetDebugMessage(), "input 0 witness program is undefined");
+ - ]
486 : 2 : }
487 [ + - + - : 37 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ - - - -
- - ]
488 : :
489 : : BOOST_AUTO_TEST_SUITE_END()
|