Skip to content

Commit

Permalink
db: replace HeadInfo with ChainHead
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Mar 14, 2024
1 parent 9850135 commit 76804fe
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 85 deletions.
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
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
1 change: 1 addition & 0 deletions silkworm/node/stagedsync/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <boost/asio/io_context.hpp>

#include <silkworm/core/types/block.hpp>
#include <silkworm/core/types/block_id.hpp>
#include <silkworm/node/stagedsync/types.hpp>

namespace silkworm::execution {
Expand Down
1 change: 1 addition & 0 deletions silkworm/node/stagedsync/forks/canonical_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <silkworm/core/common/lru_cache.hpp>
#include <silkworm/core/types/block.hpp>
#include <silkworm/core/types/block_id.hpp>
#include <silkworm/db/access_layer.hpp>
#include <silkworm/db/stage.hpp>
#include <silkworm/node/stagedsync/execution_pipeline.hpp>
Expand Down
22 changes: 11 additions & 11 deletions silkworm/sentry/eth/status_data_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

namespace silkworm::sentry::eth {

static void log_head_info(const HeadInfo& info) {
static void log_chain_head(const ChainHead& info) {
log::Debug(
"StatusDataProvider::HeadInfo",
"StatusDataProvider::ChainHead",
{
"head hash",
info.hash.to_hex(),
Expand All @@ -35,32 +35,32 @@ static void log_head_info(const HeadInfo& info) {
intx::to_string(info.total_difficulty),

"head block num",
std::to_string(info.block_num),
std::to_string(info.height),
});
}

StatusDataProvider::StatusData StatusDataProvider::make_status_data(
HeadInfo head_info,
ChainHead chain_head,
uint8_t eth_version,
const ChainConfig& chain_config) {
auto fork_numbers = chain_config.distinct_fork_numbers();
auto fork_times = chain_config.distinct_fork_times();
auto best_block_hash = Bytes{ByteView{head_info.hash}};
auto best_block_hash = Bytes{ByteView{chain_head.hash}};
auto genesis_hash = ByteView{chain_config.genesis_hash.value()};

silkworm::sentry::eth::StatusMessage status_message = {
eth_version,
chain_config.chain_id,
head_info.total_difficulty,
chain_head.total_difficulty,
best_block_hash,
Bytes{genesis_hash},
silkworm::sentry::eth::ForkId(genesis_hash, fork_numbers, fork_times, head_info.block_num),
silkworm::sentry::eth::ForkId(genesis_hash, fork_numbers, fork_times, chain_head.height),
};

silkworm::sentry::eth::StatusData status_data = {
std::move(fork_numbers),
std::move(fork_times),
head_info.block_num,
chain_head.height,
std::move(status_message),
};

Expand All @@ -72,10 +72,10 @@ StatusDataProvider::StatusData StatusDataProvider::get_status_data(uint8_t eth_v
throw std::runtime_error("StatusDataProvider::get_status_data: unsupported eth version " + std::to_string(eth_version));
}

auto head_info = head_info_provider_();
log_head_info(head_info);
auto chain_head = chain_head_provider_();
log_chain_head(chain_head);

return make_status_data(head_info, eth_version, chain_config_);
return make_status_data(chain_head, eth_version, chain_config_);
}

StatusDataProvider::StatusDataProviderFactory StatusDataProvider::to_factory_function() {
Expand Down
10 changes: 5 additions & 5 deletions silkworm/sentry/eth/status_data_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <silkworm/infra/concurrency/task.hpp>

#include <silkworm/core/chain/config.hpp>
#include <silkworm/core/types/head_info.hpp>
#include <silkworm/core/types/chain_head.hpp>

#include "status_data.hpp"

Expand All @@ -30,9 +30,9 @@ namespace silkworm::sentry::eth {
class StatusDataProvider {
public:
StatusDataProvider(
std::function<HeadInfo()> head_info_provider,
std::function<ChainHead()> chain_head_provider,
const ChainConfig& chain_config)
: head_info_provider_(std::move(head_info_provider)),
: chain_head_provider_(std::move(chain_head_provider)),
chain_config_(chain_config) {}

using StatusData = silkworm::sentry::eth::StatusData;
Expand All @@ -43,11 +43,11 @@ class StatusDataProvider {

private:
static StatusData make_status_data(
HeadInfo head_info,
ChainHead chain_head,
uint8_t eth_version,
const ChainConfig& chain_config);

std::function<HeadInfo()> head_info_provider_;
std::function<ChainHead()> chain_head_provider_;
const ChainConfig& chain_config_;
};

Expand Down
1 change: 1 addition & 0 deletions silkworm/sync/internals/chain_fork_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <silkworm/core/common/lru_cache.hpp>
#include <silkworm/core/types/block.hpp>
#include <silkworm/core/types/chain_head.hpp>
#include <silkworm/sync/internals/types.hpp>

namespace silkworm::chainsync {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/sync/internals/header_chain_plus_exec_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ TEST_CASE("Headers receiving and saving") {
auto& tx = exec_engine.main_chain_.tx(); // mdbx refuses to open a ROTxn when there is a RWTxn in the same thread

auto head = exec_engine.last_fork_choice();
REQUIRE(height(head) == 0);
REQUIRE(head.number == 0);

std::vector<BlockHeader> last_headers = exec_engine.get_last_headers(1);
REQUIRE(last_headers.size() == 1);
Expand Down
4 changes: 2 additions & 2 deletions silkworm/sync/sync_pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ PoWSync::NewHeight PoWSync::resume() { // find the point (head) where we left o
auto last_fcu = sync_wait(in(exec_engine_), exec_engine_.last_fork_choice()); // previously was get_canonical_head()
auto block_progress = sync_wait(in(exec_engine_), exec_engine_.block_progress());

ensure_invariant(height(last_fcu) <= block_progress, "canonical head beyond block progress");
ensure_invariant(last_fcu.number <= block_progress, "canonical head beyond block progress");

if (block_progress == height(last_fcu)) {
if (block_progress == last_fcu.number) {
// if fcu and header progress match than we have the actual canonical, we only need to do a forward sync...
ensure_invariant(last_fcu == chain_fork_view_.head(), "last fcu misaligned with canonical head");
chain_fork_view_.reset_head({last_fcu.number, last_fcu.hash,
Expand Down

0 comments on commit 76804fe

Please sign in to comment.