Skip to content

Commit

Permalink
chore(iterators): replace iter_all with iter_all_keys where required (#…
Browse files Browse the repository at this point in the history
…2076)

## Linked Issues

Related #1997

## Description

Replaces usages of `iter_all` with `iter_all_keys` in instances where we
only need to iterate over the keys. A follow up PR will be made to
actually iterate over the keys without making allocations for the
values, as described in
https://github.com/FuelLabs/fuel-core/blob/a9e5e89f5fb38e3b3d6e6bdfe1ad339a01f2f3b9/crates/storage/src/iter.rs#L130

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog (no breaking changes)
- [x] New behavior is reflected in tests (existing test cases which test
behaviour of iterators pass)
- [x] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (no changes to the spec)

### Before requesting review
- [x] I have reviewed the code myself
- [ ] I have created follow-up issues caused by this PR and linked them
here

### After merging, notify other teams

[Add or remove entries as needed]

- [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/)
- [ ] [Sway compiler](https://github.com/FuelLabs/sway/)
- [ ] [Platform
documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+)
(for out-of-organization contributors, the person merging the PR will do
this)
- [ ] Someone else?
  • Loading branch information
rymnc authored Aug 14, 2024
1 parent 07533b6 commit ff2b703
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
-[2076](https://github.com/FuelLabs/fuel-core/pull/2076): Replace usages of `iter_all` with `iter_all_keys` where necessary.

## [Version 0.32.1]

### Added
Expand Down
Binary file not shown.
12 changes: 4 additions & 8 deletions crates/fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ pub type GenesisDatabase<Description = OnChain> = Database<Description, GenesisS

impl OnChainIterableKeyValueView {
pub fn latest_height(&self) -> StorageResult<BlockHeight> {
self.iter_all::<FuelBlocks>(Some(IterDirection::Reverse))
self.iter_all_keys::<FuelBlocks>(Some(IterDirection::Reverse))
.next()
.ok_or(not_found!("BlockHeight"))?
.map(|(height, _)| height)
}

pub fn latest_block(&self) -> StorageResult<CompressedBlock> {
Expand Down Expand Up @@ -328,8 +327,7 @@ where
impl Modifiable for Database<OnChain> {
fn commit_changes(&mut self, changes: Changes) -> StorageResult<()> {
commit_changes_with_height_update(self, changes, |iter| {
iter.iter_all::<FuelBlocks>(Some(IterDirection::Reverse))
.map(|result| result.map(|(height, _)| height))
iter.iter_all_keys::<FuelBlocks>(Some(IterDirection::Reverse))
.try_collect()
})
}
Expand All @@ -348,8 +346,7 @@ impl Modifiable for Database<OffChain> {
impl Modifiable for Database<GasPriceDatabase> {
fn commit_changes(&mut self, changes: Changes) -> StorageResult<()> {
commit_changes_with_height_update(self, changes, |iter| {
iter.iter_all::<GasPriceMetadata>(Some(IterDirection::Reverse))
.map(|result| result.map(|(height, _)| height))
iter.iter_all_keys::<GasPriceMetadata>(Some(IterDirection::Reverse))
.try_collect()
})
}
Expand All @@ -359,10 +356,9 @@ impl Modifiable for Database<GasPriceDatabase> {
impl Modifiable for Database<Relayer> {
fn commit_changes(&mut self, changes: Changes) -> StorageResult<()> {
commit_changes_with_height_update(self, changes, |iter| {
iter.iter_all::<fuel_core_relayer::storage::EventsHistory>(Some(
iter.iter_all_keys::<fuel_core_relayer::storage::EventsHistory>(Some(
IterDirection::Reverse,
))
.map(|result| result.map(|(height, _)| height))
.try_collect()
})
}
Expand Down
10 changes: 5 additions & 5 deletions crates/fuel-core/src/database/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ impl OffChainIterableKeyValueView {
direction: Option<IterDirection>,
) -> impl Iterator<Item = StorageResult<UtxoId>> + '_ {
let start_coin = start_coin.map(|b| owner_coin_id_key(owner, &b));
self.iter_all_filtered::<OwnedCoins, _>(
Some(*owner), start_coin.as_ref(),
self.iter_all_filtered_keys::<OwnedCoins, _>(
Some(*owner),
start_coin.as_ref(),
direction,
)
// Safety: key is always 64 bytes
.map(|res| {
res.map(|(key, _)| {
res.map(|key| {
UtxoId::new(
TxId::try_from(&key[32..64]).expect("The slice has size 32"),
u16::from_be_bytes(
key[64..].try_into().expect("The slice has size 2"),
)
),
)
})
})
Expand Down
4 changes: 2 additions & 2 deletions crates/fuel-core/src/database/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ impl OffChainIterableKeyValueView {
) -> impl Iterator<Item = StorageResult<Nonce>> + '_ {
let start_message_id =
start_message_id.map(|msg_id| OwnedMessageKey::new(owner, &msg_id));
self.iter_all_filtered::<OwnedMessageIds, _>(
self.iter_all_filtered_keys::<OwnedMessageIds, _>(
Some(*owner),
start_message_id.as_ref(),
direction,
)
.map(|res| res.map(|(key, _)| *key.nonce()))
.map(|res| res.map(|key| *key.nonce()))
}

pub fn message_is_spent(&self, id: &Nonce) -> StorageResult<bool> {
Expand Down
6 changes: 2 additions & 4 deletions crates/fuel-core/src/database/sealed_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ impl OnChainIterableKeyValueView {
}

pub fn genesis_height(&self) -> StorageResult<Option<BlockHeight>> {
Ok(self
.iter_all::<FuelBlocks>(Some(IterDirection::Forward))
self.iter_all_keys::<FuelBlocks>(Some(IterDirection::Forward))
.next()
.transpose()?
.map(|(height, _)| height))
.transpose()
}

pub fn genesis_block(&self) -> StorageResult<Option<CompressedBlock>> {
Expand Down
6 changes: 2 additions & 4 deletions crates/fuel-core/src/service/adapters/block_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ impl BlockVerifier for VerifierAdapter {

impl ImporterDatabase for Database {
fn latest_block_height(&self) -> StorageResult<Option<BlockHeight>> {
Ok(self
.iter_all::<FuelBlocks>(Some(IterDirection::Reverse))
self.iter_all_keys::<FuelBlocks>(Some(IterDirection::Reverse))
.next()
.transpose()?
.map(|(height, _)| height))
.transpose()
}

fn latest_block_root(&self) -> StorageResult<Option<MerkleRoot>> {
Expand Down
6 changes: 2 additions & 4 deletions crates/fuel-core/src/service/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ pub async fn execute_genesis_block(

let genesis_progress_on_chain: Vec<String> = db
.on_chain()
.iter_all::<GenesisMetadata<OnChain>>(None)
.map_ok(|(k, _)| k)
.iter_all_keys::<GenesisMetadata<OnChain>>(None)
.try_collect()?;
let genesis_progress_off_chain: Vec<String> = db
.off_chain()
.iter_all::<GenesisMetadata<OffChain>>(None)
.map_ok(|(k, _)| k)
.iter_all_keys::<GenesisMetadata<OffChain>>(None)
.try_collect()?;

let chain_config = config.snapshot_reader.chain_config();
Expand Down

0 comments on commit ff2b703

Please sign in to comment.