Skip to content

Code size experiment #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
32 changes: 32 additions & 0 deletions src/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use primitives::{Address, B256};
use revm::interpreter::StateLoad;

use crate::ScrollWiring;

type KeccakHash = B256;

pub trait ScrollCodeSizeDatabase {
fn code_size(&self, code_hash: KeccakHash) -> Option<usize>;
}

pub trait ScrollCodeHost {
fn code_size(&mut self, code_hash: Address) -> Option<(usize, bool)>;
}

impl<EvmWiringT> ScrollCodeHost for revm::Context<EvmWiringT>
where
EvmWiringT: ScrollWiring,
EvmWiringT::Database: ScrollCodeSizeDatabase,
{
fn code_size(&mut self, address: Address) -> Option<(usize, bool)> {
let StateLoad {
data: account,
is_cold,
} = self.evm.load_account(address).ok()?;
let code_hash = account.info.code_hash();
self.evm
.db
.code_size(code_hash)
.map(|code_size| (code_size, is_cold))
}
}
6 changes: 4 additions & 2 deletions src/handle_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use revm::{
use std::sync::Arc;

use crate::{
instruction::make_scroll_instruction_tables, precompile, scroll_spec_to_generic,
spec::ScrollSpec, L1BlockInfo, ScrollContext, ScrollTransaction, ScrollWiring,
code::ScrollCodeSizeDatabase, instruction::make_scroll_instruction_tables, precompile,
scroll_spec_to_generic, spec::ScrollSpec, L1BlockInfo, ScrollContext, ScrollTransaction,
ScrollWiring,
};

/// Configure the handler for the Scroll chain.
Expand All @@ -33,6 +34,7 @@ use crate::{
pub fn scroll_handle_register<EvmWiringT>(handler: &mut EvmHandler<'_, EvmWiringT>)
where
EvmWiringT: ScrollWiring,
EvmWiringT::Database: ScrollCodeSizeDatabase,
{
scroll_spec_to_generic!(handler.spec_id, {
// Load `L1BlockInfo` from the database and invoke standard `load_accounts` handler.
Expand Down
15 changes: 6 additions & 9 deletions src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use revm::{
wiring::Block,
};

use crate::{ScrollSpec, ScrollSpecId};
use crate::{code::ScrollCodeHost, ScrollSpec, ScrollSpecId};

/// Creates a table of instructions for the Scroll hardfork.
///
Expand All @@ -24,7 +24,7 @@ use crate::{ScrollSpec, ScrollSpecId};
/// - `TLOAD`
/// - `SELFDESTRUCT`
/// - `MCOPY`
pub fn make_scroll_instruction_tables<'a, H: Host + ?Sized, SPEC: ScrollSpec>(
pub fn make_scroll_instruction_tables<'a, H: Host + ?Sized + ScrollCodeHost, SPEC: ScrollSpec>(
) -> InstructionTables<'a, H> {
let mut table = make_instruction_table::<H, SPEC>();

Expand Down Expand Up @@ -71,18 +71,15 @@ fn blockhash<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
};
}

fn extcodesize<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
fn extcodesize<H: Host + ?Sized + ScrollCodeHost>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
let Some(code) = host.code(address) else {
let Some((code_size, is_cold)) = host.code_size(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};

let (code, load) = code.into_components();

gas!(interpreter, warm_cold_cost(load.state_load.is_cold));

push!(interpreter, U256::from(code.len()));
gas!(interpreter, warm_cold_cost(is_cold));
push!(interpreter, U256::from(code_size));
}

fn selfdestruct<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use revm::{primitives::Bytes, wiring::TransactionValidation};

mod code;
mod env;
mod handle_register;
mod instruction;
Expand Down
9 changes: 6 additions & 3 deletions src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{env::TxEnv, handle_register::scroll_handle_register, ScrollContext};
use crate::{
code::ScrollCodeSizeDatabase, env::TxEnv, handle_register::scroll_handle_register,
ScrollContext,
};
use core::marker::PhantomData;
use revm::{
database_interface::Database,
Expand All @@ -15,7 +18,7 @@ pub struct ScrollEvmWiring<DB: Database, EXT> {
_phantom: PhantomData<(DB, EXT)>,
}

impl<DB: Database, EXT> EvmWiring for ScrollEvmWiring<DB, EXT> {
impl<DB: Database + ScrollCodeSizeDatabase, EXT> EvmWiring for ScrollEvmWiring<DB, EXT> {
type Block = BlockEnv;
type Database = DB;
type ChainContext = Context;
Expand All @@ -25,7 +28,7 @@ impl<DB: Database, EXT> EvmWiring for ScrollEvmWiring<DB, EXT> {
type Transaction = TxEnv;
}

impl<DB: Database, EXT> revm::EvmWiring for ScrollEvmWiring<DB, EXT> {
impl<DB: Database + ScrollCodeSizeDatabase, EXT> revm::EvmWiring for ScrollEvmWiring<DB, EXT> {
fn handler<'evm>(hardfork: Self::Hardfork) -> revm::EvmHandler<'evm, Self> {
let mut handler = EvmHandler::mainnet_with_spec(hardfork);

Expand Down