Skip to content

Commit

Permalink
Avoid assigning shared ptr dereference to reference.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Oct 31, 2024
1 parent cf58cbb commit 3c93e8c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
62 changes: 31 additions & 31 deletions include/bitcoin/database/impl/query/archive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ bool CLASS::populate(const transaction& tx) const NOEXCEPT
BC_ASSERT(!tx.is_coinbase());

auto result = true;
const auto& ins = *tx.inputs_ptr();
std::for_each(ins.begin(), ins.end(), [&](const auto& in) NOEXCEPT
const auto& ins = tx.inputs_ptr();
std::for_each(ins->begin(), ins->end(), [&](const auto& in) NOEXCEPT
{
result &= populate(*in);
});
Expand All @@ -166,16 +166,16 @@ bool CLASS::populate(const transaction& tx) const NOEXCEPT
TEMPLATE
bool CLASS::populate(const block& block) const NOEXCEPT
{
const auto& txs = *block.transactions_ptr();
if (txs.empty())
const auto& txs = block.transactions_ptr();
if (txs->empty())
return false;

auto result = true;
std::for_each(std::next(txs.begin()), txs.end(),
std::for_each(std::next(txs->begin()), txs->end(),
[&](const auto& tx) NOEXCEPT
{
const auto& ins = *tx->inputs_ptr();
std::for_each(ins.begin(), ins.end(), [&](const auto& in) NOEXCEPT
const auto& ins = tx->inputs_ptr();
std::for_each(ins->begin(), ins->end(), [&](const auto& in) NOEXCEPT
{
result &= populate(*in);
});
Expand Down Expand Up @@ -225,8 +225,8 @@ bool CLASS::populate_with_metadata(const transaction& tx,
const tx_link& link) const NOEXCEPT
{
auto result = true;
const auto& ins = *tx.inputs_ptr();
std::for_each(ins.begin(), ins.end(), [&](const auto& in) NOEXCEPT
const auto& ins = tx.inputs_ptr();
std::for_each(ins->begin(), ins->end(), [&](const auto& in) NOEXCEPT
{
result &= populate_with_metadata(*in, link);
});
Expand All @@ -240,37 +240,37 @@ bool CLASS::populate_with_metadata(const transaction& tx) const NOEXCEPT
BC_ASSERT(is_coinbase(tx));

// A coinbase tx is allowed only one input.
const auto& input = *tx.inputs_ptr()->front();
const auto& input = tx.inputs_ptr()->front();

// Find any confirmed unspent duplicates of tx (unspent_coinbase_collision).
const auto ec = unspent_duplicates(tx);
if (ec == error::integrity)
return false;

// The prevout of a coinbase is null (not an output of a coinbase tx).
input.metadata.coinbase = false;
input.metadata.spent = (ec != error::unspent_coinbase_collision);
input.metadata.median_time_past = max_uint32;
input.metadata.height = zero;
input->metadata.coinbase = false;
input->metadata.spent = (ec != error::unspent_coinbase_collision);
input->metadata.median_time_past = max_uint32;
input->metadata.height = zero;
return true;
}

TEMPLATE
bool CLASS::populate_with_metadata(const block& block) const NOEXCEPT
{
const auto& txs = *block.transactions_ptr();
if (txs.empty())
const auto& txs = block.transactions_ptr();
if (txs->empty())
return false;

auto result = true;
const auto coinbase = populate_with_metadata(txs.front());
std::for_each(std::next(txs.begin()), txs.end(),
const auto coinbase = populate_with_metadata(txs->front());
std::for_each(std::next(txs->begin()), txs->end(),
[&](const auto& tx) NOEXCEPT
{
const auto link = to_tx(tx.get_hash());
result &= !link.is_terminal();
const auto& ins = *tx->inputs_ptr();
std::for_each(ins.begin(), ins.end(), [&](const auto& in) NOEXCEPT
const auto& ins = tx->inputs_ptr();
std::for_each(ins->begin(), ins->end(), [&](const auto& in) NOEXCEPT
{
result &= populate_with_metadata(*in, link);
});
Expand Down Expand Up @@ -743,15 +743,15 @@ code CLASS::set_code(tx_link& out_fk, const transaction& tx) NOEXCEPT
const auto& key = tx.get_hash(false);

// Declare puts record.
const auto& ins = *tx.inputs_ptr();
const auto& outs = *tx.outputs_ptr();
const auto& ins = tx.inputs_ptr();
const auto& outs = tx.outputs_ptr();
table::puts::slab puts{};
puts.spend_fks.reserve(ins.size());
puts.out_fks.reserve(outs.size());
puts.spend_fks.reserve(ins->size());
puts.out_fks.reserve(outs->size());

// Declare spends buffer.
std_vector<foreign_point> spends{};
spends.reserve(ins.size());
spends.reserve(ins->size());

// TODO: eliminate shared memory pointer reallocations.
// ========================================================================
Expand All @@ -765,13 +765,13 @@ code CLASS::set_code(tx_link& out_fk, const transaction& tx) NOEXCEPT

// Allocate spend records.
// Clean single allocation failure (e.g. disk full).
const auto count = possible_narrow_cast<spend_link::integer>(ins.size());
const auto count = possible_narrow_cast<spend_link::integer>(ins->size());
auto spend_fk = store_.spend.allocate(count);
if (spend_fk.is_terminal())
return error::tx_spend_allocate;

// Commit input records (spend records not indexed).
for (const auto& in: ins)
for (const auto& in: *ins)
{
// Commit input record.
// Safe allocation failure, blob linked by unindexed spend.
Expand Down Expand Up @@ -833,7 +833,7 @@ code CLASS::set_code(tx_link& out_fk, const transaction& tx) NOEXCEPT
}

// Commit output records.
for (const auto& out: outs)
for (const auto& out: *outs)
{
// Safe allocation failure, blob unlinked.
output_link output_fk{};
Expand Down Expand Up @@ -864,8 +864,8 @@ code CLASS::set_code(tx_link& out_fk, const transaction& tx) NOEXCEPT
{
{},
tx,
system::possible_narrow_cast<ix::integer>(ins.size()),
system::possible_narrow_cast<ix::integer>(outs.size()),
system::possible_narrow_cast<ix::integer>(ins->size()),
system::possible_narrow_cast<ix::integer>(outs->size()),
puts_fk
}))
{
Expand All @@ -888,7 +888,7 @@ code CLASS::set_code(tx_link& out_fk, const transaction& tx) NOEXCEPT
if (address_enabled())
{
auto output_fk = puts.out_fks.begin();
for (const auto& out: outs)
for (const auto& out: *outs)
{
// Safe allocation failure, unindexed tx outputs linked by address,
// others unlinked. A replay of committed addresses without indexed
Expand Down
42 changes: 21 additions & 21 deletions test/query/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,9 +1273,9 @@ BOOST_AUTO_TEST_CASE(query_archive__get_input__genesis__expected)
BOOST_REQUIRE(query.set(test::genesis, test::context, false, false));

const auto tx = test::genesis.transactions_ptr()->front();
const auto& input = *tx->inputs_ptr()->front();
BOOST_REQUIRE(input == *query.get_input(query.to_tx(tx->hash(false)), 0u));
BOOST_REQUIRE(input == *query.get_input(0));
const auto input = tx->inputs_ptr()->front();
BOOST_REQUIRE(*input == *query.get_input(query.to_tx(tx->hash(false)), 0u));
BOOST_REQUIRE(*input == *query.get_input(0));
}

BOOST_AUTO_TEST_CASE(query_archive__get_inputs__tx_not_found__nullptr)
Expand Down Expand Up @@ -1328,10 +1328,10 @@ BOOST_AUTO_TEST_CASE(query_archive__get_output__genesis__expected)
BOOST_REQUIRE(query.set(test::genesis, test::context, false, false));

const auto tx = test::genesis.transactions_ptr()->front();
const auto& output1 = *tx->outputs_ptr()->front();
BOOST_REQUIRE(output1 == *query.get_output(query.to_tx(tx->hash(false)), 0u));
BOOST_REQUIRE(output1 == *query.get_output({ tx->hash(false), 0u }));
BOOST_REQUIRE(output1 == *query.get_output(0));
const auto output1 = tx->outputs_ptr()->front();
BOOST_REQUIRE(*output1 == *query.get_output(query.to_tx(tx->hash(false)), 0u));
BOOST_REQUIRE(*output1 == *query.get_output({ tx->hash(false), 0u }));
BOOST_REQUIRE(*output1 == *query.get_output(0));
}

BOOST_AUTO_TEST_CASE(query_archive__get_outputs__tx_not_found__nullptr)
Expand Down Expand Up @@ -1473,11 +1473,11 @@ BOOST_AUTO_TEST_CASE(query_archive__get_spenders__unspent_or_not_found__expected
//// BOOST_REQUIRE_EQUAL(query.get_spenders(1, 2)->size(), 0u);
////
//// // Match the two spenders.
//// const auto& block_inputs = *test::block2a.transactions_ptr()->front()->inputs_ptr();
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 0))->front() == *block_inputs.front());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 1))->front() == *block_inputs.back());
//// BOOST_REQUIRE(*query.get_spenders(1, 0)->front() == *block_inputs.front());
//// BOOST_REQUIRE(*query.get_spenders(1, 1)->front() == *block_inputs.back());
//// const auto block_inputs = test::block2a.transactions_ptr()->front()->inputs_ptr();
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 0))->front() == *(*block_inputs).front());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 1))->front() == *(*block_inputs).back());
//// BOOST_REQUIRE(*query.get_spenders(1, 0)->front() == *(*block_inputs).front());
//// BOOST_REQUIRE(*query.get_spenders(1, 1)->front() == *(*block_inputs).back());
////
//// // Each of the two outputs of block1a spent twice (two unconfirmed double spends).
//// BOOST_REQUIRE(query.set(test::tx4));
Expand All @@ -1489,15 +1489,15 @@ BOOST_AUTO_TEST_CASE(query_archive__get_spenders__unspent_or_not_found__expected
//// BOOST_REQUIRE_EQUAL(query.get_spenders(1, 2)->size(), 0u);
////
//// // Match the four spenders.
//// const auto& tx_inputs = *test::tx4.inputs_ptr();
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 0))->front() == *tx_inputs.front());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 1))->front() == *tx_inputs.back());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 0))->back() == *block_inputs.front());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 1))->back() == *block_inputs.back());
//// BOOST_REQUIRE(*query.get_spenders(1, 0)->front() == *tx_inputs.front());
//// BOOST_REQUIRE(*query.get_spenders(1, 1)->front() == *tx_inputs.back());
//// BOOST_REQUIRE(*query.get_spenders(1, 0)->back() == *block_inputs.front());
//// BOOST_REQUIRE(*query.get_spenders(1, 1)->back() == *block_inputs.back());
//// const auto tx_inputs = test::tx4.inputs_ptr();
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 0))->front() == *(*tx_inputs).front());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 1))->front() == *(*tx_inputs).back());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 0))->back() == *(*block_inputs).front());
//// BOOST_REQUIRE(*query.get_spenders(query.to_output(1, 1))->back() == *(*block_inputs).back());
//// BOOST_REQUIRE(*query.get_spenders(1, 0)->front() == *(*tx_inputs).front());
//// BOOST_REQUIRE(*query.get_spenders(1, 1)->front() == *(*tx_inputs).back());
//// BOOST_REQUIRE(*query.get_spenders(1, 0)->back() == *(*block_inputs).front());
//// BOOST_REQUIRE(*query.get_spenders(1, 1)->back() == *(*block_inputs).back());
////}

BOOST_AUTO_TEST_CASE(query_archive__get_value__genesis__expected)
Expand Down
4 changes: 2 additions & 2 deletions test/tables/caches/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
////using namespace system;
////const chain::transaction empty{};
////const auto genesis = system::settings{ system::chain::selection::mainnet }.genesis_block;
////const auto& genesis_tx = *genesis.transactions_ptr()->front();
////const auto genesis_tx = genesis.transactions_ptr()->front();
////const table::buffer::key key1{ 0x01, 0x02, 0x03, 0x04 };
////const table::buffer::key key2{ 0xa1, 0xa2, 0xa3, 0xa4 };
////const table::buffer::slab slab1{ {}, empty };
////const table::buffer::slab slab2{ {}, genesis_tx };
////const table::buffer::slab slab2{ {}, *genesis_tx };
////const data_chunk expected_head = base16_chunk
////(
//// "0000000000"
Expand Down

0 comments on commit 3c93e8c

Please sign in to comment.