Skip to content

Commit 6bf741d

Browse files
committed
chain/ethereum: Avoid a clone for EthereumEventData in ABI conversion
1 parent c771226 commit 6bf741d

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

chain/ethereum/src/runtime/abi.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ impl ToAscObj<AscLogParamArray> for Vec<ethabi::LogParam> {
4949
}
5050
}
5151

52+
impl ToAscObj<AscLogParamArray> for &[ethabi::LogParam] {
53+
fn to_asc_obj<H: AscHeap + ?Sized>(
54+
&self,
55+
heap: &mut H,
56+
gas: &GasCounter,
57+
) -> Result<AscLogParamArray, HostExportError> {
58+
let content: Result<Vec<_>, _> = self.iter().map(|x| asc_new(heap, x, gas)).collect();
59+
let content = content?;
60+
Ok(AscLogParamArray(Array::new(&content, heap, gas)?))
61+
}
62+
}
63+
5264
impl AscIndexId for AscLogParamArray {
5365
const INDEX_ASC_TYPE_ID: IndexForAscTypeId = IndexForAscTypeId::ArrayEventParam;
5466
}
@@ -567,17 +579,17 @@ where
567579
gas: &GasCounter,
568580
) -> Result<AscEthereumEvent<T, B>, HostExportError> {
569581
Ok(AscEthereumEvent {
570-
address: asc_new(heap, &self.address, gas)?,
571-
log_index: asc_new(heap, &BigInt::from_unsigned_u256(&self.log_index), gas)?,
582+
address: asc_new(heap, self.address(), gas)?,
583+
log_index: asc_new(heap, &BigInt::from_unsigned_u256(self.log_index()), gas)?,
572584
transaction_log_index: asc_new(
573585
heap,
574-
&BigInt::from_unsigned_u256(&self.transaction_log_index),
586+
&BigInt::from_unsigned_u256(self.transaction_log_index()),
575587
gas,
576588
)?,
577589
log_type: self
578-
.log_type
579-
.clone()
580-
.map(|log_type| asc_new(heap, &log_type, gas))
590+
.log_type()
591+
.as_ref()
592+
.map(|log_type| asc_new(heap, log_type, gas))
581593
.unwrap_or(Ok(AscPtr::null()))?,
582594
block: asc_new::<B, EthereumBlockData, _>(heap, &self.block, gas)?,
583595
transaction: asc_new::<T, EthereumTransactionData, _>(heap, &self.transaction, gas)?,

chain/ethereum/src/trigger.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,12 @@ impl ToAscPtr for MappingTrigger {
146146
calls: _,
147147
} => {
148148
let api_version = heap.api_version();
149-
let ethereum_event_data = EthereumEventData {
150-
block: EthereumBlockData::from(block.as_ref()),
151-
transaction: EthereumTransactionData::new(transaction.deref()),
152-
address: log.address,
153-
log_index: log.log_index.unwrap_or(U256::zero()),
154-
transaction_log_index: log.log_index.unwrap_or(U256::zero()),
155-
log_type: log.log_type.clone(),
156-
params,
157-
};
149+
let ethereum_event_data = EthereumEventData::new(
150+
block.as_ref(),
151+
transaction.as_ref(),
152+
log.as_ref(),
153+
&params,
154+
);
158155
if api_version >= API_VERSION_0_0_7 {
159156
asc_new::<
160157
AscEthereumEvent_0_0_7<
@@ -553,13 +550,45 @@ impl<'a> EthereumTransactionData<'a> {
553550
/// An Ethereum event logged from a specific contract address and block.
554551
#[derive(Debug, Clone)]
555552
pub struct EthereumEventData<'a> {
556-
pub address: Address,
557-
pub log_index: U256,
558-
pub transaction_log_index: U256,
559-
pub log_type: Option<String>,
560553
pub block: EthereumBlockData<'a>,
561554
pub transaction: EthereumTransactionData<'a>,
562-
pub params: Vec<LogParam>,
555+
pub params: &'a [LogParam],
556+
log: &'a Log,
557+
}
558+
559+
impl<'a> EthereumEventData<'a> {
560+
pub fn new(
561+
block: &'a Block<Transaction>,
562+
tx: &'a Transaction,
563+
log: &'a Log,
564+
params: &'a [LogParam],
565+
) -> Self {
566+
EthereumEventData {
567+
block: EthereumBlockData::from(block),
568+
transaction: EthereumTransactionData::new(tx),
569+
log,
570+
params,
571+
}
572+
}
573+
574+
pub fn address(&self) -> &Address {
575+
&self.log.address
576+
}
577+
578+
pub fn log_index(&self) -> &U256 {
579+
self.log.log_index.as_ref().unwrap_or(&U256_DEFAULT)
580+
}
581+
582+
pub fn transaction_log_index(&self) -> &U256 {
583+
self.log
584+
.transaction_log_index
585+
.as_ref()
586+
.unwrap_or(&U256_DEFAULT)
587+
}
588+
589+
pub fn log_type(&self) -> &Option<String> {
590+
&self.log.log_type
591+
}
563592
}
564593

565594
/// An Ethereum call executed within a transaction within a block to a contract address.

0 commit comments

Comments
 (0)