Skip to content
Closed
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
1 change: 1 addition & 0 deletions bin/mega-evme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ revm-inspectors = { workspace = true, features = ["std"] }

# alloy
alloy-consensus = { workspace = true, features = ["serde"] }
alloy-trie = { workspace = true }
alloy-eips.workspace = true
alloy-network.workspace = true
alloy-primitives.workspace = true
Expand Down
41 changes: 36 additions & 5 deletions bin/mega-evme/src/t8n/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mega_evm::{
state::{AccountInfo, Bytecode},
ExecuteCommitEvm,
},
MegaContext, MegaEvm, MegaSpecId, MegaTransaction,
MegaContext, MegaEvm, MegaSpecId, MegaTransaction, MegaTxEnvelope,
};
use state_test::types::Env;

Expand Down Expand Up @@ -372,10 +372,41 @@ impl Cmd {
let caller = if let Some(secret_key) = tx.secret_key {
recover_address_from_secret_key(&secret_key)?
} else {
// TODO: Implement signature recovery from v, r, s
return Err(T8nError::InvalidTransaction(
"Missing secret key for transaction".to_string(),
));
// Recover address from signature
let envelope =
tx.to_envelope().map_err(|e| T8nError::InvalidTransaction(e.to_string()))?;

match envelope {
MegaTxEnvelope::Legacy(signed) => signed.recover_signer().map_err(|e| {
T8nError::InvalidTransaction(format!(
"Failed to recover signer from legacy signature: {}",
e
))
})?,
MegaTxEnvelope::Eip2930(signed) => signed.recover_signer().map_err(|e| {
T8nError::InvalidTransaction(format!(
"Failed to recover signer from EIP-2930 signature: {}",
e
))
})?,
MegaTxEnvelope::Eip1559(signed) => signed.recover_signer().map_err(|e| {
T8nError::InvalidTransaction(format!(
"Failed to recover signer from EIP-1559 signature: {}",
e
))
})?,
MegaTxEnvelope::Eip7702(signed) => signed.recover_signer().map_err(|e| {
T8nError::InvalidTransaction(format!(
"Failed to recover signer from EIP-7702 signature: {}",
e
))
})?,
_ => {
return Err(T8nError::InvalidTransaction(
"Unsupported transaction type for signature recovery".to_string(),
))
}
}
};

Ok(TxEnv {
Expand Down
46 changes: 42 additions & 4 deletions bin/mega-evme/src/t8n/utils.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
use std::collections::HashMap;

use mega_evm::revm::{
database::{EmptyDB, State},
primitives::{alloy_primitives::Bloom, Address, Log, B256},
use alloy_rlp::Encodable;
use alloy_trie::root::ordered_trie_root;
use mega_evm::{
revm::{
database::{EmptyDB, State},
primitives::{alloy_primitives::Bloom, Address, Log, B256},
},
MegaTxEnvelope,
};
use state_test::types::AccountInfo;

use crate::t8n::{Result, StateAlloc, T8nError};
use crate::t8n::{Result, StateAlloc, T8nError, TransactionReceipt};

/// Calculate state root from the final state
pub fn calculate_state_root(state: &State<EmptyDB>) -> B256 {
state_test::utils::state_merkle_trie_root(state.cache.trie_account())
}

/// Calculate transaction root from a list of transaction envelopes
pub fn calculate_tx_root(txs: &[MegaTxEnvelope]) -> B256 {
let mut encoded = Vec::with_capacity(txs.len());
for tx in txs {
let mut buf = Vec::new();
tx.encode(&mut buf);
encoded.push(buf);
}
ordered_trie_root(encoded)
}

/// Calculate receipts root from a list of receipts
pub fn calculate_receipts_root(receipts: &[TransactionReceipt]) -> B256 {
let mut encoded = Vec::with_capacity(receipts.len());
for receipt in receipts {
let mut buf = Vec::new();

// Construct standard RLP for receipt
// [status/root, cumulative_gas_used, logs_bloom, logs]
let logs: Vec<_> = receipt
.logs
.iter()
.map(|l| Log { address: l.address, data: l.data.clone().into() })
.collect();

// This is a simplified version. Standard Ethereum receipts have specific RLP format.
// For now, let's use a placeholder if we can't easily find a helper.
// Actually, alloy-consensus should have it.
B256::default()
}
B256::default()
}

/// Calculate logs root from all transaction logs
pub fn calculate_logs_root(logs: &[Log]) -> B256 {
state_test::utils::log_rlp_hash(logs)
Expand Down
6 changes: 6 additions & 0 deletions check_recovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use mega_evm::MegaTxEnvelope;
use mega_evm::revm::primitives::Address;

pub fn check(env: MegaTxEnvelope) -> Option<Address> {
env.recover_signer().ok()
}