Changeset View
Changeset View
Standalone View
Standalone View
src/test/script_P2SH_tests.cpp
Show First 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(sign) { | ||||
std::string reason; | std::string reason; | ||||
txFrom.vout.resize(8); | txFrom.vout.resize(8); | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
txFrom.vout[i].scriptPubKey = evalScripts[i]; | txFrom.vout[i].scriptPubKey = evalScripts[i]; | ||||
txFrom.vout[i].nValue = COIN; | txFrom.vout[i].nValue = COIN; | ||||
txFrom.vout[i + 4].scriptPubKey = standardScripts[i]; | txFrom.vout[i + 4].scriptPubKey = standardScripts[i]; | ||||
txFrom.vout[i + 4].nValue = COIN; | txFrom.vout[i + 4].nValue = COIN; | ||||
} | } | ||||
BOOST_CHECK(IsStandardTx(txFrom, reason)); | BOOST_CHECK(IsStandardTx(CTransaction(txFrom), reason)); | ||||
CMutableTransaction txTo[8]; // Spending transactions | CMutableTransaction txTo[8]; // Spending transactions | ||||
for (int i = 0; i < 8; i++) { | for (int i = 0; i < 8; i++) { | ||||
txTo[i].vin.resize(1); | txTo[i].vin.resize(1); | ||||
txTo[i].vout.resize(1); | txTo[i].vout.resize(1); | ||||
txTo[i].vin[0].prevout.n = i; | txTo[i].vin[0].prevout.n = i; | ||||
txTo[i].vin[0].prevout.hash = txFrom.GetId(); | txTo[i].vin[0].prevout.hash = txFrom.GetId(); | ||||
txTo[i].vout[0].nValue = Amount(1); | txTo[i].vout[0].nValue = Amount(1); | ||||
BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), | BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), | ||||
strprintf("IsMine %d", i)); | strprintf("IsMine %d", i)); | ||||
} | } | ||||
for (int i = 0; i < 8; i++) { | for (int i = 0; i < 8; i++) { | ||||
BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, | BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), | ||||
txTo[i], 0, | |||||
SigHashType().withForkId(true)), | SigHashType().withForkId(true)), | ||||
strprintf("SignSignature %d", i)); | strprintf("SignSignature %d", i)); | ||||
} | } | ||||
// All of the above should be OK, and the txTos have valid signatures | // All of the above should be OK, and the txTos have valid signatures | ||||
// Check to make sure signature verification fails if we use the wrong | // Check to make sure signature verification fails if we use the wrong | ||||
// ScriptSig: | // ScriptSig: | ||||
for (int i = 0; i < 8; i++) { | for (int i = 0; i < 8; i++) { | ||||
PrecomputedTransactionData txdata(txTo[i]); | PrecomputedTransactionData txdata(CTransaction(txTo[i])); | ||||
for (int j = 0; j < 8; j++) { | for (int j = 0; j < 8; j++) { | ||||
CScript sigSave = txTo[i].vin[0].scriptSig; | CScript sigSave = txTo[i].vin[0].scriptSig; | ||||
txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig; | txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig; | ||||
const CTxOut &output = txFrom.vout[txTo[i].vin[0].prevout.n]; | const CTxOut &output = txFrom.vout[txTo[i].vin[0].prevout.n]; | ||||
bool sigOK = | bool sigOK = CScriptCheck( | ||||
CScriptCheck(output.scriptPubKey, output.nValue, txTo[i], 0, | output.scriptPubKey, output.nValue, CTransaction(txTo[i]), 0, | ||||
SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC | | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC | | ||||
SCRIPT_ENABLE_SIGHASH_FORKID, | SCRIPT_ENABLE_SIGHASH_FORKID, | ||||
false, txdata)(); | false, txdata)(); | ||||
if (i == j) { | if (i == j) { | ||||
BOOST_CHECK_MESSAGE(sigOK, | BOOST_CHECK_MESSAGE(sigOK, | ||||
strprintf("VerifySignature %d %d", i, j)); | strprintf("VerifySignature %d %d", i, j)); | ||||
} else { | } else { | ||||
BOOST_CHECK_MESSAGE(!sigOK, | BOOST_CHECK_MESSAGE(!sigOK, | ||||
strprintf("VerifySignature %d %d", i, j)); | strprintf("VerifySignature %d %d", i, j)); | ||||
} | } | ||||
txTo[i].vin[0].scriptSig = sigSave; | txTo[i].vin[0].scriptSig = sigSave; | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(set) { | ||||
// Funding transaction: | // Funding transaction: | ||||
CMutableTransaction txFrom; | CMutableTransaction txFrom; | ||||
std::string reason; | std::string reason; | ||||
txFrom.vout.resize(4); | txFrom.vout.resize(4); | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
txFrom.vout[i].scriptPubKey = outer[i]; | txFrom.vout[i].scriptPubKey = outer[i]; | ||||
txFrom.vout[i].nValue = CENT; | txFrom.vout[i].nValue = CENT; | ||||
} | } | ||||
BOOST_CHECK(IsStandardTx(txFrom, reason)); | BOOST_CHECK(IsStandardTx(CTransaction(txFrom), reason)); | ||||
// Spending transactions | // Spending transactions | ||||
CMutableTransaction txTo[4]; | CMutableTransaction txTo[4]; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
txTo[i].vin.resize(1); | txTo[i].vin.resize(1); | ||||
txTo[i].vout.resize(1); | txTo[i].vout.resize(1); | ||||
txTo[i].vin[0].prevout.n = i; | txTo[i].vin[0].prevout.n = i; | ||||
txTo[i].vin[0].prevout.hash = txFrom.GetId(); | txTo[i].vin[0].prevout.hash = txFrom.GetId(); | ||||
txTo[i].vout[0].nValue = 1 * CENT; | txTo[i].vout[0].nValue = 1 * CENT; | ||||
txTo[i].vout[0].scriptPubKey = inner[i]; | txTo[i].vout[0].scriptPubKey = inner[i]; | ||||
BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), | BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), | ||||
strprintf("IsMine %d", i)); | strprintf("IsMine %d", i)); | ||||
} | } | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, | BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), | ||||
txTo[i], 0, | |||||
SigHashType().withForkId(true)), | SigHashType().withForkId(true)), | ||||
strprintf("SignSignature %d", i)); | strprintf("SignSignature %d", i)); | ||||
BOOST_CHECK_MESSAGE(IsStandardTx(txTo[i], reason), | BOOST_CHECK_MESSAGE(IsStandardTx(CTransaction(txTo[i]), reason), | ||||
strprintf("txTo[%d].IsStandard", i)); | strprintf("txTo[%d].IsStandard", i)); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(is) { | BOOST_AUTO_TEST_CASE(is) { | ||||
// Test CScript::IsPayToScriptHash() | // Test CScript::IsPayToScriptHash() | ||||
uint160 dummy; | uint160 dummy; | ||||
CScript p2sh; | CScript p2sh; | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(AreInputsStandard) { | ||||
txFrom.vout[5].nValue = Amount(5000); | txFrom.vout[5].nValue = Amount(5000); | ||||
CScript twentySigops; | CScript twentySigops; | ||||
twentySigops << OP_CHECKMULTISIG; | twentySigops << OP_CHECKMULTISIG; | ||||
keystore.AddCScript(twentySigops); | keystore.AddCScript(twentySigops); | ||||
txFrom.vout[6].scriptPubKey = | txFrom.vout[6].scriptPubKey = | ||||
GetScriptForDestination(CScriptID(twentySigops)); | GetScriptForDestination(CScriptID(twentySigops)); | ||||
txFrom.vout[6].nValue = Amount(6000); | txFrom.vout[6].nValue = Amount(6000); | ||||
AddCoins(coins, txFrom, 0); | AddCoins(coins, CTransaction(txFrom), 0); | ||||
CMutableTransaction txTo; | CMutableTransaction txTo; | ||||
txTo.vout.resize(1); | txTo.vout.resize(1); | ||||
txTo.vout[0].scriptPubKey = | txTo.vout[0].scriptPubKey = | ||||
GetScriptForDestination(key[1].GetPubKey().GetID()); | GetScriptForDestination(key[1].GetPubKey().GetID()); | ||||
txTo.vin.resize(5); | txTo.vin.resize(5); | ||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
txTo.vin[i].prevout.n = i; | txTo.vin[i].prevout.n = i; | ||||
txTo.vin[i].prevout.hash = txFrom.GetId(); | txTo.vin[i].prevout.hash = txFrom.GetId(); | ||||
} | } | ||||
BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 0, | BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, | ||||
SigHashType().withForkId(true))); | SigHashType().withForkId(true))); | ||||
BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 1, | BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 1, | ||||
SigHashType().withForkId(true))); | SigHashType().withForkId(true))); | ||||
BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 2, | BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 2, | ||||
SigHashType().withForkId(true))); | SigHashType().withForkId(true))); | ||||
// SignSignature doesn't know how to sign these. We're not testing | // SignSignature doesn't know how to sign these. We're not testing | ||||
// validating signatures, so just create dummy signatures that DO include | // validating signatures, so just create dummy signatures that DO include | ||||
// the correct P2SH scripts: | // the correct P2SH scripts: | ||||
txTo.vin[3].scriptSig << OP_11 << OP_11 | txTo.vin[3].scriptSig << OP_11 << OP_11 | ||||
<< std::vector<uint8_t>(oneAndTwo.begin(), | << std::vector<uint8_t>(oneAndTwo.begin(), | ||||
oneAndTwo.end()); | oneAndTwo.end()); | ||||
txTo.vin[4].scriptSig << std::vector<uint8_t>(fifteenSigops.begin(), | txTo.vin[4].scriptSig << std::vector<uint8_t>(fifteenSigops.begin(), | ||||
fifteenSigops.end()); | fifteenSigops.end()); | ||||
BOOST_CHECK(::AreInputsStandard(txTo, coins)); | BOOST_CHECK(::AreInputsStandard(CTransaction(txTo), coins)); | ||||
// 22 P2SH sigops for all inputs (1 for vin[0], 6 for vin[3], 15 for vin[4] | // 22 P2SH sigops for all inputs (1 for vin[0], 6 for vin[3], 15 for vin[4] | ||||
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txTo, coins), 22U); | BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txTo), coins), 22U); | ||||
CMutableTransaction txToNonStd1; | CMutableTransaction txToNonStd1; | ||||
txToNonStd1.vout.resize(1); | txToNonStd1.vout.resize(1); | ||||
txToNonStd1.vout[0].scriptPubKey = | txToNonStd1.vout[0].scriptPubKey = | ||||
GetScriptForDestination(key[1].GetPubKey().GetID()); | GetScriptForDestination(key[1].GetPubKey().GetID()); | ||||
txToNonStd1.vout[0].nValue = Amount(1000); | txToNonStd1.vout[0].nValue = Amount(1000); | ||||
txToNonStd1.vin.resize(1); | txToNonStd1.vin.resize(1); | ||||
txToNonStd1.vin[0].prevout.n = 5; | txToNonStd1.vin[0].prevout.n = 5; | ||||
txToNonStd1.vin[0].prevout.hash = txFrom.GetId(); | txToNonStd1.vin[0].prevout.hash = txFrom.GetId(); | ||||
txToNonStd1.vin[0].scriptSig | txToNonStd1.vin[0].scriptSig | ||||
<< std::vector<uint8_t>(sixteenSigops.begin(), sixteenSigops.end()); | << std::vector<uint8_t>(sixteenSigops.begin(), sixteenSigops.end()); | ||||
BOOST_CHECK(!::AreInputsStandard(txToNonStd1, coins)); | BOOST_CHECK(!::AreInputsStandard(CTransaction(txToNonStd1), coins)); | ||||
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd1, coins), 16U); | BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd1), coins), 16U); | ||||
CMutableTransaction txToNonStd2; | CMutableTransaction txToNonStd2; | ||||
txToNonStd2.vout.resize(1); | txToNonStd2.vout.resize(1); | ||||
txToNonStd2.vout[0].scriptPubKey = | txToNonStd2.vout[0].scriptPubKey = | ||||
GetScriptForDestination(key[1].GetPubKey().GetID()); | GetScriptForDestination(key[1].GetPubKey().GetID()); | ||||
txToNonStd2.vout[0].nValue = Amount(1000); | txToNonStd2.vout[0].nValue = Amount(1000); | ||||
txToNonStd2.vin.resize(1); | txToNonStd2.vin.resize(1); | ||||
txToNonStd2.vin[0].prevout.n = 6; | txToNonStd2.vin[0].prevout.n = 6; | ||||
txToNonStd2.vin[0].prevout.hash = txFrom.GetId(); | txToNonStd2.vin[0].prevout.hash = txFrom.GetId(); | ||||
txToNonStd2.vin[0].scriptSig | txToNonStd2.vin[0].scriptSig | ||||
<< std::vector<uint8_t>(twentySigops.begin(), twentySigops.end()); | << std::vector<uint8_t>(twentySigops.begin(), twentySigops.end()); | ||||
BOOST_CHECK(!::AreInputsStandard(txToNonStd2, coins)); | BOOST_CHECK(!::AreInputsStandard(CTransaction(txToNonStd2), coins)); | ||||
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd2, coins), 20U); | BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd2), coins), 20U); | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |