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 d5f1b17 commit 93ff8d9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
24 changes: 12 additions & 12 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ const inputs_cptr block::inputs_ptr() const NOEXCEPT
const auto inputs = std::make_shared<input_cptrs>();
const auto append_ins = [&inputs](const auto& tx) NOEXCEPT
{
const auto& tx_ins = *tx->inputs_ptr();
inputs->insert(inputs->end(), tx_ins.begin(), tx_ins.end());
const auto& tx_ins = tx->inputs_ptr();
inputs->insert(inputs->end(), tx_ins->begin(), tx_ins->end());
};

std::for_each(txs_->begin(), txs_->end(), append_ins);
Expand Down Expand Up @@ -377,8 +377,8 @@ bool block::is_forward_reference() const NOEXCEPT

const auto spend = [&spent, &hashes](const auto& tx) NOEXCEPT
{
const auto& ins = *tx->inputs_ptr();
const auto forward = std::any_of(ins.begin(), ins.end(), spent);
const auto& ins = tx->inputs_ptr();
const auto forward = std::any_of(ins->begin(), ins->end(), spent);
hashes.emplace(tx->get_hash(false));
return forward;
};
Expand Down Expand Up @@ -410,8 +410,8 @@ bool block::is_internal_double_spend() const NOEXCEPT

const auto double_spent = [&spent](const auto& tx) NOEXCEPT
{
const auto& ins = *tx->inputs_ptr();
return std::any_of(ins.begin(), ins.end(), spent);
const auto& ins = tx->inputs_ptr();
return std::any_of(ins->begin(), ins->end(), spent);
};

return std::any_of(tx1, txs_->end(), double_spent);
Expand Down Expand Up @@ -472,10 +472,10 @@ bool block::is_hash_limit_exceeded() const NOEXCEPT
{
// Insert the transaction hash.
hashes.emplace((*tx)->get_hash(false));
const auto& inputs = *(*tx)->inputs_ptr();
const auto& inputs = (*tx)->inputs_ptr();

// Insert all input point hashes.
for (const auto& input: inputs)
for (const auto& input: *inputs)
hashes.emplace(input->point().hash());
}

Expand Down Expand Up @@ -585,16 +585,16 @@ bool block::is_invalid_witness_commitment() const NOEXCEPT
if (txs_->empty())
return false;

const auto& coinbase = *txs_->front();
if (coinbase.inputs_ptr()->empty())
const auto& coinbase = txs_->front();
if (coinbase->inputs_ptr()->empty())
return false;

// If there is a valid commitment, return false (valid).
// Coinbase input witness must be 32 byte witness reserved value (bip141).
// Last output of commitment pattern holds the committed value (bip141).
hash_digest reserved{}, committed{};
if (coinbase.inputs_ptr()->front()->reserved_hash(reserved))
for (const auto& output: views_reverse(*coinbase.outputs_ptr()))
if (coinbase->inputs_ptr()->front()->reserved_hash(reserved))
for (const auto& output: views_reverse(*coinbase->outputs_ptr()))
if (output->committed_hash(committed))
if (committed == sha256::double_hash(
generate_merkle_root(true), reserved))
Expand Down
29 changes: 11 additions & 18 deletions src/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,7 @@ hash_digest transaction::outputs_hash() const NOEXCEPT
stream::out::fast stream{ digest };
hash::sha256x2::fast sink{ stream };

const auto& outs = *outputs_;
for (const auto& output: outs)
for (const auto& output: *outputs_)
output->to_data(sink);

sink.flush();
Expand All @@ -540,8 +539,7 @@ hash_digest transaction::points_hash() const NOEXCEPT
stream::out::fast stream{ digest };
hash::sha256x2::fast sink{ stream };

const auto& ins = *inputs_;
for (const auto& input: ins)
for (const auto& input: *inputs_)
input->point().to_data(sink);

sink.flush();
Expand All @@ -560,8 +558,7 @@ hash_digest transaction::sequences_hash() const NOEXCEPT
stream::out::fast stream{ digest };
hash::sha256x2::fast sink{ stream };

const auto& ins = *inputs_;
for (const auto& input: ins)
for (const auto& input: *inputs_)
sink.write_4_bytes_little_endian(input->sequence());

sink.flush();
Expand Down Expand Up @@ -616,7 +613,6 @@ void transaction::signature_hash_single(writer& sink,
const auto write_inputs = [this, &input, &sub, sighash_flags](
writer& sink) NOEXCEPT
{
const auto& self = **input;
const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
input_cptrs::const_iterator in;

Expand All @@ -629,9 +625,9 @@ void transaction::signature_hash_single(writer& sink,
sink.write_bytes(zero_sequence());
}

self.point().to_data(sink);
(*input)->point().to_data(sink);
sub.to_data(sink, prefixed);
sink.write_4_bytes_little_endian(self.sequence());
sink.write_4_bytes_little_endian((*input)->sequence());

for (++in; !anyone && in != inputs_->end(); ++in)
{
Expand Down Expand Up @@ -668,7 +664,6 @@ void transaction::signature_hash_none(writer& sink,
const auto write_inputs = [this, &input, &sub, sighash_flags](
writer& sink) NOEXCEPT
{
const auto& self = **input;
const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
input_cptrs::const_iterator in;

Expand All @@ -681,9 +676,9 @@ void transaction::signature_hash_none(writer& sink,
sink.write_bytes(zero_sequence());
}

self.point().to_data(sink);
(*input)->point().to_data(sink);
sub.to_data(sink, prefixed);
sink.write_4_bytes_little_endian(self.sequence());
sink.write_4_bytes_little_endian((*input)->sequence());

for (++in; !anyone && in != inputs_->end(); ++in)
{
Expand All @@ -707,7 +702,6 @@ void transaction::signature_hash_all(writer& sink,
const auto write_inputs = [this, &input, &sub, flags](
writer& sink) NOEXCEPT
{
const auto& self = **input;
const auto anyone = to_bool(flags & coverage::anyone_can_pay);
input_cptrs::const_iterator in;

Expand All @@ -720,9 +714,9 @@ void transaction::signature_hash_all(writer& sink,
sink.write_4_bytes_little_endian((*in)->sequence());
}

self.point().to_data(sink);
(*input)->point().to_data(sink);
sub.to_data(sink, prefixed);
sink.write_4_bytes_little_endian(self.sequence());
sink.write_4_bytes_little_endian((*input)->sequence());

for (++in; !anyone && in != inputs_->end(); ++in)
{
Expand Down Expand Up @@ -849,7 +843,6 @@ hash_digest transaction::version_0_signature_hash(const input_iterator& input,
const auto flag = mask_sighash(sighash_flags);
const auto all = (flag == coverage::hash_all);
const auto single = (flag == coverage::hash_single);
const auto& self = **input;

// Create hash writer.
BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
Expand All @@ -871,10 +864,10 @@ hash_digest transaction::version_0_signature_hash(const input_iterator& input,
// sequences
sink.write_bytes(!anyone && all ? sequences_hash() : null_hash);

self.point().to_data(sink);
(*input)->point().to_data(sink);
sub.to_data(sink, prefixed);
sink.write_little_endian(value);
sink.write_little_endian(self.sequence());
sink.write_little_endian((*input)->sequence());

// outputs
if (single)
Expand Down
8 changes: 4 additions & 4 deletions test/wallet/neutrino_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ bool add_metadata(const prevout_data::list& metadata, const chain::block& block)
for (auto& meta: metadata)
{
result = false;
const auto& txs = *block.transactions_ptr();
for (const auto& tx: txs)
const auto& txs = block.transactions_ptr();
for (const auto& tx: *txs)
{
if (tx->hash(false) == meta.block_tx)
{
auto& inputs = *tx->inputs_ptr();
auto& output = inputs[meta.input_index]->prevout;
auto& inputs = tx->inputs_ptr();
auto& output = (*inputs)[meta.input_index]->prevout;
output = to_shared<chain::output>(meta.output_value,
chain::script{ meta.script, false });
result = true;
Expand Down

0 comments on commit 93ff8d9

Please sign in to comment.