|
| 1 | +/* |
| 2 | + Copyright 2024 The Silkworm Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "active_direct_service.hpp" |
| 18 | + |
| 19 | +#include <silkworm/infra/concurrency/co_spawn_sw.hpp> |
| 20 | + |
| 21 | +namespace silkworm::execution::api { |
| 22 | + |
| 23 | +ActiveDirectService::ActiveDirectService(stagedsync::ExecutionEngine& exec_engine, boost::asio::io_context& context) |
| 24 | + : DirectService{exec_engine}, context_{context}, executor_{context_.get_executor()} {} |
| 25 | + |
| 26 | +void ActiveDirectService::execution_loop() { |
| 27 | + exec_engine_.open(); |
| 28 | + |
| 29 | + boost::asio::executor_work_guard<decltype(executor_)> work{executor_}; |
| 30 | + context_.run(); |
| 31 | + |
| 32 | + exec_engine_.close(); |
| 33 | +} |
| 34 | + |
| 35 | +bool ActiveDirectService::stop() { |
| 36 | + context_.stop(); |
| 37 | + return ActiveComponent::stop(); |
| 38 | +} |
| 39 | + |
| 40 | +/** Chain Putters **/ |
| 41 | + |
| 42 | +// rpc InsertBlocks(InsertBlocksRequest) returns(InsertionResult); |
| 43 | +Task<InsertionResult> ActiveDirectService::insert_blocks(const Blocks& blocks) { |
| 44 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto bb) { |
| 45 | + return self->DirectService::insert_blocks(bb); |
| 46 | + }(this, blocks)); |
| 47 | +} |
| 48 | + |
| 49 | +/** Chain Validation and ForkChoice **/ |
| 50 | + |
| 51 | +// rpc ValidateChain(ValidationRequest) returns(ValidationReceipt); |
| 52 | +Task<ValidationResult> ActiveDirectService::validate_chain(BlockNumAndHash number_and_hash) { |
| 53 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto num_and_hash) { |
| 54 | + return self->DirectService::validate_chain(num_and_hash); |
| 55 | + }(this, number_and_hash)); |
| 56 | +} |
| 57 | + |
| 58 | +// rpc UpdateForkChoice(ForkChoice) returns(ForkChoiceReceipt); |
| 59 | +Task<ForkChoiceResult> ActiveDirectService::update_fork_choice(const ForkChoice& fork_choice) { |
| 60 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto choice) { |
| 61 | + return self->DirectService::update_fork_choice(choice); |
| 62 | + }(this, fork_choice)); |
| 63 | +} |
| 64 | + |
| 65 | +/** Block Assembly **/ |
| 66 | + |
| 67 | +// rpc AssembleBlock(AssembleBlockRequest) returns(AssembleBlockResponse); |
| 68 | +Task<AssembleBlockResult> ActiveDirectService::assemble_block(const api::BlockUnderConstruction& block) { |
| 69 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto b) { |
| 70 | + return self->DirectService::assemble_block(b); |
| 71 | + }(this, block)); |
| 72 | +} |
| 73 | + |
| 74 | +// rpc GetAssembledBlock(GetAssembledBlockRequest) returns(GetAssembledBlockResponse); |
| 75 | +Task<AssembledBlockResult> ActiveDirectService::get_assembled_block(PayloadId payload_id) { |
| 76 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto id) { |
| 77 | + return self->DirectService::get_assembled_block(id); |
| 78 | + }(this, payload_id)); |
| 79 | +} |
| 80 | + |
| 81 | +/** Chain Getters **/ |
| 82 | + |
| 83 | +// rpc CurrentHeader(google.protobuf.Empty) returns(GetHeaderResponse); |
| 84 | +Task<std::optional<BlockHeader>> ActiveDirectService::current_header() { |
| 85 | + return concurrency::co_spawn_and_await(executor_, [](auto* self) { |
| 86 | + return self->DirectService::current_header(); |
| 87 | + }(this)); |
| 88 | +} |
| 89 | + |
| 90 | +// rpc GetTD(GetSegmentRequest) returns(GetTDResponse); |
| 91 | +Task<std::optional<TotalDifficulty>> ActiveDirectService::get_td(BlockNumberOrHash number_or_hash) { |
| 92 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto num_or_hash) { |
| 93 | + return self->DirectService::get_td(num_or_hash); |
| 94 | + }(this, number_or_hash)); |
| 95 | +} |
| 96 | + |
| 97 | +// rpc GetHeader(GetSegmentRequest) returns(GetHeaderResponse); |
| 98 | +Task<std::optional<BlockHeader>> ActiveDirectService::get_header(BlockNumberOrHash number_or_hash) { |
| 99 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto num_or_hash) { |
| 100 | + return self->DirectService::get_header(num_or_hash); |
| 101 | + }(this, number_or_hash)); |
| 102 | +} |
| 103 | + |
| 104 | +// rpc GetBody(GetSegmentRequest) returns(GetBodyResponse); |
| 105 | +Task<std::optional<BlockBody>> ActiveDirectService::get_body(BlockNumberOrHash number_or_hash) { |
| 106 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto num_or_hash) { |
| 107 | + return self->DirectService::get_body(num_or_hash); |
| 108 | + }(this, number_or_hash)); |
| 109 | +} |
| 110 | + |
| 111 | +// rpc HasBlock(GetSegmentRequest) returns(HasBlockResponse); |
| 112 | +Task<bool> ActiveDirectService::has_block(BlockNumberOrHash number_or_hash) { |
| 113 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto num_or_hash) { |
| 114 | + return self->DirectService::has_block(num_or_hash); |
| 115 | + }(this, number_or_hash)); |
| 116 | +} |
| 117 | + |
| 118 | +/** Ranges **/ |
| 119 | + |
| 120 | +// rpc GetBodiesByRange(GetBodiesByRangeRequest) returns(GetBodiesBatchResponse); |
| 121 | +Task<BlockBodies> ActiveDirectService::get_bodies_by_range(BlockNumRange number_range) { |
| 122 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto num_range) { |
| 123 | + return self->DirectService::get_bodies_by_range(num_range); |
| 124 | + }(this, number_range)); |
| 125 | +} |
| 126 | + |
| 127 | +// rpc GetBodiesByHashes(GetBodiesByHashesRequest) returns(GetBodiesBatchResponse); |
| 128 | +Task<BlockBodies> ActiveDirectService::get_bodies_by_hashes(const BlockHashes& hashes) { |
| 129 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto hh) { |
| 130 | + return self->DirectService::get_bodies_by_hashes(hh); |
| 131 | + }(this, hashes)); |
| 132 | +} |
| 133 | + |
| 134 | +/** Chain Checkers **/ |
| 135 | + |
| 136 | +// rpc IsCanonicalHash(types.H256) returns(IsCanonicalResponse); |
| 137 | +Task<bool> ActiveDirectService::is_canonical_hash(Hash block_hash) { |
| 138 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto h) { |
| 139 | + return self->DirectService::is_canonical_hash(h); |
| 140 | + }(this, block_hash)); |
| 141 | +} |
| 142 | + |
| 143 | +// rpc GetHeaderHashNumber(types.H256) returns(GetHeaderHashNumberResponse); |
| 144 | +Task<std::optional<BlockNum>> ActiveDirectService::get_header_hash_number(Hash block_hash) { |
| 145 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto h) { |
| 146 | + return self->DirectService::get_header_hash_number(h); |
| 147 | + }(this, block_hash)); |
| 148 | +} |
| 149 | + |
| 150 | +// rpc GetForkChoice(google.protobuf.Empty) returns(ForkChoice); |
| 151 | +Task<ForkChoice> ActiveDirectService::get_fork_choice() { |
| 152 | + return concurrency::co_spawn_and_await(executor_, [](auto* self) { |
| 153 | + return self->DirectService::get_fork_choice(); |
| 154 | + }(this)); |
| 155 | +} |
| 156 | + |
| 157 | +/** Misc **/ |
| 158 | + |
| 159 | +// rpc Ready(google.protobuf.Empty) returns(ReadyResponse); |
| 160 | +Task<bool> ActiveDirectService::ready() { |
| 161 | + return concurrency::co_spawn_and_await(executor_, [](auto* self) { |
| 162 | + return self->DirectService::ready(); |
| 163 | + }(this)); |
| 164 | +} |
| 165 | + |
| 166 | +// rpc FrozenBlocks(google.protobuf.Empty) returns(FrozenBlocksResponse); |
| 167 | +Task<uint64_t> ActiveDirectService::frozen_blocks() { |
| 168 | + return concurrency::co_spawn_and_await(executor_, [](auto* self) { |
| 169 | + return self->DirectService::frozen_blocks(); |
| 170 | + }(this)); |
| 171 | +} |
| 172 | + |
| 173 | +/** Additional non-RPC methods **/ |
| 174 | + |
| 175 | +Task<BlockHeaders> ActiveDirectService::get_last_headers(uint64_t n) { |
| 176 | + return concurrency::co_spawn_and_await(executor_, [](auto* self, auto how_many) { |
| 177 | + return self->DirectService::get_last_headers(how_many); |
| 178 | + }(this, n)); |
| 179 | +} |
| 180 | + |
| 181 | +Task<BlockNum> ActiveDirectService::block_progress() { |
| 182 | + return concurrency::co_spawn_and_await(executor_, [](auto* self) { |
| 183 | + return self->DirectService::block_progress(); |
| 184 | + }(this)); |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace silkworm::execution::api |
0 commit comments