Skip to content

Commit

Permalink
Merge pull request #2184 from eqlabs/sistemd/rpc-version-in-handlers
Browse files Browse the repository at this point in the history
feat(rpc): add overload for rpc handler with version
  • Loading branch information
sistemd committed Aug 27, 2024
2 parents 618e148 + 21daafe commit 0e8f823
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
3 changes: 0 additions & 3 deletions crates/rpc/src/dto/simulation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// TODO In v06 some stuff is different, see with_v06_format in
// simulate_transactions

use anyhow::anyhow;
use pathfinder_common::{ContractAddress, ContractNonce};
use serde::ser::Error;
Expand Down
49 changes: 49 additions & 0 deletions crates/rpc/src/jsonrpc/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,55 @@ mod sealed {
fn into_method(self) -> Box<dyn RpcMethod>;
}

/// ```
/// async fn example(RpcContext, impl Deserialize, RpcVersion) -> Result<Output, Into<RpcError>>
/// ```
impl<'a, F, Input, Output, Error, Fut>
Sealed<((), (), Input), ((), (), Output), ((), (), RpcContext)> for F
where
F: Fn(RpcContext, Input, RpcVersion) -> Fut + Sync + Send + 'static,
Input: DeserializeOwned + Send + Sync + 'static,
Output: SerializeForVersion + Send + Sync + 'static,
Error: Into<RpcError> + Send + Sync + 'static,
Fut: Future<Output = Result<Output, Error>> + Send,
{
fn into_method(self) -> Box<dyn RpcMethod> {
struct Helper<F, Input, Output, Error> {
f: F,
_marker: PhantomData<(Input, Output, Error)>,
}

#[axum::async_trait]
impl<F, Input, Output, Error, Fut> RpcMethod for Helper<F, Input, Output, Error>
where
F: Fn(RpcContext, Input, RpcVersion) -> Fut + Sync + Send,
Input: DeserializeOwned + Send + Sync,
Output: SerializeForVersion + Send + Sync,
Error: Into<RpcError> + Send + Sync,
Fut: Future<Output = Result<Output, Error>> + Send,
{
async fn invoke<'a>(
&self,
state: RpcContext,
input: RawParams<'a>,
version: RpcVersion,
) -> RpcResult {
let input = input.deserialize()?;
(self.f)(state, input, version)
.await
.map_err(Into::into)?
.serialize(Serializer::new(version))
.map_err(|e| RpcError::InternalError(e.into()))
}
}

Box::new(Helper {
f: self,
_marker: Default::default(),
})
}
}

/// ```
/// async fn example(RpcContext, impl Deserialize) -> Result<Output, Into<RpcError>>
/// ```
Expand Down
1 change: 0 additions & 1 deletion crates/rpc/src/method/estimate_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ pub async fn estimate_fee(context: RpcContext, input: Input) -> Result<Output, E
context.chain_id,
header,
pending,
// TODO Disabled for v06
L1BlobDataAvailability::Enabled,
context.config.custom_versioned_constants,
);
Expand Down
1 change: 0 additions & 1 deletion crates/rpc/src/method/estimate_message_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub async fn estimate_message_fee(
context.chain_id,
header,
pending,
// TODO Disabled for v06
L1BlobDataAvailability::Enabled,
context.config.custom_versioned_constants,
);
Expand Down
1 change: 0 additions & 1 deletion crates/rpc/src/method/simulate_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ pub async fn simulate_transactions(
context.chain_id,
header,
pending,
// TODO This is disabled for v06
pathfinder_executor::L1BlobDataAvailability::Enabled,
context.config.custom_versioned_constants,
);
Expand Down

0 comments on commit 0e8f823

Please sign in to comment.