Skip to content
Draft
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
33 changes: 30 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ revm-bytecode = { version = "7.1.1", default-features = false }
# alloy
alloy-trie = "0.9.1"
alloy-eips = "1.0.41"
alloy-eip7928 = { git = "https://github.com/alloy-rs/eips", version = "0.1.0" }
alloy-serde = "1.0.41"
alloy-genesis = "1.0.41"
alloy-hardforks = "0.4.4"
Expand All @@ -109,6 +110,7 @@ alloy-sol-types = "1.4.1"
alloy-sol-macro = "1.4.1"
alloy-primitives = "1.4.1"
alloy-consensus = "1.0.41"
alloy-rlp = "0.3.10"
alloy-rpc-types = "1.0.41"
alloy-rpc-client = "1.0.41"
alloy-rpc-types-eth = "1.0.41"
Expand All @@ -121,6 +123,9 @@ op-alloy-consensus = "0.22.0"
op-alloy-rpc-jsonrpsee = "0.22.0"
op-alloy-rpc-types-engine = "0.22.0"

# op-revm
op-revm = { version = "12.0.2", default-features = false }

# tokio
tokio = "1.48.0"
tokio-stream = "0.1.17"
Expand Down
18 changes: 17 additions & 1 deletion crates/fbal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "base-reth-fbal"
name = "base-fbal"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
Expand All @@ -12,5 +12,21 @@ description = "FBAL library crate"
workspace = true

[dependencies]
alloy-primitives.workspace = true
alloy-eip7928 = {workspace = true, features = ["serde", "rlp"]}
alloy-rlp = {workspace = true, features = ["derive"]}

revm.workspace = true

serde.workspace = true

[dev-dependencies]
alloy-consensus.workspace = true
alloy-contract.workspace = true
alloy-sol-macro = { workspace = true, features = ["json"] }
alloy-sol-types.workspace = true
op-revm.workspace = true
reth-evm.workspace = true
reth-optimism-chainspec.workspace = true
reth-optimism-evm.workspace = true
serde_json.workspace = true
Empty file added crates/fbal/README.md
Empty file.
82 changes: 82 additions & 0 deletions crates/fbal/src/inspector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use alloy_primitives::{Address, FixedBytes, map::foldhash::HashMap};
use revm::{
Inspector,
bytecode::opcode,
context::ContextTr,
inspector::JournalExt,
interpreter::{
CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter,
interpreter_types::{InputsTr, Jumps},
},
};

/// Inspector that tracks all accounts and storage slots touched during execution.
#[derive(Debug, Default)]
pub struct TouchedAccountsInspector {
/// Map of touched accounts to the storage slots accessed.
pub touched_accounts: HashMap<Address, Vec<FixedBytes<32>>>,
// pub account_changes: HashMap<Address, AccountChanges>,
}

impl<CTX> Inspector<CTX> for TouchedAccountsInspector
where
CTX: ContextTr<Journal: JournalExt>,
{
fn step(&mut self, interp: &mut Interpreter, _context: &mut CTX) {
match interp.bytecode.opcode() {
opcode::SLOAD => {
let slot = interp.stack.peek(0).expect("should be able to load slot");
let contract = interp.input.target_address();
self.touched_accounts.entry(contract).or_default().push(slot.into());
}
opcode::SSTORE => {
// TODO: This is likely not needed as it can be gotten from the block executor
//
// let slot = interp.stack.peek(0).expect("should be able to load slot");
// let new_value = interp.stack.peek(1).expect("should be able to load new value");
// let contract = interp.input.target_address();
}
opcode::EXTCODECOPY | opcode::EXTCODEHASH | opcode::EXTCODESIZE | opcode::BALANCE => {
let slot = interp.stack.peek(0).expect("should be able to load slot");
let addr = Address::from_word(slot.into());
println!("EXTCODECOPY | EXT CODE HASH | EXT CODE SIZE | BALANCE {}", addr);
self.touched_accounts.entry(addr).or_default();
}
opcode::DELEGATECALL | opcode::CALL | opcode::STATICCALL | opcode::CALLCODE => {
let addr_slot = interp.stack.peek(1).expect("should be able to load slot");
let addr = Address::from_word(addr_slot.into());
println!("DELEGATECALL | CALL | STATICCALL | CALLCODE {}", addr);
self.touched_accounts.entry(addr).or_default();
}

_ => {}
}
}

fn call(&mut self, _context: &mut CTX, inputs: &mut CallInputs) -> Option<CallOutcome> {
self.touched_accounts.entry(inputs.target_address).or_default();
self.touched_accounts.entry(inputs.caller).or_default();

// let caller_account = context.journal_mut().load_account(inputs.caller).ok()?;
// let is_eoa_caller = caller_account.info.is_empty_code_hash();
// if is_eoa_caller {
// caller_entry.nonce_changes.push(NonceChange::new(0, caller_account.info.nonce));
// }

None
}

fn create(&mut self, _context: &mut CTX, _inputs: &mut CreateInputs) -> Option<CreateOutcome> {
// let nonce = context.journal_mut().load_account(inputs.caller).ok()?.data.info.nonce;
// self.account_changes.entry(inputs.caller).or_default();

// let deployed_contract = inputs.created_address(nonce);
// self.account_changes
// .entry(deployed_contract)
// .or_default()
// .code_changes
// .push(CodeChange::new(0, inputs.init_code.clone()));

None
}
}
11 changes: 9 additions & 2 deletions crates/fbal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//! FBAL library crate
#![doc = include_str!("../README.md")]
#![doc(issue_tracker_base_url = "https://github.com/base/node-reth/issues/")]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

#![warn(missing_docs)]
mod inspector;
mod types;

pub use inspector::TouchedAccountsInspector;
pub use types::FlashblockAccessList;
48 changes: 48 additions & 0 deletions crates/fbal/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use alloy_eip7928::AccountChanges;
use alloy_primitives::{B256, keccak256};
use alloy_rlp::{Encodable, RlpDecodable, RlpEncodable};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Default, Deserialize, Serialize, RlpEncodable, RlpDecodable)]
/// FlashblockAccessList represents the access list for a single flashblock
pub struct FlashblockAccessList {
/// All the account changes in this access list
pub account_changes: Vec<AccountChanges>,
/// Minimum txn index from the full block that's included in this access list
pub min_tx_index: u64,
/// Maximum txn index from the full block that's included in this access list
pub max_tx_index: u64,
/// keccak256 hash of the RLP-encoded account changes list
pub fal_hash: B256,
}

impl FlashblockAccessList {
/// Merge account changes from a new transaction into the access list
pub fn merge_account_changes(&mut self, extension: Vec<AccountChanges>) {
for new in extension.iter() {
let mut found = false;
for old in self.account_changes.iter_mut() {
if old.address() == new.address() {
old.storage_changes.extend(new.storage_changes.clone());
old.storage_reads.extend(new.storage_reads.clone());
old.balance_changes.extend(new.balance_changes.clone());
old.nonce_changes.extend(new.nonce_changes.clone());
old.code_changes.extend(new.code_changes.clone());
found = true;
break;
}
}

if !found {
self.account_changes.push(new.clone());
}
}
}

/// Finalize the access list by computing the hash of the RLP-encoded account changes list
pub fn finalize(&mut self) {
let mut encoded = Vec::new();
self.account_changes.encode(&mut encoded);
self.fal_hash = keccak256(encoded);
}
}
Loading