Changeset View
Changeset View
Standalone View
Standalone View
src/test/transaction_tests.cpp
Show First 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | for (size_t idx = 0; idx < tests.size(); idx++) { | ||||
CAmount amount = 0; | CAmount amount = 0; | ||||
if (mapprevOutValues.count(tx.vin[i].prevout)) { | if (mapprevOutValues.count(tx.vin[i].prevout)) { | ||||
amount = mapprevOutValues[tx.vin[i].prevout]; | amount = mapprevOutValues[tx.vin[i].prevout]; | ||||
} | } | ||||
uint32_t verify_flags = ParseScriptFlags(test[2].get_str()); | uint32_t verify_flags = ParseScriptFlags(test[2].get_str()); | ||||
BOOST_CHECK_MESSAGE( | BOOST_CHECK_MESSAGE( | ||||
VerifyScript(tx.vin[i].scriptSig, | VerifyScript( | ||||
tx.vin[i].scriptSig, | |||||
mapprevOutScriptPubKeys[tx.vin[i].prevout], | mapprevOutScriptPubKeys[tx.vin[i].prevout], | ||||
verify_flags, TransactionSignatureChecker( | verify_flags, | ||||
&tx, i, amount, txdata), | TransactionSignatureChecker(&tx, i, amount, txdata), | ||||
&err), | &err), | ||||
strTest); | strTest); | ||||
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, | BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, | ||||
ScriptErrorString(err)); | ScriptErrorString(err)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | SetupDummyInputs(CBasicKeyStore &keystoreRet, CCoinsViewCache &coinsRet) { | ||||
CKey key[4]; | CKey key[4]; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
key[i].MakeNewKey(i % 2); | key[i].MakeNewKey(i % 2); | ||||
keystoreRet.AddKey(key[i]); | keystoreRet.AddKey(key[i]); | ||||
} | } | ||||
// Create some dummy input transactions | // Create some dummy input transactions | ||||
dummyTransactions[0].vout.resize(2); | dummyTransactions[0].vout.resize(2); | ||||
dummyTransactions[0].vout[0].nValue = 11 * CENT; | dummyTransactions[0].vout[0].nValue = 11 * CENT.GetSatoshis(); | ||||
dummyTransactions[0].vout[0].scriptPubKey | dummyTransactions[0].vout[0].scriptPubKey | ||||
<< ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; | << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; | ||||
dummyTransactions[0].vout[1].nValue = 50 * CENT; | dummyTransactions[0].vout[1].nValue = 50 * CENT.GetSatoshis(); | ||||
dummyTransactions[0].vout[1].scriptPubKey | dummyTransactions[0].vout[1].scriptPubKey | ||||
<< ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; | << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; | ||||
AddCoins(coinsRet, dummyTransactions[0], 0); | AddCoins(coinsRet, dummyTransactions[0], 0); | ||||
dummyTransactions[1].vout.resize(2); | dummyTransactions[1].vout.resize(2); | ||||
dummyTransactions[1].vout[0].nValue = 21 * CENT; | dummyTransactions[1].vout[0].nValue = 21 * CENT.GetSatoshis(); | ||||
dummyTransactions[1].vout[0].scriptPubKey = | dummyTransactions[1].vout[0].scriptPubKey = | ||||
GetScriptForDestination(key[2].GetPubKey().GetID()); | GetScriptForDestination(key[2].GetPubKey().GetID()); | ||||
dummyTransactions[1].vout[1].nValue = 22 * CENT; | dummyTransactions[1].vout[1].nValue = 22 * CENT.GetSatoshis(); | ||||
dummyTransactions[1].vout[1].scriptPubKey = | dummyTransactions[1].vout[1].scriptPubKey = | ||||
GetScriptForDestination(key[3].GetPubKey().GetID()); | GetScriptForDestination(key[3].GetPubKey().GetID()); | ||||
AddCoins(coinsRet, dummyTransactions[1], 0); | AddCoins(coinsRet, dummyTransactions[1], 0); | ||||
return dummyTransactions; | return dummyTransactions; | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(test_Get) { | BOOST_AUTO_TEST_CASE(test_Get) { | ||||
Show All 12 Lines | BOOST_AUTO_TEST_CASE(test_Get) { | ||||
t1.vin[1].prevout.n = 0; | t1.vin[1].prevout.n = 0; | ||||
t1.vin[1].scriptSig << std::vector<uint8_t>(65, 0) | t1.vin[1].scriptSig << std::vector<uint8_t>(65, 0) | ||||
<< std::vector<uint8_t>(33, 4); | << std::vector<uint8_t>(33, 4); | ||||
t1.vin[2].prevout.hash = dummyTransactions[1].GetId(); | t1.vin[2].prevout.hash = dummyTransactions[1].GetId(); | ||||
t1.vin[2].prevout.n = 1; | t1.vin[2].prevout.n = 1; | ||||
t1.vin[2].scriptSig << std::vector<uint8_t>(65, 0) | t1.vin[2].scriptSig << std::vector<uint8_t>(65, 0) | ||||
<< std::vector<uint8_t>(33, 4); | << std::vector<uint8_t>(33, 4); | ||||
t1.vout.resize(2); | t1.vout.resize(2); | ||||
t1.vout[0].nValue = 90 * CENT; | t1.vout[0].nValue = 90 * CENT.GetSatoshis(); | ||||
t1.vout[0].scriptPubKey << OP_1; | t1.vout[0].scriptPubKey << OP_1; | ||||
BOOST_CHECK(AreInputsStandard(t1, coins)); | BOOST_CHECK(AreInputsStandard(t1, coins)); | ||||
BOOST_CHECK_EQUAL(coins.GetValueIn(t1), (50 + 21 + 22) * CENT); | BOOST_CHECK_EQUAL(coins.GetValueIn(t1), | ||||
(50 + 21 + 22) * CENT.GetSatoshis()); | |||||
} | } | ||||
void CreateCreditAndSpend(const CKeyStore &keystore, const CScript &outscript, | void CreateCreditAndSpend(const CKeyStore &keystore, const CScript &outscript, | ||||
CTransactionRef &output, CMutableTransaction &input, | CTransactionRef &output, CMutableTransaction &input, | ||||
bool success = true) { | bool success = true) { | ||||
CMutableTransaction outputm; | CMutableTransaction outputm; | ||||
outputm.nVersion = 1; | outputm.nVersion = 1; | ||||
outputm.vin.resize(1); | outputm.vin.resize(1); | ||||
▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(test_witness) { | ||||
// Normal 2-of-2 multisig | // Normal 2-of-2 multisig | ||||
CreateCreditAndSpend(keystore, scriptMulti, output1, input1, false); | CreateCreditAndSpend(keystore, scriptMulti, output1, input1, false); | ||||
CheckWithFlag(output1, input1, 0, false); | CheckWithFlag(output1, input1, 0, false); | ||||
CreateCreditAndSpend(keystore2, scriptMulti, output2, input2, false); | CreateCreditAndSpend(keystore2, scriptMulti, output2, input2, false); | ||||
CheckWithFlag(output2, input2, 0, false); | CheckWithFlag(output2, input2, 0, false); | ||||
BOOST_CHECK(*output1 == *output2); | BOOST_CHECK(*output1 == *output2); | ||||
UpdateTransaction( | UpdateTransaction( | ||||
input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, | input1, 0, | ||||
CombineSignatures(output1->vout[0].scriptPubKey, | |||||
MutableTransactionSignatureChecker( | MutableTransactionSignatureChecker( | ||||
&input1, 0, output1->vout[0].nValue), | &input1, 0, output1->vout[0].nValue), | ||||
DataFromTransaction(input1, 0), | DataFromTransaction(input1, 0), | ||||
DataFromTransaction(input2, 0))); | DataFromTransaction(input2, 0))); | ||||
CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true); | CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true); | ||||
// P2SH 2-of-2 multisig | // P2SH 2-of-2 multisig | ||||
CreateCreditAndSpend(keystore, | CreateCreditAndSpend(keystore, | ||||
GetScriptForDestination(CScriptID(scriptMulti)), | GetScriptForDestination(CScriptID(scriptMulti)), | ||||
output1, input1, false); | output1, input1, false); | ||||
CheckWithFlag(output1, input1, 0, true); | CheckWithFlag(output1, input1, 0, true); | ||||
CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, false); | CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, false); | ||||
CreateCreditAndSpend(keystore2, | CreateCreditAndSpend(keystore2, | ||||
GetScriptForDestination(CScriptID(scriptMulti)), | GetScriptForDestination(CScriptID(scriptMulti)), | ||||
output2, input2, false); | output2, input2, false); | ||||
CheckWithFlag(output2, input2, 0, true); | CheckWithFlag(output2, input2, 0, true); | ||||
CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, false); | CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, false); | ||||
BOOST_CHECK(*output1 == *output2); | BOOST_CHECK(*output1 == *output2); | ||||
UpdateTransaction( | UpdateTransaction( | ||||
input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, | input1, 0, | ||||
CombineSignatures(output1->vout[0].scriptPubKey, | |||||
MutableTransactionSignatureChecker( | MutableTransactionSignatureChecker( | ||||
&input1, 0, output1->vout[0].nValue), | &input1, 0, output1->vout[0].nValue), | ||||
DataFromTransaction(input1, 0), | DataFromTransaction(input1, 0), | ||||
DataFromTransaction(input2, 0))); | DataFromTransaction(input2, 0))); | ||||
CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true); | CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true); | ||||
CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true); | CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(test_IsStandard) { | BOOST_AUTO_TEST_CASE(test_IsStandard) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CBasicKeyStore keystore; | CBasicKeyStore keystore; | ||||
CCoinsView coinsDummy; | CCoinsView coinsDummy; | ||||
CCoinsViewCache coins(&coinsDummy); | CCoinsViewCache coins(&coinsDummy); | ||||
std::vector<CMutableTransaction> dummyTransactions = | std::vector<CMutableTransaction> dummyTransactions = | ||||
SetupDummyInputs(keystore, coins); | SetupDummyInputs(keystore, coins); | ||||
CMutableTransaction t; | CMutableTransaction t; | ||||
t.vin.resize(1); | t.vin.resize(1); | ||||
t.vin[0].prevout.hash = dummyTransactions[0].GetId(); | t.vin[0].prevout.hash = dummyTransactions[0].GetId(); | ||||
t.vin[0].prevout.n = 1; | t.vin[0].prevout.n = 1; | ||||
t.vin[0].scriptSig << std::vector<uint8_t>(65, 0); | t.vin[0].scriptSig << std::vector<uint8_t>(65, 0); | ||||
t.vout.resize(1); | t.vout.resize(1); | ||||
t.vout[0].nValue = 90 * CENT; | t.vout[0].nValue = 90 * CENT.GetSatoshis(); | ||||
CKey key; | CKey key; | ||||
key.MakeNewKey(true); | key.MakeNewKey(true); | ||||
t.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID()); | t.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID()); | ||||
std::string reason; | std::string reason; | ||||
BOOST_CHECK(IsStandardTx(t, reason)); | BOOST_CHECK(IsStandardTx(t, reason)); | ||||
// Check dust with default relay fee: | // Check dust with default relay fee: | ||||
Show All 38 Lines | t.vout[0].scriptPubKey = | ||||
"271967f1a67130b7105cd6a828e03909a67962e0ea1f61de" | "271967f1a67130b7105cd6a828e03909a67962e0ea1f61de" | ||||
"b649f6bc3f4cef3800"); | "b649f6bc3f4cef3800"); | ||||
BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY + 1, t.vout[0].scriptPubKey.size()); | BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY + 1, t.vout[0].scriptPubKey.size()); | ||||
BOOST_CHECK(!IsStandardTx(t, reason)); | BOOST_CHECK(!IsStandardTx(t, reason)); | ||||
// Data payload can be encoded in any way... | // Data payload can be encoded in any way... | ||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex(""); | t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex(""); | ||||
BOOST_CHECK(IsStandardTx(t, reason)); | BOOST_CHECK(IsStandardTx(t, reason)); | ||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("00") | t.vout[0].scriptPubKey = CScript() | ||||
<< ParseHex("01"); | << OP_RETURN << ParseHex("00") << ParseHex("01"); | ||||
BOOST_CHECK(IsStandardTx(t, reason)); | BOOST_CHECK(IsStandardTx(t, reason)); | ||||
// OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()! | // OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()! | ||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RESERVED << -1 << 0 | t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RESERVED << -1 << 0 | ||||
<< ParseHex("01") << 2 << 3 << 4 << 5 | << ParseHex("01") << 2 << 3 << 4 << 5 | ||||
<< 6 << 7 << 8 << 9 << 10 << 11 << 12 | << 6 << 7 << 8 << 9 << 10 << 11 << 12 | ||||
<< 13 << 14 << 15 << 16; | << 13 << 14 << 15 << 16; | ||||
BOOST_CHECK(IsStandardTx(t, reason)); | BOOST_CHECK(IsStandardTx(t, reason)); | ||||
t.vout[0].scriptPubKey = CScript() | t.vout[0].scriptPubKey = CScript() | ||||
Show All 39 Lines |