Changeset View
Changeset View
Standalone View
Standalone View
src/test/script_P2SH_tests.cpp
Show All 27 Lines | static bool Verify(const CScript &scriptSig, const CScript &scriptPubKey, | ||||
// Create dummy to/from transactions: | // Create dummy to/from transactions: | ||||
CMutableTransaction txFrom; | CMutableTransaction txFrom; | ||||
txFrom.vout.resize(1); | txFrom.vout.resize(1); | ||||
txFrom.vout[0].scriptPubKey = scriptPubKey; | txFrom.vout[0].scriptPubKey = scriptPubKey; | ||||
CMutableTransaction txTo; | CMutableTransaction txTo; | ||||
txTo.vin.resize(1); | txTo.vin.resize(1); | ||||
txTo.vout.resize(1); | txTo.vout.resize(1); | ||||
txTo.vin[0].prevout.n = 0; | txTo.vin[0].prevout = COutPoint(txFrom.GetId(), 0); | ||||
txTo.vin[0].prevout.hash = txFrom.GetId(); | |||||
txTo.vin[0].scriptSig = scriptSig; | txTo.vin[0].scriptSig = scriptSig; | ||||
txTo.vout[0].nValue = Amount(1); | txTo.vout[0].nValue = Amount(1); | ||||
return VerifyScript( | return VerifyScript( | ||||
scriptSig, scriptPubKey, | scriptSig, scriptPubKey, | ||||
(fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE) | | (fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE) | | ||||
SCRIPT_ENABLE_SIGHASH_FORKID, | SCRIPT_ENABLE_SIGHASH_FORKID, | ||||
MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue), | MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue), | ||||
Show All 40 Lines | for (int i = 0; i < 4; i++) { | ||||
txFrom.vout[i + 4].nValue = COIN; | txFrom.vout[i + 4].nValue = COIN; | ||||
} | } | ||||
BOOST_CHECK(IsStandardTx(CTransaction(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 = COutPoint(txFrom.GetId(), i); | ||||
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, CTransaction(txFrom), | BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), | ||||
txTo[i], 0, | txTo[i], 0, | ||||
SigHashType().withForkId()), | SigHashType().withForkId()), | ||||
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++) { | ||||
CTransaction tx(txTo[i]); | CTransaction tx(txTo[i]); | ||||
PrecomputedTransactionData txdata(tx); | PrecomputedTransactionData txdata(tx); | ||||
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.GetN()]; | ||||
bool sigOK = CScriptCheck( | bool sigOK = CScriptCheck( | ||||
output.scriptPubKey, output.nValue, CTransaction(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)); | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(set) { | ||||
} | } | ||||
BOOST_CHECK(IsStandardTx(CTransaction(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 = COutPoint(txFrom.GetId(), i); | ||||
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, CTransaction(txFrom), | BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), | ||||
txTo[i], 0, | txTo[i], 0, | ||||
▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(AreInputsStandard) { | ||||
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 = COutPoint(txFrom.GetId(), i); | ||||
txTo.vin[i].prevout.hash = txFrom.GetId(); | |||||
} | } | ||||
BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, | BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, | ||||
SigHashType().withForkId())); | SigHashType().withForkId())); | ||||
BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 1, | BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 1, | ||||
SigHashType().withForkId())); | SigHashType().withForkId())); | ||||
BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 2, | BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 2, | ||||
SigHashType().withForkId())); | SigHashType().withForkId())); | ||||
// 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 | ||||
Show All 9 Lines | BOOST_AUTO_TEST_CASE(AreInputsStandard) { | ||||
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(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 = COutPoint(txFrom.GetId(), 5); | ||||
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(CTransaction(txToNonStd1), coins)); | BOOST_CHECK(!::AreInputsStandard(CTransaction(txToNonStd1), coins)); | ||||
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(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 = COutPoint(txFrom.GetId(), 6); | ||||
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(CTransaction(txToNonStd2), coins)); | BOOST_CHECK(!::AreInputsStandard(CTransaction(txToNonStd2), coins)); | ||||
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd2), coins), 20U); | BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd2), coins), 20U); | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |