From 8cca727fbda68b9d8678573ab1ade0c3d6c9f104 Mon Sep 17 00:00:00 2001 From: PG Herveou <pgherveou@gmail.com> Date: Mon, 17 Feb 2025 12:25:02 +0100 Subject: [PATCH] [pallet-revive] rpc add --earliest-receipt-block (#7589) Add a cli option to skip searching receipts for blocks older than the specified limit --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- prdoc/pr_7589.prdoc | 8 +++++++ substrate/frame/revive/rpc/src/cli.rs | 12 +++++++++- .../frame/revive/rpc/src/receipt_extractor.rs | 22 ++++++++++++++----- .../revive/rpc/src/receipt_provider/db.rs | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 prdoc/pr_7589.prdoc diff --git a/prdoc/pr_7589.prdoc b/prdoc/pr_7589.prdoc new file mode 100644 index 00000000000..da4eb3ca219 --- /dev/null +++ b/prdoc/pr_7589.prdoc @@ -0,0 +1,8 @@ +title: '[pallet-revive] rpc add --earliest-receipt-block' +doc: +- audience: Runtime Dev + description: "Add a cli option to skip searching receipts for blocks older than\ + \ the specified limit\r\n" +crates: +- name: pallet-revive-eth-rpc + bump: minor diff --git a/substrate/frame/revive/rpc/src/cli.rs b/substrate/frame/revive/rpc/src/cli.rs index 5844d36a87f..bf9a0e1ec11 100644 --- a/substrate/frame/revive/rpc/src/cli.rs +++ b/substrate/frame/revive/rpc/src/cli.rs @@ -51,6 +51,10 @@ pub struct CliCommand { #[clap(long, default_value = "256")] pub cache_size: usize, + /// Earliest block number to consider when searching for transaction receipts. + #[clap(long)] + pub earliest_receipt_block: Option<SubstrateBlockNumber>, + /// The database used to store Ethereum transaction hashes. /// This is only useful if the node needs to act as an archive node and respond to Ethereum RPC /// queries for transactions that are not in the in memory cache. @@ -98,6 +102,7 @@ fn init_logger(params: &SharedParams) -> anyhow::Result<()> { fn build_client( tokio_handle: &tokio::runtime::Handle, cache_size: usize, + earliest_receipt_block: Option<SubstrateBlockNumber>, node_rpc_url: &str, database_url: &str, abort_signal: Signals, @@ -112,7 +117,10 @@ fn build_client( log::info!( target: LOG_TARGET, "Using in-memory database, keeping only {cache_size} blocks in memory"); } - let receipt_extractor = ReceiptExtractor::new(native_to_eth_ratio(&api).await?); + let receipt_extractor = ReceiptExtractor::new( + native_to_eth_ratio(&api).await?, + earliest_receipt_block); + let receipt_provider: Arc<dyn ReceiptProvider> = Arc::new(( CacheReceiptProvider::default(), DBReceiptProvider::new( @@ -148,6 +156,7 @@ pub fn run(cmd: CliCommand) -> anyhow::Result<()> { node_rpc_url, cache_size, database_url, + earliest_receipt_block, index_until_block, shared_params, .. @@ -188,6 +197,7 @@ pub fn run(cmd: CliCommand) -> anyhow::Result<()> { let client = build_client( tokio_handle, cache_size, + earliest_receipt_block, &node_rpc_url, &database_url, tokio_runtime.block_on(async { Signals::capture() })?, diff --git a/substrate/frame/revive/rpc/src/receipt_extractor.rs b/substrate/frame/revive/rpc/src/receipt_extractor.rs index 6338f42ee0c..37f47016052 100644 --- a/substrate/frame/revive/rpc/src/receipt_extractor.rs +++ b/substrate/frame/revive/rpc/src/receipt_extractor.rs @@ -14,9 +14,8 @@ // 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. - use crate::{ - client::SubstrateBlock, + client::{SubstrateBlock, SubstrateBlockNumber}, subxt_client::{ revive::{calls::types::EthTransact, events::ContractEmitted}, system::events::ExtrinsicSuccess, @@ -37,16 +36,22 @@ use sp_core::keccak_256; pub struct ReceiptExtractor { /// The native to eth decimal ratio, used to calculated gas from native fees. native_to_eth_ratio: u32, + + /// Earliest block number to consider when searching for transaction receipts. + earliest_receipt_block: Option<SubstrateBlockNumber>, } impl ReceiptExtractor { /// Create a new `ReceiptExtractor` with the given native to eth ratio. - pub fn new(native_to_eth_ratio: u32) -> Self { - Self { native_to_eth_ratio } + pub fn new( + native_to_eth_ratio: u32, + earliest_receipt_block: Option<SubstrateBlockNumber>, + ) -> Self { + Self { native_to_eth_ratio, earliest_receipt_block } } /// Extract a [`TransactionSigned`] and a [`ReceiptInfo`] and from an extrinsic. - pub async fn extract_from_extrinsic( + async fn extract_from_extrinsic( &self, block: &SubstrateBlock, ext: subxt::blocks::ExtrinsicDetails<SrcChainConfig, subxt::OnlineClient<SrcChainConfig>>, @@ -139,6 +144,13 @@ impl ReceiptExtractor { &self, block: &SubstrateBlock, ) -> Result<Vec<(TransactionSigned, ReceiptInfo)>, ClientError> { + if let Some(earliest_receipt_block) = self.earliest_receipt_block { + if block.number() < earliest_receipt_block { + log::trace!(target: LOG_TARGET, "Block number {block_number} is less than earliest receipt block {earliest_receipt_block}. Skipping.", block_number = block.number(), earliest_receipt_block = earliest_receipt_block); + return Ok(vec![]); + } + } + // Filter extrinsics from pallet_revive let extrinsics = block.extrinsics().await.inspect_err(|err| { log::debug!(target: LOG_TARGET, "Error fetching for #{:?} extrinsics: {err:?}", block.number()); diff --git a/substrate/frame/revive/rpc/src/receipt_provider/db.rs b/substrate/frame/revive/rpc/src/receipt_provider/db.rs index c471d009022..2a20eaa411f 100644 --- a/substrate/frame/revive/rpc/src/receipt_provider/db.rs +++ b/substrate/frame/revive/rpc/src/receipt_provider/db.rs @@ -410,7 +410,7 @@ mod tests { DBReceiptProvider { pool, block_provider: Arc::new(MockBlockInfoProvider {}), - receipt_extractor: ReceiptExtractor::new(1_000_000), + receipt_extractor: ReceiptExtractor::new(1_000_000, None), prune_old_blocks: true, } } -- GitLab