Skip to content

Commit

Permalink
[pallet-revive] set logs_bloom (#6460)
Browse files Browse the repository at this point in the history
Set the logs_bloom in the transaction receipt

---------

Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: Cyrill Leutwiler <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2024
1 parent 5da4063 commit dbeea18
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 16 deletions.
9 changes: 9 additions & 0 deletions prdoc/pr_6460.prdoc
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion substrate/frame/revive/rpc/examples/js/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)

Expand Down
20 changes: 10 additions & 10 deletions substrate/frame/revive/rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ impl ClientInner {
let event = event_details.as_event::<ContractEmitted>().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,
Expand All @@ -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)))
})
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/revive/src/evm/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
102 changes: 101 additions & 1 deletion substrate/frame/revive/src/evm/api/rpc_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -33,8 +33,108 @@ impl TransactionInfo {
}

impl ReceiptInfo {
/// Initialize a new Receipt
pub fn new(
block_hash: H256,
block_number: U256,
contract_address: Option<Address>,
from: Address,
logs: Vec<Log>,
to: Option<Address>,
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));
}
7 changes: 3 additions & 4 deletions substrate/frame/revive/src/evm/api/rpc_types_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ impl Default for H256OrTransactionInfo {
)]
pub struct Log {
/// address
#[serde(skip_serializing_if = "Option::is_none")]
pub address: Option<Address>,
pub address: Address,
/// block hash
#[serde(rename = "blockHash", skip_serializing_if = "Option::is_none")]
pub block_hash: Option<H256>,
Expand All @@ -393,8 +392,8 @@ pub struct Log {
#[serde(skip_serializing_if = "Option::is_none")]
pub removed: Option<bool>,
/// topics
#[serde(skip_serializing_if = "Option::is_none")]
pub topics: Option<Vec<H256>>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub topics: Vec<H256>,
/// transaction hash
#[serde(rename = "transactionHash")]
pub transaction_hash: H256,
Expand Down

0 comments on commit dbeea18

Please sign in to comment.