Skip to content

Commit 06d3805

Browse files
committed
[qa] Add segwit support to script_tests
Contains fix by Johnson Lau.
1 parent 00f46cb commit 06d3805

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/test/data/script_tests.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
["Format is: [scriptSig, scriptPubKey, flags, expected_scripterror, ... comments]"],
2+
["Format is: [[wit...]?, scriptSig, scriptPubKey, flags, expected_scripterror, ... comments]"],
33
["It is evaluated as if there was a crediting coinbase transaction with two 0"],
44
["pushes as scriptSig, and one output of 0 satoshi and given scriptPubKey,"],
55
["followed by a spending transaction which spends this output as only input (and"],
@@ -1253,6 +1253,12 @@
12531253
["0x17 0x3014021077777777777777777777777777777777020001", "0 CHECKSIG NOT", "DERSIG", "SIG_DER", "Zero-length S is incorrectly encoded for DERSIG"],
12541254
["0x27 0x302402107777777777777777777777777777777702108777777777777777777777777777777701", "0 CHECKSIG NOT", "DERSIG", "SIG_DER", "Negative S is incorrectly encoded for DERSIG"],
12551255

1256+
["Some basic segwit checks"],
1257+
[["00"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "P2SH,WITNESS", "EVAL_FALSE", "Invalid witness script"],
1258+
[["51"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "P2SH,WITNESS", "WITNESS_PROGRAM_MISMATCH", "Witness script hash mismatch"],
1259+
[["00"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "", "OK", "Invalid witness script without WITNESS"],
1260+
[["51"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "", "OK", "Witness script hash mismatch without WITNESS"],
1261+
12561262
["Automatically generated test cases"],
12571263
[
12581264
"0x47 0x304402200a5c6163f07b8d3b013c4d1d6dba25e780b39658d79ba37af7057a3b7f15ffa102201fd9b4eaa9943f734928b99a83592c2e7bf342ea2680f6a2bb705167966b742001",

src/test/script_tests.cpp

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,14 @@ static ScriptErrorDesc script_errors[]={
8888
{SCRIPT_ERR_SIG_NULLDUMMY, "SIG_NULLDUMMY"},
8989
{SCRIPT_ERR_PUBKEYTYPE, "PUBKEYTYPE"},
9090
{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"},
9299
};
93100

94101
const char *FormatScriptError(ScriptError_t err)
@@ -127,13 +134,15 @@ CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey)
127134
return txCredit;
128135
}
129136

130-
CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CMutableTransaction& txCredit)
137+
CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScriptWitness& scriptWitness, const CMutableTransaction& txCredit)
131138
{
132139
CMutableTransaction txSpend;
133140
txSpend.nVersion = 1;
134141
txSpend.nLockTime = 0;
135142
txSpend.vin.resize(1);
136143
txSpend.vout.resize(1);
144+
txSpend.wit.vtxinwit.resize(1);
145+
txSpend.wit.vtxinwit[0].scriptWitness = scriptWitness;
137146
txSpend.vin[0].prevout.hash = txCredit.GetHash();
138147
txSpend.vin[0].prevout.n = 0;
139148
txSpend.vin[0].scriptSig = scriptSig;
@@ -144,7 +153,7 @@ CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CMu
144153
return txSpend;
145154
}
146155

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)
148157
{
149158
bool expect = (scriptError == SCRIPT_ERR_OK);
150159
if (flags & SCRIPT_VERIFY_CLEANSTACK) {
@@ -153,12 +162,12 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, co
153162
}
154163
ScriptError err;
155164
CMutableTransaction txCredit = BuildCreditingTransaction(scriptPubKey);
156-
CMutableTransaction tx = BuildSpendingTransaction(scriptSig, txCredit);
165+
CMutableTransaction tx = BuildSpendingTransaction(scriptSig, scriptWitness, txCredit);
157166
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);
159168
BOOST_CHECK_MESSAGE(err == scriptError, std::string(FormatScriptError(err)) + " where " + std::string(FormatScriptError((ScriptError_t)scriptError)) + " expected: " + message);
160169
#if defined(HAVE_CONSENSUS_LIB)
161-
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
170+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_WITNESS);
162171
stream << tx2;
163172
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
164173
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
280289
} else {
281290
creditTx = BuildCreditingTransaction(redeemScript);
282291
}
283-
spendTx = BuildSpendingTransaction(CScript(), creditTx);
292+
spendTx = BuildSpendingTransaction(CScript(), CScriptWitness(), creditTx);
284293
}
285294

286295
TestBuilder& ScriptError(ScriptError_t err)
@@ -363,7 +372,7 @@ class TestBuilder
363372
{
364373
TestBuilder copy = *this; // Make a copy so we can rollback the push.
365374
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);
367376
*this = copy;
368377
return *this;
369378
}
@@ -706,29 +715,37 @@ BOOST_AUTO_TEST_CASE(script_json_test)
706715
{
707716
// Read tests from test/data/script_tests.json
708717
// 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" ]
710719
// ... where scriptSig and scriptPubKey are stringified
711720
// scripts.
712721
UniValue tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests)));
713722

714723
for (unsigned int idx = 0; idx < tests.size(); idx++) {
715724
UniValue test = tests[idx];
716725
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)
718735
{
719736
if (test.size() != 1) {
720737
BOOST_ERROR("Bad test: " << strTest);
721738
}
722739
continue;
723740
}
724-
string scriptSigString = test[0].get_str();
741+
string scriptSigString = test[pos++].get_str();
725742
CScript scriptSig = ParseScript(scriptSigString);
726-
string scriptPubKeyString = test[1].get_str();
743+
string scriptPubKeyString = test[pos++].get_str();
727744
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());
730747

731-
DoTest(scriptPubKey, scriptSig, scriptflags, strTest, scriptError);
748+
DoTest(scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError);
732749
}
733750
}
734751

@@ -806,7 +823,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
806823
scriptPubKey12 << OP_1 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
807824

808825
CMutableTransaction txFrom12 = BuildCreditingTransaction(scriptPubKey12);
809-
CMutableTransaction txTo12 = BuildSpendingTransaction(CScript(), txFrom12);
826+
CMutableTransaction txTo12 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom12);
810827

811828
CScript goodsig1 = sign_multisig(scriptPubKey12, key1, txTo12);
812829
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)
837854
scriptPubKey23 << OP_2 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << ToByteVector(key3.GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
838855

839856
CMutableTransaction txFrom23 = BuildCreditingTransaction(scriptPubKey23);
840-
CMutableTransaction txTo23 = BuildSpendingTransaction(CScript(), txFrom23);
857+
CMutableTransaction txTo23 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom23);
841858

842859
std::vector<CKey> keys;
843860
keys.push_back(key1); keys.push_back(key2);
@@ -910,7 +927,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs)
910927
}
911928

912929
CMutableTransaction txFrom = BuildCreditingTransaction(GetScriptForDestination(keys[0].GetPubKey().GetID()));
913-
CMutableTransaction txTo = BuildSpendingTransaction(CScript(), txFrom);
930+
CMutableTransaction txTo = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom);
914931
CScript& scriptPubKey = txFrom.vout[0].scriptPubKey;
915932
CScript& scriptSig = txTo.vin[0].scriptSig;
916933

0 commit comments

Comments
 (0)