From 0cd8c5f32db8408dd77e76a65d96053a24c5bfec Mon Sep 17 00:00:00 2001 From: Renato Dinhani <101204870+dinhani-cw@users.noreply.github.com> Date: Thu, 26 Sep 2024 15:01:43 -0300 Subject: [PATCH] refactor: document and rename topics method (#1735) --- src/eth/primitives/execution.rs | 6 +++--- src/eth/primitives/log.rs | 10 ++++++---- src/eth/primitives/log_filter.rs | 2 +- src/eth/primitives/log_mined.rs | 8 ++++---- src/eth/primitives/logs_bloom.rs | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/eth/primitives/execution.rs b/src/eth/primitives/execution.rs index b38dc106f..d70a17955 100644 --- a/src/eth/primitives/execution.rs +++ b/src/eth/primitives/execution.rs @@ -131,18 +131,18 @@ impl EvmExecution { // compare logs pairs for (log_index, (execution_log, receipt_log)) in self.logs.iter().zip(&receipt.logs).enumerate() { // compare log topics length - if execution_log.topics().len() != receipt_log.topics.len() { + if execution_log.topics_non_empty().len() != receipt_log.topics.len() { return log_and_err!(format!( "log topics length mismatch | hash={} log_index={} execution={} receipt={}", receipt.hash(), log_index, - execution_log.topics().len(), + execution_log.topics_non_empty().len(), receipt_log.topics.len(), )); } // compare log topics content - for (topic_index, (execution_log_topic, receipt_log_topic)) in execution_log.topics().iter().zip(&receipt_log.topics).enumerate() { + for (topic_index, (execution_log_topic, receipt_log_topic)) in execution_log.topics_non_empty().iter().zip(&receipt_log.topics).enumerate() { if execution_log_topic.as_ref() != receipt_log_topic.as_ref() { return log_and_err!(format!( "log topic content mismatch | hash={} log_index={} topic_index={} execution={} receipt={:#x}", diff --git a/src/eth/primitives/log.rs b/src/eth/primitives/log.rs index 74d9e7878..ed6910363 100644 --- a/src/eth/primitives/log.rs +++ b/src/eth/primitives/log.rs @@ -23,12 +23,14 @@ pub struct Log { } impl Log { - pub fn topics(&self) -> Vec { - self.topics_array().into_iter().flatten().collect() + /// Returns all topics in the log. + pub fn topics(&self) -> [Option; 4] { + [self.topic0, self.topic1, self.topic2, self.topic3] } - pub fn topics_array(&self) -> [Option; 4] { - [self.topic0, self.topic1, self.topic2, self.topic3] + /// Returns all non-empty topics in the log. + pub fn topics_non_empty(&self) -> Vec { + self.topics().into_iter().flatten().collect() } } diff --git a/src/eth/primitives/log_filter.rs b/src/eth/primitives/log_filter.rs index 9dc38b055..2680bcf12 100644 --- a/src/eth/primitives/log_filter.rs +++ b/src/eth/primitives/log_filter.rs @@ -37,7 +37,7 @@ impl LogFilter { } let filter_topics = &self.original_input.topics; - let log_topics = log.log.topics_array(); + let log_topics = log.log.topics(); // (https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getlogs) // Matching rules for filtering topics in `eth_getLogs`: diff --git a/src/eth/primitives/log_mined.rs b/src/eth/primitives/log_mined.rs index 5cd5f86f3..2e4e16ca4 100644 --- a/src/eth/primitives/log_mined.rs +++ b/src/eth/primitives/log_mined.rs @@ -40,9 +40,9 @@ impl LogMined { &self.log.address } - /// Returns the topics emitted in the log. - pub fn topics(&self) -> Vec { - self.log.topics() + /// Returns all non-empty topics in the log. + pub fn topics_non_empty(&self) -> Vec { + self.log.topics_non_empty() } /// Serializes itself to JSON-RPC log format. @@ -77,7 +77,7 @@ impl From for EthersLog { Self { // log address: value.log.address.into(), - topics: value.topics().into_iter().map_into().collect_vec(), + topics: value.topics_non_empty().into_iter().map_into().collect_vec(), data: value.log.data.into(), log_index: Some(value.log_index.into()), removed: Some(false), diff --git a/src/eth/primitives/logs_bloom.rs b/src/eth/primitives/logs_bloom.rs index 60c62cd33..5df636fd3 100644 --- a/src/eth/primitives/logs_bloom.rs +++ b/src/eth/primitives/logs_bloom.rs @@ -21,7 +21,7 @@ impl LogsBloom { pub fn accrue_log(&mut self, log: &Log) { self.accrue(ethereum_types::BloomInput::Raw(log.address.as_ref())); - for topic in log.topics() { + for topic in log.topics_non_empty() { self.accrue(ethereum_types::BloomInput::Raw(topic.as_ref())); } }