Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid assigning shared ptr dereference to reference. #1544

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ libbitcoin.pc

bin
obj
build
.*.swp
*~
/*.kdev4
Expand All @@ -24,4 +25,4 @@ obj
/config.*
/configure
/libtool
.dirstamp
.dirstamp
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/obj/nix-gnu-debug-static-without_icu/libbitcoin-system-test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/obj/nix-gnu-debug-static-without_icu",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
35 changes: 35 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "cmake",
"label": "CMake: build",
"command": "build",
"targets": [
"bitcoin-system",
"libbitcoin-system-examples",
"libbitcoin-system-test"
],
"preset": "${command:cmake.activeBuildPresetName}",
"group": "build",
"problemMatcher": [],
"detail": "CMake template build task"
},
{
"type": "cmake",
"label": "CMake: clean",
"command": "clean",
"preset": "${command:cmake.activeBuildPresetName}",
"problemMatcher": [],
"detail": "CMake template clean task"
},
{
"type": "cmake",
"label": "CMake: install",
"command": "install",
"preset": "${command:cmake.activeBuildPresetName}",
"problemMatcher": [],
"detail": "CMake template install task"
}
]
}
6 changes: 3 additions & 3 deletions builds/cmake/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "nix-base",
"description": "Factored base settings for non-windows *nix based platforms.",
"hidden": true,
"installDir": "${sourceParentDir}/../../prefix/${presetName}",
"installDir": "${sourceParentDir}/../../../prefix/${presetName}",
"binaryDir": "${sourceParentDir}/../obj/${presetName}",
"condition": {
"type": "inList",
Expand All @@ -18,11 +18,11 @@
"cacheVariables": {
"CMAKE_PREFIX_PATH": {
"type": "PATH",
"value": "${sourceParentDir}/../../prefix/${presetName}"
"value": "${sourceParentDir}/../../../prefix/${presetName}"
},
"CMAKE_LIBRARY_PATH": {
"type": "PATH",
"value": "${sourceParentDir}/../../prefix/${presetName}/lib:$env{CMAKE_LIBRARY_PATH}"
"value": "${sourceParentDir}/../../../prefix/${presetName}/lib:$env{CMAKE_LIBRARY_PATH}"
}
}
},
Expand Down
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
Loading