Skip to content

Commit 642f13f

Browse files
fedemagnanidrunDaniPopesmattsse
authored
feat(anvil): use Alloy otterscan types (#8318)
* deprecate src/eth/otterscan/types.rs * fmt * Revert "fmt" This reverts commit bf1969f. * (less) fmt * requested changes * CallType into String * clippy + comment --------- Co-authored-by: drun <[email protected]> Co-authored-by: DaniPopes <[email protected]> Co-authored-by: Matthias Seitz <[email protected]>
1 parent 09306b3 commit 642f13f

File tree

6 files changed

+315
-446
lines changed

6 files changed

+315
-446
lines changed

crates/anvil/core/src/eth/transaction/mod.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
1010
use alloy_primitives::{Address, Bloom, Bytes, Log, Signature, TxHash, TxKind, B256, U256, U64};
1111
use alloy_rlp::{length_of_length, Decodable, Encodable, Header};
1212
use alloy_rpc_types::{
13-
request::TransactionRequest, AccessList, AnyTransactionReceipt, ConversionError,
14-
Signature as RpcSignature, Transaction as RpcTransaction, TransactionReceipt,
13+
request::TransactionRequest, trace::otterscan::OtsReceipt, AccessList, AnyTransactionReceipt,
14+
ConversionError, Signature as RpcSignature, Transaction as RpcTransaction, TransactionReceipt,
1515
};
1616
use alloy_serde::{OtherFields, WithOtherFields};
1717
use bytes::BufMut;
@@ -70,7 +70,7 @@ pub fn transaction_request_to_typed(
7070
gas_limit: gas.unwrap_or_default(),
7171
is_system_tx: other.get_deserialized::<bool>("isSystemTx")?.ok()?,
7272
input: input.into_input().unwrap_or_default(),
73-
}))
73+
}));
7474
}
7575

7676
match (
@@ -198,7 +198,7 @@ impl MaybeImpersonatedTransaction {
198198
#[cfg(feature = "impersonated-tx")]
199199
pub fn recover(&self) -> Result<Address, alloy_primitives::SignatureError> {
200200
if let Some(sender) = self.impersonated_sender {
201-
return Ok(sender)
201+
return Ok(sender);
202202
}
203203
self.transaction.recover()
204204
}
@@ -211,7 +211,7 @@ impl MaybeImpersonatedTransaction {
211211
pub fn hash(&self) -> B256 {
212212
if self.transaction.is_impersonated() {
213213
if let Some(sender) = self.impersonated_sender {
214-
return self.transaction.impersonated_hash(sender)
214+
return self.transaction.impersonated_hash(sender);
215215
}
216216
}
217217
self.transaction.hash()
@@ -1016,7 +1016,7 @@ impl Decodable for TypedTransaction {
10161016

10171017
// Legacy TX
10181018
if header.list {
1019-
return Ok(TxEnvelope::decode(buf)?.into())
1019+
return Ok(TxEnvelope::decode(buf)?.into());
10201020
}
10211021

10221022
// Check byte after header
@@ -1062,7 +1062,7 @@ impl Encodable2718 for TypedTransaction {
10621062
impl Decodable2718 for TypedTransaction {
10631063
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Result<Self, Eip2718Error> {
10641064
if ty == 0x7E {
1065-
return Ok(Self::Deposit(DepositTransaction::decode(buf)?))
1065+
return Ok(Self::Deposit(DepositTransaction::decode(buf)?));
10661066
}
10671067
match TxEnvelope::typed_decode(ty, buf)? {
10681068
TxEnvelope::Eip2930(tx) => Ok(Self::EIP2930(tx)),
@@ -1245,6 +1245,37 @@ impl<T> TypedReceipt<T> {
12451245
}
12461246
}
12471247

1248+
impl<T> From<TypedReceipt<T>> for ReceiptWithBloom<T> {
1249+
fn from(value: TypedReceipt<T>) -> Self {
1250+
match value {
1251+
TypedReceipt::Legacy(r) |
1252+
TypedReceipt::EIP1559(r) |
1253+
TypedReceipt::EIP2930(r) |
1254+
TypedReceipt::EIP4844(r) => r,
1255+
TypedReceipt::Deposit(r) => r.inner,
1256+
}
1257+
}
1258+
}
1259+
1260+
impl From<TypedReceipt<alloy_rpc_types::Log>> for OtsReceipt {
1261+
fn from(value: TypedReceipt<alloy_rpc_types::Log>) -> Self {
1262+
let r#type = match value {
1263+
TypedReceipt::Legacy(_) => 0x00,
1264+
TypedReceipt::EIP2930(_) => 0x01,
1265+
TypedReceipt::EIP1559(_) => 0x02,
1266+
TypedReceipt::EIP4844(_) => 0x03,
1267+
TypedReceipt::Deposit(_) => 0x7E,
1268+
} as u8;
1269+
let receipt = ReceiptWithBloom::<alloy_rpc_types::Log>::from(value);
1270+
let status = receipt.status();
1271+
let cumulative_gas_used = receipt.cumulative_gas_used() as u64;
1272+
let logs = receipt.receipt.logs.into_iter().map(|x| x.inner).collect();
1273+
let logs_bloom = receipt.logs_bloom;
1274+
1275+
Self { status, cumulative_gas_used, logs: Some(logs), logs_bloom: Some(logs_bloom), r#type }
1276+
}
1277+
}
1278+
12481279
impl TypedReceipt {
12491280
pub fn cumulative_gas_used(&self) -> u128 {
12501281
self.as_receipt_with_bloom().cumulative_gas_used()
@@ -1395,7 +1426,7 @@ impl Encodable2718 for TypedReceipt {
13951426
impl Decodable2718 for TypedReceipt {
13961427
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Result<Self, Eip2718Error> {
13971428
if ty == 0x7E {
1398-
return Ok(Self::Deposit(DepositReceipt::decode(buf)?))
1429+
return Ok(Self::Deposit(DepositReceipt::decode(buf)?));
13991430
}
14001431
match ReceiptEnvelope::typed_decode(ty, buf)? {
14011432
ReceiptEnvelope::Eip2930(tx) => Ok(Self::EIP2930(tx)),

crates/anvil/src/eth/backend/mem/storage.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use alloy_rpc_types::{
1414
FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType,
1515
GethDebugTracingOptions, GethTrace, NoopFrame,
1616
},
17+
otterscan::{InternalOperation, OperationType},
1718
parity::LocalizedTransactionTrace,
1819
},
1920
BlockId, BlockNumberOrTag, TransactionInfo as RethTransactionInfo,
@@ -25,7 +26,9 @@ use anvil_core::eth::{
2526
use anvil_rpc::error::RpcError;
2627
use foundry_evm::{
2728
revm::primitives::Env,
28-
traces::{FourByteInspector, GethTraceBuilder, ParityTraceBuilder, TracingInspectorConfig},
29+
traces::{
30+
CallKind, FourByteInspector, GethTraceBuilder, ParityTraceBuilder, TracingInspectorConfig,
31+
},
2932
};
3033
use parking_lot::RwLock;
3134
use std::{
@@ -164,7 +167,7 @@ impl InMemoryBlockStates {
164167
if let Some(state) = self.on_disk_states.get_mut(hash) {
165168
if let Some(cached) = self.disk_cache.read(*hash) {
166169
state.init_from_snapshot(cached);
167-
return Some(state)
170+
return Some(state);
168171
}
169172
}
170173
None
@@ -426,6 +429,31 @@ impl MinedTransaction {
426429
})
427430
}
428431

432+
pub fn ots_internal_operations(&self) -> Vec<InternalOperation> {
433+
self.info
434+
.traces
435+
.iter()
436+
.filter_map(|node| {
437+
let r#type = match node.trace.kind {
438+
_ if node.is_selfdestruct() => OperationType::OpSelfDestruct,
439+
CallKind::Call if !node.trace.value.is_zero() => OperationType::OpTransfer,
440+
CallKind::Create => OperationType::OpCreate,
441+
CallKind::Create2 => OperationType::OpCreate2,
442+
_ => return None,
443+
};
444+
let mut from = node.trace.caller;
445+
let mut to = node.trace.address;
446+
let mut value = node.trace.value;
447+
if node.is_selfdestruct() {
448+
from = node.trace.address;
449+
to = node.trace.selfdestruct_refund_target.unwrap_or_default();
450+
value = node.trace.selfdestruct_transferred_value.unwrap_or_default();
451+
}
452+
Some(InternalOperation { r#type, from, to, value })
453+
})
454+
.collect()
455+
}
456+
429457
pub fn geth_trace(&self, opts: GethDebugTracingOptions) -> Result<GethTrace, BlockchainError> {
430458
let GethDebugTracingOptions { config, tracer, tracer_config, .. } = opts;
431459

@@ -434,7 +462,7 @@ impl MinedTransaction {
434462
GethDebugTracerType::BuiltInTracer(tracer) => match tracer {
435463
GethDebugBuiltInTracerType::FourByteTracer => {
436464
let inspector = FourByteInspector::default();
437-
return Ok(FourByteFrame::from(inspector).into())
465+
return Ok(FourByteFrame::from(inspector).into());
438466
}
439467
GethDebugBuiltInTracerType::CallTracer => {
440468
return match tracer_config.into_call_config() {

0 commit comments

Comments
 (0)