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

snapshots: refactorings #1911

Merged
merged 8 commits into from
Mar 18, 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
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Checks: >
-bugprone-unchecked-optional-access,
-bugprone-unused-raii,
cert-*,
-cert-dcl21-cpp,
-cert-err58-cpp,
-clang-analyzer-*,
clang-diagnostic-*,
Expand Down
37 changes: 19 additions & 18 deletions cmd/capi/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ std::vector<SilkwormChainSnapshot> collect_all_snapshots(const SnapshotRepositor
SilkwormHeadersSnapshot raw_headers_snapshot{
.segment{
.file_path = make_path(segment_file),
.memory_address = header_snapshot->memory_file_address(),
.memory_length = header_snapshot->memory_file_size()},
.memory_address = header_snapshot->memory_file_region().data(),
.memory_length = header_snapshot->memory_file_region().size()},
.header_hash_index{
.file_path = make_path(segment_file.index_file()),
.memory_address = idx_header_hash->memory_file_address(),
.memory_length = idx_header_hash->memory_file_size()}};
.memory_address = idx_header_hash->memory_file_region().data(),
.memory_length = idx_header_hash->memory_file_region().size()}};
headers_snapshot_sequence.push_back(raw_headers_snapshot);
} break;
case SnapshotType::bodies: {
Expand All @@ -172,12 +172,12 @@ std::vector<SilkwormChainSnapshot> collect_all_snapshots(const SnapshotRepositor
SilkwormBodiesSnapshot raw_bodies_snapshot{
.segment{
.file_path = make_path(segment_file),
.memory_address = body_snapshot->memory_file_address(),
.memory_length = body_snapshot->memory_file_size()},
.memory_address = body_snapshot->memory_file_region().data(),
.memory_length = body_snapshot->memory_file_region().size()},
.block_num_index{
.file_path = make_path(segment_file.index_file()),
.memory_address = idx_body_number->memory_file_address(),
.memory_length = idx_body_number->memory_file_size()}};
.memory_address = idx_body_number->memory_file_region().data(),
.memory_length = idx_body_number->memory_file_region().size()}};
bodies_snapshot_sequence.push_back(raw_bodies_snapshot);
} break;
case SnapshotType::transactions: {
Expand All @@ -187,16 +187,16 @@ std::vector<SilkwormChainSnapshot> collect_all_snapshots(const SnapshotRepositor
SilkwormTransactionsSnapshot raw_transactions_snapshot{
.segment{
.file_path = make_path(segment_file),
.memory_address = tx_snapshot->memory_file_address(),
.memory_length = tx_snapshot->memory_file_size()},
.memory_address = tx_snapshot->memory_file_region().data(),
.memory_length = tx_snapshot->memory_file_region().size()},
.tx_hash_index{
.file_path = make_path(segment_file.index_file()),
.memory_address = idx_txn_hash->memory_file_address(),
.memory_length = idx_txn_hash->memory_file_size()},
.memory_address = idx_txn_hash->memory_file_region().data(),
.memory_length = idx_txn_hash->memory_file_region().size()},
.tx_hash_2_block_index{
.file_path = make_path(segment_file.index_file_for_type(SnapshotType::transactions_to_block)),
.memory_address = idx_txn_hash_2_block->memory_file_address(),
.memory_length = idx_txn_hash_2_block->memory_file_size()}};
.memory_address = idx_txn_hash_2_block->memory_file_region().data(),
.memory_length = idx_txn_hash_2_block->memory_file_region().size()}};
transactions_snapshot_sequence.push_back(raw_transactions_snapshot);
} break;
default:
Expand Down Expand Up @@ -351,10 +351,11 @@ int build_indexes(SilkwormHandle handle, const BuildIndexesSettings& settings, c
throw std::runtime_error("Snapshot not found in the repository:" + snapshot_name);
}

auto mmf = new SilkwormMemoryMappedFile();
mmf->file_path = make_path(snapshot->path());
mmf->memory_address = snapshot->memory_file_address();
mmf->memory_length = snapshot->memory_file_size();
auto mmf = new SilkwormMemoryMappedFile{
.file_path = make_path(snapshot->path()),
.memory_address = snapshot->memory_file_region().data(),
.memory_length = snapshot->memory_file_region().size(),
};
snapshots.push_back(mmf);
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/dev/backend_kv_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <silkworm/core/types/address.hpp>
#include <silkworm/core/types/evmc_bytes32.hpp>
#include <silkworm/db/access_layer.hpp>
#include <silkworm/db/head_info.hpp>
#include <silkworm/db/chain_head.hpp>
#include <silkworm/db/mdbx/mdbx.hpp>
#include <silkworm/infra/common/directories.hpp>
#include <silkworm/infra/common/log.hpp>
Expand Down Expand Up @@ -116,10 +116,10 @@ std::shared_ptr<silkworm::sentry::api::SentryClient> make_sentry_client(
db::ROAccess db_access) {
std::shared_ptr<silkworm::sentry::api::SentryClient> sentry_client;

auto head_info_provider = [db_access = std::move(db_access)]() {
return db::read_head_info(db_access);
auto chain_head_provider = [db_access = std::move(db_access)]() {
return db::read_chain_head(db_access);
};
silkworm::sentry::eth::StatusDataProvider eth_status_data_provider{std::move(head_info_provider), node_settings.chain_config.value()};
silkworm::sentry::eth::StatusDataProvider eth_status_data_provider{std::move(chain_head_provider), node_settings.chain_config.value()};

if (node_settings.remote_sentry_addresses.empty()) {
assert(false);
Expand Down
8 changes: 4 additions & 4 deletions cmd/silkworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <boost/asio/use_future.hpp>

#include <silkworm/buildinfo.h>
#include <silkworm/db/head_info.hpp>
#include <silkworm/db/chain_head.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/concurrency/awaitable_wait_for_all.hpp>
#include <silkworm/infra/concurrency/awaitable_wait_for_one.hpp>
Expand Down Expand Up @@ -278,10 +278,10 @@ int main(int argc, char* argv[]) {
settings.sentry_settings.data_dir_path = node_settings.data_directory->path();
settings.sentry_settings.network_id = node_settings.network_id;

auto head_info_provider = [db_access = db::ROAccess{chaindata_db}] {
return db::read_head_info(db_access);
auto chain_head_provider = [db_access = db::ROAccess{chaindata_db}] {
return db::read_chain_head(db_access);
};
sentry::eth::StatusDataProvider eth_status_data_provider{std::move(head_info_provider), node_settings.chain_config.value()};
sentry::eth::StatusDataProvider eth_status_data_provider{std::move(chain_head_provider), node_settings.chain_config.value()};

auto [sentry_client, sentry_server] = sentry::SentryClientFactory::make_sentry(
std::move(settings.sentry_settings),
Expand Down
28 changes: 14 additions & 14 deletions silkworm/capi/silkworm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,42 +426,42 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_snapshot", "[silkworm][capi]") {
SilkwormHeadersSnapshot valid_shs{
.segment = SilkwormMemoryMappedFile{
.file_path = header_snapshot_path_string.c_str(),
.memory_address = header_snapshot.memory_file_address(),
.memory_length = header_snapshot.memory_file_size(),
.memory_address = header_snapshot.memory_file_region().data(),
.memory_length = header_snapshot.memory_file_region().size(),
},
.header_hash_index = SilkwormMemoryMappedFile{
.file_path = header_index_path_string.c_str(),
.memory_address = header_snapshot.idx_header_hash()->memory_file_address(),
.memory_length = header_snapshot.idx_header_hash()->memory_file_size(),
.memory_address = header_snapshot.idx_header_hash()->memory_file_region().data(),
.memory_length = header_snapshot.idx_header_hash()->memory_file_region().size(),
},
};
SilkwormBodiesSnapshot valid_sbs{
.segment = SilkwormMemoryMappedFile{
.file_path = body_snapshot_path_string.c_str(),
.memory_address = body_snapshot.memory_file_address(),
.memory_length = body_snapshot.memory_file_size(),
.memory_address = body_snapshot.memory_file_region().data(),
.memory_length = body_snapshot.memory_file_region().size(),
},
.block_num_index = SilkwormMemoryMappedFile{
.file_path = body_index_path_string.c_str(),
.memory_address = body_snapshot.idx_body_number()->memory_file_address(),
.memory_length = body_snapshot.idx_body_number()->memory_file_size(),
.memory_address = body_snapshot.idx_body_number()->memory_file_region().data(),
.memory_length = body_snapshot.idx_body_number()->memory_file_region().size(),
},
};
SilkwormTransactionsSnapshot valid_sts{
.segment = SilkwormMemoryMappedFile{
.file_path = tx_snapshot_path_string.c_str(),
.memory_address = tx_snapshot.memory_file_address(),
.memory_length = tx_snapshot.memory_file_size(),
.memory_address = tx_snapshot.memory_file_region().data(),
.memory_length = tx_snapshot.memory_file_region().size(),
},
.tx_hash_index = SilkwormMemoryMappedFile{
.file_path = tx_hash_index_path_string.c_str(),
.memory_address = tx_snapshot.idx_txn_hash()->memory_file_address(),
.memory_length = tx_snapshot.idx_txn_hash()->memory_file_size(),
.memory_address = tx_snapshot.idx_txn_hash()->memory_file_region().data(),
.memory_length = tx_snapshot.idx_txn_hash()->memory_file_region().size(),
},
.tx_hash_2_block_index = SilkwormMemoryMappedFile{
.file_path = tx_hash2block_index_path_string.c_str(),
.memory_address = tx_snapshot.idx_txn_hash_2_block()->memory_file_address(),
.memory_length = tx_snapshot.idx_txn_hash_2_block()->memory_file_size(),
.memory_address = tx_snapshot.idx_txn_hash_2_block()->memory_file_region().data(),
.memory_length = tx_snapshot.idx_txn_hash_2_block()->memory_file_region().size(),
},
};

Expand Down
2 changes: 0 additions & 2 deletions silkworm/core/types/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

namespace silkworm {

BlockNum height(const BlockId& b) { return b.number; }

evmc::bytes32 BlockHeader::hash(bool for_sealing, bool exclude_extra_data_sig) const {
Bytes rlp;
rlp::encode(rlp, *this, for_sealing, exclude_extra_data_sig);
Expand Down
34 changes: 0 additions & 34 deletions silkworm/core/types/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ namespace silkworm {

using TotalDifficulty = intx::uint256;

struct BlockId { // TODO(canepat) rename BlockNumberAndHash
BlockNum number{};
Hash hash;
};

BlockNum height(const BlockId& b);

struct ChainHead {
BlockNum height{};
Hash hash;
TotalDifficulty total_difficulty{};
};

struct BlockHeader {
using NonceType = std::array<uint8_t, 8>;

Expand Down Expand Up @@ -124,25 +111,4 @@ namespace rlp {
DecodingResult decode(ByteView& from, Block& to, Leftover mode = Leftover::kProhibit) noexcept;
} // namespace rlp

// Comparison operator ==
inline bool operator==(const BlockId& a, const BlockId& b) {
return a.number == b.number && a.hash == b.hash;
}

inline bool operator==(const ChainHead& a, const BlockId& b) {
return a.height == b.number && a.hash == b.hash;
}

inline bool operator==(const BlockId& a, const ChainHead& b) {
return a.number == b.height && a.hash == b.hash;
}

inline bool operator==(const ChainHead& a, const ChainHead& b) {
return a.height == b.height && a.hash == b.hash && a.total_difficulty == b.total_difficulty;
}

inline BlockId to_BlockId(const ChainHead& head) {
return {.number = head.height, .hash = head.hash};
}

} // namespace silkworm
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

#pragma once

#include <intx/intx.hpp>

#include <silkworm/core/common/base.hpp>

#include "hash.hpp"
#include <silkworm/core/types/hash.hpp>

namespace silkworm {

struct HeadInfo {
BlockNum block_num{0};
// TODO(canepat) rename BlockNumberAndHash
struct BlockId {
BlockNum number{};
Hash hash;
intx::uint256 total_difficulty;

friend bool operator==(const BlockId&, const BlockId&) = default;
};

} // namespace silkworm
48 changes: 48 additions & 0 deletions silkworm/core/types/chain_head.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 The Silkworm Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <intx/intx.hpp>

#include <silkworm/core/common/base.hpp>

#include "block_id.hpp"
#include "hash.hpp"

namespace silkworm {

struct ChainHead {
BlockNum height{0};
Hash hash;
intx::uint256 total_difficulty;

friend bool operator==(const ChainHead&, const ChainHead&) = default;
};

inline bool operator==(const ChainHead& a, const BlockId& b) {
return a.height == b.number && a.hash == b.hash;
}

inline bool operator==(const BlockId& a, const ChainHead& b) {
return a.number == b.height && a.hash == b.hash;
}

inline BlockId to_BlockId(const ChainHead& head) {
return {.number = head.height, .hash = head.hash};
}

} // namespace silkworm
24 changes: 12 additions & 12 deletions silkworm/db/head_info.cpp → silkworm/db/chain_head.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

#include "head_info.hpp"
#include "chain_head.hpp"

#include <gsl/util>

Expand All @@ -24,35 +24,35 @@

namespace silkworm::db {

HeadInfo read_head_info(ROTxn& txn) {
HeadInfo head_info;
ChainHead read_chain_head(ROTxn& txn) {
ChainHead chain_head;

BlockNum head_height = db::stages::read_stage_progress(txn, db::stages::kBlockBodiesKey);
head_info.block_num = head_height;
chain_head.height = head_height;

auto head_hash = db::read_canonical_hash(txn, head_height);
if (head_hash) {
head_info.hash = head_hash.value();
chain_head.hash = head_hash.value();
} else {
log::Warning("db::HeadInfo") << "canonical hash at height " << std::to_string(head_height) << " not found in db";
return head_info;
log::Warning("db::ChainHead") << "canonical hash at height " << std::to_string(head_height) << " not found in db";
return chain_head;
}

auto head_total_difficulty = db::read_total_difficulty(txn, head_height, *head_hash);
if (head_total_difficulty) {
head_info.total_difficulty = head_total_difficulty.value();
chain_head.total_difficulty = head_total_difficulty.value();
} else {
log::Warning("db::HeadInfo") << "total difficulty of canonical hash at height " << std::to_string(head_height) << " not found in db";
log::Warning("db::ChainHead") << "total difficulty of canonical hash at height " << std::to_string(head_height) << " not found in db";
}

return head_info;
return chain_head;
}

HeadInfo read_head_info(db::ROAccess db_access) {
ChainHead read_chain_head(db::ROAccess db_access) {
auto txn = db_access.start_ro_tx();
[[maybe_unused]] auto _ = gsl::finally([&txn] { txn.abort(); });

return read_head_info(txn);
return read_chain_head(txn);
}

} // namespace silkworm::db
6 changes: 3 additions & 3 deletions silkworm/db/head_info.hpp → silkworm/db/chain_head.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

#pragma once

#include <silkworm/core/types/head_info.hpp>
#include <silkworm/core/types/chain_head.hpp>
#include <silkworm/db/mdbx/mdbx.hpp>

namespace silkworm::db {

HeadInfo read_head_info(ROTxn& txn);
HeadInfo read_head_info(db::ROAccess db_access);
ChainHead read_chain_head(ROTxn& txn);
ChainHead read_chain_head(db::ROAccess db_access);

} // namespace silkworm::db
Loading