Skip to content

Commit

Permalink
Merge pull request ProvableHQ#1967 from niklaslong/cache-committee
Browse files Browse the repository at this point in the history
Cache the current committee in ledger
  • Loading branch information
howardwu authored Sep 18, 2023
2 parents 3a11532 + 98a5d1b commit 6d29869
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ledger/src/advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
// Drop the write lock on the current block.
drop(current_block);

// Update the cached committee from storage.
if let Ok(current_committee) = self.vm.finalize_store().committee_store().current_committee() {
*self.current_committee.write() = Some(current_committee);
}

// If the block is the start of a new epoch, or the epoch challenge has not been set, update the current epoch challenge.
if block.height() % N::NUM_BLOCKS_PER_EPOCH == 0 || self.current_epoch_challenge.read().is_none() {
// Update the current epoch challenge.
self.current_epoch_challenge.write().clone_from(&self.get_epoch_challenge(block.height()).ok());
}

Ok(())
}
}
Expand Down
16 changes: 13 additions & 3 deletions ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub struct Ledger<N: Network, C: ConsensusStorage<N>> {
coinbase_puzzle: CoinbasePuzzle<N>,
/// The current epoch challenge.
current_epoch_challenge: Arc<RwLock<Option<EpochChallenge<N>>>>,
/// The current committee.
current_committee: Arc<RwLock<Option<Committee<N>>>>,
/// The current block.
current_block: Arc<RwLock<Block<N>>>,
}
Expand Down Expand Up @@ -161,12 +163,16 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
let vm = VM::from(store)?;
lap!(timer, "Initialize a new VM");

// Retrieve the current committee.
let current_committee = vm.finalize_store().committee_store().current_committee().ok();

// Initialize the ledger.
let mut ledger = Self {
vm,
genesis_block: genesis_block.clone(),
coinbase_puzzle: CoinbasePuzzle::<N>::load()?,
current_epoch_challenge: Default::default(),
current_committee: Arc::new(RwLock::new(current_committee)),
current_block: Arc::new(RwLock::new(genesis_block.clone())),
};

Expand All @@ -187,11 +193,12 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {

// Set the current block.
ledger.current_block = Arc::new(RwLock::new(block));
// Set the current committee (and ensures the latest committee exists).
ledger.current_committee = Arc::new(RwLock::new(Some(ledger.latest_committee()?)));
// Set the current epoch challenge.
ledger.current_epoch_challenge = Arc::new(RwLock::new(Some(ledger.get_epoch_challenge(latest_height)?)));
lap!(timer, "Initialize ledger");

finish!(timer);
finish!(timer, "Initialize ledger");
Ok(ledger)
}

Expand All @@ -207,7 +214,10 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {

/// Returns the latest committee.
pub fn latest_committee(&self) -> Result<Committee<N>> {
self.vm.finalize_store().committee_store().current_committee()
match self.current_committee.read().as_ref() {
Some(committee) => Ok(committee.clone()),
None => self.vm.finalize_store().committee_store().current_committee(),
}
}

/// Returns the latest state root.
Expand Down

0 comments on commit 6d29869

Please sign in to comment.