Skip to content

Commit

Permalink
refactor: document and rename topics method (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored Sep 26, 2024
1 parent 39478fa commit 0cd8c5f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/eth/primitives/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
10 changes: 6 additions & 4 deletions src/eth/primitives/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ pub struct Log {
}

impl Log {
pub fn topics(&self) -> Vec<LogTopic> {
self.topics_array().into_iter().flatten().collect()
/// Returns all topics in the log.
pub fn topics(&self) -> [Option<LogTopic>; 4] {
[self.topic0, self.topic1, self.topic2, self.topic3]
}

pub fn topics_array(&self) -> [Option<LogTopic>; 4] {
[self.topic0, self.topic1, self.topic2, self.topic3]
/// Returns all non-empty topics in the log.
pub fn topics_non_empty(&self) -> Vec<LogTopic> {
self.topics().into_iter().flatten().collect()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/log_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
8 changes: 4 additions & 4 deletions src/eth/primitives/log_mined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl LogMined {
&self.log.address
}

/// Returns the topics emitted in the log.
pub fn topics(&self) -> Vec<LogTopic> {
self.log.topics()
/// Returns all non-empty topics in the log.
pub fn topics_non_empty(&self) -> Vec<LogTopic> {
self.log.topics_non_empty()
}

/// Serializes itself to JSON-RPC log format.
Expand Down Expand Up @@ -77,7 +77,7 @@ impl From<LogMined> 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),
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/logs_bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Expand Down

0 comments on commit 0cd8c5f

Please sign in to comment.