Skip to content

Commit

Permalink
BLOBBASEFEE implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Apr 2, 2024
1 parent 9f61f32 commit 214aa99
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 13 deletions.
11 changes: 6 additions & 5 deletions benches/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ fn run_loop_contract() {
chain_id: U256::one(),
block_base_fee_per_gas: U256::zero(),
block_randomness: None,
blob_base_fee: None,
};

let mut state = BTreeMap::new();
state.insert(
H160::from_str("0x1000000000000000000000000000000000000000").unwrap(),
MemoryAccount {
nonce: U256::one(),
balance: U256::from(10000000),
storage: BTreeMap::new(),
code: hex::decode("6080604052348015600f57600080fd5b506004361060285760003560e01c80630f14a40614602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b6000806000905060005b83811015608f5760018201915080806001019150506076565b508091505091905056fea26469706673582212202bc9ec597249a9700278fe4ce78da83273cb236e76d4d6797b441454784f901d64736f6c63430007040033").unwrap(),
}
nonce: U256::one(),
balance: U256::from(10000000),
storage: BTreeMap::new(),
code: hex::decode("6080604052348015600f57600080fd5b506004361060285760003560e01c80630f14a40614602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b6000806000905060005b83811015608f5760018201915080806001019150506076565b508091505091905056fea26469706673582212202bc9ec597249a9700278fe4ce78da83273cb236e76d4d6797b441454784f901d64736f6c63430007040033").unwrap(),
},
);
state.insert(
H160::from_str("0xf000000000000000000000000000000000000000").unwrap(),
Expand Down
4 changes: 4 additions & 0 deletions evm-tests/ethjson/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ pub struct Env {
#[serde(rename = "currentRandom")]
#[serde(default)]
pub random: Option<Uint>,
/// EIP-7516: Blob base fee
#[serde(default)]
pub blob_base_fee: Option<u128>,
}

#[cfg(test)]
Expand Down Expand Up @@ -199,6 +202,7 @@ mod tests {
timestamp: Uint(1.into()),
block_base_fee_per_gas: Uint(0.into()),
random: Some(Uint(1.into())),
blob_base_fee: None,
}
);
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions evm-tests/jsontests/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl Test {
chain_id: U256::one(),
block_base_fee_per_gas,
block_randomness,
blob_base_fee: self.0.env.blob_base_fee,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions evm-tests/jsontests/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Test {
chain_id: U256::zero(),
block_base_fee_per_gas: self.0.transaction.gas_price.into(),
block_randomness,
blob_base_fee: None,
}
}

Expand Down
17 changes: 10 additions & 7 deletions runtime/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,19 @@ pub fn gasprice<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
}

pub fn base_fee<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
let mut ret = H256::default();
handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]);
push_h256!(runtime, ret);

// let mut ret = H256::default();
// handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]);
push_u256!(runtime, handler.block_base_fee_per_gas());
Control::Continue
}

pub fn blob_base_fee<H: Handler>(_runtime: &mut Runtime, _handler: &H) -> Control<H> {
// CANCUN
todo!()
/// CANCUN hard fork
/// EIP-7516: BLOBBASEFEE opcode
pub fn blob_base_fee<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
let x = handler.blob_base_fee().unwrap_or_default();
let ret = U256::from(x);
push_u256!(runtime, ret);
Control::Continue
}

pub fn blob_hash<H: Handler>(_runtime: &mut Runtime, _handler: &H) -> Control<H> {
Expand Down
8 changes: 7 additions & 1 deletion runtime/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Transfer {
}

/// EVM context handler.
#[auto_impl::auto_impl(&mut, Box)]
#[auto_impl::auto_impl(& mut, Box)]
pub trait Handler {
/// Type of `CREATE` interrupt.
type CreateInterrupt;
Expand Down Expand Up @@ -117,4 +117,10 @@ pub trait Handler {

/// Records some associated `ExternalOperation`.
fn record_external_operation(&mut self, op: crate::ExternalOperation) -> Result<(), ExitError>;

/// Returns `None` if `Cancun` is not enabled.
/// CANCUN hard fork.
/// [EIP-4844]: Shard Blob Transactions
/// [EIP-7516]: BLOBBASEFEE instruction
fn blob_base_fee(&self) -> Option<u128>;
}
9 changes: 9 additions & 0 deletions src/backend/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ pub struct MemoryVicinity {
/// Environmental block gas limit.
pub block_gas_limit: U256,
/// Environmental base fee per gas.
///
pub block_base_fee_per_gas: U256,
/// Environmental randomness.
///
/// In Ethereum, this is the randomness beacon provided by the beacon
/// chain and is only enabled post Merge.
pub block_randomness: Option<H256>,
/// Environmental blob base fee per gas.
/// CANCUN hard fork
/// [EIP-4844]: Shard Blob Transactions
/// [EIP-7516]: BLOBBASEFEE instruction
pub blob_base_fee: Option<u128>,
}

/// Account information of a memory backend.
Expand Down Expand Up @@ -159,6 +165,9 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> {
fn original_storage(&self, address: H160, index: H256) -> Option<H256> {
Some(self.storage(address, index))
}
fn blob_base_fee(&self) -> Option<u128> {
self.vicinity.blob_base_fee
}
}

impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> {
Expand Down
3 changes: 3 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub trait Backend {
fn storage(&self, address: H160, index: H256) -> H256;
/// Get original storage value of address at index, if available.
fn original_storage(&self, address: H160, index: H256) -> Option<H256>;
/// [EIP-4844]: Shard Blob Transactions
/// [EIP-7516]: BLOBBASEFEE instruction
fn blob_base_fee(&self) -> Option<u128>;
}

/// EVM backend that can apply changes.
Expand Down
13 changes: 13 additions & 0 deletions src/executor/stack/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,19 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
fn record_external_operation(&mut self, op: crate::ExternalOperation) -> Result<(), ExitError> {
self.state.record_external_operation(op)
}

/// Returns `None` if `Cancun` hard fork is not enabled
/// via `has_blob_base_fee` config.
///
/// [EIP-4844]: Shard Blob Transactions
/// [EIP-7516]: BLOBBASEFEE instruction
fn blob_base_fee(&self) -> Option<u128> {
if self.config.has_blob_base_fee {
self.state.blob_base_fee()
} else {
None
}
}
}

struct StackExecutorHandle<'inner, 'config, 'precompiles, S, P> {
Expand Down
3 changes: 3 additions & 0 deletions src/executor/stack/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf

self.backend.original_storage(address, key)
}
fn blob_base_fee(&self) -> Option<u128> {
self.backend.blob_base_fee()
}
}

impl<'backend, 'config, B: Backend> StackState<'config> for MemoryStackState<'backend, 'config, B> {
Expand Down

0 comments on commit 214aa99

Please sign in to comment.