Skip to content

Commit

Permalink
Remove usage of iter_all_filtered from merkle_contract_state_range (#…
Browse files Browse the repository at this point in the history
…1512)

Closes FuelLabs/fuel-core#1510

---------

Co-authored-by: xgreenx <xgreenx9999@gmail.com>
  • Loading branch information
crypto523 and xgreenx committed Nov 23, 2023
1 parent b955466 commit ac30dc5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions benches/src/default_gas_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 9 additions & 40 deletions crates/fuel-core/src/database/vm_database.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -227,47 +225,18 @@ impl InterpreterStorage for VmDatabase {
start_key: &Bytes32,
range: usize,
) -> Result<Vec<Option<Cow<Bytes32>>>, Self::DataError> {
// TODO: Optimization: Iterate only over `range` elements.
let mut iterator = self.database.iter_all_filtered::<Vec<u8>, 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::<ContractsState>().get(&multikey)?);
key.increase()?;
}

Ok(results)
}

Expand Down
4 changes: 1 addition & 3 deletions crates/types/src/blockchain/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions deployment/scripts/chainspec/beta_chainspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@
},
"srwq": {
"HeavyOperation": {
"base": 579,
"gas_per_unit": 24
"base": 262,
"gas_per_unit": 249
}
},
"swwq": {
Expand Down
4 changes: 2 additions & 2 deletions deployment/scripts/chainspec/dev_chainspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@
},
"srwq": {
"HeavyOperation": {
"base": 579,
"gas_per_unit": 24
"base": 262,
"gas_per_unit": 249
}
},
"swwq": {
Expand Down

0 comments on commit ac30dc5

Please sign in to comment.