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

[Perf] Speed up the fetching of the latest block height #2515

Merged
merged 3 commits into from
Oct 22, 2024
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
6 changes: 3 additions & 3 deletions ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
const NUM_BLOCKS: usize = 10;
// Retrieve the latest height.
let latest_height = ledger.current_block.read().height();
debug_assert_eq!(latest_height, *ledger.vm.block_store().heights().max().unwrap(), "Mismatch in latest height");
debug_assert_eq!(latest_height, ledger.vm.block_store().max_height().unwrap(), "Mismatch in latest height");
// Sample random block heights.
let block_heights: Vec<u32> =
(0..=latest_height).choose_multiple(&mut OsRng, (latest_height as usize).min(NUM_BLOCKS));
Expand Down Expand Up @@ -169,15 +169,15 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
};

// If the block store is empty, initialize the genesis block.
if ledger.vm.block_store().heights().max().is_none() {
if ledger.vm.block_store().max_height().is_none() {
// Add the genesis block.
ledger.advance_to_next_block(&genesis_block)?;
}
lap!(timer, "Initialize genesis");

// Retrieve the latest height.
let latest_height =
*ledger.vm.block_store().heights().max().ok_or_else(|| anyhow!("Failed to load blocks from the ledger"))?;
ledger.vm.block_store().max_height().ok_or_else(|| anyhow!("Failed to load blocks from the ledger"))?;
// Fetch the latest block.
let block = ledger
.get_block(latest_height)
Expand Down
5 changes: 5 additions & 0 deletions ledger/store/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,11 @@ impl<N: Network, B: BlockStorage<N>> BlockStore<N, B> {
self.storage.id_map().keys_confirmed()
}

/// Returns the height of the latest block in the storage.
pub fn max_height(&self) -> Option<u32> {
self.storage.id_map().len_confirmed().checked_sub(1)?.try_into().ok()
}

/// Returns an iterator over the block hashes, for all blocks in `self`.
pub fn hashes(&self) -> impl '_ + Iterator<Item = Cow<'_, N::BlockHash>> {
self.storage.reverse_id_map().keys_confirmed()
Expand Down
4 changes: 1 addition & 3 deletions synthesizer/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ pub(crate) mod test_helpers {

use indexmap::IndexMap;
use once_cell::sync::OnceCell;
use std::borrow::Borrow;
#[cfg(feature = "rocks")]
use std::path::Path;
use synthesizer_snark::VerifyingKey;
Expand Down Expand Up @@ -763,8 +762,7 @@ function compute:
rng: &mut R,
) -> Result<Block<MainnetV0>> {
// Get the most recent block.
let block_hash =
vm.block_store().get_block_hash(*vm.block_store().heights().max().unwrap().borrow()).unwrap().unwrap();
let block_hash = vm.block_store().get_block_hash(vm.block_store().max_height().unwrap()).unwrap().unwrap();
let previous_block = vm.block_store().get_block(&block_hash).unwrap().unwrap();

// Construct the new block header.
Expand Down
6 changes: 2 additions & 4 deletions synthesizer/tests/test_vm_execute_and_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use anyhow::Result;
use console::account::Address;
use indexmap::IndexMap;
use rayon::prelude::*;
use std::borrow::Borrow;
use utilities::*;

#[test]
Expand Down Expand Up @@ -457,8 +456,7 @@ fn construct_next_block<C: ConsensusStorage<CurrentNetwork>, R: Rng + CryptoRng>
rng: &mut R,
) -> Result<Block<CurrentNetwork>> {
// Get the most recent block.
let block_hash =
vm.block_store().get_block_hash(*vm.block_store().heights().max().unwrap().borrow()).unwrap().unwrap();
let block_hash = vm.block_store().get_block_hash(vm.block_store().max_height().unwrap()).unwrap().unwrap();
let previous_block = vm.block_store().get_block(&block_hash).unwrap().unwrap();

// Construct the metadata associated with the block.
Expand Down Expand Up @@ -523,7 +521,7 @@ fn construct_finalize_global_state<C: ConsensusStorage<CurrentNetwork>>(
vm: &VM<CurrentNetwork, C>,
) -> FinalizeGlobalState {
// Retrieve the latest block.
let block_height = *vm.block_store().heights().max().unwrap().clone();
let block_height = vm.block_store().max_height().unwrap();
let latest_block_hash = vm.block_store().get_block_hash(block_height).unwrap().unwrap();
let latest_block = vm.block_store().get_block(&latest_block_hash).unwrap().unwrap();
// Retrieve the latest round.
Expand Down