From dbeea184b9afbb41e5ef12f88c4b2db7e80cd45f Mon Sep 17 00:00:00 2001 From: PG Herveou Date: Thu, 14 Nov 2024 21:40:58 +0100 Subject: [PATCH] [pallet-revive] set logs_bloom (#6460) Set the logs_bloom in the transaction receipt --------- Co-authored-by: GitHub Action Co-authored-by: Cyrill Leutwiler --- prdoc/pr_6460.prdoc | 9 ++ .../frame/revive/rpc/examples/js/src/lib.ts | 2 +- substrate/frame/revive/rpc/src/client.rs | 20 ++-- substrate/frame/revive/src/evm/api.rs | 2 + .../frame/revive/src/evm/api/rpc_types.rs | 102 +++++++++++++++++- .../frame/revive/src/evm/api/rpc_types_gen.rs | 7 +- 6 files changed, 126 insertions(+), 16 deletions(-) create mode 100644 prdoc/pr_6460.prdoc diff --git a/prdoc/pr_6460.prdoc b/prdoc/pr_6460.prdoc new file mode 100644 index 000000000000..e1fd1a740228 --- /dev/null +++ b/prdoc/pr_6460.prdoc @@ -0,0 +1,9 @@ +title: '[pallet-revive] set logs_bloom' +doc: +- audience: Runtime Dev + description: Set the logs_bloom in the transaction receipt +crates: +- name: pallet-revive-eth-rpc + bump: minor +- name: pallet-revive + bump: minor diff --git a/substrate/frame/revive/rpc/examples/js/src/lib.ts b/substrate/frame/revive/rpc/examples/js/src/lib.ts index 7f6fc19aea75..40f2d6f05824 100644 --- a/substrate/frame/revive/rpc/examples/js/src/lib.ts +++ b/substrate/frame/revive/rpc/examples/js/src/lib.ts @@ -54,7 +54,7 @@ if (geth) { await new Promise((resolve) => setTimeout(resolve, 500)) } -const provider = new JsonRpcProvider( +export const provider = new JsonRpcProvider( westend ? 'https://westend-asset-hub-eth-rpc.polkadot.io' : 'http://localhost:8545' ) diff --git a/substrate/frame/revive/rpc/src/client.rs b/substrate/frame/revive/rpc/src/client.rs index bc4f59b5e26e..b7efae15c4aa 100644 --- a/substrate/frame/revive/rpc/src/client.rs +++ b/substrate/frame/revive/rpc/src/client.rs @@ -315,8 +315,8 @@ impl ClientInner { let event = event_details.as_event::().ok()??; Some(Log { - address: Some(event.contract), - topics: Some(event.topics), + address: event.contract, + topics: event.topics, data: Some(event.data.into()), block_number: Some(block_number), transaction_hash, @@ -329,20 +329,20 @@ impl ClientInner { log::debug!(target: LOG_TARGET, "Adding receipt for tx hash: {transaction_hash:?} - block: {block_number:?}"); - let receipt = ReceiptInfo { + let receipt = ReceiptInfo::new( block_hash, block_number, contract_address, from, logs, - to: tx.transaction_legacy_unsigned.to, - effective_gas_price: gas_price, - gas_used: gas_used.into(), - status: Some(if success { U256::one() } else { U256::zero() }), + tx.transaction_legacy_unsigned.to, + gas_price, + gas_used.into(), + success, transaction_hash, - transaction_index: transaction_index.into(), - ..Default::default() - }; + transaction_index.into(), + tx.transaction_legacy_unsigned.r#type.as_byte() + ); Ok::<_, ClientError>((receipt.transaction_hash, (tx.into(), receipt))) }) diff --git a/substrate/frame/revive/src/evm/api.rs b/substrate/frame/revive/src/evm/api.rs index fe18c8735bed..8185a2c8f6f8 100644 --- a/substrate/frame/revive/src/evm/api.rs +++ b/substrate/frame/revive/src/evm/api.rs @@ -25,7 +25,9 @@ pub use rlp; mod type_id; pub use type_id::*; +#[cfg(feature = "std")] mod rpc_types; + mod rpc_types_gen; pub use rpc_types_gen::*; diff --git a/substrate/frame/revive/src/evm/api/rpc_types.rs b/substrate/frame/revive/src/evm/api/rpc_types.rs index 84390563d05a..dd1a2642724d 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. //! Utility impl for the RPC types. -use super::{ReceiptInfo, TransactionInfo, TransactionSigned}; +use super::*; use sp_core::U256; impl TransactionInfo { @@ -33,8 +33,108 @@ impl TransactionInfo { } impl ReceiptInfo { + /// Initialize a new Receipt + pub fn new( + block_hash: H256, + block_number: U256, + contract_address: Option
, + from: Address, + logs: Vec, + to: Option
, + effective_gas_price: U256, + gas_used: U256, + success: bool, + transaction_hash: H256, + transaction_index: U256, + r#type: Byte, + ) -> Self { + let logs_bloom = Self::logs_bloom(&logs); + ReceiptInfo { + block_hash, + block_number, + contract_address, + from, + logs, + logs_bloom, + to, + effective_gas_price, + gas_used, + status: Some(if success { U256::one() } else { U256::zero() }), + transaction_hash, + transaction_index, + r#type: Some(r#type), + ..Default::default() + } + } + /// Returns `true` if the transaction was successful. pub fn is_success(&self) -> bool { self.status.map_or(false, |status| status == U256::one()) } + + /// Calculate receipt logs bloom. + fn logs_bloom(logs: &[Log]) -> Bytes256 { + let mut bloom = [0u8; 256]; + for log in logs { + m3_2048(&mut bloom, &log.address.as_ref()); + for topic in &log.topics { + m3_2048(&mut bloom, topic.as_ref()); + } + } + bloom.into() + } +} +/// Specialised Bloom filter that sets three bits out of 2048, given an +/// arbitrary byte sequence. +/// +/// See Section 4.4.1 "Transaction Receipt" of the [Ethereum Yellow Paper][ref]. +/// +/// [ref]: https://ethereum.github.io/yellowpaper/paper.pdf +fn m3_2048(bloom: &mut [u8; 256], bytes: &[u8]) { + let hash = sp_core::keccak_256(bytes); + for i in [0, 2, 4] { + let bit = (hash[i + 1] as usize + ((hash[i] as usize) << 8)) & 0x7FF; + bloom[256 - 1 - bit / 8] |= 1 << (bit % 8); + } +} + +#[test] +fn logs_bloom_works() { + let receipt: ReceiptInfo = serde_json::from_str( + r#" + { + "blockHash": "0x835ee379aaabf4802a22a93ad8164c02bbdde2cc03d4552d5c642faf4e09d1f3", + "blockNumber": "0x2", + "contractAddress": null, + "cumulativeGasUsed": "0x5d92", + "effectiveGasPrice": "0x2dcd5c2d", + "from": "0xb4f1f9ecfe5a28633a27f57300bda217e99b8969", + "gasUsed": "0x5d92", + "logs": [ + { + "address": "0x82bdb002b9b1f36c42df15fbdc6886abcb2ab31d", + "topics": [ + "0x1585375487296ff2f0370daeec4214074a032b31af827c12622fa9a58c16c7d0", + "0x000000000000000000000000b4f1f9ecfe5a28633a27f57300bda217e99b8969" + ], + "data": "0x00000000000000000000000000000000000000000000000000000000000030390000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20776f726c64000000000000000000000000000000000000000000", + "blockNumber": "0x2", + "transactionHash": "0xad0075127962bdf73d787f2944bdb5f351876f23c35e6a48c1f5b6463a100af4", + "transactionIndex": "0x0", + "blockHash": "0x835ee379aaabf4802a22a93ad8164c02bbdde2cc03d4552d5c642faf4e09d1f3", + "logIndex": "0x0", + "removed": false + } + ], + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000008000000000000000000000000000000000000000000000000800000000040000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000004000000000000000800000000000000000080000000000000000000000000000000000000000000", + "status": "0x1", + "to": "0x82bdb002b9b1f36c42df15fbdc6886abcb2ab31d", + "transactionHash": "0xad0075127962bdf73d787f2944bdb5f351876f23c35e6a48c1f5b6463a100af4", + "transactionIndex": "0x0", + "type": "0x2" + } + "#, + ) + .unwrap(); + assert_eq!(receipt.logs_bloom, ReceiptInfo::logs_bloom(&receipt.logs)); } 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 1f391ae846a5..48045a6acc86 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs @@ -375,8 +375,7 @@ impl Default for H256OrTransactionInfo { )] pub struct Log { /// address - #[serde(skip_serializing_if = "Option::is_none")] - pub address: Option
, + pub address: Address, /// block hash #[serde(rename = "blockHash", skip_serializing_if = "Option::is_none")] pub block_hash: Option, @@ -393,8 +392,8 @@ pub struct Log { #[serde(skip_serializing_if = "Option::is_none")] pub removed: Option, /// topics - #[serde(skip_serializing_if = "Option::is_none")] - pub topics: Option>, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub topics: Vec, /// transaction hash #[serde(rename = "transactionHash")] pub transaction_hash: H256,