Skip to content

Commit

Permalink
refactor: make submission impl non blocking (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 9, 2024
1 parent 6b061b2 commit f459784
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 52 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ secp256k1 = { version = "0.28.0", features = ["rand-std"] }
serde = "1.0.188"
serde_json = "1.0.107"
serde_with = "3.3.0"
tokio = "1.35"
uuid = "1.6.1"

[dev-dependencies]
Expand Down
11 changes: 2 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
use std::sync::Arc;

pub mod cli_ext;
pub use cli_ext::ValidationCliExt;

pub mod cli_ext;
pub mod rpc;
use rpc::ValidationApiInner;

/// The type that implements the `validation` rpc namespace trait
pub struct ValidationApi<Provider> {
inner: Arc<ValidationApiInner<Provider>>,
}
pub use rpc::ValidationApi;
16 changes: 16 additions & 0 deletions src/rpc/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::rpc::ValidationRequestBody;
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;

/// trait interface for a custom rpc namespace: `validation`
///
/// This defines an additional namespace where all methods are configured as trait functions.
#[rpc(client, server, namespace = "flashbots")]
pub trait ValidationApi {
/// Validates a block submitted to the relay
#[method(name = "validateBuilderSubmissionV2")]
async fn validate_builder_submission_v2(
&self,
request_body: ValidationRequestBody,
) -> RpcResult<()>;
}
37 changes: 16 additions & 21 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
pub use crate::rpc::api::{ValidationApiClient, ValidationApiServer};
use async_trait::async_trait;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

use crate::ValidationApi;
use jsonrpsee::core::RpcResult;
use jsonrpsee::types::error::ErrorCode;
use reth::providers::{
AccountReader, BlockReaderIdExt, ChainSpecProvider, HeaderProvider, StateProviderFactory,
WithdrawalsProvider,
};
use std::sync::Arc;

mod types;
pub use types::*;
use validation::ValidationRequest;

mod api;
mod result;
mod types;
mod utils;
mod validation;
use validation::ValidationRequest;

/// trait interface for a custom rpc namespace: `validation`
///
/// This defines an additional namespace where all methods are configured as trait functions.
#[rpc(client, server, namespace = "flashbots")]
#[async_trait]
pub trait ValidationApi {
/// Validates a block submitted to the relay
#[method(name = "validateBuilderSubmissionV2")]
async fn validate_builder_submission_v2(
&self,
request_body: ValidationRequestBody,
) -> RpcResult<()>;
/// The type that implements the `validation` rpc namespace trait
pub struct ValidationApi<Provider> {
inner: Arc<ValidationApiInner<Provider>>,
}

impl<Provider> ValidationApi<Provider>
Expand All @@ -38,11 +29,12 @@ where
+ HeaderProvider
+ AccountReader
+ WithdrawalsProvider
+ Clone
+ 'static,
{
/// The provider that can interact with the chain.
pub fn provider(&self) -> &Provider {
&self.inner.provider
pub fn provider(&self) -> Provider {
self.inner.provider.clone()
}

/// Create a new instance of the [ValidationApi]
Expand All @@ -61,6 +53,7 @@ where
+ HeaderProvider
+ AccountReader
+ WithdrawalsProvider
+ Clone
+ 'static,
{
/// Validates a block submitted to the relay
Expand All @@ -69,7 +62,9 @@ where
request_body: ValidationRequestBody,
) -> RpcResult<()> {
let request = ValidationRequest::new(request_body, self.provider());
request.validate().await
tokio::task::spawn_blocking(move || request.validate())
.await
.map_err(|_| ErrorCode::InternalError)?
}
}

Expand Down
32 changes: 14 additions & 18 deletions src/rpc/validation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::rpc::result::internal_rpc_err;
use crate::rpc::types::*;
use crate::rpc::utils::*;
use jsonrpsee::core::RpcResult;

use reth::consensus_common::validation::full_validation;
use reth::primitives::{
revm_primitives::AccountInfo, Address, Receipts, SealedBlock, TransactionSigned, U256,
Expand All @@ -11,28 +13,22 @@ use reth::providers::{
use reth::revm::{database::StateProviderDatabase, db::BundleState, processor::EVMProcessor};
use reth::rpc::compat::engine::payload::try_into_sealed_block;
use reth::rpc::result::ToRpcResult;

use reth_tracing::tracing;
use uuid::Uuid;

use crate::rpc::result::internal_rpc_err;
use crate::rpc::types::*;
use crate::rpc::utils::*;

use std::time::Instant;
use uuid::Uuid;

pub struct ValidationRequest<'a, Provider> {
pub struct ValidationRequest<Provider> {
request_id: Uuid,
start_time: Instant,
request_body: ValidationRequestBody,
provider: &'a Provider,
provider: Provider,
}

impl<'a, Provider> ValidationRequest<'a, Provider> {
impl<Provider> ValidationRequest<Provider> {
pub fn new(
request_body: ValidationRequestBody,
provider: &'a Provider,
) -> ValidationRequest<'a, Provider> {
provider: Provider,
) -> ValidationRequest<Provider> {
let request_id = Uuid::new_v4();
let start_time = Instant::now();
tracing::info!(block_hash = ?request_body.message.block_hash, ?request_id, "Received Validation Request");
Expand All @@ -45,7 +41,7 @@ impl<'a, Provider> ValidationRequest<'a, Provider> {
}
}

impl<Provider> ValidationRequest<'_, Provider>
impl<Provider> ValidationRequest<Provider>
where
Provider: BlockReaderIdExt
+ ChainSpecProvider
Expand All @@ -55,9 +51,8 @@ where
+ WithdrawalsProvider
+ 'static,
{
pub async fn validate(&self) -> RpcResult<()> {
pub fn validate(&self) -> RpcResult<()> {
self.validate_inner()
.await
.inspect_err(|error| {
tracing::warn!(
request_id = self.request_id.to_string(),
Expand All @@ -74,7 +69,8 @@ where
);
})
}
async fn validate_inner(&self) -> RpcResult<()> {

fn validate_inner(&self) -> RpcResult<()> {
self.trace_validation_step(self.check_gas_limit(), "Check Gas Limit")?;

self.trace_validation_step(
Expand Down Expand Up @@ -148,7 +144,7 @@ where
}

fn validate_header(&self, block: &SealedBlock) -> RpcResult<()> {
full_validation(block, self.provider, &self.provider.chain_spec()).to_rpc_result()
full_validation(block, &self.provider, &self.provider.chain_spec()).to_rpc_result()
}

fn execute_and_verify_block(&self, block: &SealedBlock) -> RpcResult<BundleStateWithReceipts> {
Expand Down

0 comments on commit f459784

Please sign in to comment.