Skip to content

Commit b0bc318

Browse files
authored
rpcdaemon: remove optional in read chain config (#2130)
1 parent be9a92a commit b0bc318

20 files changed

+100
-143
lines changed

silkworm/rpc/commands/debug_api.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,14 @@ Task<void> DebugRpcApi::handle_debug_account_at(const nlohmann::json& request, n
302302

303303
SILK_TRACE << "Block number: " << block_number << " #tnx: " << transactions.size();
304304

305-
auto chain_config_ptr = co_await chain_storage->read_chain_config();
306-
ensure(chain_config_ptr.has_value(), "cannot read chain config");
307-
305+
const auto chain_config = co_await chain_storage->read_chain_config();
308306
auto this_executor = co_await boost::asio::this_coro::executor;
309307
auto result = co_await async_task(workers_.executor(), [&]() -> nlohmann::json {
310308
auto state = tx->create_state(this_executor, *chain_storage, block_number - 1);
311309
auto account_opt = state->read_account(address);
312310
account_opt.value_or(silkworm::Account{});
313311

314-
EVMExecutor executor{*chain_config_ptr, workers_, state};
312+
EVMExecutor executor{chain_config, workers_, state};
315313

316314
uint64_t index = std::min(static_cast<uint64_t>(transactions.size()), tx_index);
317315
for (uint64_t idx{0}; idx < index; idx++) {
@@ -710,9 +708,9 @@ Task<void> DebugRpcApi::handle_debug_get_raw_header(const nlohmann::json& reques
710708
const auto chain_storage = tx->create_storage();
711709
const auto block_number = co_await core::get_block_number(block_id, *tx);
712710
const auto block_hash = co_await chain_storage->read_canonical_hash(block_number);
713-
auto header = co_await chain_storage->read_header(block_number, block_hash->bytes);
711+
const auto header = co_await chain_storage->read_header(block_number, block_hash->bytes);
714712
if (!header) {
715-
throw std::invalid_argument("header not found");
713+
throw std::invalid_argument("header " + std::to_string(block_number) + " not found");
716714
}
717715
Bytes encoded_header;
718716
rlp::encode(encoded_header, *header);

silkworm/rpc/commands/engine_api.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -318,25 +318,24 @@ Task<void> EngineRpcApi::handle_engine_new_payload_v2(const nlohmann::json& requ
318318
#endif
319319
const auto storage{tx->create_storage()};
320320
const auto config{co_await storage->read_chain_config()};
321-
ensure(config.has_value(), "execution layer has invalid configuration");
322-
ensure(config->shanghai_time.has_value(), "execution layer has no Shanghai timestamp in configuration");
321+
ensure(config.shanghai_time.has_value(), "execution layer has no Shanghai timestamp in configuration");
323322

324323
// We MUST check that CL has sent the expected ExecutionPayload version [Specification for params]
325-
if (payload.timestamp < config->shanghai_time && payload.version != ExecutionPayload::V1) {
324+
if (payload.timestamp < config.shanghai_time && payload.version != ExecutionPayload::V1) {
326325
const auto error_msg = "consensus layer must use ExecutionPayloadV1 if timestamp lower than Shanghai";
327326
SILK_ERROR << error_msg;
328327
reply = make_json_error(request, kInvalidParams, error_msg);
329328
co_await tx->close();
330329
co_return;
331330
}
332-
if (payload.timestamp >= config->shanghai_time && payload.version != ExecutionPayload::V2) {
331+
if (payload.timestamp >= config.shanghai_time && payload.version != ExecutionPayload::V2) {
333332
const auto error_msg = "consensus layer must use ExecutionPayloadV2 if timestamp greater or equal to Shanghai";
334333
SILK_ERROR << error_msg;
335334
reply = make_json_error(request, kInvalidParams, error_msg);
336335
co_await tx->close();
337336
co_return;
338337
}
339-
if (config->cancun_time && payload.timestamp >= config->cancun_time) {
338+
if (config.cancun_time && payload.timestamp >= config.cancun_time) {
340339
const auto error_msg = "consensus layer must use ExecutionPayloadV3 if timestamp greater or equal to Cancun";
341340
SILK_ERROR << error_msg;
342341
reply = make_json_error(request, kUnsupportedFork, error_msg);
@@ -382,12 +381,11 @@ Task<void> EngineRpcApi::handle_engine_new_payload_v3(const nlohmann::json& requ
382381
#endif
383382
const auto storage{tx->create_storage()};
384383
const auto config{co_await storage->read_chain_config()};
385-
ensure(config.has_value(), "execution layer has invalid configuration");
386-
ensure(config->shanghai_time.has_value(), "execution layer has no Shanghai timestamp in configuration");
387-
ensure(config->cancun_time.has_value(), "execution layer has no Cancun timestamp in configuration");
384+
ensure(config.shanghai_time.has_value(), "execution layer has no Shanghai timestamp in configuration");
385+
ensure(config.cancun_time.has_value(), "execution layer has no Cancun timestamp in configuration");
388386

389387
// We MUST check that CL has sent the expected ExecutionPayload version [Specification for params]
390-
if (payload.timestamp >= config->cancun_time && payload.version != ExecutionPayload::V3) {
388+
if (payload.timestamp >= config.cancun_time && payload.version != ExecutionPayload::V3) {
391389
const auto error_msg = "consensus layer must use ExecutionPayloadV3 if timestamp greater or equal to Cancun";
392390
SILK_ERROR << error_msg;
393391
reply = make_json_error(request, kUnsupportedFork, error_msg);
@@ -581,13 +579,12 @@ Task<void> EngineRpcApi::handle_engine_exchange_transition_configuration_v1(cons
581579
#endif
582580
const auto storage{tx->create_storage()};
583581
const auto config{co_await storage->read_chain_config()};
584-
ensure(config.has_value(), "execution layer has invalid configuration");
585-
ensure(config->terminal_total_difficulty.has_value(), "execution layer does not have terminal total difficulty");
582+
ensure(config.terminal_total_difficulty.has_value(), "execution layer does not have terminal total difficulty");
586583

587584
// We SHOULD check for any configuration mismatch except `terminalBlockNumber` [Specification 2.]
588-
if (config->terminal_total_difficulty != cl_configuration.terminal_total_difficulty) {
585+
if (config.terminal_total_difficulty != cl_configuration.terminal_total_difficulty) {
589586
SILK_ERROR << "execution layer has the incorrect terminal total difficulty, expected: "
590-
<< cl_configuration.terminal_total_difficulty << " got: " << config->terminal_total_difficulty.value();
587+
<< cl_configuration.terminal_total_difficulty << " got: " << config.terminal_total_difficulty.value();
591588
reply = make_json_error(request, kInvalidParams, "consensus layer terminal total difficulty does not match");
592589
co_await tx->close();
593590
co_return;
@@ -602,7 +599,7 @@ Task<void> EngineRpcApi::handle_engine_exchange_transition_configuration_v1(cons
602599

603600
// We MUST respond with configurable setting values set according to EIP-3675 [Specification 1.]
604601
const TransitionConfiguration transition_configuration{
605-
.terminal_total_difficulty = config->terminal_total_difficulty.value(),
602+
.terminal_total_difficulty = config.terminal_total_difficulty.value(),
606603
.terminal_block_hash = kZeroHash, // terminal_block_hash removed from chain_config, return zero
607604
.terminal_block_number = 0 // terminal_block_number removed from chain_config, return zero
608605
};

silkworm/rpc/commands/erigon_api.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::js
129129

130130
// Lookup the first and last block headers
131131
const auto first_header = co_await chain_storage->read_canonical_header(kEarliestBlockNumber);
132+
ensure(first_header.has_value(), "cannot find earliest header");
132133
const auto head_header_hash = co_await core::rawdb::read_head_header_hash(*tx);
133-
const auto header_header_block_number = co_await chain_storage->read_block_number(head_header_hash);
134-
const auto current_header = co_await chain_storage->read_header(*header_header_block_number, head_header_hash);
134+
const auto head_header_block_number = co_await chain_storage->read_block_number(head_header_hash);
135+
ensure(head_header_block_number.has_value(), "cannot find head header hash");
136+
const auto current_header = co_await chain_storage->read_header(*head_header_block_number, head_header_hash);
137+
ensure(current_header.has_value(), "cannot find head header");
135138
const BlockNum current_block_number = current_header->number;
136139

137140
// Find the lowest block header w/ timestamp greater or equal to provided timestamp
@@ -142,13 +145,14 @@ Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::js
142145
block_number = kEarliestBlockNumber;
143146
} else {
144147
// Good-old binary search to find the lowest block header matching timestamp
148+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines)
145149
auto matching_block_number = co_await binary_search(current_block_number, [&](uint64_t bn) -> Task<bool> {
146150
const auto header = co_await chain_storage->read_canonical_header(bn);
147-
co_return header->timestamp >= timestamp;
151+
co_return header && header->timestamp >= timestamp;
148152
});
149153
// TODO(canepat) we should try to avoid this block header lookup (just done in search)
150154
auto matching_header = co_await chain_storage->read_canonical_header(matching_block_number);
151-
while (matching_header->timestamp > timestamp) {
155+
while (matching_header && matching_header->timestamp > timestamp) {
152156
const auto header = co_await chain_storage->read_canonical_header(matching_block_number - 1);
153157
if (!header || header->timestamp < timestamp) {
154158
break;
@@ -161,14 +165,9 @@ Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::js
161165

162166
// Lookup and return the matching block
163167
const auto block_with_hash = co_await core::read_block_by_number(*block_cache_, *chain_storage, block_number);
164-
if (!block_with_hash) {
165-
const std::string error_msg = "block not found ";
166-
SILK_ERROR << "erigon_get_block_by_timestamp: core::read_block_by_number: " << error_msg << request.dump();
167-
make_glaze_json_error(request, 100, error_msg, reply);
168-
co_await tx->close(); // RAII not (yet) available with coroutines
169-
co_return;
170-
}
168+
ensure(block_with_hash != nullptr, [&]() { return "block " + std::to_string(block_number) + " not found"; });
171169
const auto total_difficulty = co_await chain_storage->read_total_difficulty(block_with_hash->hash, block_number);
170+
ensure(total_difficulty.has_value(), [&]() { return "no total difficulty for block " + std::to_string(block_number); });
172171
const Block extended_block{block_with_hash, *total_difficulty, full_tx};
173172

174173
make_glaze_json_content(request, extended_block, reply);
@@ -449,11 +448,7 @@ Task<void> ErigonRpcApi::handle_erigon_forks(const nlohmann::json& request, nloh
449448
const auto chain_storage = tx->create_storage();
450449

451450
const auto chain_config{co_await chain_storage->read_chain_config()};
452-
if (!chain_config) {
453-
throw std::runtime_error("Chain config missing");
454-
}
455-
SILK_DEBUG << "chain config: " << *chain_config;
456-
Forks forks{*chain_config};
451+
Forks forks{chain_config};
457452

458453
reply = make_json_content(request, forks);
459454
} catch (const std::exception& e) {

0 commit comments

Comments
 (0)