Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions prdoc/pr_10612.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: revive eth-rpc Add polkadot_postDispatchWeight rpc methods
doc:
- audience: Runtime Dev
description: "Add a new RPC method to return the post-dispatch weight of a transaction\r\
\n"
crates:
- name: pallet-revive-eth-rpc
bump: patch
3 changes: 3 additions & 0 deletions substrate/frame/revive/rpc/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ pub use execution_apis::*;

mod health_api;
pub use health_api::*;

mod polkadot_api;
pub use polkadot_api::*;
46 changes: 46 additions & 0 deletions substrate/frame/revive/rpc/src/apis/polkadot_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Polkadot-specific JSON-RPC methods.

use crate::*;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use sp_runtime::Weight;

#[rpc(server, client)]
pub trait PolkadotRpc {
/// Get the post dispatch weight for a given transaction hash.
#[method(name = "polkadot_postDispatchWeight")]
async fn post_dispatch_weight(&self, transaction_hash: H256) -> RpcResult<Option<Weight>>;
}

pub struct PolkadotRpcServerImpl {
client: client::Client,
}

impl PolkadotRpcServerImpl {
pub fn new(client: client::Client) -> Self {
Self { client }
}
}

#[async_trait]
impl PolkadotRpcServer for PolkadotRpcServerImpl {
async fn post_dispatch_weight(&self, transaction_hash: H256) -> RpcResult<Option<Weight>> {
let weight = self.client.post_dispatch_weight(&transaction_hash).await;
Ok(weight)
}
}
2 changes: 1 addition & 1 deletion substrate/frame/revive/rpc/src/block_info_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub trait BlockInfoProvider: Send + Sync {

/// Return the latest block number
async fn latest_block_number(&self) -> SubstrateBlockNumber {
return self.latest_block().await.number()
return self.latest_block().await.number();
}

/// Get block by block_number.
Expand Down
12 changes: 8 additions & 4 deletions substrate/frame/revive/rpc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
//! The Ethereum JSON-RPC server.
use crate::{
client::{connect, Client, SubscriptionType, SubstrateBlockNumber},
DebugRpcServer, DebugRpcServerImpl, EthRpcServer, EthRpcServerImpl, ReceiptExtractor,
ReceiptProvider, SubxtBlockInfoProvider, SystemHealthRpcServer, SystemHealthRpcServerImpl,
LOG_TARGET,
DebugRpcServer, DebugRpcServerImpl, EthRpcServer, EthRpcServerImpl, PolkadotRpcServer,
PolkadotRpcServerImpl, ReceiptExtractor, ReceiptProvider, SubxtBlockInfoProvider,
SystemHealthRpcServer, SystemHealthRpcServerImpl, LOG_TARGET,
};
use clap::Parser;
use futures::{future::BoxFuture, pin_mut, FutureExt};
Expand Down Expand Up @@ -266,11 +266,15 @@ fn rpc_module(is_dev: bool, client: Client) -> Result<RpcModule<()>, sc_service:
.into_rpc();

let health_api = SystemHealthRpcServerImpl::new(client.clone()).into_rpc();
let debug_api = DebugRpcServerImpl::new(client).into_rpc();
let debug_api = DebugRpcServerImpl::new(client.clone()).into_rpc();
let polkadot_api = PolkadotRpcServerImpl::new(client).into_rpc();

let mut module = RpcModule::new(());
module.merge(eth_api).map_err(|e| sc_service::Error::Application(e.into()))?;
module.merge(health_api).map_err(|e| sc_service::Error::Application(e.into()))?;
module.merge(debug_api).map_err(|e| sc_service::Error::Application(e.into()))?;
module
.merge(polkadot_api)
.map_err(|e| sc_service::Error::Application(e.into()))?;
Ok(module)
}
11 changes: 11 additions & 0 deletions substrate/frame/revive/rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ impl Client {
self.receipt_provider.receipt_by_hash(tx_hash).await
}

/// Get The post dispatch weight associated with this Ethereum transaction hash.
pub async fn post_dispatch_weight(&self, tx_hash: &H256) -> Option<Weight> {
use crate::subxt_client::system::events::ExtrinsicSuccess;
let ReceiptInfo { block_hash, transaction_index, .. } = self.receipt(tx_hash).await?;
let block_hash = self.resolve_substrate_hash(&block_hash).await?;
let block = self.block_provider.block_by_hash(&block_hash).await.ok()??;
let ext = block.extrinsics().await.ok()?.iter().nth(transaction_index.as_u32() as _)?;
let event = ext.events().await.ok()?.find_first::<ExtrinsicSuccess>().ok()??;
Some(event.dispatch_info.weight.0)
}

pub async fn sync_state(
&self,
) -> Result<sc_rpc::system::SyncState<SubstrateBlockNumber>, ClientError> {
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/revive/rpc/src/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<Client: EthRpcClient + Sync + Send> SubmittedTransaction<Client> {
self.gas() > receipt.gas_used,
"Gas used should be less than gas estimated."
);
return Ok(receipt)
return Ok(receipt);
} else {
anyhow::bail!("Transaction failed receipt: {receipt:?}")
}
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/revive/rpc/src/fee_history_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl FeeHistoryProvider {
base_fee_per_gas: vec![],
gas_used_ratio: vec![],
reward: vec![],
})
});
};

let lowest = highest.saturating_sub(block_count.saturating_sub(1)).max(lowest_in_cache);
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/revive/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl EthRpcServerImpl {
)
.await
else {
return Ok(None)
return Ok(None);
};
let Some(signed_tx) = self.client.signed_tx_by_hash(&receipt.transaction_hash).await else {
return Ok(None);
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/revive/rpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn ws_client_with_retry(url: &str) -> WsClient {
tokio::time::timeout(timeout, async {
loop {
if let Ok(client) = WsClientBuilder::default().build(url).await {
return client
return client;
} else {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
Expand Down
Loading