#include <arith_uint256.h>
#include <consensus/validation.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <pubkey.h>
#include <script/sign.h>
#include <script/signingprovider.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <test/util/transaction_utils.h>
#include <txorphanage.h>
#include <array>
#include <cstdint>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(orphanage_tests, TestingSetup)
class TxOrphanageTest : public TxOrphanage
{
public:
TxOrphanageTest(FastRandomContext& rng) : m_rng{rng} {}
inline size_t CountOrphans() const
{
return m_orphans.size();
}
CTransactionRef RandomOrphan()
{
std::map<Wtxid, OrphanTx>::iterator it;
it = m_orphans.lower_bound(Wtxid::FromUint256(m_rng.rand256()));
if (it == m_orphans.end())
it = m_orphans.begin();
return it->second.tx;
}
FastRandomContext& m_rng;
};
static void MakeNewKeyWithFastRandomContext(CKey& key, FastRandomContext& rand_ctx)
{
std::vector<unsigned char> keydata;
keydata = rand_ctx.randbytes(32);
key.Set(keydata.data(), keydata.data() + keydata.size(), true);
assert(key.IsValid());
}
static CTransactionRef MakeTransactionSpending(const std::vector<COutPoint>& outpoints, FastRandomContext& det_rand)
{
CKey key;
MakeNewKeyWithFastRandomContext(key, det_rand);
CMutableTransaction tx;
if (outpoints.empty()) {
tx.vin.emplace_back(Txid::FromUint256(det_rand.rand256()), 0);
} else {
for (const auto& outpoint : outpoints) {
tx.vin.emplace_back(outpoint);
}
}
tx.vin[0].scriptWitness.stack.push_back({1});
tx.vout.resize(2);
tx.vout[0].nValue = CENT;
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
tx.vout[1].nValue = 3 * CENT;
tx.vout[1].scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(key.GetPubKey()));
return MakeTransactionRef(tx);
}
static CTransactionRef MakeMutation(const CTransactionRef& ptx)
{
CMutableTransaction tx(*ptx);
tx.vin[0].scriptWitness.stack.push_back({5});
auto mutated_tx = MakeTransactionRef(tx);
assert(ptx->GetHash() == mutated_tx->GetHash());
return mutated_tx;
}
static bool EqualTxns(const std::set<CTransactionRef>& set_txns, const std::vector<CTransactionRef>& vec_txns)
{
if (vec_txns.size() != set_txns.size()) return false;
for (const auto& tx : vec_txns) {
if (!set_txns.contains(tx)) return false;
}
return true;
}
BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
{
m_rng.Reseed(uint256{33});
TxOrphanageTest orphanage{m_rng};
CKey key;
MakeNewKeyWithFastRandomContext(key, m_rng);
FillableSigningProvider keystore;
BOOST_CHECK(keystore.AddKey(key));
auto now{GetTime<std::chrono::seconds>()};
SetMockTime(now);
for (int i = 0; i < 50; i++)
{
CMutableTransaction tx;
tx.vin.resize(1);
tx.vin[0].prevout.n = 0;
tx.vin[0].prevout.hash = Txid::FromUint256(m_rng.rand256());
tx.vin[0].scriptSig << OP_1;
tx.vout.resize(1);
tx.vout[0].nValue = i*CENT;
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
orphanage.AddTx(MakeTransactionRef(tx), i);
}
for (int i = 0; i < 50; i++)
{
CTransactionRef txPrev = orphanage.RandomOrphan();
CMutableTransaction tx;
tx.vin.resize(1);
tx.vin[0].prevout.n = 0;
tx.vin[0].prevout.hash = txPrev->GetHash();
tx.vout.resize(1);
tx.vout[0].nValue = i*CENT;
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
SignatureData empty;
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));
orphanage.AddTx(MakeTransactionRef(tx), i);
}
for (int i = 0; i < 10; i++)
{
CTransactionRef txPrev = orphanage.RandomOrphan();
CMutableTransaction tx;
tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT;
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
tx.vin.resize(2777);
for (unsigned int j = 0; j < tx.vin.size(); j++)
{
tx.vin[j].prevout.n = j;
tx.vin[j].prevout.hash = txPrev->GetHash();
}
SignatureData empty;
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));
for (unsigned int j = 1; j < tx.vin.size(); j++)
tx.vin[j].scriptSig = tx.vin[0].scriptSig;
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
}
size_t expected_num_orphans = orphanage.CountOrphans();
orphanage.EraseForPeer(-1);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
for (NodeId i = 0; i < 3; i++)
{
orphanage.EraseForPeer(i);
expected_num_orphans -= 2;
BOOST_CHECK(orphanage.CountOrphans() == expected_num_orphans);
}
FastRandomContext rng{true};
orphanage.LimitOrphans(expected_num_orphans, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
expected_num_orphans -= 1;
orphanage.LimitOrphans(expected_num_orphans, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
assert(expected_num_orphans > 40);
orphanage.LimitOrphans(40, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 40);
orphanage.LimitOrphans(10, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 10);
orphanage.LimitOrphans(0, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
auto timeout_tx = MakeTransactionSpending({}, rng);
orphanage.AddTx(timeout_tx, 0);
orphanage.LimitOrphans(1, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
SetMockTime(now + ORPHAN_TX_EXPIRE_TIME - 1s);
orphanage.LimitOrphans(1, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
SetMockTime(now + ORPHAN_TX_EXPIRE_TIME);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
orphanage.LimitOrphans(1, rng);
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
}
BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
{
FastRandomContext det_rand{true};
TxOrphanage orphanage;
NodeId peer{0};
std::vector<COutPoint> empty_outpoints;
auto parent = MakeTransactionSpending(empty_outpoints, det_rand);
auto child_normal = MakeTransactionSpending({{parent->GetHash(), 0}}, det_rand);
auto child_mutated = MakeMutation(child_normal);
const auto& normal_wtxid = child_normal->GetWitnessHash();
const auto& mutated_wtxid = child_mutated->GetWitnessHash();
BOOST_CHECK(normal_wtxid != mutated_wtxid);
BOOST_CHECK(orphanage.AddTx(child_normal, peer));
BOOST_CHECK_EQUAL(orphanage.EraseTx(mutated_wtxid), 0);
BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
BOOST_CHECK(orphanage.GetTx(normal_wtxid) == child_normal);
BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
BOOST_CHECK(orphanage.GetTx(mutated_wtxid) == nullptr);
BOOST_CHECK(orphanage.AddTx(child_mutated, peer));
BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
BOOST_CHECK(orphanage.HaveTx(mutated_wtxid));
std::set<CTransactionRef> expected_children{child_normal, child_mutated};
BOOST_CHECK(EqualTxns(expected_children, orphanage.GetChildrenFromSamePeer(parent, peer)));
BOOST_CHECK_EQUAL(orphanage.EraseTx(mutated_wtxid), 1);
BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
BOOST_CHECK_EQUAL(orphanage.EraseTx(normal_wtxid), 1);
BOOST_CHECK(!orphanage.HaveTx(normal_wtxid));
BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
}
BOOST_AUTO_TEST_CASE(get_children)
{
FastRandomContext det_rand{true};
std::vector<COutPoint> empty_outpoints;
auto parent1 = MakeTransactionSpending(empty_outpoints, det_rand);
auto parent2 = MakeTransactionSpending(empty_outpoints, det_rand);
while (parent1->GetHash() == parent2->GetHash()) {
parent2 = MakeTransactionSpending(empty_outpoints, det_rand);
}
auto child_p1n0 = MakeTransactionSpending({{parent1->GetHash(), 0}}, det_rand);
auto child_p2n1 = MakeTransactionSpending({{parent2->GetHash(), 1}}, det_rand);
auto child_p1n0_p1n1 = MakeTransactionSpending({{parent1->GetHash(), 0}, {parent1->GetHash(), 1}}, det_rand);
auto child_p1n0_p2n0 = MakeTransactionSpending({{parent1->GetHash(), 0}, {parent2->GetHash(), 0}}, det_rand);
const NodeId node1{1};
const NodeId node2{2};
{
TxOrphanage orphanage;
BOOST_CHECK(orphanage.AddTx(child_p1n0, node1));
BOOST_CHECK(orphanage.AddTx(child_p2n1, node1));
BOOST_CHECK(orphanage.AddTx(child_p1n0_p1n1, node1));
BOOST_CHECK(orphanage.AddTx(child_p1n0_p2n0, node1));
std::set<CTransactionRef> expected_parent1_children{child_p1n0, child_p1n0_p2n0, child_p1n0_p1n1};
std::set<CTransactionRef> expected_parent2_children{child_p2n1, child_p1n0_p2n0};
BOOST_CHECK(EqualTxns(expected_parent1_children, orphanage.GetChildrenFromSamePeer(parent1, node1)));
BOOST_CHECK(EqualTxns(expected_parent2_children, orphanage.GetChildrenFromSamePeer(parent2, node1)));
BOOST_CHECK(orphanage.GetChildrenFromSamePeer(parent1, node2).empty());
BOOST_CHECK(orphanage.GetChildrenFromSamePeer(parent2, node2).empty());
BOOST_CHECK(orphanage.GetChildrenFromSamePeer(child_p1n0_p2n0, node1).empty());
BOOST_CHECK(orphanage.GetChildrenFromSamePeer(child_p1n0_p2n0, node2).empty());
}
{
TxOrphanage orphanage;
BOOST_CHECK(orphanage.AddTx(child_p1n0, node1));
BOOST_CHECK(orphanage.AddTx(child_p2n1, node1));
BOOST_CHECK(orphanage.AddTx(child_p1n0_p1n1, node2));
BOOST_CHECK(orphanage.AddTx(child_p1n0_p2n0, node2));
{
std::set<CTransactionRef> expected_parent1_node1{child_p1n0};
BOOST_CHECK(EqualTxns(expected_parent1_node1, orphanage.GetChildrenFromSamePeer(parent1, node1)));
}
{
std::set<CTransactionRef> expected_parent2_node1{child_p2n1};
BOOST_CHECK(EqualTxns(expected_parent2_node1, orphanage.GetChildrenFromSamePeer(parent2, node1)));
}
{
std::set<CTransactionRef> expected_parent1_node2{child_p1n0_p1n1, child_p1n0_p2n0};
BOOST_CHECK(EqualTxns(expected_parent1_node2, orphanage.GetChildrenFromSamePeer(parent1, node2)));
}
{
std::set<CTransactionRef> expected_parent2_node2{child_p1n0_p2n0};
BOOST_CHECK(EqualTxns(expected_parent2_node2, orphanage.GetChildrenFromSamePeer(parent2, node2)));
}
}
}
BOOST_AUTO_TEST_CASE(too_large_orphan_tx)
{
TxOrphanage orphanage;
CMutableTransaction tx;
tx.vin.resize(1);
BulkTransaction(tx, MAX_STANDARD_TX_WEIGHT + 4);
BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(tx)), MAX_STANDARD_TX_WEIGHT + 4);
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), 0));
tx.vout.clear();
BulkTransaction(tx, MAX_STANDARD_TX_WEIGHT);
BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(tx)), MAX_STANDARD_TX_WEIGHT);
BOOST_CHECK(orphanage.AddTx(MakeTransactionRef(tx), 0));
}
BOOST_AUTO_TEST_CASE(process_block)
{
FastRandomContext det_rand{true};
TxOrphanageTest orphanage{det_rand};
std::vector<COutPoint> outpoints;
const uint32_t num_outpoints{6};
outpoints.reserve(num_outpoints);
for (uint32_t i{0}; i < num_outpoints; ++i) {
outpoints.emplace_back(Txid::FromUint256(det_rand.rand256()), i);
}
CBlock block;
const NodeId node{0};
auto control_tx = MakeTransactionSpending({}, det_rand);
BOOST_CHECK(orphanage.AddTx(control_tx, node));
auto bo_tx_same_txid = MakeTransactionSpending({outpoints.at(0)}, det_rand);
BOOST_CHECK(orphanage.AddTx(bo_tx_same_txid, node));
block.vtx.emplace_back(bo_tx_same_txid);
auto b_tx_same_txid_diff_witness = MakeTransactionSpending({outpoints.at(1)}, det_rand);
block.vtx.emplace_back(b_tx_same_txid_diff_witness);
auto o_tx_same_txid_diff_witness = MakeMutation(b_tx_same_txid_diff_witness);
BOOST_CHECK(orphanage.AddTx(o_tx_same_txid_diff_witness, node));
auto b_tx_conflict = MakeTransactionSpending({outpoints.at(2)}, det_rand);
block.vtx.emplace_back(b_tx_conflict);
auto o_tx_conflict = MakeTransactionSpending({outpoints.at(2)}, det_rand);
BOOST_CHECK(orphanage.AddTx(o_tx_conflict, node));
auto b_tx_conflict_partial = MakeTransactionSpending({outpoints.at(3), outpoints.at(4)}, det_rand);
block.vtx.emplace_back(b_tx_conflict_partial);
auto o_tx_conflict_partial_2 = MakeTransactionSpending({outpoints.at(4), outpoints.at(5)}, det_rand);
BOOST_CHECK(orphanage.AddTx(o_tx_conflict_partial_2, node));
orphanage.EraseForBlock(block);
for (const auto& expected_removed : {bo_tx_same_txid, o_tx_same_txid_diff_witness, o_tx_conflict, o_tx_conflict_partial_2}) {
const auto& expected_removed_wtxid = expected_removed->GetWitnessHash();
BOOST_CHECK(!orphanage.HaveTx(expected_removed_wtxid));
}
BOOST_CHECK_EQUAL(orphanage.Size(), 1);
BOOST_CHECK(orphanage.HaveTx(control_tx->GetWitnessHash()));
}
BOOST_AUTO_TEST_CASE(multiple_announcers)
{
const NodeId node0{0};
const NodeId node1{1};
const NodeId node2{2};
size_t expected_total_count{0};
FastRandomContext det_rand{true};
TxOrphanageTest orphanage{det_rand};
{
auto ptx = MakeTransactionSpending({}, det_rand);
const auto& wtxid = ptx->GetWitnessHash();
BOOST_CHECK(orphanage.AddTx(ptx, node0));
BOOST_CHECK(orphanage.HaveTx(wtxid));
expected_total_count += 1;
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
BOOST_CHECK(!orphanage.AddTx(ptx, node0));
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
auto ptx_mutated{MakeMutation(ptx)};
BOOST_CHECK(orphanage.AddTx(ptx_mutated, node0));
BOOST_CHECK(orphanage.HaveTx(ptx_mutated->GetWitnessHash()));
expected_total_count += 1;
BOOST_CHECK(!orphanage.AddTx(ptx, node0));
BOOST_CHECK(orphanage.AddAnnouncer(ptx->GetWitnessHash(), node2));
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
BOOST_CHECK(orphanage.HaveTxFromPeer(ptx->GetWitnessHash(), node2));
BOOST_CHECK(!orphanage.AddAnnouncer(ptx->GetWitnessHash(), node2));
BOOST_CHECK(!orphanage.AddTx(ptx, node1));
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
orphanage.EraseForPeer(node0);
BOOST_CHECK(orphanage.HaveTx(ptx->GetWitnessHash()));
BOOST_CHECK(!orphanage.HaveTxFromPeer(ptx->GetWitnessHash(), node0));
BOOST_CHECK(!orphanage.HaveTx(ptx_mutated->GetWitnessHash()));
expected_total_count -= 1;
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
orphanage.EraseForPeer(node1);
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
BOOST_CHECK(orphanage.HaveTx(ptx->GetWitnessHash()));
orphanage.EraseForPeer(node2);
expected_total_count -= 1;
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
BOOST_CHECK(!orphanage.HaveTx(ptx->GetWitnessHash()));
}
{
CBlock block;
auto tx_block = MakeTransactionSpending({}, det_rand);
block.vtx.emplace_back(tx_block);
BOOST_CHECK(orphanage.AddTx(tx_block, node0));
BOOST_CHECK(!orphanage.AddTx(tx_block, node1));
expected_total_count += 1;
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
orphanage.EraseForBlock(block);
expected_total_count -= 1;
BOOST_CHECK_EQUAL(orphanage.Size(), expected_total_count);
}
}
BOOST_AUTO_TEST_CASE(peer_worksets)
{
const NodeId node0{0};
const NodeId node1{1};
const NodeId node2{2};
FastRandomContext det_rand{true};
TxOrphanageTest orphanage{det_rand};
{
auto tx_missing_parent = MakeTransactionSpending({}, det_rand);
auto tx_orphan = MakeTransactionSpending({COutPoint{tx_missing_parent->GetHash(), 0}}, det_rand);
const auto& orphan_wtxid = tx_orphan->GetWitnessHash();
BOOST_CHECK(orphanage.AddTx(tx_orphan, node0));
BOOST_CHECK(!orphanage.AddTx(tx_orphan, node1));
BOOST_CHECK(orphanage.AddAnnouncer(orphan_wtxid, node2));
for (NodeId node = node0; node <= node2; ++node) {
BOOST_CHECK(orphanage.HaveTxFromPeer(orphan_wtxid, node));
}
orphanage.AddChildrenToWorkSet(*tx_missing_parent, det_rand);
int node0_reconsider = orphanage.HaveTxToReconsider(node0);
int node1_reconsider = orphanage.HaveTxToReconsider(node1);
int node2_reconsider = orphanage.HaveTxToReconsider(node2);
BOOST_CHECK_EQUAL(node0_reconsider + node1_reconsider + node2_reconsider, 1);
NodeId assigned_peer;
if (node0_reconsider) {
assigned_peer = node0;
} else if (node1_reconsider) {
assigned_peer = node1;
} else {
BOOST_CHECK(node2_reconsider);
assigned_peer = node2;
}
orphanage.EraseForPeer(assigned_peer);
BOOST_CHECK_EQUAL(orphanage.GetTxToReconsider(node0), nullptr);
BOOST_CHECK_EQUAL(orphanage.EraseTx(orphan_wtxid), 1);
BOOST_CHECK_EQUAL(orphanage.Size(), 0);
for (NodeId node = node0; node <= node2; ++node) {
BOOST_CHECK_EQUAL(orphanage.GetTxToReconsider(node), nullptr);
BOOST_CHECK(!orphanage.HaveTxFromPeer(orphan_wtxid, node));
}
}
}
BOOST_AUTO_TEST_SUITE_END()