Skip to content

Commit 63f372a

Browse files
committed
Add segwit support to script_tests
1 parent a9df5b6 commit 63f372a

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"],
@@ -1251,6 +1251,12 @@
12511251
["0x17 0x3014021077777777777777777777777777777777020001", "0 CHECKSIG NOT", "DERSIG", "SIG_DER", "Zero-length S is incorrectly encoded for DERSIG"],
12521252
["0x27 0x302402107777777777777777777777777777777702108777777777777777777777777777777701", "0 CHECKSIG NOT", "DERSIG", "SIG_DER", "Negative S is incorrectly encoded for DERSIG"],
12531253

1254+
["Some basic segwit checks"],
1255+
[["00"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "WITNESS", "EVAL_FALSE", "Invalid witness script"],
1256+
[["51"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "WITNESS", "WITNESS_PROGRAM_MISMATCH", "Witness script hash mismatch"],
1257+
[["00"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "", "OK", "Invalid witness script without WITNESS"],
1258+
[["51"], "", "0 0x206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", "", "OK", "Witness script hash mismatch without WITNESS"],
1259+
12541260
["Automatically generated test cases"],
12551261
[
12561262
"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
}
@@ -703,29 +712,37 @@ BOOST_AUTO_TEST_CASE(script_json_test)
703712
{
704713
// Read tests from test/data/script_tests.json
705714
// 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" ]
707716
// ... where scriptSig and scriptPubKey are stringified
708717
// scripts.
709718
UniValue tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests)));
710719

711720
for (unsigned int idx = 0; idx < tests.size(); idx++) {
712721
UniValue test = tests[idx];
713722
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)
715732
{
716733
if (test.size() != 1) {
717734
BOOST_ERROR("Bad test: " << strTest);
718735
}
719736
continue;
720737
}
721-
string scriptSigString = test[0].get_str();
738+
string scriptSigString = test[pos++].get_str();
722739
CScript scriptSig = ParseScript(scriptSigString);
723-
string scriptPubKeyString = test[1].get_str();
740+
string scriptPubKeyString = test[pos++].get_str();
724741
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());
727744

728-
DoTest(scriptPubKey, scriptSig, scriptflags, strTest, scriptError);
745+
DoTest(scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError);
729746
}
730747
}
731748

@@ -803,7 +820,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
803820
scriptPubKey12 << OP_1 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
804821

805822
CMutableTransaction txFrom12 = BuildCreditingTransaction(scriptPubKey12);
806-
CMutableTransaction txTo12 = BuildSpendingTransaction(CScript(), txFrom12);
823+
CMutableTransaction txTo12 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom12);
807824

808825
CScript goodsig1 = sign_multisig(scriptPubKey12, key1, txTo12);
809826
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)
834851
scriptPubKey23 << OP_2 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << ToByteVector(key3.GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
835852

836853
CMutableTransaction txFrom23 = BuildCreditingTransaction(scriptPubKey23);
837-
CMutableTransaction txTo23 = BuildSpendingTransaction(CScript(), txFrom23);
854+
CMutableTransaction txTo23 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom23);
838855

839856
std::vector<CKey> keys;
840857
keys.push_back(key1); keys.push_back(key2);
@@ -907,7 +924,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs)
907924
}
908925

909926
CMutableTransaction txFrom = BuildCreditingTransaction(GetScriptForDestination(keys[0].GetPubKey().GetID()));
910-
CMutableTransaction txTo = BuildSpendingTransaction(CScript(), txFrom);
927+
CMutableTransaction txTo = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom);
911928
CScript& scriptPubKey = txFrom.vout[0].scriptPubKey;
912929
CScript& scriptSig = txTo.vin[0].scriptSig;
913930

0 commit comments

Comments
 (0)