From 6556b180a0fd55aeff50458280f1a1920ad490f3 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Thu, 23 Nov 2023 21:33:13 +0200 Subject: [PATCH 1/4] Remove usage of iter_all_filtered from merkle_contract_state_range --- crates/fuel-core/src/database/vm_database.rs | 47 ++++---------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/crates/fuel-core/src/database/vm_database.rs b/crates/fuel-core/src/database/vm_database.rs index cb461da1edb..4bb3aec5516 100644 --- a/crates/fuel-core/src/database/vm_database.rs +++ b/crates/fuel-core/src/database/vm_database.rs @@ -227,47 +227,16 @@ 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 - } + let mut multikey = [0u8; 64]; + multikey[..32].copy_from_slice(contract_id.as_ref()); + let mut key = U256::from_big_endian(start_key.as_ref()); - 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()?; - } - } - - // 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(&mut multikey[32..]); + results.push(self.database.get(&multikey, Column::ContractsState)?); + key.increase()?; } - Ok(results) } From 46d0f3bd1c5aaa35ee65b7df81f405ee6dc05d94 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Thu, 23 Nov 2023 21:37:09 +0200 Subject: [PATCH 2/4] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9be2bca069..f158f070fdf 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. From ed7b235b73c53a585bd96626a4944e3d5b58037a Mon Sep 17 00:00:00 2001 From: xgreenx Date: Thu, 23 Nov 2023 20:51:53 +0000 Subject: [PATCH 3/4] Applied comment --- crates/fuel-core/src/database/vm_database.rs | 12 ++++++------ crates/types/src/blockchain/header.rs | 4 +--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/crates/fuel-core/src/database/vm_database.rs b/crates/fuel-core/src/database/vm_database.rs index 4bb3aec5516..47a79f1582b 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,14 +225,16 @@ impl InterpreterStorage for VmDatabase { start_key: &Bytes32, range: usize, ) -> Result>>, Self::DataError> { - let mut multikey = [0u8; 64]; - multikey[..32].copy_from_slice(contract_id.as_ref()); + use fuel_core_storage::StorageAsRef; + let mut key = U256::from_big_endian(start_key.as_ref()); + let mut state_key = Bytes32::zeroed(); let mut results = Vec::new(); for _ in 0..range { - key.to_big_endian(&mut multikey[32..]); - results.push(self.database.get(&multikey, Column::ContractsState)?); + 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 c27dbde55f5..68497d3f30c 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(); From a1a33455170dbf50a9f9e6b31820de64cfa6b645 Mon Sep 17 00:00:00 2001 From: xgreenx Date: Thu, 23 Nov 2023 21:12:54 +0000 Subject: [PATCH 4/4] Updated prices according to the modifications --- benches/src/default_gas_costs.rs | 4 ++-- deployment/scripts/chainspec/beta_chainspec.json | 4 ++-- deployment/scripts/chainspec/dev_chainspec.json | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/benches/src/default_gas_costs.rs b/benches/src/default_gas_costs.rs index 9173fab92a1..3bcaa5a595b 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/deployment/scripts/chainspec/beta_chainspec.json b/deployment/scripts/chainspec/beta_chainspec.json index 5e4da862e16..a2695bc4da5 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 fc88b1c08b1..f71e0c7bbc4 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": {