From 89b299d7622ae32af25c1e4933ad249fd1330552 Mon Sep 17 00:00:00 2001 From: d1r1 Date: Mon, 6 Oct 2025 14:40:52 +0400 Subject: [PATCH 1/3] refactor(gas): adjust FUEL_DENOM_RATE to reflect actual performance ratio Context: Benchmarks show WASM instructions are ~20x faster than EVM instructions, not 1000x as originally estimated Change: Updated FUEL_DENOM_RATE from 1000 to 20 to align with measured performance characteristics Rationale: Original 1000:1 ratio was placeholder value. New 20:1 ratio based on empirical benchmarking maintains gas equivalence with EVM while accurately reflecting execution costs Verification: All fuel cost constants in fuel_procedure.rs scaled proportionally (divided by 50). Test assertions updated to expect new gas consumption values BREAKING CHANGE: Existing WASM contracts with explicit ConsumeFuel() calls will consume 50x more gas per unit of fuel. System builtin operations remain gas-equivalent to EVM --- crates/interpreter/src/gas/constants.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/interpreter/src/gas/constants.rs b/crates/interpreter/src/gas/constants.rs index bf4bacf875..8b786bd9c8 100644 --- a/crates/interpreter/src/gas/constants.rs +++ b/crates/interpreter/src/gas/constants.rs @@ -104,4 +104,4 @@ pub const MIN_CALLEE_GAS: u64 = CALL_STIPEND; /// A fuel denomination rate for rWasm vs. EVM opcodes /// /// Make sure this value is synchronized with a Fluentbase version -pub const FUEL_DENOM_RATE: u64 = 1000; +pub const FUEL_DENOM_RATE: u64 = 20; From bc8c2c0ef208c270c5185db37c5d422a65d98e91 Mon Sep 17 00:00:00 2001 From: d1r1 Date: Mon, 6 Oct 2025 20:07:00 +0400 Subject: [PATCH 2/3] refactor(gas): remove FUEL_DENOM_RATE, add fluent-testnet feature Context: FUEL_DENOM_RATE was duplicated between fluentbase and revm. Testnet uses legacy rate (1000), correct rate is 20. Change: Removed FUEL_DENOM_RATE constant from interpreter. Inlined fuel denomination logic in handler/validation.rs with 'fluent-testnet' feature for conditional compilation. Deleted unused Gas methods record_denominated_cost() and record_denominated_refund(). Added cfg-if dependency. Rationale: Eliminates constant duplication - fluentbase remains source of truth. Feature flag enables testnet backward compatibility while defaulting to correct rate for new deployments. --- Cargo.lock | 1 + crates/handler/Cargo.toml | 4 +++- crates/handler/src/validation.rs | 16 ++++++++++++++-- crates/interpreter/src/gas.rs | 17 ----------------- crates/interpreter/src/gas/constants.rs | 5 ----- 5 files changed, 18 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8824686daa..cdd43581f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4298,6 +4298,7 @@ dependencies = [ "alloy-signer", "alloy-signer-local", "auto_impl", + "cfg-if", "derive-where", "revm-bytecode", "revm-context", diff --git a/crates/handler/Cargo.toml b/crates/handler/Cargo.toml index 176e337938..f5ce9e055f 100644 --- a/crates/handler/Cargo.toml +++ b/crates/handler/Cargo.toml @@ -30,6 +30,7 @@ bytecode.workspace = true auto_impl.workspace = true derive-where.workspace = true +cfg-if.workspace = true # Optional serde = { version = "1.0", default-features = false, features = [ @@ -73,4 +74,5 @@ serde = [ "derive-where/serde" ] serde-json = ["serde"] -debug-print = [] \ No newline at end of file +debug-print = [] +fluent-testnet = [] # Compatibility with Fluent testnet \ No newline at end of file diff --git a/crates/handler/src/validation.rs b/crates/handler/src/validation.rs index 35d0e83911..c5b1271b8e 100644 --- a/crates/handler/src/validation.rs +++ b/crates/handler/src/validation.rs @@ -4,7 +4,7 @@ use context_interface::{ Block, Cfg, ContextTr, }; use core::cmp; -use interpreter::gas::{self, InitialAndFloorGas, FUEL_DENOM_RATE}; +use interpreter::gas::{self, InitialAndFloorGas}; use primitives::wasm::WASM_MAGIC_BYTES; use primitives::{eip4844, hardfork::SpecId, wasm::wasm_max_code_size, B256}; @@ -299,8 +299,20 @@ pub fn validate_initial_tx_gas( } let mut floor_gas = gas.floor_gas; + + // Fuel denomination rate rwasm -> evm + // Testnet uses legacy rate (1000), correct rate is 20 + // see more details here: + // https://github.com/fluentlabs-xyz/fluentbase/blob/devel/crates/types/src/lib.rs#L63 + if tx.input().starts_with(&WASM_MAGIC_BYTES) { - floor_gas /= FUEL_DENOM_RATE; + cfg_if::cfg_if! { + if #[cfg(feature = "fluent-testnet")] { + floor_gas /= 1000; + } else { + floor_gas /= 20; + } + } } // EIP-7623: Increase calldata cost diff --git a/crates/interpreter/src/gas.rs b/crates/interpreter/src/gas.rs index 7a2b1c5bfd..8cc52c2026 100644 --- a/crates/interpreter/src/gas.rs +++ b/crates/interpreter/src/gas.rs @@ -167,23 +167,6 @@ impl Gas { MemoryExtensionResult::Extended } - - /// Records the denominated fuel cost by converting the provided raw fuel cost - /// with a predefined fuel denomination rate (FUEL_DENOM_RATE) and logs it. - /// This operation does not round up due to syncing requirements between gas - /// and fuel rates. - #[inline] - pub fn record_denominated_cost(&mut self, fuel_cost: u64) -> bool { - // TODO(dmitry123): "we can't do round ceil here because we need to sync gas/fuel rates" - // self.record_cost((fuel_cost + FUEL_DENOM_RATE - 1) / FUEL_DENOM_RATE) - self.record_cost(fuel_cost / FUEL_DENOM_RATE) - } - - /// Records a denominated fuel refund value. - #[inline] - pub fn record_denominated_refund(&mut self, fuel_refund: i64) { - self.record_refund(fuel_refund / FUEL_DENOM_RATE as i64) - } } /// Result of attempting to extend memory during execution. diff --git a/crates/interpreter/src/gas/constants.rs b/crates/interpreter/src/gas/constants.rs index 8b786bd9c8..057d2d899f 100644 --- a/crates/interpreter/src/gas/constants.rs +++ b/crates/interpreter/src/gas/constants.rs @@ -100,8 +100,3 @@ pub const INITCODE_WORD_COST: u64 = 2; pub const CALL_STIPEND: u64 = 2300; /// Minimum gas that must be provided to a callee. pub const MIN_CALLEE_GAS: u64 = CALL_STIPEND; - -/// A fuel denomination rate for rWasm vs. EVM opcodes -/// -/// Make sure this value is synchronized with a Fluentbase version -pub const FUEL_DENOM_RATE: u64 = 20; From a30020ea2c951504a7faf4c78c8bf3f11c001cb5 Mon Sep 17 00:00:00 2001 From: d1r1 Date: Mon, 17 Nov 2025 16:52:24 +0400 Subject: [PATCH 3/3] chore: update rwasm deps --- Cargo.lock | 15 ++++++++------- Cargo.toml | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cdd43581f6..6cf56a93ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1389,7 +1389,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" dependencies = [ "lazy_static", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -2056,7 +2056,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -2870,7 +2870,7 @@ checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" dependencies = [ "hermit-abi", "libc", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -4591,7 +4591,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -4624,7 +4624,8 @@ dependencies = [ [[package]] name = "rwasm" version = "0.3.2" -source = "git+https://github.com/fluentlabs-xyz/rwasm?branch=devel#61894b725fb6a9f31ccb237328a74e29e7b9002d" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b36169175e8ed473a54baf2a23ff6cf3391c12abf12c339a18d18e4990ee3c8" dependencies = [ "bincode 2.0.1", "bitvec", @@ -5193,7 +5194,7 @@ dependencies = [ "getrandom 0.3.3", "once_cell", "rustix", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -6240,7 +6241,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 22516286ea..2a49558831 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -125,7 +125,7 @@ triehash = "0.8" walkdir = "2.5" # rwasm -rwasm = { git = "https://github.com/fluentlabs-xyz/rwasm", branch = "devel", default-features = false } +rwasm = { version = "0.3.2", default-features = false } #rwasm = { path = "../rwasm", default-features = false } [workspace.package]