@@ -88,7 +88,14 @@ static ScriptErrorDesc script_errors[]={
88
88
{SCRIPT_ERR_SIG_NULLDUMMY, " SIG_NULLDUMMY" },
89
89
{SCRIPT_ERR_PUBKEYTYPE, " PUBKEYTYPE" },
90
90
{SCRIPT_ERR_CLEANSTACK, " CLEANSTACK" },
91
- {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS, " DISCOURAGE_UPGRADABLE_NOPS" }
91
+ {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS, " DISCOURAGE_UPGRADABLE_NOPS" },
92
+ {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, " DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM" },
93
+ {SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH, " WITNESS_PROGRAM_WRONG_LENGTH" },
94
+ {SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY, " WITNESS_PROGRAM_WITNESS_EMPTY" },
95
+ {SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH, " WITNESS_PROGRAM_MISMATCH" },
96
+ {SCRIPT_ERR_WITNESS_MALLEATED, " WITNESS_MALLEATED" },
97
+ {SCRIPT_ERR_WITNESS_MALLEATED_P2SH, " WITNESS_MALLEATED_P2SH" },
98
+ {SCRIPT_ERR_WITNESS_UNEXPECTED, " WITNESS_UNEXPECTED" },
92
99
};
93
100
94
101
const char *FormatScriptError (ScriptError_t err)
@@ -127,13 +134,15 @@ CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey)
127
134
return txCredit;
128
135
}
129
136
130
- CMutableTransaction BuildSpendingTransaction (const CScript& scriptSig, const CMutableTransaction& txCredit)
137
+ CMutableTransaction BuildSpendingTransaction (const CScript& scriptSig, const CScriptWitness& scriptWitness, const CMutableTransaction& txCredit)
131
138
{
132
139
CMutableTransaction txSpend;
133
140
txSpend.nVersion = 1 ;
134
141
txSpend.nLockTime = 0 ;
135
142
txSpend.vin .resize (1 );
136
143
txSpend.vout .resize (1 );
144
+ txSpend.wit .vtxinwit .resize (1 );
145
+ txSpend.wit .vtxinwit [0 ].scriptWitness = scriptWitness;
137
146
txSpend.vin [0 ].prevout .hash = txCredit.GetHash ();
138
147
txSpend.vin [0 ].prevout .n = 0 ;
139
148
txSpend.vin [0 ].scriptSig = scriptSig;
@@ -144,7 +153,7 @@ CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CMu
144
153
return txSpend;
145
154
}
146
155
147
- void DoTest (const CScript& scriptPubKey, const CScript& scriptSig, int flags, const std::string& message, int scriptError)
156
+ void DoTest (const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, int flags, const std::string& message, int scriptError)
148
157
{
149
158
bool expect = (scriptError == SCRIPT_ERR_OK);
150
159
if (flags & SCRIPT_VERIFY_CLEANSTACK) {
@@ -153,12 +162,12 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, co
153
162
}
154
163
ScriptError err;
155
164
CMutableTransaction txCredit = BuildCreditingTransaction (scriptPubKey);
156
- CMutableTransaction tx = BuildSpendingTransaction (scriptSig, txCredit);
165
+ CMutableTransaction tx = BuildSpendingTransaction (scriptSig, scriptWitness, txCredit);
157
166
CMutableTransaction tx2 = tx;
158
- BOOST_CHECK_MESSAGE (VerifyScript (scriptSig, scriptPubKey, NULL , flags, MutableTransactionSignatureChecker (&tx, 0 , txCredit.vout [0 ].nValue ), &err) == expect, message);
167
+ BOOST_CHECK_MESSAGE (VerifyScript (scriptSig, scriptPubKey, &scriptWitness , flags, MutableTransactionSignatureChecker (&tx, 0 , txCredit.vout [0 ].nValue ), &err) == expect, message);
159
168
BOOST_CHECK_MESSAGE (err == scriptError, std::string (FormatScriptError (err)) + " where " + std::string (FormatScriptError ((ScriptError_t)scriptError)) + " expected: " + message);
160
169
#if defined(HAVE_CONSENSUS_LIB)
161
- CDataStream stream (SER_NETWORK, PROTOCOL_VERSION);
170
+ CDataStream stream (SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_WITNESS );
162
171
stream << tx2;
163
172
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
164
173
BOOST_CHECK_MESSAGE (bitcoinconsensus_verify_script_with_amount (begin_ptr (scriptPubKey), scriptPubKey.size (), txCredit.vout [0 ].nValue , (const unsigned char *)&stream[0 ], stream.size (), 0 , flags, NULL ) == expect, message);
@@ -280,7 +289,7 @@ class TestBuilder
280
289
} else {
281
290
creditTx = BuildCreditingTransaction (redeemScript);
282
291
}
283
- spendTx = BuildSpendingTransaction (CScript (), creditTx);
292
+ spendTx = BuildSpendingTransaction (CScript (), CScriptWitness (), creditTx);
284
293
}
285
294
286
295
TestBuilder& ScriptError (ScriptError_t err)
@@ -363,7 +372,7 @@ class TestBuilder
363
372
{
364
373
TestBuilder copy = *this ; // Make a copy so we can rollback the push.
365
374
DoPush ();
366
- DoTest (creditTx.vout [0 ].scriptPubKey , spendTx.vin [0 ].scriptSig , flags, comment, scriptError);
375
+ DoTest (creditTx.vout [0 ].scriptPubKey , spendTx.vin [0 ].scriptSig , CScriptWitness (), flags, comment, scriptError);
367
376
*this = copy;
368
377
return *this ;
369
378
}
@@ -706,29 +715,37 @@ BOOST_AUTO_TEST_CASE(script_json_test)
706
715
{
707
716
// Read tests from test/data/script_tests.json
708
717
// Format is an array of arrays
709
- // Inner arrays are [ "scriptSig", "scriptPubKey", "flags", "expected_scripterror" ]
718
+ // Inner arrays are [ ["wit"...]?, "scriptSig", "scriptPubKey", "flags", "expected_scripterror" ]
710
719
// ... where scriptSig and scriptPubKey are stringified
711
720
// scripts.
712
721
UniValue tests = read_json (std::string (json_tests::script_tests, json_tests::script_tests + sizeof (json_tests::script_tests)));
713
722
714
723
for (unsigned int idx = 0 ; idx < tests.size (); idx++) {
715
724
UniValue test = tests[idx];
716
725
string strTest = test.write ();
717
- if (test.size () < 4 ) // Allow size > 3; extra stuff ignored (useful for comments)
726
+ CScriptWitness witness;
727
+ unsigned int pos = 0 ;
728
+ if (test.size () > 0 && test[pos].isArray ()) {
729
+ for (unsigned int i = 0 ; i < test[pos].size (); i++) {
730
+ witness.stack .push_back (ParseHex (test[pos][i].get_str ()));
731
+ }
732
+ pos++;
733
+ }
734
+ if (test.size () < 4 + pos) // Allow size > 3; extra stuff ignored (useful for comments)
718
735
{
719
736
if (test.size () != 1 ) {
720
737
BOOST_ERROR (" Bad test: " << strTest);
721
738
}
722
739
continue ;
723
740
}
724
- string scriptSigString = test[0 ].get_str ();
741
+ string scriptSigString = test[pos++ ].get_str ();
725
742
CScript scriptSig = ParseScript (scriptSigString);
726
- string scriptPubKeyString = test[1 ].get_str ();
743
+ string scriptPubKeyString = test[pos++ ].get_str ();
727
744
CScript scriptPubKey = ParseScript (scriptPubKeyString);
728
- unsigned int scriptflags = ParseScriptFlags (test[2 ].get_str ());
729
- int scriptError = ParseScriptError (test[3 ].get_str ());
745
+ unsigned int scriptflags = ParseScriptFlags (test[pos++ ].get_str ());
746
+ int scriptError = ParseScriptError (test[pos++ ].get_str ());
730
747
731
- DoTest (scriptPubKey, scriptSig, scriptflags, strTest, scriptError);
748
+ DoTest (scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError);
732
749
}
733
750
}
734
751
@@ -806,7 +823,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
806
823
scriptPubKey12 << OP_1 << ToByteVector (key1.GetPubKey ()) << ToByteVector (key2.GetPubKey ()) << OP_2 << OP_CHECKMULTISIG;
807
824
808
825
CMutableTransaction txFrom12 = BuildCreditingTransaction (scriptPubKey12);
809
- CMutableTransaction txTo12 = BuildSpendingTransaction (CScript (), txFrom12);
826
+ CMutableTransaction txTo12 = BuildSpendingTransaction (CScript (), CScriptWitness (), txFrom12);
810
827
811
828
CScript goodsig1 = sign_multisig (scriptPubKey12, key1, txTo12);
812
829
BOOST_CHECK (VerifyScript (goodsig1, scriptPubKey12, NULL , flags, MutableTransactionSignatureChecker (&txTo12, 0 , txFrom12.vout [0 ].nValue ), &err));
@@ -837,7 +854,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
837
854
scriptPubKey23 << OP_2 << ToByteVector (key1.GetPubKey ()) << ToByteVector (key2.GetPubKey ()) << ToByteVector (key3.GetPubKey ()) << OP_3 << OP_CHECKMULTISIG;
838
855
839
856
CMutableTransaction txFrom23 = BuildCreditingTransaction (scriptPubKey23);
840
- CMutableTransaction txTo23 = BuildSpendingTransaction (CScript (), txFrom23);
857
+ CMutableTransaction txTo23 = BuildSpendingTransaction (CScript (), CScriptWitness (), txFrom23);
841
858
842
859
std::vector<CKey> keys;
843
860
keys.push_back (key1); keys.push_back (key2);
@@ -910,7 +927,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs)
910
927
}
911
928
912
929
CMutableTransaction txFrom = BuildCreditingTransaction (GetScriptForDestination (keys[0 ].GetPubKey ().GetID ()));
913
- CMutableTransaction txTo = BuildSpendingTransaction (CScript (), txFrom);
930
+ CMutableTransaction txTo = BuildSpendingTransaction (CScript (), CScriptWitness (), txFrom);
914
931
CScript& scriptPubKey = txFrom.vout [0 ].scriptPubKey ;
915
932
CScript& scriptSig = txTo.vin [0 ].scriptSig ;
916
933
0 commit comments