Skip to content
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

EIP-7516: BLOBBASEFEE opcode #721

Merged
merged 9 commits into from
Sep 21, 2023
2 changes: 1 addition & 1 deletion bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn execute_test_suite(
unit.env.parent_blob_gas_used,
unit.env.parent_excess_blob_gas,
) {
env.block.excess_blob_gas = Some(calc_excess_blob_gas(
env.block.set_blob_gas_and_fee(calc_excess_blob_gas(
parent_blob_gas_used.to(),
parent_excess_blob_gas.to(),
));
Expand Down
1 change: 1 addition & 0 deletions crates/interpreter/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub fn eval<H: Host, S: Spec>(opcode: u8, interp: &mut Interpreter, host: &mut H
opcode::BALANCE => host::balance::<S>(interp, host),
opcode::SELFBALANCE => host::selfbalance::<S>(interp, host),
opcode::BLOBHASH => host_env::blob_hash::<S>(interp, host),
opcode::BLOBFEE => host_env::blobfee::<S>(interp, host),
opcode::CODESIZE => system::codesize(interp, host),
opcode::CODECOPY => system::codecopy(interp, host),
opcode::CALLDATALOAD => system::calldataload(interp, host),
Expand Down
10 changes: 10 additions & 0 deletions crates/interpreter/src/instructions/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ pub fn basefee<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
push!(interpreter, host.env().block.basefee);
}

pub fn blobfee<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
gas!(interpreter, gas::BASE);
// EIP-7516: BLOBBASEFEE opcode
check!(interpreter, SPEC::enabled(CANCUN));
push!(
interpreter,
U256::from(host.env().block.get_blob_fee().unwrap_or_default())
);
}

pub fn origin(interpreter: &mut Interpreter, host: &mut dyn Host) {
gas!(interpreter, gas::BASE);
push_b256!(interpreter, host.env().tx.caller.into());
Expand Down
10 changes: 8 additions & 2 deletions crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub const DIFFICULTY: u8 = 0x44;
pub const GASLIMIT: u8 = 0x45;
pub const SELFBALANCE: u8 = 0x47;
pub const BLOBHASH: u8 = 0x49;
pub const BLOBFEE: u8 = 0x4a;
pub const SLOAD: u8 = 0x54;
pub const SSTORE: u8 = 0x55;
pub const GAS: u8 = 0x5a;
Expand Down Expand Up @@ -264,7 +265,7 @@ pub const OPCODE_JUMPMAP: [Option<&'static str>; 256] = [
/* 0x47 */ Some("SELFBALANCE"),
/* 0x48 */ Some("BASEFEE"),
/* 0x49 */ Some("BLOBHASH"),
/* 0x4a */ None,
/* 0x4a */ Some("BLOBFEE"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it matter that the opcode is called BLOBBASEFEE in the spec here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This: #721 (comment)
I like BLOBFEE more :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I missed that, yeah I like BLOBFEE more too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed back to BLOBBASEFEE per Eth R&D discussion

/* 0x4b */ None,
/* 0x4c */ None,
/* 0x4d */ None,
Expand Down Expand Up @@ -649,7 +650,12 @@ macro_rules! gas_opcodee {
} else {
0
}),
/* 0x4a */ OpInfo::none(),
/* 0x4a BLOBFEE */
OpInfo::gas(if SpecId::enabled($spec_id, SpecId::CANCUN) {
gas::BASE
} else {
0
}),
/* 0x4b */ OpInfo::none(),
/* 0x4c */ OpInfo::none(),
/* 0x4d */ OpInfo::none(),
Expand Down
58 changes: 47 additions & 11 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ pub struct BlockEnv {
pub timestamp: U256,
/// The gas limit of the block.
pub gas_limit: U256,

/// The base fee per gas, added in the London upgrade with [EIP-1559].
///
/// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
pub basefee: U256,

/// The difficulty of the block.
///
/// Unused after the Paris (AKA the merge) upgrade, and replaced by `prevrandao`.
Expand All @@ -45,24 +43,62 @@ pub struct BlockEnv {
///
/// [EIP-4399]: https://eips.ethereum.org/EIPS/eip-4399
pub prevrandao: Option<B256>,

/// Excess blob gas. See also [`calc_excess_blob_gas`](crate::calc_excess_blob_gas).
/// Excess blob gas and blob fee.
/// See also [`calc_excess_blob_gas`](crate::calc_excess_blob_gas)
///
/// Incorporated as part of the Cancun upgrade via [EIP-4844].
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
pub excess_blob_gas: Option<u64>,
pub blob_gas_and_fee: Option<BlobGasAndFee>,
}

/// Structure holding block blob excess gas and it calculates blob fee.
///
/// Incorporated as part of the Cancun upgrade via [EIP-4844].
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlobGasAndFee {
pub excess_blob_gas: u64,
pub blob_fee: u64,
}

impl BlobGasAndFee {
/// Takes excess blob gas and calculated blob fee with [`calc_blob_fee`]
pub fn new(excess_blob_gas: u64) -> Self {
let blob_fee = calc_blob_fee(excess_blob_gas);
Self {
excess_blob_gas,
blob_fee,
}
}
}

impl BlockEnv {
/// Takes `blob_excess_gas` saves it inside env
/// and calculates `blob_fee` with [`BlobGasAndFee`].
pub fn set_blob_gas_and_fee(&mut self, excess_blob_gas: u64) {
self.blob_gas_and_fee = Some(BlobGasAndFee::new(excess_blob_gas));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, makes it very easy to set both when initializing the env

/// See [EIP-4844] and [`Env::calc_data_fee`].
///
/// Returns `None` if `Cancun` is not enabled. This is enforced in [`Env::validate_block_env`].
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
#[inline]
pub fn get_blob_gasprice(&self) -> Option<u64> {
self.excess_blob_gas.map(calc_blob_fee)
pub fn get_blob_fee(&self) -> Option<u64> {
self.blob_gas_and_fee.as_ref().map(|a| a.blob_fee)
}

/// Return `blob_excess_gas` header field. See [EIP-4844].
///
/// Returns `None` if `Cancun` is not enabled. This is enforced in [`Env::validate_block_env`].
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
#[inline]
pub fn get_blob_excess_gas(&self) -> Option<u64> {
self.blob_gas_and_fee.as_ref().map(|a| a.excess_blob_gas)
}
}

Expand Down Expand Up @@ -340,7 +376,7 @@ impl Default for BlockEnv {
basefee: U256::ZERO,
difficulty: U256::ZERO,
prevrandao: Some(B256::zero()),
excess_blob_gas: Some(0),
blob_gas_and_fee: Some(BlobGasAndFee::new(0)),
}
}
}
Expand Down Expand Up @@ -383,7 +419,7 @@ impl Env {
#[inline]
pub fn calc_data_fee(&self) -> Option<u64> {
self.block
.get_blob_gasprice()
.get_blob_fee()
.map(|blob_gas_price| blob_gas_price * self.tx.get_total_blob_gas())
}

Expand All @@ -395,7 +431,7 @@ impl Env {
return Err(EVMError::PrevrandaoNotSet);
}
// `excess_blob_gas` is required for Cancun
if SPEC::enabled(SpecId::CANCUN) && self.block.excess_blob_gas.is_none() {
if SPEC::enabled(SpecId::CANCUN) && self.block.blob_gas_and_fee.is_none() {
return Err(EVMError::ExcessBlobGasNotSet);
}
Ok(())
Expand Down Expand Up @@ -459,7 +495,7 @@ impl Env {
// - For before CANCUN, check that `blob_hashes` and `max_fee_per_blob_gas` are empty / not set
if SPEC::enabled(SpecId::CANCUN) {
if let Some(max) = self.tx.max_fee_per_blob_gas {
let price = self.block.get_blob_gasprice().expect("already checked");
let price = self.block.get_blob_fee().expect("already checked");
if U256::from(price) > max {
return Err(InvalidTransaction::BlobGasPriceGreaterThanMax);
}
Expand Down