diff --git a/CHANGELOG.md b/CHANGELOG.md index b9be2bca..f158f070 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ Description of the upcoming release here. - [#1395](https://github.com/FuelLabs/fuel-core/pull/1395): Add DependentCost benchmarks for `k256`, `s256` and `mcpi` instructions. - [#1408](https://github.com/FuelLabs/fuel-core/pull/1408): Update gas benchmarks for storage opcodes to use a pre-populated database to get more accurate worst-case costs. - [#1454](https://github.com/FuelLabs/fuel-core/pull/1454): Update gas benchmarks for opcodes that append receipts. +- [#1512](https://github.com/FuelLabs/fuel-core/pull/1512): Internally simplify merkle_contract_state_range. #### Breaking - [#1506](https://github.com/FuelLabs/fuel-core/pull/1506): Added validation of the coin's fields during block production and validation. Before, it was possible to submit a transaction that didn't match the coin's values in the database, allowing printing/using unavailable assets. diff --git a/benches/src/default_gas_costs.rs b/benches/src/default_gas_costs.rs index 9173fab9..3bcaa5a5 100644 --- a/benches/src/default_gas_costs.rs +++ b/benches/src/default_gas_costs.rs @@ -151,8 +151,8 @@ pub fn default_gas_costs() -> GasCostsValues { units_per_gas: 1, }, srwq: DependentCost::HeavyOperation { - base: 579, - gas_per_unit: 24, + base: 262, + gas_per_unit: 249, }, swwq: DependentCost::HeavyOperation { base: 28484, diff --git a/crates/fuel-core/src/database/vm_database.rs b/crates/fuel-core/src/database/vm_database.rs index cb461da1..47a79f15 100644 --- a/crates/fuel-core/src/database/vm_database.rs +++ b/crates/fuel-core/src/database/vm_database.rs @@ -1,11 +1,9 @@ use crate::database::{ - Column, Database, Error as DatabaseError, }; use anyhow::anyhow; use fuel_core_storage::{ - iter::IterDirection, not_found, tables::ContractsState, ContractsAssetsStorage, @@ -227,47 +225,18 @@ impl InterpreterStorage for VmDatabase { start_key: &Bytes32, range: usize, ) -> Result>>, Self::DataError> { - // TODO: Optimization: Iterate only over `range` elements. - let mut iterator = self.database.iter_all_filtered::, Bytes32, _, _>( - Column::ContractsState, - Some(contract_id), - Some(ContractsStateKey::new(contract_id, start_key)), - Some(IterDirection::Forward), - ); - - let mut expected_key = U256::from_big_endian(start_key.as_ref()); - let mut results = vec![]; - - while results.len() < range { - let entry = iterator.next().transpose()?; - - if entry.is_none() { - // We out of `contract_id` prefix - break - } + use fuel_core_storage::StorageAsRef; - let (multikey, value) = - entry.expect("We did a check before, so the entry should be `Some`"); - let actual_key = U256::from_big_endian(&multikey[32..]); - - while (expected_key <= actual_key) && results.len() < range { - if expected_key == actual_key { - // We found expected key, put value into results - results.push(Some(Cow::Owned(value))); - } else { - // Iterator moved beyond next expected key, push none until we find the key - results.push(None); - } - expected_key.increase()?; - } - } + let mut key = U256::from_big_endian(start_key.as_ref()); + let mut state_key = Bytes32::zeroed(); - // Fill not initialized slots with `None`. - while results.len() < range { - results.push(None); - expected_key.increase()?; + let mut results = Vec::new(); + for _ in 0..range { + key.to_big_endian(state_key.as_mut()); + let multikey = ContractsStateKey::new(contract_id, &state_key); + results.push(self.database.storage::().get(&multikey)?); + key.increase()?; } - Ok(results) } diff --git a/crates/types/src/blockchain/header.rs b/crates/types/src/blockchain/header.rs index c27dbde5..68497d3f 100644 --- a/crates/types/src/blockchain/header.rs +++ b/crates/types/src/blockchain/header.rs @@ -282,9 +282,7 @@ impl PartialBlockHeader { } fn generate_txns_root(transactions: &[Transaction]) -> Bytes32 { - // TODO: The `to_bytes` requires mutability(but it is problem of the API). - // Remove `clone` when we can use `to_bytes` without mutability. - let transaction_ids = transactions.iter().map(|tx| tx.clone().to_bytes()); + let transaction_ids = transactions.iter().map(|tx| tx.to_bytes()); // Generate the transaction merkle root. let mut transaction_tree = fuel_merkle::binary::root_calculator::MerkleRootCalculator::new(); diff --git a/deployment/scripts/chainspec/beta_chainspec.json b/deployment/scripts/chainspec/beta_chainspec.json index 5e4da862..a2695bc4 100644 --- a/deployment/scripts/chainspec/beta_chainspec.json +++ b/deployment/scripts/chainspec/beta_chainspec.json @@ -256,8 +256,8 @@ }, "srwq": { "HeavyOperation": { - "base": 579, - "gas_per_unit": 24 + "base": 262, + "gas_per_unit": 249 } }, "swwq": { diff --git a/deployment/scripts/chainspec/dev_chainspec.json b/deployment/scripts/chainspec/dev_chainspec.json index fc88b1c0..f71e0c7b 100644 --- a/deployment/scripts/chainspec/dev_chainspec.json +++ b/deployment/scripts/chainspec/dev_chainspec.json @@ -251,8 +251,8 @@ }, "srwq": { "HeavyOperation": { - "base": 579, - "gas_per_unit": 24 + "base": 262, + "gas_per_unit": 249 } }, "swwq": {