Skip to content

Commit

Permalink
review: naming and sections
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyrd committed Jan 27, 2025
1 parent aafa399 commit ba997fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/rust-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
//! // Instantiate the client using a Tonic RPC client
//! let endpoint = Endpoint::new("https".into(), "localhost".into(), Some(57291));
//! let client: Client<RpoRandomCoin> = Client::new(
//! Box::new(TonicRpcClient::new(endpoint, 10_000)),
//! Arc::new(TonicRpcClient::new(endpoint, 10_000).await?),
//! rng,
//! store,
//! Arc::new(authenticator),
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-client/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a Tonic RPC client instance (assumes default endpoint configuration).
//! let endpoint = Endpoint::new("https".into(), "localhost".into(), Some(57291));
//! let mut rpc_client = TonicRpcClient::new(endpoint, 1000);
//! let mut rpc_client = TonicRpcClient::new(endpoint, 1000).await?;
//!
//! // Fetch the latest block header (by passing None).
//! let (block_header, mmr_proof) = rpc_client.get_block_header_by_number(None, true).await?;
Expand Down
60 changes: 32 additions & 28 deletions crates/rust-client/src/sync/state_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub struct StateSync {
/// Callback to be executed when a new note inclusion is received.
on_note_received: OnNoteReceived,
/// Callback to be executed when a transaction is committed.
on_committed_transaction: OnTransactionCommitted,
on_transaction_committed: OnTransactionCommitted,
/// Callback to be executed when a nullifier is received.
on_nullifier_received: OnNullifierReceived,
}
Expand All @@ -169,7 +169,7 @@ impl StateSync {
Self {
rpc_api,
on_note_received,
on_committed_transaction,
on_transaction_committed: on_committed_transaction,
on_nullifier_received,
}
}
Expand Down Expand Up @@ -261,6 +261,7 @@ impl StateSync {

// HELPERS
// --------------------------------------------------------------------------------------------

/// Compares the state of tracked accounts with the updates received from the node and returns
/// the accounts that need to be updated.
///
Expand Down Expand Up @@ -373,7 +374,7 @@ impl StateSync {

for transaction_update in transactions {
let (new_note_update, new_transaction_update) =
(self.on_committed_transaction)(transaction_update).await?;
(self.on_transaction_committed)(transaction_update).await?;

// Remove nullifiers if they were consumed by the transaction
nullifiers.retain(|nullifier| {
Expand Down Expand Up @@ -421,6 +422,34 @@ impl StateSync {
}
}

// HELPERS
// ================================================================================================

/// Applies changes to the current MMR structure, returns the updated [MmrPeaks] and the
/// authentication nodes for leaves we track.
pub(crate) async fn apply_mmr_changes(
current_block: BlockHeader,
current_block_has_relevant_notes: bool,
mut current_partial_mmr: PartialMmr,
mmr_delta: MmrDelta,
) -> Result<(MmrPeaks, Vec<(InOrderIndex, Digest)>), ClientError> {
// First, apply curent_block to the MMR. This is needed as the MMR delta received from the
// node doesn't contain the request block itself.
let new_authentication_nodes = current_partial_mmr
.add(current_block.hash(), current_block_has_relevant_notes)
.into_iter();

// Apply the MMR delta to bring MMR to forest equal to chain tip
let new_authentication_nodes: Vec<(InOrderIndex, Digest)> = current_partial_mmr
.apply(mmr_delta)
.map_err(StoreError::MmrError)?
.into_iter()
.chain(new_authentication_nodes)
.collect();

Ok((current_partial_mmr.peaks(), new_authentication_nodes))
}

// DEFAULT CALLBACK IMPLEMENTATIONS
// ================================================================================================

Expand Down Expand Up @@ -479,31 +508,6 @@ pub async fn on_note_received(
))
}

/// Applies changes to the current MMR structure, returns the updated [MmrPeaks] and the
/// authentication nodes for leaves we track.
pub(crate) async fn apply_mmr_changes(
current_block: BlockHeader,
current_block_has_relevant_notes: bool,
mut current_partial_mmr: PartialMmr,
mmr_delta: MmrDelta,
) -> Result<(MmrPeaks, Vec<(InOrderIndex, Digest)>), ClientError> {
// First, apply curent_block to the MMR. This is needed as the MMR delta received from the
// node doesn't contain the request block itself.
let new_authentication_nodes = current_partial_mmr
.add(current_block.hash(), current_block_has_relevant_notes)
.into_iter();

// Apply the MMR delta to bring MMR to forest equal to chain tip
let new_authentication_nodes: Vec<(InOrderIndex, Digest)> = current_partial_mmr
.apply(mmr_delta)
.map_err(StoreError::MmrError)?
.into_iter()
.chain(new_authentication_nodes)
.collect();

Ok((current_partial_mmr.peaks(), new_authentication_nodes))
}

/// Default implementation of the [OnTransactionCommitted] callback. It queries the store for the
/// input notes that were consumed by the transaction and updates the note records accordingly. It
/// also returns the committed transaction update to be applied to the store.
Expand Down

0 comments on commit ba997fc

Please sign in to comment.