use alloy_primitives::{address, b256, Address, TxKind, B256};
use op_revm::transaction::deposit::DEPOSIT_TRANSACTION_TYPE;
use revm::context::Transaction;
use crate::{types::MegaTransaction, ORACLE_CONTRACT_ADDRESS};
pub const MEGA_SYSTEM_ADDRESS: Address = address!("0xA887dCB9D5f39Ef79272801d05Abdf707CFBbD1d");
pub const MEGA_SYSTEM_TX_WHITELIST: &[Address] = &[ORACLE_CONTRACT_ADDRESS];
pub const MEGA_SYSTEM_TRANSACTION_SOURCE_HASH: B256 =
b256!("852c082c0faff590c6300c2c34815d1f79882552fa95ba413cd5aeb1dba84957");
pub const SANDBOX_TX_SOURCE_HASH: B256 =
b256!("3bc757a39ecb6dc5b5e5715352016c2b6fd38968a78cc57e878d9be249bc62f4");
pub fn sent_from_system_address(tx: &MegaTransaction, system_address: Address) -> bool {
tx.caller() == system_address
}
pub fn is_mega_system_transaction_with(tx: &MegaTransaction, system_address: Address) -> bool {
check_if_mega_system_transaction(tx.caller(), tx.tx_type(), tx.kind(), system_address)
}
pub fn check_if_mega_system_transaction(
tx_signer: Address,
tx_type: u8,
tx_kind: TxKind,
system_address: Address,
) -> bool {
if tx_type == 0x0 && tx_signer == system_address {
match tx_kind {
TxKind::Create => false,
TxKind::Call(address) => MEGA_SYSTEM_TX_WHITELIST.contains(&address),
}
} else {
false
}
}
pub fn is_deposit_like_transaction(tx: &MegaTransaction, system_address: Address) -> bool {
if tx.tx_type() == DEPOSIT_TRANSACTION_TYPE {
return true;
}
is_mega_system_transaction_with(tx, system_address)
}
pub fn is_system_originated(tx: &MegaTransaction, system_address: Address) -> bool {
tx.caller() == alloy_eips::eip4788::SYSTEM_ADDRESS ||
tx.deposit.source_hash == MEGA_SYSTEM_TRANSACTION_SOURCE_HASH ||
is_mega_system_transaction_with(tx, system_address)
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::TxKind;
const EIP_SYSTEM_ADDRESS: Address = alloy_eips::eip4788::SYSTEM_ADDRESS;
const USER: Address = address!("0000000000000000000000000000000000009999");
const NON_WHITELIST: Address = address!("00000000000000000000000000000000000000ab");
fn legacy_call_tx(caller: Address, to: Address) -> MegaTransaction {
let mut tx = MegaTransaction::default();
tx.base.tx_type = 0;
tx.base.caller = caller;
tx.base.kind = TxKind::Call(to);
tx
}
#[test]
fn test_is_system_originated_matches_eip_system_address_caller() {
let tx = legacy_call_tx(EIP_SYSTEM_ADDRESS, USER);
assert!(is_system_originated(&tx, MEGA_SYSTEM_ADDRESS));
assert!(is_system_originated(&tx, USER));
}
#[test]
fn test_is_system_originated_matches_mega_system_tx() {
let tx = legacy_call_tx(MEGA_SYSTEM_ADDRESS, ORACLE_CONTRACT_ADDRESS);
assert!(is_system_originated(&tx, MEGA_SYSTEM_ADDRESS));
}
#[test]
fn test_is_system_originated_rejects_system_caller_to_non_whitelist() {
let tx = legacy_call_tx(MEGA_SYSTEM_ADDRESS, NON_WHITELIST);
assert!(!is_system_originated(&tx, MEGA_SYSTEM_ADDRESS));
}
#[test]
fn test_is_system_originated_matches_promoted_mega_system_tx() {
let mut tx = legacy_call_tx(MEGA_SYSTEM_ADDRESS, ORACLE_CONTRACT_ADDRESS);
tx.deposit.source_hash = MEGA_SYSTEM_TRANSACTION_SOURCE_HASH;
assert_eq!(tx.tx_type(), DEPOSIT_TRANSACTION_TYPE, "promotion flips tx_type to deposit");
assert!(
!is_mega_system_transaction_with(&tx, MEGA_SYSTEM_ADDRESS),
"the legacy-typed check no longer matches after promotion",
);
assert!(is_system_originated(&tx, MEGA_SYSTEM_ADDRESS), "but the source-hash branch does");
}
#[test]
fn test_is_system_originated_rejects_user_tx() {
let tx = legacy_call_tx(USER, ORACLE_CONTRACT_ADDRESS);
assert!(!is_system_originated(&tx, MEGA_SYSTEM_ADDRESS));
}
#[test]
fn test_is_system_originated_rejects_user_deposit_tx() {
let mut tx = legacy_call_tx(USER, ORACLE_CONTRACT_ADDRESS);
tx.deposit.source_hash = B256::repeat_byte(0x11);
assert_eq!(tx.tx_type(), DEPOSIT_TRANSACTION_TYPE);
assert!(is_deposit_like_transaction(&tx, MEGA_SYSTEM_ADDRESS));
assert!(!is_system_originated(&tx, MEGA_SYSTEM_ADDRESS));
}
}