@@ -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
}
@@ -703,29 +712,37 @@ BOOST_AUTO_TEST_CASE(script_json_test)
703
712
{
704
713
// Read tests from test/data/script_tests.json
705
714
// Format is an array of arrays
706
- // Inner arrays are [ "scriptSig", "scriptPubKey", "flags", "expected_scripterror" ]
715
+ // Inner arrays are [ ["wit"...]?, "scriptSig", "scriptPubKey", "flags", "expected_scripterror" ]
707
716
// ... where scriptSig and scriptPubKey are stringified
708
717
// scripts.
709
718
UniValue tests = read_json (std::string (json_tests::script_tests, json_tests::script_tests + sizeof (json_tests::script_tests)));
710
719
711
720
for (unsigned int idx = 0 ; idx < tests.size (); idx++) {
712
721
UniValue test = tests[idx];
713
722
string strTest = test.write ();
714
- if (test.size () < 4 ) // Allow size > 3; extra stuff ignored (useful for comments)
723
+ CScriptWitness witness;
724
+ unsigned int pos = 0 ;
725
+ if (test.size () > 0 && test[pos].isArray ()) {
726
+ for (unsigned int i = 0 ; i < test[pos].size (); i++) {
727
+ witness.stack .push_back (ParseHex (test[pos][i].get_str ()));
728
+ }
729
+ pos++;
730
+ }
731
+ if (test.size () < 4 + pos) // Allow size > 3; extra stuff ignored (useful for comments)
715
732
{
716
733
if (test.size () != 1 ) {
717
734
BOOST_ERROR (" Bad test: " << strTest);
718
735
}
719
736
continue ;
720
737
}
721
- string scriptSigString = test[0 ].get_str ();
738
+ string scriptSigString = test[pos++ ].get_str ();
722
739
CScript scriptSig = ParseScript (scriptSigString);
723
- string scriptPubKeyString = test[1 ].get_str ();
740
+ string scriptPubKeyString = test[pos++ ].get_str ();
724
741
CScript scriptPubKey = ParseScript (scriptPubKeyString);
725
- unsigned int scriptflags = ParseScriptFlags (test[2 ].get_str ());
726
- int scriptError = ParseScriptError (test[3 ].get_str ());
742
+ unsigned int scriptflags = ParseScriptFlags (test[pos++ ].get_str ());
743
+ int scriptError = ParseScriptError (test[pos++ ].get_str ());
727
744
728
- DoTest (scriptPubKey, scriptSig, scriptflags, strTest, scriptError);
745
+ DoTest (scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError);
729
746
}
730
747
}
731
748
@@ -803,7 +820,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
803
820
scriptPubKey12 << OP_1 << ToByteVector (key1.GetPubKey ()) << ToByteVector (key2.GetPubKey ()) << OP_2 << OP_CHECKMULTISIG;
804
821
805
822
CMutableTransaction txFrom12 = BuildCreditingTransaction (scriptPubKey12);
806
- CMutableTransaction txTo12 = BuildSpendingTransaction (CScript (), txFrom12);
823
+ CMutableTransaction txTo12 = BuildSpendingTransaction (CScript (), CScriptWitness (), txFrom12);
807
824
808
825
CScript goodsig1 = sign_multisig (scriptPubKey12, key1, txTo12);
809
826
BOOST_CHECK (VerifyScript (goodsig1, scriptPubKey12, NULL , flags, MutableTransactionSignatureChecker (&txTo12, 0 , txFrom12.vout [0 ].nValue ), &err));
@@ -834,7 +851,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
834
851
scriptPubKey23 << OP_2 << ToByteVector (key1.GetPubKey ()) << ToByteVector (key2.GetPubKey ()) << ToByteVector (key3.GetPubKey ()) << OP_3 << OP_CHECKMULTISIG;
835
852
836
853
CMutableTransaction txFrom23 = BuildCreditingTransaction (scriptPubKey23);
837
- CMutableTransaction txTo23 = BuildSpendingTransaction (CScript (), txFrom23);
854
+ CMutableTransaction txTo23 = BuildSpendingTransaction (CScript (), CScriptWitness (), txFrom23);
838
855
839
856
std::vector<CKey> keys;
840
857
keys.push_back (key1); keys.push_back (key2);
@@ -907,7 +924,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs)
907
924
}
908
925
909
926
CMutableTransaction txFrom = BuildCreditingTransaction (GetScriptForDestination (keys[0 ].GetPubKey ().GetID ()));
910
- CMutableTransaction txTo = BuildSpendingTransaction (CScript (), txFrom);
927
+ CMutableTransaction txTo = BuildSpendingTransaction (CScript (), CScriptWitness (), txFrom);
911
928
CScript& scriptPubKey = txFrom.vout [0 ].scriptPubKey ;
912
929
CScript& scriptSig = txTo.vin [0 ].scriptSig ;
913
930
0 commit comments