Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MoonbeamFinalityApi to fully async/await #2784

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions client/rpc/finality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ pub trait MoonbeamFinalityApi {
/// Reports whether a Substrate or Ethereum block is finalized.
/// Returns false if the block is not found.
#[method(name = "moon_isBlockFinalized")]
fn is_block_finalized(&self, block_hash: H256) -> RpcResult<bool>;
async fn is_block_finalized(&self, block_hash: H256) -> RpcResult<bool>;

/// Reports whether an Ethereum transaction is finalized.
/// Returns false if the transaction is not found
#[method(name = "moon_isTxFinalized")]
fn is_tx_finalized(&self, tx_hash: H256) -> RpcResult<bool>;
async fn is_tx_finalized(&self, tx_hash: H256) -> RpcResult<bool>;
}

pub struct MoonbeamFinality<B: Block, C> {
Expand All @@ -53,42 +53,44 @@ impl<B: Block, C> MoonbeamFinality<B, C> {
}
}

#[async_trait::async_trait]
impl<B, C> MoonbeamFinalityApiServer for MoonbeamFinality<B, C>
where
B: Block<Hash = H256>,
C: HeaderBackend<B> + Send + Sync + 'static,
{
fn is_block_finalized(&self, raw_hash: H256) -> RpcResult<bool> {
async fn is_block_finalized(&self, raw_hash: H256) -> RpcResult<bool> {
let client = self.client.clone();
is_block_finalized_inner::<B, C>(self.backend.as_ref(), &client, raw_hash)
is_block_finalized_inner::<B, C>(self.backend.as_ref(), &client, raw_hash).await
}

fn is_tx_finalized(&self, tx_hash: H256) -> RpcResult<bool> {
async fn is_tx_finalized(&self, tx_hash: H256) -> RpcResult<bool> {
let client = self.client.clone();

if let Some((ethereum_block_hash, _ethereum_index)) =
futures::executor::block_on(frontier_backend_client::load_transactions::<B, C>(
frontier_backend_client::load_transactions::<B, C>(
&client,
self.backend.as_ref(),
tx_hash,
true,
))? {
)
.await?
{
is_block_finalized_inner::<B, C>(self.backend.as_ref(), &client, ethereum_block_hash)
.await
} else {
Ok(false)
}
}
}

fn is_block_finalized_inner<B: Block<Hash = H256>, C: HeaderBackend<B> + 'static>(
async fn is_block_finalized_inner<B: Block<Hash = H256>, C: HeaderBackend<B> + 'static>(
backend: &(dyn fc_api::Backend<B>),
client: &C,
raw_hash: H256,
) -> RpcResult<bool> {
let substrate_hash =
match futures::executor::block_on(frontier_backend_client::load_hash::<B, C>(
client, backend, raw_hash,
))? {
match frontier_backend_client::load_hash::<B, C>(client, backend, raw_hash).await? {
// If we find this hash in the frontier data base, we know it is an eth hash
Some(hash) => hash,
// Otherwise, we assume this is a Substrate hash.
Expand Down
Loading