Skip to content

Commit

Permalink
chain/ethereum: Avoid a clone for EthereumCallData in ABI conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
lutter committed Mar 7, 2025
1 parent 6bf741d commit 737014a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
22 changes: 5 additions & 17 deletions chain/ethereum/src/runtime/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ impl AscType for AscLogParamArray {
}
}

impl ToAscObj<AscLogParamArray> for Vec<ethabi::LogParam> {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscLogParamArray, HostExportError> {
let content: Result<Vec<_>, _> = self.iter().map(|x| asc_new(heap, x, gas)).collect();
let content = content?;
Ok(AscLogParamArray(Array::new(&content, heap, gas)?))
}
}

impl ToAscObj<AscLogParamArray> for &[ethabi::LogParam] {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
Expand Down Expand Up @@ -737,7 +725,7 @@ impl<'a> ToAscObj<AscEthereumCall> for EthereumCallData<'a> {
gas: &GasCounter,
) -> Result<AscEthereumCall, HostExportError> {
Ok(AscEthereumCall {
address: asc_new(heap, &self.to, gas)?,
address: asc_new(heap, self.to(), gas)?,
block: asc_new(heap, &self.block, gas)?,
transaction: asc_new(heap, &self.transaction, gas)?,
inputs: asc_new(heap, &self.inputs, gas)?,
Expand All @@ -758,8 +746,8 @@ impl<'a> ToAscObj<AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_2, AscEthereu
HostExportError,
> {
Ok(AscEthereumCall_0_0_3 {
to: asc_new(heap, &self.to, gas)?,
from: asc_new(heap, &self.from, gas)?,
to: asc_new(heap, self.to(), gas)?,
from: asc_new(heap, self.from(), gas)?,
block: asc_new(heap, &self.block, gas)?,
transaction: asc_new(heap, &self.transaction, gas)?,
inputs: asc_new(heap, &self.inputs, gas)?,
Expand All @@ -780,8 +768,8 @@ impl<'a> ToAscObj<AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_6, AscEthereu
HostExportError,
> {
Ok(AscEthereumCall_0_0_3 {
to: asc_new(heap, &self.to, gas)?,
from: asc_new(heap, &self.from, gas)?,
to: asc_new(heap, self.to(), gas)?,
from: asc_new(heap, self.from(), gas)?,
block: asc_new(heap, &self.block, gas)?,
transaction: asc_new(heap, &self.transaction, gas)?,
inputs: asc_new(heap, &self.inputs, gas)?,
Expand Down
43 changes: 30 additions & 13 deletions chain/ethereum/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use graph::runtime::AscPtr;
use graph::runtime::HostExportError;
use graph::semver::Version;
use graph_runtime_wasm::module::ToAscPtr;
use std::ops::Deref;
use std::{cmp::Ordering, sync::Arc};

use crate::runtime::abi::AscEthereumBlock;
Expand Down Expand Up @@ -192,14 +191,7 @@ impl ToAscPtr for MappingTrigger {
inputs,
outputs,
} => {
let call = EthereumCallData {
to: call.to,
from: call.from,
block: EthereumBlockData::from(block.as_ref()),
transaction: EthereumTransactionData::new(transaction.deref()),
inputs,
outputs,
};
let call = EthereumCallData::new(&block, &transaction, &call, &inputs, &outputs);
if heap.api_version() >= Version::new(0, 0, 6) {
asc_new::<
AscEthereumCall_0_0_3<AscEthereumTransaction_0_0_6, AscEthereumBlock_0_0_6>,
Expand Down Expand Up @@ -594,10 +586,35 @@ impl<'a> EthereumEventData<'a> {
/// An Ethereum call executed within a transaction within a block to a contract address.
#[derive(Debug, Clone)]
pub struct EthereumCallData<'a> {
pub from: Address,
pub to: Address,
pub block: EthereumBlockData<'a>,
pub transaction: EthereumTransactionData<'a>,
pub inputs: Vec<LogParam>,
pub outputs: Vec<LogParam>,
pub inputs: &'a [LogParam],
pub outputs: &'a [LogParam],
call: &'a EthereumCall,
}

impl<'a> EthereumCallData<'a> {
fn new(
block: &'a Block<Transaction>,
transaction: &'a Transaction,
call: &'a EthereumCall,
inputs: &'a [LogParam],
outputs: &'a [LogParam],
) -> EthereumCallData<'a> {
EthereumCallData {
block: EthereumBlockData::from(block),
transaction: EthereumTransactionData::new(transaction),
inputs,
outputs,
call,
}
}

pub fn from(&self) -> &Address {
&self.call.from
}

pub fn to(&self) -> &Address {
&self.call.to
}
}

0 comments on commit 737014a

Please sign in to comment.