Skip to content

Commit

Permalink
[BROKEN] Adapt existing unit tests to CA
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Mar 20, 2019
1 parent f4553d4 commit cd56626
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 58 deletions.
19 changes: 10 additions & 9 deletions src/test/blockfilter_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <test/data/blockfilters.json.h>
#include <test/test_bitcoin.h>

#include <asset.h>
#include <blockfilter.h>
#include <core_io.h>
#include <serialize.h>
Expand Down Expand Up @@ -61,23 +62,23 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
excluded_scripts[1] << std::vector<unsigned char>(5, 33) << OP_CHECKSIG;

CMutableTransaction tx_1;
tx_1.vout.emplace_back(100, included_scripts[0]);
tx_1.vout.emplace_back(200, included_scripts[1]);
tx_1.vout.emplace_back(CTxOut(CAsset(), 100, included_scripts[0]));
tx_1.vout.emplace_back(CTxOut(CAsset(), 200, included_scripts[1]));

CMutableTransaction tx_2;
tx_2.vout.emplace_back(300, included_scripts[2]);
tx_2.vout.emplace_back(0, excluded_scripts[0]);
tx_2.vout.emplace_back(400, excluded_scripts[2]); // Script is empty
tx_2.vout.emplace_back(CTxOut(CAsset(), 300, included_scripts[2]));
tx_2.vout.emplace_back(CTxOut(CAsset(), 0, excluded_scripts[0]));
tx_2.vout.emplace_back(CTxOut(CAsset(), 400, excluded_scripts[2])); // Script is empty

CBlock block;
block.vtx.push_back(MakeTransactionRef(tx_1));
block.vtx.push_back(MakeTransactionRef(tx_2));

CBlockUndo block_undo;
block_undo.vtxundo.emplace_back();
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(500, included_scripts[3]), 1000, true);
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(600, included_scripts[4]), 10000, false);
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(700, excluded_scripts[2]), 100000, false);
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(CAsset(), 500, included_scripts[3]), 1000, true);
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(CAsset(), 600, included_scripts[4]), 10000, false);
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(CAsset(), 700, excluded_scripts[2]), 100000, false);

BlockFilter block_filter(BlockFilterType::BASIC, block, block_undo);
const GCSFilter& filter = block_filter.GetFilter();
Expand Down Expand Up @@ -125,7 +126,7 @@ BOOST_AUTO_TEST_CASE(blockfilters_json_test)
const UniValue& prev_scripts = test[pos++].get_array();
for (unsigned int ii = 0; ii < prev_scripts.size(); ii++) {
std::vector<unsigned char> raw_script = ParseHex(prev_scripts[ii].get_str());
CTxOut txout(0, CScript(raw_script.begin(), raw_script.end()));
CTxOut txout(CAsset(), 0, CScript(raw_script.begin(), raw_script.end()));
tx_undo.vprevout.emplace_back(txout, 0, false);
}

Expand Down
34 changes: 19 additions & 15 deletions src/test/checkqueue_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static void Correct_Queue_range(std::vector<size_t> range)
tg.create_thread([&]{small_queue->Thread();});
}
// Make vChecks here to save on malloc (this test can be slow...)
std::vector<FakeCheckCheckCompletion> vChecks;
std::vector<FakeCheckCheckCompletion*> vChecks;
for (const size_t i : range) {
size_t total = i;
FakeCheckCheckCompletion::n_calls = 0;
Expand Down Expand Up @@ -226,10 +226,10 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
while (remaining) {
size_t r = InsecureRandRange(10);

std::vector<FailingCheck> vChecks;
vChecks.reserve(r);
for (size_t k = 0; k < r && remaining; k++, remaining--)
vChecks.emplace_back(remaining == 1);
std::vector<FailingCheck*> vChecks;
for (size_t k = 0; k < r && remaining; k++, remaining--) {
vChecks.push_back(new FailingCheck(remaining == 1));
}
control.Add(vChecks);
}
bool success = control.Wait();
Expand All @@ -256,9 +256,11 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
for (const bool end_fails : {true, false}) {
CCheckQueueControl<FailingCheck> control(fail_queue.get());
{
std::vector<FailingCheck> vChecks;
vChecks.resize(100, false);
vChecks[99] = end_fails;
std::vector<FailingCheck*> vChecks;
for (size_t i = 0; i < 100; i++) {
vChecks.push_back(new FailingCheck(false));
}
vChecks[99] = new FailingCheck(end_fails);
control.Add(vChecks);
}
bool r =control.Wait();
Expand Down Expand Up @@ -287,9 +289,10 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
CCheckQueueControl<UniqueCheck> control(queue.get());
while (total) {
size_t r = InsecureRandRange(10);
std::vector<UniqueCheck> vChecks;
for (size_t k = 0; k < r && total; k++)
vChecks.emplace_back(--total);
std::vector<UniqueCheck*> vChecks;
for (size_t k = 0; k < r && total; k++) {
vChecks.emplace_back(new UniqueCheck(--total));
}
control.Add(vChecks);
}
}
Expand Down Expand Up @@ -321,12 +324,12 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
CCheckQueueControl<MemoryCheck> control(queue.get());
while (total) {
size_t r = InsecureRandRange(10);
std::vector<MemoryCheck> vChecks;
std::vector<MemoryCheck*> vChecks;
for (size_t k = 0; k < r && total; k++) {
total--;
// Each iteration leaves data at the front, back, and middle
// to catch any sort of deallocation failure
vChecks.emplace_back(total == 0 || total == i || total == i/2);
vChecks.emplace_back(new MemoryCheck(total == 0 || total == i || total == i/2));
}
control.Add(vChecks);
}
Expand All @@ -349,11 +352,12 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
}
std::thread t0([&]() {
CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
std::vector<FrozenCleanupCheck> vChecks(1);
std::vector<FrozenCleanupCheck*> vChecks;
// Freezing can't be the default initialized behavior given how the queue
// swaps in default initialized Checks (otherwise freezing destructor
// would get called twice).
vChecks[0].should_freeze = true;
vChecks.push_back(new FrozenCleanupCheck());
vChecks[0]->should_freeze = true;
control.Add(vChecks);
control.Wait(); // Hangs here
});
Expand Down
4 changes: 2 additions & 2 deletions src/test/coins_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ BOOST_AUTO_TEST_CASE(ccoins_serialization)
ss3 >> cc3;
BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
BOOST_CHECK_EQUAL(cc3.out.nValue.GetAmount(), 0);
BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);

// scriptPubKey that ends beyond the end of the stream
Expand Down Expand Up @@ -577,7 +577,7 @@ void GetCoinsMapEntry(const CCoinsMap& map, CAmount& value, char& flags)
if (it->second.coin.IsSpent()) {
value = PRUNED;
} else {
value = it->second.coin.out.nValue;
value = it->second.coin.out.nValue.GetAmount();
}
flags = it->second.flags;
assert(flags != NO_ENTRY);
Expand Down
12 changes: 6 additions & 6 deletions src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static void TestPackageSelection(const CChainParams& chainparams, const CScript&
// of the transactions is below the min relay fee
// Remove the low fee transaction and replace with a higher fee transaction
mempool.removeRecursive(tx);
tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
tx.vout[0].nValue = tx.vout[0].nValue.GetAmount() - 2; // Now we should be just over the min relay fee
hashLowFeeTx = tx.GetHash();
mempool.addUnchecked(entry.Fee(feeToUse+2).FromTx(tx));
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
Expand Down Expand Up @@ -273,7 +273,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
tx.vout[0].nValue = BLOCKSUBSIDY;
for (unsigned int i = 0; i < 1001; ++i)
{
tx.vout[0].nValue -= LOWFEE;
tx.vout[0].nValue = tx.vout[0].nValue.GetAmount() - LOWFEE;
hash = tx.GetHash();
bool spendsCoinbase = i == 0; // only first tx spends coinbase
// If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
Expand All @@ -288,7 +288,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
tx.vout[0].nValue = BLOCKSUBSIDY;
for (unsigned int i = 0; i < 1001; ++i)
{
tx.vout[0].nValue -= LOWFEE;
tx.vout[0].nValue = tx.vout[0].nValue.GetAmount() - LOWFEE;
hash = tx.GetHash();
bool spendsCoinbase = i == 0; // only first tx spends coinbase
// If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
Expand All @@ -309,7 +309,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
tx.vout[0].nValue = BLOCKSUBSIDY;
for (unsigned int i = 0; i < 128; ++i)
{
tx.vout[0].nValue -= LOWFEE;
tx.vout[0].nValue = tx.vout[0].nValue.GetAmount() - LOWFEE;
hash = tx.GetHash();
bool spendsCoinbase = i == 0; // only first tx spends coinbase
mempool.addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
Expand All @@ -335,7 +335,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
tx.vin[1].scriptSig = CScript() << OP_1;
tx.vin[1].prevout.hash = txFirst[0]->GetHash();
tx.vin[1].prevout.n = 0;
tx.vout[0].nValue = tx.vout[0].nValue+BLOCKSUBSIDY-HIGHERFEE; //First txn output + fresh coinbase - new txn fee
tx.vout[0].nValue = tx.vout[0].nValue.GetAmount() + BLOCKSUBSIDY - HIGHERFEE; //First txn output + fresh coinbase - new txn fee
hash = tx.GetHash();
mempool.addUnchecked(entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
Expand Down Expand Up @@ -404,7 +404,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
mempool.addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
tx.vin[0].prevout.hash = hash;
tx.vin[0].scriptSig = CScript() << std::vector<unsigned char>(script.begin(), script.end());
tx.vout[0].nValue -= LOWFEE;
tx.vout[0].nValue = tx.vout[0].nValue.GetAmount() - LOWFEE;
hash = tx.GetHash();
mempool.addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
// Should throw block-validation-failed
Expand Down
29 changes: 14 additions & 15 deletions src/test/pegin_witness_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@

std::vector<std::vector<unsigned char> > witness_stack = {
ParseHex("00ca9a3b00000000"),
ParseHex("e48a1a02a8f799892fda58347c2d794144311d4307dbfd10f77ffe28088c60be"),
ParseHex("ef4699c160d014d5ff79636d8a4cb990b9df4ebab649f144d19f5c495c585e47"),
ParseHex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f"),
ParseHex("00141eef6361cd1507a303834285d1521d6baf1b19ae"),
ParseHex("0200000001b399292c8100b8a1b66eb23896f799c1712390d560af0f70e81acd2d17a3b06e0000000049483045022100c3c749623486ea57ea93dfaf78d85590d78c7590a25768fe80f0ea4d6047419002202a0a00a90392b86c53c0fdda908c4591ba28040c16c25734c23b7df3c8b70acd01feffffff0228196bee000000001976a914470dd41542ee1a1bd75f1a838878648c8d65622488ac00ca9a3b0000000017a914cb60b1d7f76ba12b45a116c482c165a74c5d7e388765000000"),
ParseHex("000000205e3913a320cd2e3a2efa141e47419f54cb9e82320cf8dbc812fc19b9a1b2413a57f5e9fb4fa22de191454a241387f5d10cc794ee0fbf72ae2841baf3129a4eab8133025affff7f20000000000200000002f9d0be670007d38fceece999cb6144658a99c307ccc37f6d8f69129ed0f4545ff321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce0105")
};

//std::vector<unsigned char> pegin_transaction = ParseHex("020000000101f321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce0100004000ffffffff0201e48a1a02a8f799892fda58347c2d794144311d4307dbfd10f77ffe28088c60be01000000003b9ab2e0001976a914809326f7628dc976fbe63806479a1b8dfcc8c4b988ac01e48a1a02a8f799892fda58347c2d794144311d4307dbfd10f77ffe28088c60be010000000000001720000000000000000002483045022100ae17064745d80650a6a5cbcbe15c8c45ba498d1c6f45a7c0f5f32d871b463fc60220799f2836471702c21f7cfe124651727b530ad41f7af4dc213c65f5030a2f6fc4012103a9d3c6c7c161a565a76113632fe13330cf2c0207ba79a76d1154cdc3cb94d940060800ca9a3b0000000020e48a1a02a8f799892fda58347c2d794144311d4307dbfd10f77ffe28088c60be2006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f1600141eef6361cd1507a303834285d1521d6baf1b19aebe0200000001b399292c8100b8a1b66eb23896f799c1712390d560af0f70e81acd2d17a3b06e0000000049483045022100c3c749623486ea57ea93dfaf78d85590d78c7590a25768fe80f0ea4d6047419002202a0a00a90392b86c53c0fdda908c4591ba28040c16c25734c23b7df3c8b70acd01feffffff0228196bee000000001976a914470dd41542ee1a1bd75f1a838878648c8d65622488ac00ca9a3b0000000017a914cb60b1d7f76ba12b45a116c482c165a74c5d7e38876500000097000000205e3913a320cd2e3a2efa141e47419f54cb9e82320cf8dbc812fc19b9a1b2413a57f5e9fb4fa22de191454a241387f5d10cc794ee0fbf72ae2841baf3129a4eab8133025affff7f20000000000200000002f9d0be670007d38fceece999cb6144658a99c307ccc37f6d8f69129ed0f4545ff321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce010500000000");
std::vector<unsigned char> pegin_transaction = ParseHex("020000000101f321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce0100004000ffffffff02e0b29a3b000000001976a914809326f7628dc976fbe63806479a1b8dfcc8c4b988ac2017000000000000000000000002483045022100ae17064745d80650a6a5cbcbe15c8c45ba498d1c6f45a7c0f5f32d871b463fc60220799f2836471702c21f7cfe124651727b530ad41f7af4dc213c65f5030a2f6fc4012103a9d3c6c7c161a565a76113632fe13330cf2c0207ba79a76d1154cdc3cb94d940060800ca9a3b0000000020e48a1a02a8f799892fda58347c2d794144311d4307dbfd10f77ffe28088c60be2006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f1600141eef6361cd1507a303834285d1521d6baf1b19aebe0200000001b399292c8100b8a1b66eb23896f799c1712390d560af0f70e81acd2d17a3b06e0000000049483045022100c3c749623486ea57ea93dfaf78d85590d78c7590a25768fe80f0ea4d6047419002202a0a00a90392b86c53c0fdda908c4591ba28040c16c25734c23b7df3c8b70acd01feffffff0228196bee000000001976a914470dd41542ee1a1bd75f1a838878648c8d65622488ac00ca9a3b0000000017a914cb60b1d7f76ba12b45a116c482c165a74c5d7e38876500000097000000205e3913a320cd2e3a2efa141e47419f54cb9e82320cf8dbc812fc19b9a1b2413a57f5e9fb4fa22de191454a241387f5d10cc794ee0fbf72ae2841baf3129a4eab8133025affff7f20000000000200000002f9d0be670007d38fceece999cb6144658a99c307ccc37f6d8f69129ed0f4545ff321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce0105");

std::vector<unsigned char> pegin_transaction = ParseHex("020000000101f321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce0100004000ffffffff0201ef4699c160d014d5ff79636d8a4cb990b9df4ebab649f144d19f5c495c585e4701000000003b9ab2e0001976a914809326f7628dc976fbe63806479a1b8dfcc8c4b988ac01ef4699c160d014d5ff79636d8a4cb990b9df4ebab649f144d19f5c495c585e47010000000000001720000000000000000002483045022100ae17064745d80650a6a5cbcbe15c8c45ba498d1c6f45a7c0f5f32d871b463fc60220799f2836471702c21f7cfe124651727b530ad41f7af4dc213c65f5030a2f6fc4012103a9d3c6c7c161a565a76113632fe13330cf2c0207ba79a76d1154cdc3cb94d940060800ca9a3b0000000020ef4699c160d014d5ff79636d8a4cb990b9df4ebab649f144d19f5c495c585e472006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f1600141eef6361cd1507a303834285d1521d6baf1b19aebe0200000001b399292c8100b8a1b66eb23896f799c1712390d560af0f70e81acd2d17a3b06e0000000049483045022100c3c749623486ea57ea93dfaf78d85590d78c7590a25768fe80f0ea4d6047419002202a0a00a90392b86c53c0fdda908c4591ba28040c16c25734c23b7df3c8b70acd01feffffff0228196bee000000001976a914470dd41542ee1a1bd75f1a838878648c8d65622488ac00ca9a3b0000000017a914cb60b1d7f76ba12b45a116c482c165a74c5d7e38876500000097000000205e3913a320cd2e3a2efa141e47419f54cb9e82320cf8dbc812fc19b9a1b2413a57f5e9fb4fa22de191454a241387f5d10cc794ee0fbf72ae2841baf3129a4eab8133025affff7f20000000000200000002f9d0be670007d38fceece999cb6144658a99c307ccc37f6d8f69129ed0f4545ff321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce010500000000");

COutPoint prevout(uint256S("ce9b0ee70f82e48f78e2a2e66e61ee4281df74419c23673cc33b639097df21f3"), 1);

Expand All @@ -59,10 +57,6 @@ BOOST_AUTO_TEST_CASE(witness_valid)
// Missing byte on each field to make claim ill-formatted
// This will break deserialization and other data-matching checks
for (unsigned int i = 0; i < witness.stack.size(); i++) {
//TODO(rebase) CA remove this exception
if (i == 1) {
continue;
}
witness.stack[i].pop_back();
BOOST_CHECK(!IsValidPeginWitness(witness, prevout, err, false));
witness.stack = witness_stack;
Expand Down Expand Up @@ -95,32 +89,37 @@ BOOST_AUTO_TEST_CASE(witness_valid)
// Check validation of peg-in transaction's inputs and balance
CDataStream ssTx(pegin_transaction, SER_NETWORK, PROTOCOL_VERSION);
CTransactionRef txRef;
ssTx >> txRef;
try {
ssTx >> txRef;
} catch (...) {
BOOST_CHECK(false);
return;
}
CTransaction tx(*txRef);

// Only one(valid) input witness should exist, and should match
BOOST_CHECK(tx.witness.vtxinwit.size() == 1);
BOOST_CHECK(tx.witness.vtxinwit[0].m_pegin_witness.stack == witness_stack);
BOOST_CHECK(tx.vin[0].m_is_pegin);
// Check that serialization doesn't cause issuance to become non-null
//TODO(rebase) CA
//BOOST_CHECK(tx.vin[0].assetIssuance.IsNull());
BOOST_CHECK(tx.vin[0].assetIssuance.IsNull());
BOOST_CHECK(IsValidPeginWitness(tx.witness.vtxinwit[0].m_pegin_witness, prevout, err, false));

CAmountMap fee_map;

std::set<std::pair<uint256, COutPoint> > setPeginsSpent;
CValidationState state;
CCoinsView coinsDummy;
CCoinsViewCache coins(&coinsDummy);
CAmount txfee;
BOOST_CHECK(Consensus::CheckTxInputs(tx, state, coins, 0, txfee, setPeginsSpent));
BOOST_CHECK(Consensus::CheckTxInputs(tx, state, coins, 0, fee_map, setPeginsSpent, NULL, false, true));
BOOST_CHECK(setPeginsSpent.size() == 1);
setPeginsSpent.clear();

// Strip pegin_witness
CMutableTransaction mtxn(tx);
mtxn.witness.vtxinwit[0].m_pegin_witness.SetNull();
CTransaction tx2(mtxn);
BOOST_CHECK(!Consensus::CheckTxInputs(tx2, state, coins, 0, txfee, setPeginsSpent));
BOOST_CHECK(!Consensus::CheckTxInputs(tx2, state, coins, 0, fee_map, setPeginsSpent, NULL, false, true));
BOOST_CHECK(setPeginsSpent.empty());

// Invalidate peg-in (and spending) authorization by pegin marker.
Expand All @@ -129,7 +128,7 @@ BOOST_AUTO_TEST_CASE(witness_valid)
CMutableTransaction mtxn2(tx);
mtxn2.vin[0].m_is_pegin = false;
CTransaction tx3(mtxn2);
BOOST_CHECK(!Consensus::CheckTxInputs(tx3, state, coins, 0, txfee, setPeginsSpent));
BOOST_CHECK(!Consensus::CheckTxInputs(tx3, state, coins, 0, fee_map, setPeginsSpent, NULL, false, true));
BOOST_CHECK(setPeginsSpent.empty());


Expand Down
Loading

0 comments on commit cd56626

Please sign in to comment.