Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of iter_all_filtered from merkle_contract_state_range #1512

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 8 additions & 39 deletions crates/fuel-core/src/database/vm_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,47 +227,16 @@ 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
}
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)?);
Dentosal marked this conversation as resolved.
Show resolved Hide resolved
key.increase()?;
}

Ok(results)
}

Expand Down
Loading