Skip to content

Commit cecc5b4

Browse files
pgherveouactions-userxermicus
authored andcommitted
[pallet-revive] set logs_bloom (paritytech#6460)
Set the logs_bloom in the transaction receipt --------- Co-authored-by: GitHub Action <[email protected]> Co-authored-by: Cyrill Leutwiler <[email protected]>
1 parent 1ea80a3 commit cecc5b4

6 files changed

Lines changed: 126 additions & 16 deletions

File tree

prdoc/pr_6460.prdoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: '[pallet-revive] set logs_bloom'
2+
doc:
3+
- audience: Runtime Dev
4+
description: Set the logs_bloom in the transaction receipt
5+
crates:
6+
- name: pallet-revive-eth-rpc
7+
bump: minor
8+
- name: pallet-revive
9+
bump: minor

substrate/frame/revive/rpc/examples/js/src/lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ if (geth) {
5454
await new Promise((resolve) => setTimeout(resolve, 500))
5555
}
5656

57-
const provider = new JsonRpcProvider(
57+
export const provider = new JsonRpcProvider(
5858
westend ? 'https://westend-asset-hub-eth-rpc.polkadot.io' : 'http://localhost:8545'
5959
)
6060

substrate/frame/revive/rpc/src/client.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ impl ClientInner {
315315
let event = event_details.as_event::<ContractEmitted>().ok()??;
316316

317317
Some(Log {
318-
address: Some(event.contract),
319-
topics: Some(event.topics),
318+
address: event.contract,
319+
topics: event.topics,
320320
data: Some(event.data.into()),
321321
block_number: Some(block_number),
322322
transaction_hash,
@@ -329,20 +329,20 @@ impl ClientInner {
329329

330330

331331
log::debug!(target: LOG_TARGET, "Adding receipt for tx hash: {transaction_hash:?} - block: {block_number:?}");
332-
let receipt = ReceiptInfo {
332+
let receipt = ReceiptInfo::new(
333333
block_hash,
334334
block_number,
335335
contract_address,
336336
from,
337337
logs,
338-
to: tx.transaction_legacy_unsigned.to,
339-
effective_gas_price: gas_price,
340-
gas_used: gas_used.into(),
341-
status: Some(if success { U256::one() } else { U256::zero() }),
338+
tx.transaction_legacy_unsigned.to,
339+
gas_price,
340+
gas_used.into(),
341+
success,
342342
transaction_hash,
343-
transaction_index: transaction_index.into(),
344-
..Default::default()
345-
};
343+
transaction_index.into(),
344+
tx.transaction_legacy_unsigned.r#type.as_byte()
345+
);
346346

347347
Ok::<_, ClientError>((receipt.transaction_hash, (tx.into(), receipt)))
348348
})

substrate/frame/revive/src/evm/api.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ pub use rlp;
2525
mod type_id;
2626
pub use type_id::*;
2727

28+
#[cfg(feature = "std")]
2829
mod rpc_types;
30+
2931
mod rpc_types_gen;
3032
pub use rpc_types_gen::*;
3133

substrate/frame/revive/src/evm/api/rpc_types.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717
//! Utility impl for the RPC types.
18-
use super::{ReceiptInfo, TransactionInfo, TransactionSigned};
18+
use super::*;
1919
use sp_core::U256;
2020

2121
impl TransactionInfo {
@@ -33,8 +33,108 @@ impl TransactionInfo {
3333
}
3434

3535
impl ReceiptInfo {
36+
/// Initialize a new Receipt
37+
pub fn new(
38+
block_hash: H256,
39+
block_number: U256,
40+
contract_address: Option<Address>,
41+
from: Address,
42+
logs: Vec<Log>,
43+
to: Option<Address>,
44+
effective_gas_price: U256,
45+
gas_used: U256,
46+
success: bool,
47+
transaction_hash: H256,
48+
transaction_index: U256,
49+
r#type: Byte,
50+
) -> Self {
51+
let logs_bloom = Self::logs_bloom(&logs);
52+
ReceiptInfo {
53+
block_hash,
54+
block_number,
55+
contract_address,
56+
from,
57+
logs,
58+
logs_bloom,
59+
to,
60+
effective_gas_price,
61+
gas_used,
62+
status: Some(if success { U256::one() } else { U256::zero() }),
63+
transaction_hash,
64+
transaction_index,
65+
r#type: Some(r#type),
66+
..Default::default()
67+
}
68+
}
69+
3670
/// Returns `true` if the transaction was successful.
3771
pub fn is_success(&self) -> bool {
3872
self.status.map_or(false, |status| status == U256::one())
3973
}
74+
75+
/// Calculate receipt logs bloom.
76+
fn logs_bloom(logs: &[Log]) -> Bytes256 {
77+
let mut bloom = [0u8; 256];
78+
for log in logs {
79+
m3_2048(&mut bloom, &log.address.as_ref());
80+
for topic in &log.topics {
81+
m3_2048(&mut bloom, topic.as_ref());
82+
}
83+
}
84+
bloom.into()
85+
}
86+
}
87+
/// Specialised Bloom filter that sets three bits out of 2048, given an
88+
/// arbitrary byte sequence.
89+
///
90+
/// See Section 4.4.1 "Transaction Receipt" of the [Ethereum Yellow Paper][ref].
91+
///
92+
/// [ref]: https://ethereum.github.io/yellowpaper/paper.pdf
93+
fn m3_2048(bloom: &mut [u8; 256], bytes: &[u8]) {
94+
let hash = sp_core::keccak_256(bytes);
95+
for i in [0, 2, 4] {
96+
let bit = (hash[i + 1] as usize + ((hash[i] as usize) << 8)) & 0x7FF;
97+
bloom[256 - 1 - bit / 8] |= 1 << (bit % 8);
98+
}
99+
}
100+
101+
#[test]
102+
fn logs_bloom_works() {
103+
let receipt: ReceiptInfo = serde_json::from_str(
104+
r#"
105+
{
106+
"blockHash": "0x835ee379aaabf4802a22a93ad8164c02bbdde2cc03d4552d5c642faf4e09d1f3",
107+
"blockNumber": "0x2",
108+
"contractAddress": null,
109+
"cumulativeGasUsed": "0x5d92",
110+
"effectiveGasPrice": "0x2dcd5c2d",
111+
"from": "0xb4f1f9ecfe5a28633a27f57300bda217e99b8969",
112+
"gasUsed": "0x5d92",
113+
"logs": [
114+
{
115+
"address": "0x82bdb002b9b1f36c42df15fbdc6886abcb2ab31d",
116+
"topics": [
117+
"0x1585375487296ff2f0370daeec4214074a032b31af827c12622fa9a58c16c7d0",
118+
"0x000000000000000000000000b4f1f9ecfe5a28633a27f57300bda217e99b8969"
119+
],
120+
"data": "0x00000000000000000000000000000000000000000000000000000000000030390000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20776f726c64000000000000000000000000000000000000000000",
121+
"blockNumber": "0x2",
122+
"transactionHash": "0xad0075127962bdf73d787f2944bdb5f351876f23c35e6a48c1f5b6463a100af4",
123+
"transactionIndex": "0x0",
124+
"blockHash": "0x835ee379aaabf4802a22a93ad8164c02bbdde2cc03d4552d5c642faf4e09d1f3",
125+
"logIndex": "0x0",
126+
"removed": false
127+
}
128+
],
129+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000008000000000000000000000000000000000000000000000000800000000040000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000004000000000000000800000000000000000080000000000000000000000000000000000000000000",
130+
"status": "0x1",
131+
"to": "0x82bdb002b9b1f36c42df15fbdc6886abcb2ab31d",
132+
"transactionHash": "0xad0075127962bdf73d787f2944bdb5f351876f23c35e6a48c1f5b6463a100af4",
133+
"transactionIndex": "0x0",
134+
"type": "0x2"
135+
}
136+
"#,
137+
)
138+
.unwrap();
139+
assert_eq!(receipt.logs_bloom, ReceiptInfo::logs_bloom(&receipt.logs));
40140
}

substrate/frame/revive/src/evm/api/rpc_types_gen.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ impl Default for H256OrTransactionInfo {
375375
)]
376376
pub struct Log {
377377
/// address
378-
#[serde(skip_serializing_if = "Option::is_none")]
379-
pub address: Option<Address>,
378+
pub address: Address,
380379
/// block hash
381380
#[serde(rename = "blockHash", skip_serializing_if = "Option::is_none")]
382381
pub block_hash: Option<H256>,
@@ -393,8 +392,8 @@ pub struct Log {
393392
#[serde(skip_serializing_if = "Option::is_none")]
394393
pub removed: Option<bool>,
395394
/// topics
396-
#[serde(skip_serializing_if = "Option::is_none")]
397-
pub topics: Option<Vec<H256>>,
395+
#[serde(skip_serializing_if = "Vec::is_empty")]
396+
pub topics: Vec<H256>,
398397
/// transaction hash
399398
#[serde(rename = "transactionHash")]
400399
pub transaction_hash: H256,

0 commit comments

Comments
 (0)