Skip to content

Commit

Permalink
Moves limit code into method
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Oct 12, 2023
1 parent f442214 commit 7662b6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
32 changes: 16 additions & 16 deletions zebra-state/src/service/finalized_state/disk_format/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,36 +416,36 @@ impl AddressTransaction {
}
}

/// Create an [`AddressTransaction`] which starts iteration for the supplied
/// Create a range of [`AddressTransaction`]s which starts iteration for the supplied
/// address. Starts at the first UTXO, or at the `query_start` height,
/// whichever is greater.
/// whichever is greater. Ends at the last transaction index for an [`AddressLocation`].
///
/// Used to look up the first transaction with
/// [`ReadDisk::zs_next_key_value_from`][1].
/// Used to look up transactions with
/// [`DiskDb::zs_range_iter`][1].
///
/// The transaction location might be invalid, if it is based on the
/// `query_start` height. But this is not an issue, since
/// [`ReadDisk::zs_next_key_value_from`][1] will fetch the next existing
/// (valid) value.
/// [`DiskDb::zs_range_iter`][1] will fetch all existing
/// (valid) values in the range.
///
/// [1]: super::super::disk_db::ReadDisk::zs_next_key_value_from
pub fn address_iterator_start(
/// [1]: super::super::disk_db::DiskDb
pub fn address_iterator_range(
address_location: AddressLocation,
query_start: Height,
) -> AddressTransaction {
) -> std::ops::RangeInclusive<AddressTransaction> {
// Iterating from the lowest possible transaction location gets us the first transaction.
//
// The address location is the output location of the first UTXO sent to the address,
// and addresses can not spend funds until they receive their first UTXO.
let first_utxo_location = address_location.transaction_location();

//
// Iterating from the start height filters out transactions that aren't needed.
let query_start_location = TransactionLocation::from_usize(query_start, 0);
let start_height = max(query_start, address_location.height());
let first_utxo_idx = address_location.transaction_index().0;

AddressTransaction {
address_location,
transaction_location: max(first_utxo_location, query_start_location),
}
let tx_loc = |tx_idx| TransactionLocation::from_index(start_height, tx_idx);
let addr_tx = |tx_idx| AddressTransaction::new(address_location, tx_loc(tx_idx));

addr_tx(first_utxo_idx)..=addr_tx(u16::MAX)
}

/// Update the transaction location to the next possible transaction for the
Expand Down
12 changes: 2 additions & 10 deletions zebra-state/src/service/finalized_state/zebra_db/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,13 @@ impl ZebraDb {

// A potentially invalid key representing the first UTXO send to the address,
// or the query start height.
let transaction_location = AddressTransaction::address_iterator_start(
let transaction_location_range = AddressTransaction::address_iterator_range(
address_location,
*query_height_range.start(),
);

let last_transaction_location = AddressTransaction::new(
address_location,
TransactionLocation::from_usize(*query_height_range.end(), u16::MAX.into()),
);

self.db
.zs_range_iter(
&tx_loc_by_transparent_addr_loc,
&transaction_location..=&last_transaction_location,
)
.zs_range_iter(&tx_loc_by_transparent_addr_loc, transaction_location_range)
.map(|(tx_loc, ())| tx_loc)
.collect()
}
Expand Down

0 comments on commit 7662b6b

Please sign in to comment.