From 39c3bb117da537e8c55c0070328eab5d82aeebb5 Mon Sep 17 00:00:00 2001 From: Alexandru Cihodaru Date: Wed, 5 Nov 2025 13:17:40 +0200 Subject: [PATCH 1/7] Notify when there is a new best block Modify the tx_notifier to send the block hash to the subscribers instead of all transactions hashes. Modify the send_raw_transaction to look for the transaction hash in the transactions field of the evm block. Signed-off-by: Alexandru Cihodaru --- substrate/frame/revive/rpc/src/client.rs | 30 +++++++++++-------- substrate/frame/revive/rpc/src/lib.rs | 12 ++++++-- .../frame/revive/src/evm/api/rpc_types_gen.rs | 8 +++++ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/substrate/frame/revive/rpc/src/client.rs b/substrate/frame/revive/rpc/src/client.rs index 75b49495cabc1..08c619d4294a4 100644 --- a/substrate/frame/revive/rpc/src/client.rs +++ b/substrate/frame/revive/rpc/src/client.rs @@ -172,9 +172,8 @@ pub struct Client { max_block_weight: Weight, /// Whether the node has automine enabled. automine: bool, - /// A notifier, that informs subscribers of new transaction hashes that are included in a - /// block, when automine is enabled. - tx_notifier: Option>, + /// A notifier, that informs subscribers of new best blocks. + block_notifier: Option>, /// A lock to ensure only one subscription can perform write operations at a time. subscription_lock: Arc>, } @@ -247,13 +246,18 @@ impl Client { chain_id, max_block_weight, automine, - tx_notifier: automine.then(|| tokio::sync::broadcast::channel::(10).0), + block_notifier: automine.then(|| tokio::sync::broadcast::channel::(10).0), subscription_lock: Arc::new(Mutex::new(())), }; Ok(client) } + /// Creates a block notifier instance. + pub fn create_block_notifier(&mut self) { + self.block_notifier = Some(tokio::sync::broadcast::channel::(10).0); + } + /// Subscribe to past blocks executing the callback for each block in `range`. async fn subscribe_past_blocks( &self, @@ -349,7 +353,8 @@ impl Client { ) -> Result<(), ClientError> { log::info!(target: LOG_TARGET, "🔌 Subscribing to new blocks ({subscription_type:?})"); self.subscribe_new_blocks(subscription_type, |block| async { - let evm_block = self.runtime_api(block.hash()).eth_block().await?; + let hash = block.hash(); + let evm_block = self.runtime_api(hash).eth_block().await?; let (_, receipts): (Vec<_>, Vec<_>) = self .receipt_provider .insert_block_receipts(&block, &evm_block.hash) @@ -361,11 +366,10 @@ impl Client { self.fee_history_provider.update_fee_history(&evm_block, &receipts).await; // Only broadcast for best blocks to avoid duplicate notifications. - match (subscription_type, &self.tx_notifier) { - (SubscriptionType::BestBlocks, Some(sender)) if sender.receiver_count() > 0 => - for receipt in &receipts { - let _ = sender.send(receipt.transaction_hash); - }, + match (subscription_type, &self.block_notifier) { + (SubscriptionType::BestBlocks, Some(sender)) if sender.receiver_count() > 0 => { + let _ = sender.send(hash); + }, _ => {}, } Ok(()) @@ -742,9 +746,9 @@ impl Client { self.max_block_weight } - /// Get the block notifier, if automine is enabled. - pub fn tx_notifier(&self) -> Option> { - self.tx_notifier.clone() + /// Get the block notifier. + pub fn block_notifier(&self) -> Option> { + self.block_notifier.clone() } /// Get the logs matching the given filter. diff --git a/substrate/frame/revive/rpc/src/lib.rs b/substrate/frame/revive/rpc/src/lib.rs index d742ad937f3b9..572143d88240c 100644 --- a/substrate/frame/revive/rpc/src/lib.rs +++ b/substrate/frame/revive/rpc/src/lib.rs @@ -169,7 +169,7 @@ impl EthRpcServer for EthRpcServerImpl { let call = subxt_client::tx().revive().eth_transact(transaction.0); // Subscribe to new block only when automine is enabled. - let receiver = self.client.tx_notifier().map(|sender| sender.subscribe()); + let receiver = self.client.block_notifier().map(|sender| sender.subscribe()); // Submit the transaction let substrate_hash = self.client.submit(call).await.map_err(|err| { @@ -183,8 +183,14 @@ impl EthRpcServer for EthRpcServerImpl { if let Some(mut receiver) = receiver { if let Err(err) = tokio::time::timeout(Duration::from_millis(500), async { loop { - if let Ok(mined_hash) = receiver.recv().await { - if mined_hash == hash { + if let Ok(block_hash) = receiver.recv().await { + let Ok(Some(block)) = self.client.block_by_hash(&block_hash).await else { + continue + }; + let Some(evm_block) = self.client.evm_block(block, false).await else { + continue + }; + if evm_block.transactions.transaction_present(hash) { log::debug!(target: LOG_TARGET, "{hash:} was included in a block"); break; } diff --git a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs index 11807ea438b76..2a1616f0eaaa1 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs @@ -540,6 +540,14 @@ impl HashesOrTransactionInfos { pub fn is_empty(&self) -> bool { self.len() == 0 } + + pub fn transaction_present(&self, hash: H256) -> bool { + match self { + HashesOrTransactionInfos::Hashes(hashes) => hashes.iter().any(|h256| *h256 == hash), + HashesOrTransactionInfos::TransactionInfos(transaction_infos) => + transaction_infos.iter().any(|ti| ti.hash == hash), + } + } } /// log From e1d7be4d905904f39a4cabe740db38d8fb5f0e7b Mon Sep 17 00:00:00 2001 From: "cmd[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 6 Nov 2025 08:52:05 +0000 Subject: [PATCH 2/7] Update from github-actions[bot] running command 'prdoc' --- prdoc/pr_10220.prdoc | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 prdoc/pr_10220.prdoc diff --git a/prdoc/pr_10220.prdoc b/prdoc/pr_10220.prdoc new file mode 100644 index 0000000000000..58466b736ae67 --- /dev/null +++ b/prdoc/pr_10220.prdoc @@ -0,0 +1,13 @@ +title: Notify when there is a new best block +doc: +- audience: Todo + description: |- + # Description + + Modify the tx_notifier to send the block hash to the subscribers instead of all transactions hashes. + Modify the send_raw_transaction to look for the transaction hash in the transactions field of the evm block. +crates: +- name: pallet-revive-eth-rpc + bump: major +- name: pallet-revive + bump: major From d34f7d3b54ac176387d8149e42f50e206383a6cd Mon Sep 17 00:00:00 2001 From: "cmd[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 6 Nov 2025 09:06:08 +0000 Subject: [PATCH 3/7] Update from github-actions[bot] running command 'prdoc --audience runtime_dev --bump patch --force' --- prdoc/pr_10220.prdoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prdoc/pr_10220.prdoc b/prdoc/pr_10220.prdoc index 58466b736ae67..3995080855a46 100644 --- a/prdoc/pr_10220.prdoc +++ b/prdoc/pr_10220.prdoc @@ -1,6 +1,6 @@ title: Notify when there is a new best block doc: -- audience: Todo +- audience: Runtime Dev description: |- # Description @@ -8,6 +8,6 @@ doc: Modify the send_raw_transaction to look for the transaction hash in the transactions field of the evm block. crates: - name: pallet-revive-eth-rpc - bump: major + bump: patch - name: pallet-revive - bump: major + bump: patch From 59f44010b48d5674ee50c966c172999f6313aab7 Mon Sep 17 00:00:00 2001 From: Alexandru Cihodaru Date: Thu, 6 Nov 2025 13:35:54 +0200 Subject: [PATCH 4/7] implement feedback Signed-off-by: Alexandru Cihodaru --- substrate/frame/revive/rpc/src/client.rs | 7 +++++-- substrate/frame/revive/rpc/src/lib.rs | 4 +++- substrate/frame/revive/src/evm/api/rpc_types_gen.rs | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/substrate/frame/revive/rpc/src/client.rs b/substrate/frame/revive/rpc/src/client.rs index 08c619d4294a4..6b1d84253762c 100644 --- a/substrate/frame/revive/rpc/src/client.rs +++ b/substrate/frame/revive/rpc/src/client.rs @@ -134,6 +134,8 @@ pub enum ClientError { const LOG_TARGET: &str = "eth-rpc::client"; const REVERT_CODE: i32 = 3; + +const NOTIFIER_CAPACITY: usize = 16; impl From for ErrorObjectOwned { fn from(err: ClientError) -> Self { match err { @@ -246,7 +248,8 @@ impl Client { chain_id, max_block_weight, automine, - block_notifier: automine.then(|| tokio::sync::broadcast::channel::(10).0), + block_notifier: automine + .then(|| tokio::sync::broadcast::channel::(NOTIFIER_CAPACITY).0), subscription_lock: Arc::new(Mutex::new(())), }; @@ -746,7 +749,7 @@ impl Client { self.max_block_weight } - /// Get the block notifier. + /// Get the block notifier, if automine is enabled war Self::create_block_notifier was called. pub fn block_notifier(&self) -> Option> { self.block_notifier.clone() } diff --git a/substrate/frame/revive/rpc/src/lib.rs b/substrate/frame/revive/rpc/src/lib.rs index 572143d88240c..6dc1f397fefaf 100644 --- a/substrate/frame/revive/rpc/src/lib.rs +++ b/substrate/frame/revive/rpc/src/lib.rs @@ -185,12 +185,14 @@ impl EthRpcServer for EthRpcServerImpl { loop { if let Ok(block_hash) = receiver.recv().await { let Ok(Some(block)) = self.client.block_by_hash(&block_hash).await else { + log::debug!(target: LOG_TARGET, "Could not find the block with the received hash: {hash:?}."); continue }; let Some(evm_block) = self.client.evm_block(block, false).await else { + log::debug!(target: LOG_TARGET, "Failed to get the EVM block for substrate block with hash: {hash:?}"); continue }; - if evm_block.transactions.transaction_present(hash) { + if evm_block.transactions.contains_tx(hash) { log::debug!(target: LOG_TARGET, "{hash:} was included in a block"); break; } diff --git a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs index 2a1616f0eaaa1..c3260b3877586 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs @@ -541,7 +541,7 @@ impl HashesOrTransactionInfos { self.len() == 0 } - pub fn transaction_present(&self, hash: H256) -> bool { + pub fn contains_tx(&self, hash: H256) -> bool { match self { HashesOrTransactionInfos::Hashes(hashes) => hashes.iter().any(|h256| *h256 == hash), HashesOrTransactionInfos::TransactionInfos(transaction_infos) => From 2618b60872c13021070d11beac3f74836d746cc9 Mon Sep 17 00:00:00 2001 From: Alexandru Cihodaru Date: Thu, 6 Nov 2025 13:40:36 +0200 Subject: [PATCH 5/7] fix prdoc Signed-off-by: Alexandru Cihodaru --- prdoc/pr_10220.prdoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prdoc/pr_10220.prdoc b/prdoc/pr_10220.prdoc index 3995080855a46..c554dd4634b2b 100644 --- a/prdoc/pr_10220.prdoc +++ b/prdoc/pr_10220.prdoc @@ -8,6 +8,6 @@ doc: Modify the send_raw_transaction to look for the transaction hash in the transactions field of the evm block. crates: - name: pallet-revive-eth-rpc - bump: patch + bump: major - name: pallet-revive - bump: patch + bump: minor From dbba1461e880b821673987e0b18bc841ef192182 Mon Sep 17 00:00:00 2001 From: Alexandru Cihodaru Date: Thu, 6 Nov 2025 13:42:19 +0200 Subject: [PATCH 6/7] fix typo Signed-off-by: Alexandru Cihodaru --- substrate/frame/revive/rpc/src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/revive/rpc/src/client.rs b/substrate/frame/revive/rpc/src/client.rs index 6b1d84253762c..b50008a563a9b 100644 --- a/substrate/frame/revive/rpc/src/client.rs +++ b/substrate/frame/revive/rpc/src/client.rs @@ -749,7 +749,7 @@ impl Client { self.max_block_weight } - /// Get the block notifier, if automine is enabled war Self::create_block_notifier was called. + /// Get the block notifier, if automine is enabled or Self::create_block_notifier was called. pub fn block_notifier(&self) -> Option> { self.block_notifier.clone() } From 4ec1be446db6e932b53709b364e478ae62739b49 Mon Sep 17 00:00:00 2001 From: Alexandru Cihodaru Date: Thu, 6 Nov 2025 13:58:44 +0200 Subject: [PATCH 7/7] feedback v2 Signed-off-by: Alexandru Cihodaru --- substrate/frame/revive/rpc/src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/revive/rpc/src/client.rs b/substrate/frame/revive/rpc/src/client.rs index b50008a563a9b..66d6ae09d47b4 100644 --- a/substrate/frame/revive/rpc/src/client.rs +++ b/substrate/frame/revive/rpc/src/client.rs @@ -258,7 +258,7 @@ impl Client { /// Creates a block notifier instance. pub fn create_block_notifier(&mut self) { - self.block_notifier = Some(tokio::sync::broadcast::channel::(10).0); + self.block_notifier = Some(tokio::sync::broadcast::channel::(NOTIFIER_CAPACITY).0); } /// Subscribe to past blocks executing the callback for each block in `range`.