Skip to content
Snippets Groups Projects
Unverified Commit 8cca727f authored by PG Herveou's avatar PG Herveou Committed by GitHub
Browse files

[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: default avatarcmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
parent 9015a0fc
Branches
No related merge requests found
Pipeline #515650 waiting for manual action with stages
in 34 minutes and 23 seconds
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
......@@ -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() })?,
......
......@@ -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());
......
......@@ -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,
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment