Skip to content

Commit

Permalink
implement fixed point division
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwen01 committed Sep 24, 2024
1 parent b2f48b4 commit 5189bf6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 5 additions & 5 deletions crates/optimism/src/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,20 +711,20 @@ mod tests {
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(10_501),
balance: U256::from(151),
..Default::default()
},
);
let mut context = Context::<TestMemOpWiring>::new_with_db(db);
*context.evm.chain.l1_block_info_mut() = Some(L1BlockInfo {
operator_fee_scalar: Some(U256::from(1_000)),
operator_fee_constant: Some(U256::from(500)),
operator_fee_scalar: Some(U256::from(10_000_000)),
operator_fee_constant: Some(U256::from(50)),
..Default::default()
});
context.evm.inner.env.tx.base.gas_limit = 10;

// operator fee cost is operator_fee_scalar * gas_limit + operator_fee_constant
// 1_000 * 10 + 500 = 10_500
// operator fee cost is operator_fee_scalar * gas_limit / 1e6 + operator_fee_constant
// 10_000_000 * 10 / 1_000_000 + 50 = 150
context.evm.inner.env.tx.enveloped_tx = Some(bytes!("FACADE"));
deduct_caller::<TestMemOpWiring, HoloceneSpec>(&mut context).unwrap();

Expand Down
12 changes: 9 additions & 3 deletions crates/optimism/src/l1block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const OPERATOR_FEE_SCALAR_OFFSET: usize = 4;
/// the storage slot of the 8-byte operatorFeeConstant attribute.
const OPERATOR_FEE_CONSTANT_OFFSET: usize = 8;

/// The fixed point decimal scaling factor associated with the operator fee scalar.
///
/// Allows users to use 6 decimal points of precision when specifying the operator_fee_scalar.
const OPERATOR_FEE_SCALAR_DECIMAL: u64 = 1_000_000;

const L1_BASE_FEE_SLOT: U256 = U256::from_limbs([1u64, 0, 0, 0]);
const L1_OVERHEAD_SLOT: U256 = U256::from_limbs([5u64, 0, 0, 0]);
const L1_SCALAR_SLOT: U256 = U256::from_limbs([6u64, 0, 0, 0]);
Expand Down Expand Up @@ -211,9 +216,10 @@ impl L1BlockInfo {
.operator_fee_constant
.expect("Missing operator fee constant for holocene L1 Block");

gas_limit
.saturating_mul(operator_fee_scalar)
.saturating_add(operator_fee_constant)
let product = gas_limit.saturating_mul(operator_fee_scalar)
/ (U256::from(OPERATOR_FEE_SCALAR_DECIMAL));

product.saturating_add(operator_fee_constant)
}

/// Calculate the operator fee for executing this transaction.
Expand Down

0 comments on commit 5189bf6

Please sign in to comment.