Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chain/ethereum: Do not clone MappingEventHandlers in match_and_decode #5244

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 45 additions & 55 deletions chain/ethereum/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,23 +404,18 @@ impl DataSource {
})
}

fn handlers_for_log(&self, log: &Log) -> Vec<MappingEventHandler> {
// Get signature from the log
let topic0 = match log.topics.get(0) {
Some(topic0) => topic0,
// Events without a topic should just be be ignored
None => return vec![],
};

self.mapping
.event_handlers
.iter()
.filter(|handler| *topic0 == handler.topic0())
.cloned()
.collect::<Vec<_>>()
fn handlers_for_log<'a>(
&'a self,
log: &'a Log,
) -> impl Iterator<Item = &'a MappingEventHandler> {
self.mapping.event_handlers.iter().filter(|handler| {
// Events without a topic should just be ignored. Making the RHS
// always `Some` ensures that
log.topics.first() == Some(&handler.topic0())
})
}

fn handler_for_call(&self, call: &EthereumCall) -> Result<Option<MappingCallHandler>, Error> {
fn handler_for_call(&self, call: &EthereumCall) -> Result<Option<&MappingCallHandler>, Error> {
// First four bytes of the input for the call are the first four
// bytes of hash of the function signature
ensure!(
Expand All @@ -430,55 +425,49 @@ impl DataSource {

let target_method_id = &call.input.0[..4];

Ok(self
.mapping
.call_handlers
.iter()
.find(move |handler| {
let fhash = keccak256(handler.function.as_bytes());
let actual_method_id = [fhash[0], fhash[1], fhash[2], fhash[3]];
target_method_id == actual_method_id
})
.cloned())
Ok(self.mapping.call_handlers.iter().find(move |handler| {
let fhash = keccak256(handler.function.as_bytes());
let actual_method_id = [fhash[0], fhash[1], fhash[2], fhash[3]];
target_method_id == actual_method_id
}))
}

fn handler_for_block(
&self,
trigger_type: &EthereumBlockTriggerType,
block: BlockNumber,
) -> Option<MappingBlockHandler> {
) -> Option<&MappingBlockHandler> {
match trigger_type {
// Start matches only initialization handlers with a `once` filter
EthereumBlockTriggerType::Start => self
.mapping
.block_handlers
.iter()
.find(move |handler| match handler.filter {
Some(BlockHandlerFilter::Once) => block == self.start_block,
_ => false,
})
.cloned(),
EthereumBlockTriggerType::Start => {
self.mapping
.block_handlers
.iter()
.find(move |handler| match handler.filter {
Some(BlockHandlerFilter::Once) => block == self.start_block,
_ => false,
})
}
// End matches all handlers without a filter or with a `polling` filter
EthereumBlockTriggerType::End => self
.mapping
.block_handlers
.iter()
.find(move |handler| match handler.filter {
Some(BlockHandlerFilter::Polling { every }) => {
let start_block = self.start_block;
let should_trigger = (block - start_block) % every.get() as i32 == 0;
should_trigger
}
None => true,
_ => false,
})
.cloned(),
EthereumBlockTriggerType::End => {
self.mapping
.block_handlers
.iter()
.find(move |handler| match handler.filter {
Some(BlockHandlerFilter::Polling { every }) => {
let start_block = self.start_block;
let should_trigger = (block - start_block) % every.get() as i32 == 0;
should_trigger
}
None => true,
_ => false,
})
}
EthereumBlockTriggerType::WithCallTo(_address) => self
.mapping
.block_handlers
.iter()
.find(move |handler| handler.filter == Some(BlockHandlerFilter::Call))
.cloned(),
.find(move |handler| handler.filter == Some(BlockHandlerFilter::Call)),
}
}

Expand Down Expand Up @@ -649,7 +638,7 @@ impl DataSource {
MappingTrigger::Block {
block: block.cheap_clone(),
},
handler.handler,
handler.handler.clone(),
block.block_ptr(),
block.timestamp(),
)))
Expand Down Expand Up @@ -751,15 +740,16 @@ impl DataSource {
"address" => format!("{}", &log.address),
"transaction" => format!("{}", &transaction.hash),
});
let handler = event_handler.handler.clone();
Ok(Some(TriggerWithHandler::<Chain>::new_with_logging_extras(
MappingTrigger::Log {
block: block.cheap_clone(),
transaction: Arc::new(transaction),
log: log,
log,
params,
receipt: receipt.map(|r| r.cheap_clone()),
},
event_handler.handler,
handler,
block.block_ptr(),
block.timestamp(),
logging_extras,
Expand Down Expand Up @@ -870,7 +860,7 @@ impl DataSource {
inputs,
outputs,
},
handler.handler,
handler.handler.clone(),
block.block_ptr(),
block.timestamp(),
logging_extras,
Expand Down
1 change: 1 addition & 0 deletions chain/ethereum/src/ethereum_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,7 @@ impl EthereumAdapterTrait for EthereumAdapter {
};

debug!(logger, "eth_call";
"fn" => &call.function.name,
"address" => hex::encode(call.address),
"data" => hex::encode(&call_data),
"block_hash" => call.block_ptr.hash_hex(),
Expand Down
Loading