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

tracking for new StateSync component changes #753

Draft
wants to merge 5 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions bin/miden-cli/src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ impl SyncCmd {
let new_details = client.sync_state().await?;

println!("State synced to block {}", new_details.block_num);
println!("New public notes: {}", new_details.received_notes.len());
println!("Tracked notes updated: {}", new_details.committed_notes.len());
println!("Committed notes: {}", new_details.committed_notes.len());
println!("Tracked notes consumed: {}", new_details.consumed_notes.len());
println!("Tracked accounts updated: {}", new_details.updated_accounts.len());
println!("Locked accounts: {}", new_details.locked_accounts.len());
Expand Down
4 changes: 2 additions & 2 deletions bin/miden-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl Cli {
let authenticator = ClientAuthenticator::new(rng, keystore.clone());

let client = Client::new(
Box::new(TonicRpcClient::new(
&(cli_config.rpc.endpoint.clone().into()),
Arc::new(TonicRpcClient::new(
&cli_config.rpc.endpoint.clone().into(),
cli_config.rpc.timeout_ms,
)),
rng,
Expand Down
5 changes: 3 additions & 2 deletions bin/miden-cli/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
fs::File,
io::{Read, Write},
path::{Path, PathBuf},
sync::Arc,
};

use assert_cmd::Command;
Expand Down Expand Up @@ -497,7 +498,7 @@ fn sync_cli(cli_path: &Path) -> u64 {
let updated_notes = String::from_utf8(output.stdout)
.unwrap()
.split_whitespace()
.skip_while(|&word| word != "updated:")
.skip_while(|&word| word != "notes:")
.find(|word| word.parse::<u64>().is_ok())
.unwrap()
.parse()
Expand Down Expand Up @@ -657,7 +658,7 @@ async fn create_rust_client_with_store_path(store_path: &Path) -> (TestClient, F
let authenticator = ClientAuthenticator::new(rng, keystore.clone());
(
TestClient::new(
Box::new(TonicRpcClient::new(&rpc_config.endpoint.into(), rpc_config.timeout_ms)),
Arc::new(TonicRpcClient::new(&rpc_config.endpoint.into(), rpc_config.timeout_ms)),
rng,
store,
std::sync::Arc::new(authenticator),
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ idxdb = ["dep:base64", "dep:serde-wasm-bindgen", "dep:wasm-bindgen", "dep:wasm-b
sqlite = ["dep:rusqlite", "dep:deadpool-sqlite", "std"]
std = ["miden-objects/std","miden-proving-service-client/std"]
testing = ["miden-objects/testing", "miden-lib/testing", "miden-tx/testing"]
tonic = ["std", "tonic/transport", "tonic/tls", "tonic/tls-native-roots"]
tonic = ["std", "tonic/transport", "tonic/tls", "tonic/tls-native-roots", "dep:tokio"]
web-tonic = ["dep:tonic-web-wasm-client"]

[dependencies]
Expand Down
9 changes: 4 additions & 5 deletions crates/rust-client/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloc::{
boxed::Box,
string::{String, ToString},
sync::Arc,
};
Expand Down Expand Up @@ -43,7 +42,7 @@ pub struct ClientBuilder {
/// An optional RPC endpoint.
rpc_endpoint: Option<Endpoint>,
/// An optional custom RPC client. If provided, this takes precedence over `rpc_endpoint`.
rpc_api: Option<Box<dyn NodeRpcClient + Send>>,
rpc_api: Option<Arc<dyn NodeRpcClient + Send>>,
/// The timeout (in milliseconds) used when constructing the RPC client.
timeout_ms: u64,
/// An optional store provided by the user.
Expand Down Expand Up @@ -84,7 +83,7 @@ impl ClientBuilder {
///
/// This method overrides any previously set RPC endpoint.
#[must_use]
pub fn with_rpc(mut self, client: Box<dyn NodeRpcClient + Send>) -> Self {
pub fn with_rpc(mut self, client: Arc<dyn NodeRpcClient + Send>) -> Self {
self.rpc_api = Some(client);
self
}
Expand Down Expand Up @@ -168,10 +167,10 @@ impl ClientBuilder {
/// - Returns an error if the keystore is not specified or fails to initialize.
pub async fn build(self) -> Result<Client<RpoRandomCoin>, ClientError> {
// Determine the RPC client to use.
let rpc_api: Box<dyn NodeRpcClient + Send> = if let Some(client) = self.rpc_api {
let rpc_api: Arc<dyn NodeRpcClient + Send> = if let Some(client) = self.rpc_api {
client
} else if let Some(endpoint) = self.rpc_endpoint {
Box::new(TonicRpcClient::new(&endpoint, self.timeout_ms))
Arc::new(TonicRpcClient::new(&endpoint, self.timeout_ms))
} else {
return Err(ClientError::ClientInitializationError(
"RPC client or endpoint is required. Call `.with_rpc(...)` or `.with_rpc_client(...)`."
Expand Down
10 changes: 4 additions & 6 deletions crates/rust-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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)),
//! rng,
//! store,
//! Arc::new(authenticator),
Expand All @@ -102,8 +102,6 @@
#[macro_use]
extern crate alloc;

use alloc::boxed::Box;

#[cfg(feature = "std")]
extern crate std;

Expand Down Expand Up @@ -221,7 +219,7 @@ pub struct Client<R: FeltRng> {
rng: R,
/// An instance of [`NodeRpcClient`] which provides a way for the client to connect to the
/// Miden node.
rpc_api: Box<dyn NodeRpcClient + Send>,
rpc_api: Arc<dyn NodeRpcClient + Send>,
/// An instance of a [`LocalTransactionProver`] which will be the default prover for the
/// client.
tx_prover: Arc<LocalTransactionProver>,
Expand Down Expand Up @@ -257,7 +255,7 @@ impl<R: FeltRng> Client<R> {
///
/// Returns an error if the client couldn't be instantiated.
pub fn new(
rpc_api: Box<dyn NodeRpcClient + Send>,
rpc_api: Arc<dyn NodeRpcClient + Send>,
rng: R,
store: Arc<dyn Store>,
authenticator: Arc<dyn TransactionAuthenticator>,
Expand Down Expand Up @@ -298,7 +296,7 @@ impl<R: FeltRng> Client<R> {
// --------------------------------------------------------------------------------------------

#[cfg(any(test, feature = "testing"))]
pub fn test_rpc_api(&mut self) -> &mut Box<dyn NodeRpcClient + Send> {
pub fn test_rpc_api(&mut self) -> &mut Arc<dyn NodeRpcClient + Send> {
&mut self.rpc_api
}

Expand Down
36 changes: 15 additions & 21 deletions crates/rust-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ use alloc::boxed::Box;
#[async_trait(?Send)]
impl NodeRpcClient for MockRpcApi {
async fn sync_notes(
&mut self,
&self,
_block_num: BlockNumber,
_note_tags: &[NoteTag],
) -> Result<NoteSyncInfo, RpcError> {
Expand All @@ -215,7 +215,7 @@ impl NodeRpcClient for MockRpcApi {

/// Executes the specified sync state request and returns the response.
async fn sync_state(
&mut self,
&self,
block_num: BlockNumber,
_account_ids: &[AccountId],
_note_tags: &[NoteTag],
Expand All @@ -229,7 +229,7 @@ impl NodeRpcClient for MockRpcApi {
/// Creates and executes a [GetBlockHeaderByNumberRequest].
/// Only used for retrieving genesis block right now so that's the only case we need to cover.
async fn get_block_header_by_number(
&mut self,
&self,
block_num: Option<BlockNumber>,
include_mmr_proof: bool,
) -> Result<(BlockHeader, Option<MmrProof>), RpcError> {
Expand All @@ -251,7 +251,7 @@ impl NodeRpcClient for MockRpcApi {
Ok((block.header().clone(), mmr_proof))
}

async fn get_notes_by_id(&mut self, note_ids: &[NoteId]) -> Result<Vec<NetworkNote>, RpcError> {
async fn get_notes_by_id(&self, note_ids: &[NoteId]) -> Result<Vec<NetworkNote>, RpcError> {
// assume all private notes for now
let hit_notes = note_ids.iter().filter_map(|id| self.notes.get(id));
let mut return_notes = vec![];
Expand All @@ -266,58 +266,52 @@ impl NodeRpcClient for MockRpcApi {
}

async fn submit_proven_transaction(
&mut self,
&self,
_proven_transaction: ProvenTransaction,
) -> std::result::Result<(), RpcError> {
// TODO: add some basic validations to test error cases
Ok(())
}

async fn get_account_details(
&mut self,
&self,
_account_id: AccountId,
) -> Result<AccountDetails, RpcError> {
unimplemented!("shouldn't be used for now")
panic!("shouldn't be used for now")
}

async fn get_account_proofs(
&mut self,
_account_ids: &BTreeSet<ForeignAccount>,
&self,
_: &BTreeSet<ForeignAccount>,
_code_commitments: Vec<AccountCode>,
) -> Result<AccountProofs, RpcError> {
// TODO: Implement fully
Ok((self.blocks.last().unwrap().header().block_num(), vec![]))
}

async fn check_nullifiers_by_prefix(
&mut self,
&self,
_prefix: &[u16],
_block_num: BlockNumber,
) -> Result<Vec<NullifierUpdate>, RpcError> {
// Always return an empty list for now since it's only used when importing
Ok(vec![])
}

async fn check_nullifiers(
&mut self,
_nullifiers: &[Nullifier],
) -> Result<Vec<SmtProof>, RpcError> {
async fn check_nullifiers(&self, _nullifiers: &[Nullifier]) -> Result<Vec<SmtProof>, RpcError> {
unimplemented!("shouldn't be used for now")
}

async fn get_account_state_delta(
&mut self,
&self,
_account_id: AccountId,
_from_block: BlockNumber,
_to_block: BlockNumber,
) -> Result<AccountDelta, RpcError> {
unimplemented!("shouldn't be used for now")
}

async fn get_block_by_number(
&mut self,
block_num: BlockNumber,
) -> Result<ProvenBlock, RpcError> {
async fn get_block_by_number(&self, block_num: BlockNumber) -> Result<ProvenBlock, RpcError> {
let block = self
.blocks
.iter()
Expand Down Expand Up @@ -345,9 +339,9 @@ pub async fn create_test_client() -> (MockClient, MockRpcApi, FilesystemKeyStore

let authenticator = ClientAuthenticator::new(rng, keystore.clone());
let rpc_api = MockRpcApi::new();
let boxed_rpc_api = Box::new(rpc_api.clone());
let arc_rpc_api = Arc::new(rpc_api.clone());

let client = MockClient::new(boxed_rpc_api, rng, store, Arc::new(authenticator.clone()), true);
let client = MockClient::new(arc_rpc_api, rng, store, Arc::new(authenticator.clone()), true);
(client, rpc_api, keystore)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rust-client/src/note/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<R: FeltRng> Client<R> {
/// - If the note doesn't exist on the node.
/// - If the note exists but is private.
async fn import_note_record_by_id(
&mut self,
&self,
previous_note: Option<InputNoteRecord>,
id: NoteId,
) -> Result<Option<InputNoteRecord>, ClientError> {
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<R: FeltRng> Client<R> {
/// If the note isn't consumed and it was committed in the past relative to the client, then
/// the MMR for the relevant block is fetched from the node and stored.
async fn import_note_record_by_proof(
&mut self,
&self,
previous_note: Option<InputNoteRecord>,
note: Note,
inclusion_proof: NoteInclusionProof,
Expand Down
Loading