From b254ebb185f000f5751e8de426866de7f6cc1ee0 Mon Sep 17 00:00:00 2001 From: Daniil Polyakov Date: Fri, 13 Mar 2026 22:38:23 +0300 Subject: [PATCH] feat: refactor sequencer RPC client-side --- Cargo.lock | 7 +- examples/program_deployment/Cargo.toml | 2 + .../src/bin/run_hello_world.rs | 4 +- .../bin/run_hello_world_through_tail_call.rs | 4 +- .../bin/run_hello_world_with_authorization.rs | 4 +- ...uthorization_through_tail_call_with_pda.rs | 4 +- .../bin/run_hello_world_with_move_function.rs | 6 +- explorer_service/src/api.rs | 11 +- .../src/pages/transaction_page.rs | 4 +- indexer/core/src/block_store.rs | 27 +++-- indexer/service/rpc/src/lib.rs | 12 ++- indexer/service/src/lib.rs | 6 +- indexer/service/src/mock_service.rs | 35 +++--- indexer/service/src/service.rs | 18 ++-- integration_tests/Cargo.toml | 1 + integration_tests/src/config.rs | 2 - integration_tests/src/lib.rs | 47 ++++---- integration_tests/tests/account.rs | 4 +- integration_tests/tests/amm.rs | 61 +++-------- .../tests/auth_transfer/private.rs | 7 +- .../tests/auth_transfer/public.rs | 27 +++-- integration_tests/tests/block_size_limit.rs | 42 +++++--- integration_tests/tests/indexer.rs | 50 +++++---- integration_tests/tests/keys_restoration.rs | 5 +- integration_tests/tests/pinata.rs | 34 +++--- integration_tests/tests/program_deployment.rs | 13 +-- integration_tests/tests/token.rs | 43 +++----- integration_tests/tests/tps.rs | 13 ++- .../src/key_management/key_tree/mod.rs | 85 ++------------- sequencer/core/src/block_store.rs | 26 +++-- sequencer/core/src/lib.rs | 7 +- sequencer/service/rpc/Cargo.toml | 1 - sequencer/service/rpc/src/lib.rs | 33 ++++-- sequencer/service/src/lib.rs | 17 ++- sequencer/service/src/service.rs | 45 ++++---- storage/src/error.rs | 7 -- storage/src/indexer.rs | 100 +++++++++++------- storage/src/sequencer.rs | 32 ++++-- wallet-ffi/Cargo.toml | 1 + wallet-ffi/src/lib.rs | 2 +- wallet-ffi/src/pinata.rs | 12 +-- wallet-ffi/src/sync.rs | 8 +- wallet-ffi/src/transfer.rs | 32 +++--- wallet/Cargo.toml | 2 + wallet/src/cli/account.rs | 7 +- wallet/src/cli/chain.rs | 16 ++- wallet/src/cli/mod.rs | 27 +++-- .../src/cli/programs/native_token_transfer.rs | 44 ++++---- wallet/src/cli/programs/pinata.rs | 10 +- wallet/src/cli/programs/token.rs | 80 ++++++-------- wallet/src/lib.rs | 74 ++++++------- wallet/src/pinata_interactions.rs | 18 ++-- wallet/src/poller.rs | 39 +++---- wallet/src/privacy_preserving_tx.rs | 3 +- wallet/src/program_facades/amm.rs | 37 +++++-- .../native_token_transfer/deshielded.rs | 6 +- .../native_token_transfer/mod.rs | 3 +- .../native_token_transfer/private.rs | 10 +- .../native_token_transfer/public.rs | 20 +++- .../native_token_transfer/shielded.rs | 8 +- wallet/src/program_facades/pinata.rs | 17 +-- wallet/src/program_facades/token.rs | 69 +++++++----- wallet/src/transaction_utils.rs | 48 +++++---- 63 files changed, 717 insertions(+), 722 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1695180d..fdd8395c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3571,6 +3571,7 @@ dependencies = [ "nssa_core", "sequencer_core", "sequencer_service", + "sequencer_service_rpc", "serde_json", "tempfile", "testcontainers", @@ -5868,8 +5869,10 @@ name = "program_deployment" version = "0.1.0" dependencies = [ "clap", + "common", "nssa", "nssa_core", + "sequencer_service_rpc", "tokio", "wallet", ] @@ -7197,7 +7200,6 @@ dependencies = [ "jsonrpsee", "nssa", "nssa_core", - "schemars 1.2.1", "serde_json", ] @@ -8667,9 +8669,11 @@ dependencies = [ "nssa_core", "optfield", "rand 0.8.5", + "sequencer_service_rpc", "serde", "serde_json", "sha2", + "thiserror 2.0.18", "token_core", "tokio", "url", @@ -8683,6 +8687,7 @@ dependencies = [ "common", "nssa", "nssa_core", + "sequencer_service_rpc", "tempfile", "tokio", "wallet", diff --git a/examples/program_deployment/Cargo.toml b/examples/program_deployment/Cargo.toml index 96964a36..c41d9247 100644 --- a/examples/program_deployment/Cargo.toml +++ b/examples/program_deployment/Cargo.toml @@ -8,8 +8,10 @@ license = { workspace = true } workspace = true [dependencies] +common.workspace = true nssa.workspace = true nssa_core.workspace = true +sequencer_service_rpc = { workspace = true, features = ["client"] } wallet.workspace = true tokio = { workspace = true, features = ["macros"] } diff --git a/examples/program_deployment/src/bin/run_hello_world.rs b/examples/program_deployment/src/bin/run_hello_world.rs index 3c0c9034..3d89b1a4 100644 --- a/examples/program_deployment/src/bin/run_hello_world.rs +++ b/examples/program_deployment/src/bin/run_hello_world.rs @@ -1,8 +1,10 @@ +use common::transaction::NSSATransaction; use nssa::{ AccountId, PublicTransaction, program::Program, public_transaction::{Message, WitnessSet}, }; +use sequencer_service_rpc::RpcClient as _; use wallet::WalletCore; // Before running this example, compile the `hello_world.rs` guest program with: @@ -58,7 +60,7 @@ async fn main() { // Submit the transaction let _response = wallet_core .sequencer_client - .send_tx_public(tx) + .send_transaction(NSSATransaction::Public(tx)) .await .unwrap(); } diff --git a/examples/program_deployment/src/bin/run_hello_world_through_tail_call.rs b/examples/program_deployment/src/bin/run_hello_world_through_tail_call.rs index 56d28084..c3c75b5f 100644 --- a/examples/program_deployment/src/bin/run_hello_world_through_tail_call.rs +++ b/examples/program_deployment/src/bin/run_hello_world_through_tail_call.rs @@ -1,8 +1,10 @@ +use common::transaction::NSSATransaction; use nssa::{ AccountId, PublicTransaction, program::Program, public_transaction::{Message, WitnessSet}, }; +use sequencer_service_rpc::RpcClient as _; use wallet::WalletCore; // Before running this example, compile the `simple_tail_call.rs` guest program with: @@ -54,7 +56,7 @@ async fn main() { // Submit the transaction let _response = wallet_core .sequencer_client - .send_tx_public(tx) + .send_transaction(NSSATransaction::Public(tx)) .await .unwrap(); } diff --git a/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs b/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs index f38443ac..39e1380f 100644 --- a/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs +++ b/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs @@ -1,9 +1,11 @@ +use common::transaction::NSSATransaction; use nssa::{ AccountId, PublicTransaction, program::Program, public_transaction::{Message, WitnessSet}, }; use nssa_core::account::Nonce; +use sequencer_service_rpc::RpcClient as _; use wallet::WalletCore; // Before running this example, compile the `hello_world_with_authorization.rs` guest program with: @@ -78,7 +80,7 @@ async fn main() { // Submit the transaction let _response = wallet_core .sequencer_client - .send_tx_public(tx) + .send_transaction(NSSATransaction::Public(tx)) .await .unwrap(); } diff --git a/examples/program_deployment/src/bin/run_hello_world_with_authorization_through_tail_call_with_pda.rs b/examples/program_deployment/src/bin/run_hello_world_with_authorization_through_tail_call_with_pda.rs index 4371b000..e6a8ca99 100644 --- a/examples/program_deployment/src/bin/run_hello_world_with_authorization_through_tail_call_with_pda.rs +++ b/examples/program_deployment/src/bin/run_hello_world_with_authorization_through_tail_call_with_pda.rs @@ -3,12 +3,14 @@ reason = "This is an example program, it's fine to print to stdout" )] +use common::transaction::NSSATransaction; use nssa::{ AccountId, PublicTransaction, program::Program, public_transaction::{Message, WitnessSet}, }; use nssa_core::program::PdaSeed; +use sequencer_service_rpc::RpcClient as _; use wallet::WalletCore; // Before running this example, compile the `simple_tail_call.rs` guest program with: @@ -56,7 +58,7 @@ async fn main() { // Submit the transaction let _response = wallet_core .sequencer_client - .send_tx_public(tx) + .send_transaction(NSSATransaction::Public(tx)) .await .unwrap(); diff --git a/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs b/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs index 0d4af502..a1c2517e 100644 --- a/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs +++ b/examples/program_deployment/src/bin/run_hello_world_with_move_function.rs @@ -1,5 +1,7 @@ use clap::{Parser, Subcommand}; +use common::transaction::NSSATransaction; use nssa::{PublicTransaction, program::Program, public_transaction}; +use sequencer_service_rpc::RpcClient as _; use wallet::{PrivacyPreservingAccount, WalletCore}; // Before running this example, compile the `hello_world_with_move_function.rs` guest program with: @@ -87,7 +89,7 @@ async fn main() { // Submit the transaction let _response = wallet_core .sequencer_client - .send_tx_public(tx) + .send_transaction(NSSATransaction::Public(tx)) .await .unwrap(); } @@ -126,7 +128,7 @@ async fn main() { // Submit the transaction let _response = wallet_core .sequencer_client - .send_tx_public(tx) + .send_transaction(NSSATransaction::Public(tx)) .await .unwrap(); } diff --git a/explorer_service/src/api.rs b/explorer_service/src/api.rs index b37145af..8c2a0e36 100644 --- a/explorer_service/src/api.rs +++ b/explorer_service/src/api.rs @@ -41,12 +41,12 @@ pub async fn search(query: String) -> Result { // Try as hash if let Ok(hash) = HashType::from_str(&query) { // Try as block hash - if let Ok(block) = client.get_block_by_hash(hash).await { + if let Ok(Some(block)) = client.get_block_by_hash(hash).await { blocks.push(block); } // Try as transaction hash - if let Ok(tx) = client.get_transaction(hash).await { + if let Ok(Some(tx)) = client.get_transaction(hash).await { transactions.push(tx); } } @@ -60,7 +60,7 @@ pub async fn search(query: String) -> Result { // Try as block ID if let Ok(block_id) = query.parse::() - && let Ok(block) = client.get_block_by_id(block_id).await + && let Ok(Some(block)) = client.get_block_by_id(block_id).await { blocks.push(block); } @@ -81,6 +81,7 @@ pub async fn get_block_by_id(block_id: BlockId) -> Result .get_block_by_id(block_id) .await .map_err(|e| ServerFnError::ServerError(format!("RPC error: {e}"))) + .and_then(|opt| opt.ok_or_else(|| ServerFnError::ServerError("Block not found".to_owned()))) } /// Get latest block ID @@ -103,6 +104,7 @@ pub async fn get_block_by_hash(block_hash: HashType) -> Result Result impl IntoView { } = witness_set; let program_id_str = program_id.to_string(); - let proof_len = proof.0.len(); + let proof_len = proof.map_or(0, |p| p.0.len()); let signatures_count = signatures_and_public_keys.len(); view! { @@ -183,7 +183,7 @@ pub fn TransactionPage() -> impl IntoView { proof, } = witness_set; - let proof_len = proof.0.len(); + let proof_len = proof.map_or(0, |p| p.0.len()); view! {

"Privacy-Preserving Transaction Details"

diff --git a/indexer/core/src/block_store.rs b/indexer/core/src/block_store.rs index db2f855b..384217ff 100644 --- a/indexer/core/src/block_store.rs +++ b/indexer/core/src/block_store.rs @@ -46,7 +46,7 @@ impl IndexerStore { Ok(self.dbio.get_meta_last_block_in_db()?) } - pub fn get_block_at_id(&self, id: u64) -> Result { + pub fn get_block_at_id(&self, id: u64) -> Result> { Ok(self.dbio.get_block(id)?) } @@ -54,20 +54,25 @@ impl IndexerStore { Ok(self.dbio.get_block_batch(before, limit)?) } - pub fn get_transaction_by_hash(&self, tx_hash: [u8; 32]) -> Result { - let block = self.get_block_at_id(self.dbio.get_block_id_by_tx_hash(tx_hash)?)?; - let transaction = block + pub fn get_transaction_by_hash(&self, tx_hash: [u8; 32]) -> Result> { + let Some(block_id) = self.dbio.get_block_id_by_tx_hash(tx_hash)? else { + return Ok(None); + }; + let Some(block) = self.get_block_at_id(block_id)? else { + return Ok(None); + }; + Ok(block .body .transactions - .iter() - .find(|enc_tx| enc_tx.hash().0 == tx_hash) - .ok_or_else(|| anyhow::anyhow!("Transaction not found in DB"))?; - - Ok(transaction.clone()) + .into_iter() + .find(|enc_tx| enc_tx.hash().0 == tx_hash)) } - pub fn get_block_by_hash(&self, hash: [u8; 32]) -> Result { - self.get_block_at_id(self.dbio.get_block_id_by_hash(hash)?) + pub fn get_block_by_hash(&self, hash: [u8; 32]) -> Result> { + let Some(id) = self.dbio.get_block_id_by_hash(hash)? else { + return Ok(None); + }; + self.get_block_at_id(id) } pub fn get_transactions_by_account( diff --git a/indexer/service/rpc/src/lib.rs b/indexer/service/rpc/src/lib.rs index be0e45ca..217c60d4 100644 --- a/indexer/service/rpc/src/lib.rs +++ b/indexer/service/rpc/src/lib.rs @@ -30,16 +30,22 @@ pub trait Rpc { async fn get_last_finalized_block_id(&self) -> Result; #[method(name = "getBlockById")] - async fn get_block_by_id(&self, block_id: BlockId) -> Result; + async fn get_block_by_id(&self, block_id: BlockId) -> Result, ErrorObjectOwned>; #[method(name = "getBlockByHash")] - async fn get_block_by_hash(&self, block_hash: HashType) -> Result; + async fn get_block_by_hash( + &self, + block_hash: HashType, + ) -> Result, ErrorObjectOwned>; #[method(name = "getAccount")] async fn get_account(&self, account_id: AccountId) -> Result; #[method(name = "getTransaction")] - async fn get_transaction(&self, tx_hash: HashType) -> Result; + async fn get_transaction( + &self, + tx_hash: HashType, + ) -> Result, ErrorObjectOwned>; #[method(name = "getBlocks")] async fn get_blocks( diff --git a/indexer/service/src/lib.rs b/indexer/service/src/lib.rs index 48e4f876..47f9289e 100644 --- a/indexer/service/src/lib.rs +++ b/indexer/service/src/lib.rs @@ -3,7 +3,7 @@ use std::net::SocketAddr; use anyhow::{Context as _, Result}; pub use indexer_core::config::*; use indexer_service_rpc::RpcServer as _; -use jsonrpsee::server::Server; +use jsonrpsee::server::{Server, ServerHandle}; use log::{error, info}; pub mod service; @@ -14,10 +14,10 @@ pub mod mock_service; pub struct IndexerHandle { addr: SocketAddr, /// Option because of `Drop` which forbids to simply move out of `self` in `stopped()`. - server_handle: Option, + server_handle: Option, } impl IndexerHandle { - const fn new(addr: SocketAddr, server_handle: jsonrpsee::server::ServerHandle) -> Self { + const fn new(addr: SocketAddr, server_handle: ServerHandle) -> Self { Self { addr, server_handle: Some(server_handle), diff --git a/indexer/service/src/mock_service.rs b/indexer/service/src/mock_service.rs index b52123bc..eb6f11f7 100644 --- a/indexer/service/src/mock_service.rs +++ b/indexer/service/src/mock_service.rs @@ -201,26 +201,23 @@ impl indexer_service_rpc::RpcServer for MockIndexerService { }) } - async fn get_block_by_id(&self, block_id: BlockId) -> Result { - self.blocks + async fn get_block_by_id(&self, block_id: BlockId) -> Result, ErrorObjectOwned> { + Ok(self + .blocks .iter() .find(|b| b.header.block_id == block_id) - .cloned() - .ok_or_else(|| { - ErrorObjectOwned::owned( - -32001, - format!("Block with ID {block_id} not found"), - None::<()>, - ) - }) + .cloned()) } - async fn get_block_by_hash(&self, block_hash: HashType) -> Result { - self.blocks + async fn get_block_by_hash( + &self, + block_hash: HashType, + ) -> Result, ErrorObjectOwned> { + Ok(self + .blocks .iter() .find(|b| b.header.hash == block_hash) - .cloned() - .ok_or_else(|| ErrorObjectOwned::owned(-32001, "Block with hash not found", None::<()>)) + .cloned()) } async fn get_account(&self, account_id: AccountId) -> Result { @@ -230,11 +227,11 @@ impl indexer_service_rpc::RpcServer for MockIndexerService { .ok_or_else(|| ErrorObjectOwned::owned(-32001, "Account not found", None::<()>)) } - async fn get_transaction(&self, tx_hash: HashType) -> Result { - self.transactions - .get(&tx_hash) - .map(|(tx, _)| tx.clone()) - .ok_or_else(|| ErrorObjectOwned::owned(-32001, "Transaction not found", None::<()>)) + async fn get_transaction( + &self, + tx_hash: HashType, + ) -> Result, ErrorObjectOwned> { + Ok(self.transactions.get(&tx_hash).map(|(tx, _)| tx.clone())) } async fn get_blocks( diff --git a/indexer/service/src/service.rs b/indexer/service/src/service.rs index 049d4a0c..e2f8a321 100644 --- a/indexer/service/src/service.rs +++ b/indexer/service/src/service.rs @@ -52,22 +52,25 @@ impl indexer_service_rpc::RpcServer for IndexerService { self.indexer.store.get_last_block_id().map_err(db_error) } - async fn get_block_by_id(&self, block_id: BlockId) -> Result { + async fn get_block_by_id(&self, block_id: BlockId) -> Result, ErrorObjectOwned> { Ok(self .indexer .store .get_block_at_id(block_id) .map_err(db_error)? - .into()) + .map(Into::into)) } - async fn get_block_by_hash(&self, block_hash: HashType) -> Result { + async fn get_block_by_hash( + &self, + block_hash: HashType, + ) -> Result, ErrorObjectOwned> { Ok(self .indexer .store .get_block_by_hash(block_hash.0) .map_err(db_error)? - .into()) + .map(Into::into)) } async fn get_account(&self, account_id: AccountId) -> Result { @@ -80,13 +83,16 @@ impl indexer_service_rpc::RpcServer for IndexerService { .into()) } - async fn get_transaction(&self, tx_hash: HashType) -> Result { + async fn get_transaction( + &self, + tx_hash: HashType, + ) -> Result, ErrorObjectOwned> { Ok(self .indexer .store .get_transaction_by_hash(tx_hash.0) .map_err(db_error)? - .into()) + .map(Into::into)) } async fn get_blocks( diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index a68609a3..137c9f54 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -19,6 +19,7 @@ indexer_service.workspace = true serde_json.workspace = true token_core.workspace = true indexer_service_rpc.workspace = true +sequencer_service_rpc = { workspace = true, features = ["client"] } wallet-ffi.workspace = true url.workspace = true diff --git a/integration_tests/src/config.rs b/integration_tests/src/config.rs index 5bcf59b9..dc24c8f0 100644 --- a/integration_tests/src/config.rs +++ b/integration_tests/src/config.rs @@ -204,7 +204,6 @@ pub fn sequencer_config( Ok(SequencerConfig { home, - override_rust_log: None, genesis_id: 1, is_genesis_random: true, max_num_tx_in_block, @@ -212,7 +211,6 @@ pub fn sequencer_config( mempool_max_size, block_create_timeout, retry_pending_blocks_timeout: Duration::from_secs(120), - port: 0, initial_accounts: initial_data.sequencer_initial_accounts(), initial_commitments: initial_data.sequencer_initial_commitments(), signing_key: [37; 32], diff --git a/integration_tests/src/lib.rs b/integration_tests/src/lib.rs index d7324f8a..016bde21 100644 --- a/integration_tests/src/lib.rs +++ b/integration_tests/src/lib.rs @@ -3,8 +3,7 @@ use std::{net::SocketAddr, path::PathBuf, sync::LazyLock}; use anyhow::{Context as _, Result, bail}; -use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64}; -use common::{HashType, sequencer_client::SequencerClient, transaction::NSSATransaction}; +use common::{HashType, transaction::NSSATransaction}; use futures::FutureExt as _; use indexer_service::IndexerHandle; use log::{debug, error, warn}; @@ -12,6 +11,7 @@ use nssa::{AccountId, PrivacyPreservingTransaction}; use nssa_core::Commitment; use sequencer_core::indexer_client::{IndexerClient, IndexerClientTrait as _}; use sequencer_service::SequencerHandle; +use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder}; use tempfile::TempDir; use testcontainers::compose::DockerCompose; use wallet::{WalletCore, config::WalletConfigOverrides}; @@ -38,7 +38,8 @@ pub struct TestContext { indexer_client: IndexerClient, wallet: WalletCore, wallet_password: String, - sequencer_handle: SequencerHandle, + /// Optional to move out value in Drop + sequencer_handle: Option, indexer_handle: IndexerHandle, bedrock_compose: DockerCompose, _temp_indexer_dir: TempDir, @@ -90,8 +91,9 @@ impl TestContext { .context("Failed to convert sequencer addr to URL")?; let indexer_url = config::addr_to_url(config::UrlProtocol::Ws, indexer_handle.addr()) .context("Failed to convert indexer addr to URL")?; - let sequencer_client = - SequencerClient::new(sequencer_url).context("Failed to create sequencer client")?; + let sequencer_client = SequencerClientBuilder::default() + .build(sequencer_url) + .context("Failed to create sequencer client")?; let indexer_client = IndexerClient::new(&indexer_url) .await .context("Failed to create indexer client")?; @@ -102,7 +104,7 @@ impl TestContext { wallet, wallet_password, bedrock_compose, - sequencer_handle, + sequencer_handle: Some(sequencer_handle), indexer_handle, _temp_indexer_dir: temp_indexer_dir, _temp_sequencer_dir: temp_sequencer_dir, @@ -229,7 +231,7 @@ impl TestContext { ) .context("Failed to create Sequencer config")?; - let sequencer_handle = sequencer_service::startup_sequencer(config).await?; + let sequencer_handle = sequencer_service::run(config, 0).await?; Ok((sequencer_handle, temp_sequencer_dir)) } @@ -333,14 +335,16 @@ impl Drop for TestContext { wallet_password: _, } = self; - if sequencer_handle.is_finished() { - let Err(err) = self - .sequencer_handle - .run_forever() + let sequencer_handle = sequencer_handle + .take() + .expect("Sequencer handle should be present in TestContext drop"); + if sequencer_handle.is_stopped() { + let Err(err) = sequencer_handle + .stopped() .now_or_never() - .expect("Future is finished and should be ready"); + .expect("Sequencer handle should be stopped"); error!( - "Sequencer handle has unexpectedly finished before TestContext drop with error: {err:#}" + "Sequencer handle has unexpectedly stopped before TestContext drop with error: {err:#}" ); } @@ -459,15 +463,8 @@ pub async fn fetch_privacy_preserving_tx( seq_client: &SequencerClient, tx_hash: HashType, ) -> PrivacyPreservingTransaction { - let transaction_encoded = seq_client - .get_transaction_by_hash(tx_hash) - .await - .unwrap() - .transaction - .unwrap(); + let tx = seq_client.get_transaction(tx_hash).await.unwrap().unwrap(); - let tx_bytes = BASE64.decode(transaction_encoded).unwrap(); - let tx = borsh::from_slice(&tx_bytes).unwrap(); match tx { NSSATransaction::PrivacyPreserving(privacy_preserving_transaction) => { privacy_preserving_transaction @@ -480,8 +477,8 @@ pub async fn verify_commitment_is_in_state( commitment: Commitment, seq_client: &SequencerClient, ) -> bool { - matches!( - seq_client.get_proof_for_commitment(commitment).await, - Ok(Some(_)) - ) + seq_client + .get_proof_for_commitment(commitment) + .await + .is_ok() } diff --git a/integration_tests/tests/account.rs b/integration_tests/tests/account.rs index 3f1d0993..60c1aeaa 100644 --- a/integration_tests/tests/account.rs +++ b/integration_tests/tests/account.rs @@ -7,6 +7,7 @@ use anyhow::Result; use integration_tests::TestContext; use log::info; use nssa::program::Program; +use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::cli::{ Command, @@ -21,8 +22,7 @@ async fn get_existing_account() -> Result<()> { let account = ctx .sequencer_client() .get_account(ctx.existing_public_accounts()[0]) - .await? - .account; + .await?; assert_eq!( account.program_owner, diff --git a/integration_tests/tests/amm.rs b/integration_tests/tests/amm.rs index bdb2da72..42aa5f3f 100644 --- a/integration_tests/tests/amm.rs +++ b/integration_tests/tests/amm.rs @@ -9,6 +9,7 @@ use std::time::Duration; use anyhow::Result; use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, format_public_account_id}; use log::info; +use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::cli::{ Command, SubcommandReturnValue, @@ -194,20 +195,14 @@ async fn amm_public() -> Result<()> { let user_holding_a_acc = ctx .sequencer_client() .get_account(recipient_account_id_1) - .await? - .account; + .await?; let user_holding_b_acc = ctx .sequencer_client() .get_account(recipient_account_id_2) - .await? - .account; + .await?; - let user_holding_lp_acc = ctx - .sequencer_client() - .get_account(user_holding_lp) - .await? - .account; + let user_holding_lp_acc = ctx.sequencer_client().get_account(user_holding_lp).await?; assert_eq!( u128::from_le_bytes(user_holding_a_acc.data[33..].try_into().unwrap()), @@ -243,20 +238,14 @@ async fn amm_public() -> Result<()> { let user_holding_a_acc = ctx .sequencer_client() .get_account(recipient_account_id_1) - .await? - .account; + .await?; let user_holding_b_acc = ctx .sequencer_client() .get_account(recipient_account_id_2) - .await? - .account; + .await?; - let user_holding_lp_acc = ctx - .sequencer_client() - .get_account(user_holding_lp) - .await? - .account; + let user_holding_lp_acc = ctx.sequencer_client().get_account(user_holding_lp).await?; assert_eq!( u128::from_le_bytes(user_holding_a_acc.data[33..].try_into().unwrap()), @@ -292,20 +281,14 @@ async fn amm_public() -> Result<()> { let user_holding_a_acc = ctx .sequencer_client() .get_account(recipient_account_id_1) - .await? - .account; + .await?; let user_holding_b_acc = ctx .sequencer_client() .get_account(recipient_account_id_2) - .await? - .account; + .await?; - let user_holding_lp_acc = ctx - .sequencer_client() - .get_account(user_holding_lp) - .await? - .account; + let user_holding_lp_acc = ctx.sequencer_client().get_account(user_holding_lp).await?; assert_eq!( u128::from_le_bytes(user_holding_a_acc.data[33..].try_into().unwrap()), @@ -342,20 +325,14 @@ async fn amm_public() -> Result<()> { let user_holding_a_acc = ctx .sequencer_client() .get_account(recipient_account_id_1) - .await? - .account; + .await?; let user_holding_b_acc = ctx .sequencer_client() .get_account(recipient_account_id_2) - .await? - .account; + .await?; - let user_holding_lp_acc = ctx - .sequencer_client() - .get_account(user_holding_lp) - .await? - .account; + let user_holding_lp_acc = ctx.sequencer_client().get_account(user_holding_lp).await?; assert_eq!( u128::from_le_bytes(user_holding_a_acc.data[33..].try_into().unwrap()), @@ -392,20 +369,14 @@ async fn amm_public() -> Result<()> { let user_holding_a_acc = ctx .sequencer_client() .get_account(recipient_account_id_1) - .await? - .account; + .await?; let user_holding_b_acc = ctx .sequencer_client() .get_account(recipient_account_id_2) - .await? - .account; + .await?; - let user_holding_lp_acc = ctx - .sequencer_client() - .get_account(user_holding_lp) - .await? - .account; + let user_holding_lp_acc = ctx.sequencer_client().get_account(user_holding_lp).await?; assert_eq!( u128::from_le_bytes(user_holding_a_acc.data[33..].try_into().unwrap()), diff --git a/integration_tests/tests/auth_transfer/private.rs b/integration_tests/tests/auth_transfer/private.rs index 93e925d9..59b4719a 100644 --- a/integration_tests/tests/auth_transfer/private.rs +++ b/integration_tests/tests/auth_transfer/private.rs @@ -8,6 +8,7 @@ use integration_tests::{ use log::info; use nssa::{AccountId, program::Program}; use nssa_core::{NullifierPublicKey, encryption::shared_key_derivation::Secp256k1Point}; +use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::cli::{ Command, SubcommandReturnValue, @@ -135,7 +136,7 @@ async fn deshielded_transfer_to_public_account() -> Result<()> { let acc_2_balance = ctx.sequencer_client().get_account_balance(to).await?; assert_eq!(from_acc.balance, 9900); - assert_eq!(acc_2_balance.balance, 20100); + assert_eq!(acc_2_balance, 20100); info!("Successfully deshielded transfer to public account"); @@ -245,7 +246,7 @@ async fn shielded_transfer_to_owned_private_account() -> Result<()> { let acc_from_balance = ctx.sequencer_client().get_account_balance(from).await?; - assert_eq!(acc_from_balance.balance, 9900); + assert_eq!(acc_from_balance, 9900); assert_eq!(acc_to.balance, 20100); info!("Successfully shielded transfer to owned private account"); @@ -290,7 +291,7 @@ async fn shielded_transfer_to_foreign_account() -> Result<()> { .await ); - assert_eq!(acc_1_balance.balance, 9900); + assert_eq!(acc_1_balance, 9900); info!("Successfully shielded transfer to foreign account"); diff --git a/integration_tests/tests/auth_transfer/public.rs b/integration_tests/tests/auth_transfer/public.rs index ce73d62f..7f8c3836 100644 --- a/integration_tests/tests/auth_transfer/public.rs +++ b/integration_tests/tests/auth_transfer/public.rs @@ -4,6 +4,7 @@ use anyhow::Result; use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, format_public_account_id}; use log::info; use nssa::program::Program; +use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::cli::{ Command, SubcommandReturnValue, @@ -41,8 +42,8 @@ async fn successful_transfer_to_existing_account() -> Result<()> { info!("Balance of sender: {acc_1_balance:#?}"); info!("Balance of receiver: {acc_2_balance:#?}"); - assert_eq!(acc_1_balance.balance, 9900); - assert_eq!(acc_2_balance.balance, 20100); + assert_eq!(acc_1_balance, 9900); + assert_eq!(acc_2_balance, 20100); Ok(()) } @@ -97,8 +98,8 @@ pub async fn successful_transfer_to_new_account() -> Result<()> { info!("Balance of sender: {acc_1_balance:#?}"); info!("Balance of receiver: {acc_2_balance:#?}"); - assert_eq!(acc_1_balance.balance, 9900); - assert_eq!(acc_2_balance.balance, 100); + assert_eq!(acc_1_balance, 9900); + assert_eq!(acc_2_balance, 100); Ok(()) } @@ -134,8 +135,8 @@ async fn failed_transfer_with_insufficient_balance() -> Result<()> { info!("Balance of sender: {acc_1_balance:#?}"); info!("Balance of receiver: {acc_2_balance:#?}"); - assert_eq!(acc_1_balance.balance, 10000); - assert_eq!(acc_2_balance.balance, 20000); + assert_eq!(acc_1_balance, 10000); + assert_eq!(acc_2_balance, 20000); Ok(()) } @@ -171,8 +172,8 @@ async fn two_consecutive_successful_transfers() -> Result<()> { info!("Balance of sender: {acc_1_balance:#?}"); info!("Balance of receiver: {acc_2_balance:#?}"); - assert_eq!(acc_1_balance.balance, 9900); - assert_eq!(acc_2_balance.balance, 20100); + assert_eq!(acc_1_balance, 9900); + assert_eq!(acc_2_balance, 20100); info!("First TX Success!"); @@ -203,8 +204,8 @@ async fn two_consecutive_successful_transfers() -> Result<()> { info!("Balance of sender: {acc_1_balance:#?}"); info!("Balance of receiver: {acc_2_balance:#?}"); - assert_eq!(acc_1_balance.balance, 9800); - assert_eq!(acc_2_balance.balance, 20200); + assert_eq!(acc_1_balance, 9800); + assert_eq!(acc_2_balance, 20200); info!("Second TX Success!"); @@ -230,11 +231,7 @@ async fn initialize_public_account() -> Result<()> { wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; info!("Checking correct execution"); - let account = ctx - .sequencer_client() - .get_account(account_id) - .await? - .account; + let account = ctx.sequencer_client().get_account(account_id).await?; assert_eq!( account.program_owner, diff --git a/integration_tests/tests/block_size_limit.rs b/integration_tests/tests/block_size_limit.rs index 41c9fc76..72f773c9 100644 --- a/integration_tests/tests/block_size_limit.rs +++ b/integration_tests/tests/block_size_limit.rs @@ -8,11 +8,12 @@ use std::time::Duration; use anyhow::Result; use bytesize::ByteSize; -use common::{block::HashableBlockData, transaction::NSSATransaction}; +use common::transaction::NSSATransaction; use integration_tests::{ TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, config::SequencerPartialConfig, }; use nssa::program::Program; +use sequencer_service_rpc::RpcClient as _; use tokio::test; #[test] @@ -36,7 +37,10 @@ async fn reject_oversized_transaction() -> Result<()> { let tx = nssa::ProgramDeploymentTransaction::new(message); // Try to submit the transaction and expect an error - let result = ctx.sequencer_client().send_tx_program(tx).await; + let result = ctx + .sequencer_client() + .send_transaction(NSSATransaction::ProgramDeployment(tx)) + .await; assert!( result.is_err(), @@ -74,7 +78,10 @@ async fn accept_transaction_within_limit() -> Result<()> { let tx = nssa::ProgramDeploymentTransaction::new(message); // This should succeed - let result = ctx.sequencer_client().send_tx_program(tx).await; + let result = ctx + .sequencer_client() + .send_transaction(NSSATransaction::ProgramDeployment(tx)) + .await; assert!( result.is_ok(), @@ -112,33 +119,38 @@ async fn transaction_deferred_to_next_block_when_current_full() -> Result<()> { let burner_id = Program::new(burner_bytecode.clone())?.id(); let chain_caller_id = Program::new(chain_caller_bytecode.clone())?.id(); - let initial_block_height = ctx.sequencer_client().get_last_block().await?.last_block; + let initial_block_height = ctx.sequencer_client().get_last_block_id().await?; // Submit both program deployments ctx.sequencer_client() - .send_tx_program(nssa::ProgramDeploymentTransaction::new( - nssa::program_deployment_transaction::Message::new(burner_bytecode), + .send_transaction(NSSATransaction::ProgramDeployment( + nssa::ProgramDeploymentTransaction::new( + nssa::program_deployment_transaction::Message::new(burner_bytecode), + ), )) .await?; ctx.sequencer_client() - .send_tx_program(nssa::ProgramDeploymentTransaction::new( - nssa::program_deployment_transaction::Message::new(chain_caller_bytecode), + .send_transaction(NSSATransaction::ProgramDeployment( + nssa::ProgramDeploymentTransaction::new( + nssa::program_deployment_transaction::Message::new(chain_caller_bytecode), + ), )) .await?; // Wait for first block tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let block1_response = ctx + let block1 = ctx .sequencer_client() .get_block(initial_block_height + 1) - .await?; - let block1: HashableBlockData = borsh::from_slice(&block1_response.block)?; + .await? + .unwrap(); // Check which program is in block 1 - let get_program_ids = |block: &HashableBlockData| -> Vec { + let get_program_ids = |block: &common::block::Block| -> Vec { block + .body .transactions .iter() .filter_map(|tx| { @@ -168,11 +180,11 @@ async fn transaction_deferred_to_next_block_when_current_full() -> Result<()> { // Wait for second block tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let block2_response = ctx + let block2 = ctx .sequencer_client() .get_block(initial_block_height + 2) - .await?; - let block2: HashableBlockData = borsh::from_slice(&block2_response.block)?; + .await? + .unwrap(); let block2_program_ids = get_program_ids(&block2); // The other program should be in block 2 diff --git a/integration_tests/tests/indexer.rs b/integration_tests/tests/indexer.rs index 0b947135..cb8cf0e9 100644 --- a/integration_tests/tests/indexer.rs +++ b/integration_tests/tests/indexer.rs @@ -22,12 +22,8 @@ async fn indexer_test_run() -> Result<()> { // RUN OBSERVATION tokio::time::sleep(std::time::Duration::from_millis(L2_TO_L1_TIMEOUT_MILLIS)).await; - let last_block_seq = ctx - .sequencer_client() - .get_last_block() - .await - .unwrap() - .last_block; + let last_block_seq = + sequencer_service_rpc::RpcClient::get_last_block_id(ctx.sequencer_client()).await?; info!("Last block on seq now is {last_block_seq}"); @@ -100,20 +96,22 @@ async fn indexer_state_consistency() -> Result<()> { tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; info!("Checking correct balance move"); - let acc_1_balance = ctx - .sequencer_client() - .get_account_balance(ctx.existing_public_accounts()[0]) - .await?; - let acc_2_balance = ctx - .sequencer_client() - .get_account_balance(ctx.existing_public_accounts()[1]) - .await?; + let acc_1_balance = sequencer_service_rpc::RpcClient::get_account_balance( + ctx.sequencer_client(), + ctx.existing_public_accounts()[0], + ) + .await?; + let acc_2_balance = sequencer_service_rpc::RpcClient::get_account_balance( + ctx.sequencer_client(), + ctx.existing_public_accounts()[1], + ) + .await?; info!("Balance of sender: {acc_1_balance:#?}"); info!("Balance of receiver: {acc_2_balance:#?}"); - assert_eq!(acc_1_balance.balance, 9900); - assert_eq!(acc_2_balance.balance, 20100); + assert_eq!(acc_1_balance, 9900); + assert_eq!(acc_2_balance, 20100); // WAIT info!("Waiting for indexer to parse blocks"); @@ -131,16 +129,16 @@ async fn indexer_state_consistency() -> Result<()> { .unwrap(); info!("Checking correct state transition"); - let acc1_seq_state = ctx - .sequencer_client() - .get_account(ctx.existing_public_accounts()[0]) - .await? - .account; - let acc2_seq_state = ctx - .sequencer_client() - .get_account(ctx.existing_public_accounts()[1]) - .await? - .account; + let acc1_seq_state = sequencer_service_rpc::RpcClient::get_account( + ctx.sequencer_client(), + ctx.existing_public_accounts()[0], + ) + .await?; + let acc2_seq_state = sequencer_service_rpc::RpcClient::get_account( + ctx.sequencer_client(), + ctx.existing_public_accounts()[1], + ) + .await?; assert_eq!(acc1_ind_state, acc1_seq_state.into()); assert_eq!(acc2_ind_state, acc2_seq_state.into()); diff --git a/integration_tests/tests/keys_restoration.rs b/integration_tests/tests/keys_restoration.rs index 0d20f8b4..cdbe2e6b 100644 --- a/integration_tests/tests/keys_restoration.rs +++ b/integration_tests/tests/keys_restoration.rs @@ -14,6 +14,7 @@ use integration_tests::{ use key_protocol::key_management::key_tree::chain_index::ChainIndex; use log::info; use nssa::{AccountId, program::Program}; +use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::cli::{ Command, SubcommandReturnValue, @@ -305,8 +306,8 @@ async fn restore_keys_from_seed() -> Result<()> { .get_account_balance(to_account_id4) .await?; - assert_eq!(acc3.balance, 91); // 102 - 11 - assert_eq!(acc4.balance, 114); // 103 + 11 + assert_eq!(acc3, 91); // 102 - 11 + assert_eq!(acc4, 114); // 103 + 11 info!("Successfully restored keys and verified transactions"); diff --git a/integration_tests/tests/pinata.rs b/integration_tests/tests/pinata.rs index 38cfeac3..3285c216 100644 --- a/integration_tests/tests/pinata.rs +++ b/integration_tests/tests/pinata.rs @@ -13,6 +13,7 @@ use integration_tests::{ format_public_account_id, verify_commitment_is_in_state, }; use log::info; +use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::cli::{ Command, SubcommandReturnValue, @@ -46,8 +47,7 @@ async fn claim_pinata_to_uninitialized_public_account_fails_fast() -> Result<()> let pinata_balance_pre = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; let claim_result = wallet::cli::execute_subcommand( ctx.wallet_mut(), @@ -70,8 +70,7 @@ async fn claim_pinata_to_uninitialized_public_account_fails_fast() -> Result<()> let pinata_balance_post = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; assert_eq!(pinata_balance_post, pinata_balance_pre); @@ -102,8 +101,7 @@ async fn claim_pinata_to_uninitialized_private_account_fails_fast() -> Result<() let pinata_balance_pre = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; let claim_result = wallet::cli::execute_subcommand( ctx.wallet_mut(), @@ -126,8 +124,7 @@ async fn claim_pinata_to_uninitialized_private_account_fails_fast() -> Result<() let pinata_balance_post = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; assert_eq!(pinata_balance_post, pinata_balance_pre); @@ -146,8 +143,7 @@ async fn claim_pinata_to_existing_public_account() -> Result<()> { let pinata_balance_pre = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; @@ -158,14 +154,12 @@ async fn claim_pinata_to_existing_public_account() -> Result<()> { let pinata_balance_post = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; let winner_balance_post = ctx .sequencer_client() .get_account_balance(ctx.existing_public_accounts()[0]) - .await? - .balance; + .await?; assert_eq!(pinata_balance_post, pinata_balance_pre - pinata_prize); assert_eq!(winner_balance_post, 10000 + pinata_prize); @@ -187,8 +181,7 @@ async fn claim_pinata_to_existing_private_account() -> Result<()> { let pinata_balance_pre = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; let result = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash: _ } = result else { @@ -211,8 +204,7 @@ async fn claim_pinata_to_existing_private_account() -> Result<()> { let pinata_balance_post = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; assert_eq!(pinata_balance_post, pinata_balance_pre - pinata_prize); @@ -268,8 +260,7 @@ async fn claim_pinata_to_new_private_account() -> Result<()> { let pinata_balance_pre = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; @@ -285,8 +276,7 @@ async fn claim_pinata_to_new_private_account() -> Result<()> { let pinata_balance_post = ctx .sequencer_client() .get_account_balance(PINATA_BASE58.parse().unwrap()) - .await? - .balance; + .await?; assert_eq!(pinata_balance_post, pinata_balance_pre - pinata_prize); diff --git a/integration_tests/tests/program_deployment.rs b/integration_tests/tests/program_deployment.rs index 1feb7290..bb46ba87 100644 --- a/integration_tests/tests/program_deployment.rs +++ b/integration_tests/tests/program_deployment.rs @@ -6,11 +6,13 @@ use std::{path::PathBuf, time::Duration}; use anyhow::Result; +use common::transaction::NSSATransaction; use integration_tests::{ NSSA_PROGRAM_FOR_TEST_DATA_CHANGER, TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, }; use log::info; use nssa::{AccountId, program::Program}; +use sequencer_service_rpc::RpcClient as _; use tokio::test; use wallet::cli::Command; @@ -47,18 +49,17 @@ async fn deploy_and_execute_program() -> Result<()> { )?; let witness_set = nssa::public_transaction::WitnessSet::for_message(&message, &[]); let transaction = nssa::PublicTransaction::new(message, witness_set); - let _response = ctx.sequencer_client().send_tx_public(transaction).await?; + let _response = ctx + .sequencer_client() + .send_transaction(NSSATransaction::Public(transaction)) + .await?; info!("Waiting for next block creation"); // Waiting for long time as it may take some time for such a big transaction to be included in a // block tokio::time::sleep(Duration::from_secs(2 * TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let post_state_account = ctx - .sequencer_client() - .get_account(account_id) - .await? - .account; + let post_state_account = ctx.sequencer_client().get_account(account_id).await?; assert_eq!(post_state_account.program_owner, data_changer.id()); assert_eq!(post_state_account.balance, 0); diff --git a/integration_tests/tests/token.rs b/integration_tests/tests/token.rs index 5efd69ef..b638b6c9 100644 --- a/integration_tests/tests/token.rs +++ b/integration_tests/tests/token.rs @@ -14,6 +14,7 @@ use integration_tests::{ use key_protocol::key_management::key_tree::chain_index::ChainIndex; use log::info; use nssa::program::Program; +use sequencer_service_rpc::RpcClient as _; use token_core::{TokenDefinition, TokenHolding}; use tokio::test; use wallet::cli::{ @@ -92,8 +93,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let definition_acc = ctx .sequencer_client() .get_account(definition_account_id) - .await? - .account; + .await?; let token_definition = TokenDefinition::try_from(&definition_acc.data)?; assert_eq!(definition_acc.program_owner, Program::token().id()); @@ -110,8 +110,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let supply_acc = ctx .sequencer_client() .get_account(supply_account_id) - .await? - .account; + .await?; // The account must be owned by the token program assert_eq!(supply_acc.program_owner, Program::token().id()); @@ -143,8 +142,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let supply_acc = ctx .sequencer_client() .get_account(supply_account_id) - .await? - .account; + .await?; assert_eq!(supply_acc.program_owner, Program::token().id()); let token_holding = TokenHolding::try_from(&supply_acc.data)?; assert_eq!( @@ -159,8 +157,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let recipient_acc = ctx .sequencer_client() .get_account(recipient_account_id) - .await? - .account; + .await?; assert_eq!(recipient_acc.program_owner, Program::token().id()); let token_holding = TokenHolding::try_from(&recipient_acc.data)?; assert_eq!( @@ -188,8 +185,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let definition_acc = ctx .sequencer_client() .get_account(definition_account_id) - .await? - .account; + .await?; let token_definition = TokenDefinition::try_from(&definition_acc.data)?; assert_eq!( @@ -205,8 +201,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let recipient_acc = ctx .sequencer_client() .get_account(recipient_account_id) - .await? - .account; + .await?; let token_holding = TokenHolding::try_from(&recipient_acc.data)?; assert_eq!( @@ -236,8 +231,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let definition_acc = ctx .sequencer_client() .get_account(definition_account_id) - .await? - .account; + .await?; let token_definition = TokenDefinition::try_from(&definition_acc.data)?; assert_eq!( @@ -253,8 +247,7 @@ async fn create_and_transfer_public_token() -> Result<()> { let recipient_acc = ctx .sequencer_client() .get_account(recipient_account_id) - .await? - .account; + .await?; let token_holding = TokenHolding::try_from(&recipient_acc.data)?; assert_eq!( @@ -341,8 +334,7 @@ async fn create_and_transfer_token_with_private_supply() -> Result<()> { let definition_acc = ctx .sequencer_client() .get_account(definition_account_id) - .await? - .account; + .await?; let token_definition = TokenDefinition::try_from(&definition_acc.data)?; assert_eq!(definition_acc.program_owner, Program::token().id()); @@ -405,8 +397,7 @@ async fn create_and_transfer_token_with_private_supply() -> Result<()> { let definition_acc = ctx .sequencer_client() .get_account(definition_account_id) - .await? - .account; + .await?; let token_definition = TokenDefinition::try_from(&definition_acc.data)?; assert_eq!( @@ -506,8 +497,7 @@ async fn create_token_with_private_definition() -> Result<()> { let supply_acc = ctx .sequencer_client() .get_account(supply_account_id) - .await? - .account; + .await?; assert_eq!(supply_acc.program_owner, Program::token().id()); let token_holding = TokenHolding::try_from(&supply_acc.data)?; @@ -586,8 +576,7 @@ async fn create_token_with_private_definition() -> Result<()> { let recipient_acc = ctx .sequencer_client() .get_account(recipient_account_id_public) - .await? - .account; + .await?; let token_holding = TokenHolding::try_from(&recipient_acc.data)?; assert_eq!( @@ -882,8 +871,7 @@ async fn shielded_token_transfer() -> Result<()> { let supply_acc = ctx .sequencer_client() .get_account(supply_account_id) - .await? - .account; + .await?; let token_holding = TokenHolding::try_from(&supply_acc.data)?; assert_eq!( token_holding, @@ -1026,8 +1014,7 @@ async fn deshielded_token_transfer() -> Result<()> { let recipient_acc = ctx .sequencer_client() .get_account(recipient_account_id) - .await? - .account; + .await?; let token_holding = TokenHolding::try_from(&recipient_acc.data)?; assert_eq!( token_holding, diff --git a/integration_tests/tests/tps.rs b/integration_tests/tests/tps.rs index 1dee3a85..bd46849e 100644 --- a/integration_tests/tests/tps.rs +++ b/integration_tests/tests/tps.rs @@ -13,6 +13,7 @@ use std::time::{Duration, Instant}; use anyhow::Result; use bytesize::ByteSize; +use common::transaction::NSSATransaction; use integration_tests::{ TestContext, config::{InitialData, SequencerPartialConfig}, @@ -30,6 +31,7 @@ use nssa_core::{ account::{AccountWithMetadata, Nonce, data::Data}, encryption::ViewingPublicKey, }; +use sequencer_service_rpc::RpcClient as _; use tokio::test; pub(crate) struct TpsTestManager { @@ -153,10 +155,9 @@ pub async fn tps_test() -> Result<()> { for (i, tx) in txs.into_iter().enumerate() { let tx_hash = ctx .sequencer_client() - .send_tx_public(tx) + .send_transaction(NSSATransaction::Public(tx)) .await - .unwrap() - .tx_hash; + .unwrap(); info!("Sent tx {i}"); tx_hashes.push(tx_hash); } @@ -170,15 +171,13 @@ pub async fn tps_test() -> Result<()> { let tx_obj = ctx .sequencer_client() - .get_transaction_by_hash(*tx_hash) + .get_transaction(*tx_hash) .await .inspect_err(|err| { log::warn!("Failed to get transaction by hash {tx_hash} with error: {err:#?}"); }); - if let Ok(tx_obj) = tx_obj - && tx_obj.transaction.is_some() - { + if tx_obj.is_ok_and(|opt| opt.is_some()) { info!("Found tx {i} with hash {tx_hash}"); break; } diff --git a/key_protocol/src/key_management/key_tree/mod.rs b/key_protocol/src/key_management/key_tree/mod.rs index a94e8291..08a576e5 100644 --- a/key_protocol/src/key_management/key_tree/mod.rs +++ b/key_protocol/src/key_management/key_tree/mod.rs @@ -1,7 +1,7 @@ -use std::{collections::BTreeMap, sync::Arc}; +use std::collections::BTreeMap; use anyhow::Result; -use common::sequencer_client::SequencerClient; +use nssa::{Account, AccountId}; use serde::{Deserialize, Serialize}; use crate::key_management::{ @@ -197,40 +197,6 @@ impl KeyTree { } impl KeyTree { - /// Cleanup of all non-initialized accounts in a private tree. - /// - /// For given `depth` checks children to a tree such that their `ChainIndex::depth(&self) < - /// depth`. - /// - /// If account is default, removes them. - /// - /// Chain must be parsed for accounts beforehand. - /// - /// Fast, leaves gaps between accounts. - pub fn cleanup_tree_remove_uninit_for_depth(&mut self, depth: u32) { - let mut id_stack = vec![ChainIndex::root()]; - - while let Some(curr_id) = id_stack.pop() { - if let Some(node) = self.key_map.get(&curr_id) - && node.value.1 == nssa::Account::default() - && curr_id != ChainIndex::root() - { - let addr = node.account_id(); - self.remove(addr); - } - - let mut next_id = curr_id.nth_child(0); - - while (next_id.depth()) < depth { - id_stack.push(next_id.clone()); - next_id = match next_id.next_in_line() { - Some(id) => id, - None => break, - }; - } - } - } - /// Cleanup of non-initialized accounts in a private tree. /// /// If account is default, removes them, stops at first non-default account. @@ -259,56 +225,17 @@ impl KeyTree { } impl KeyTree { - /// Cleanup of all non-initialized accounts in a public tree. - /// - /// For given `depth` checks children to a tree such that their `ChainIndex::depth(&self) < - /// depth`. - /// - /// If account is default, removes them. - /// - /// Fast, leaves gaps between accounts. - pub async fn cleanup_tree_remove_ininit_for_depth( - &mut self, - depth: u32, - client: Arc, - ) -> Result<()> { - let mut id_stack = vec![ChainIndex::root()]; - - while let Some(curr_id) = id_stack.pop() { - if let Some(node) = self.key_map.get(&curr_id) { - let address = node.account_id(); - let node_acc = client.get_account(address).await?.account; - - if node_acc == nssa::Account::default() && curr_id != ChainIndex::root() { - self.remove(address); - } - } - - let mut next_id = curr_id.nth_child(0); - - while (next_id.depth()) < depth { - id_stack.push(next_id.clone()); - next_id = match next_id.next_in_line() { - Some(id) => id, - None => break, - }; - } - } - - Ok(()) - } - /// Cleanup of non-initialized accounts in a public tree. /// /// If account is default, removes them, stops at first non-default account. /// - /// Walks through tree in lairs of same depth using `ChainIndex::chain_ids_at_depth()`. + /// Walks through tree in layers of same depth using `ChainIndex::chain_ids_at_depth()`. /// /// Slow, maintains tree consistency. - pub async fn cleanup_tree_remove_uninit_layered( + pub async fn cleanup_tree_remove_uninit_layered>>( &mut self, depth: u32, - client: Arc, + get_account: impl Fn(AccountId) -> F, ) -> Result<()> { let depth = usize::try_from(depth).expect("Depth is expected to fit in usize"); 'outer: for i in (1..depth).rev() { @@ -316,7 +243,7 @@ impl KeyTree { for id in ChainIndex::chain_ids_at_depth(i) { if let Some(node) = self.key_map.get(&id) { let address = node.account_id(); - let node_acc = client.get_account(address).await?.account; + let node_acc = get_account(address).await?; if node_acc == nssa::Account::default() { let addr = node.account_id(); diff --git a/sequencer/core/src/block_store.rs b/sequencer/core/src/block_store.rs index 51355f5f..7a226d45 100644 --- a/sequencer/core/src/block_store.rs +++ b/sequencer/core/src/block_store.rs @@ -42,7 +42,7 @@ impl SequencerStore { }) } - pub fn get_block_at_id(&self, id: u64) -> Result { + pub fn get_block_at_id(&self, id: u64) -> Result, DbError> { self.dbio.get_block(id) } @@ -56,16 +56,20 @@ impl SequencerStore { /// Returns the transaction corresponding to the given hash, if it exists in the blockchain. pub fn get_transaction_by_hash(&self, hash: HashType) -> Option { - let block_id = self.tx_hash_to_block_map.get(&hash); - let block = block_id.map(|&id| self.get_block_at_id(id)); - if let Some(Ok(block)) = block { - for transaction in block.body.transactions { - if transaction.hash() == hash { - return Some(transaction); - } + let block_id = *self.tx_hash_to_block_map.get(&hash)?; + let block = self + .get_block_at_id(block_id) + .ok() + .flatten() + .expect("Block should be present since the hash is in the map"); + for transaction in block.body.transactions { + if transaction.hash() == hash { + return Some(transaction); } } - None + panic!( + "Transaction hash was in the map but transaction was not found in the block. This should never happen." + ); } pub fn latest_block_meta(&self) -> Result { @@ -244,7 +248,7 @@ mod tests { node_store.update(&block, [1; 32], &dummy_state).unwrap(); // Verify initial status is Pending - let retrieved_block = node_store.get_block_at_id(block_id).unwrap(); + let retrieved_block = node_store.get_block_at_id(block_id).unwrap().unwrap(); assert!(matches!( retrieved_block.bedrock_status, common::block::BedrockStatus::Pending @@ -254,7 +258,7 @@ mod tests { node_store.mark_block_as_finalized(block_id).unwrap(); // Verify status is now Finalized - let finalized_block = node_store.get_block_at_id(block_id).unwrap(); + let finalized_block = node_store.get_block_at_id(block_id).unwrap().unwrap(); assert!(matches!( finalized_block.bedrock_status, common::block::BedrockStatus::Finalized diff --git a/sequencer/core/src/lib.rs b/sequencer/core/src/lib.rs index 7f58faf4..f3e94614 100644 --- a/sequencer/core/src/lib.rs +++ b/sequencer/core/src/lib.rs @@ -389,7 +389,6 @@ mod tests { SequencerConfig { home, - override_rust_log: Some("info".to_owned()), genesis_id: 1, is_genesis_random: false, max_num_tx_in_block: 10, @@ -476,7 +475,6 @@ mod tests { assert_eq!(sequencer.chain_height, config.genesis_id); assert_eq!(sequencer.sequencer_config.max_num_tx_in_block, 10); - assert_eq!(sequencer.sequencer_config.port, 8080); let acc1_account_id = config.initial_accounts[0].account_id; let acc2_account_id = config.initial_accounts[1].account_id; @@ -694,6 +692,7 @@ mod tests { let block = sequencer .store .get_block_at_id(sequencer.chain_height) + .unwrap() .unwrap(); // Only one should be included in the block @@ -721,6 +720,7 @@ mod tests { let block = sequencer .store .get_block_at_id(sequencer.chain_height) + .unwrap() .unwrap(); assert_eq!(block.body.transactions, vec![tx.clone()]); @@ -732,6 +732,7 @@ mod tests { let block = sequencer .store .get_block_at_id(sequencer.chain_height) + .unwrap() .unwrap(); assert!(block.body.transactions.is_empty()); } @@ -766,6 +767,7 @@ mod tests { let block = sequencer .store .get_block_at_id(sequencer.chain_height) + .unwrap() .unwrap(); assert_eq!(block.body.transactions, vec![tx.clone()]); } @@ -884,6 +886,7 @@ mod tests { let new_block = sequencer .store .get_block_at_id(sequencer.chain_height) + .unwrap() .unwrap(); assert_eq!( diff --git a/sequencer/service/rpc/Cargo.toml b/sequencer/service/rpc/Cargo.toml index 1ced7997..7fa173d1 100644 --- a/sequencer/service/rpc/Cargo.toml +++ b/sequencer/service/rpc/Cargo.toml @@ -14,7 +14,6 @@ nssa_core.workspace = true jsonrpsee = { workspace = true, features = ["macros"] } serde_json.workspace = true -schemars.workspace = true [features] client = ["jsonrpsee/client"] diff --git a/sequencer/service/rpc/src/lib.rs b/sequencer/service/rpc/src/lib.rs index 0ff68bfe..a4bb8f06 100644 --- a/sequencer/service/rpc/src/lib.rs +++ b/sequencer/service/rpc/src/lib.rs @@ -8,12 +8,29 @@ use common::{ use jsonrpsee::proc_macros::rpc; #[cfg(feature = "server")] use jsonrpsee::types::ErrorObjectOwned; +#[cfg(feature = "client")] +pub use jsonrpsee::{core::ClientError, http_client::HttpClientBuilder as SequencerClientBuilder}; use nssa::{Account, AccountId, ProgramId}; use nssa_core::{Commitment, MembershipProof, account::Nonce}; #[cfg(all(not(feature = "server"), not(feature = "client")))] compile_error!("At least one of `server` or `client` features must be enabled."); +/// Type alias for RPC client. Only available when `client` feature is enabled. +/// +/// It's cheap to clone this client, so it can be cloned and shared across the application. +/// +/// # Example +/// +/// ```ignore +/// use sequencer_service_rpc::{SequencerClientBuilder, RpcClient as _}; +/// +/// let client = SequencerClientBuilder::default().build(url)?; +/// let tx_hash = client.send_transaction(tx).await?; +/// ``` +#[cfg(feature = "client")] +pub type SequencerClient = jsonrpsee::http_client::HttpClient; + #[cfg_attr(all(feature = "server", not(feature = "client")), rpc(server))] #[cfg_attr(all(feature = "client", not(feature = "server")), rpc(client))] #[cfg_attr(all(feature = "server", feature = "client"), rpc(server, client))] @@ -30,11 +47,11 @@ pub trait Rpc { // // ============================================================================================= - #[method(name = "getBlockData")] - async fn get_block_data(&self, block_id: BlockId) -> Result; + #[method(name = "getBlock")] + async fn get_block(&self, block_id: BlockId) -> Result, ErrorObjectOwned>; - #[method(name = "getBlockRangeData")] - async fn get_block_range_data( + #[method(name = "getBlockRange")] + async fn get_block_range( &self, start_block_id: BlockId, end_block_id: BlockId, @@ -46,11 +63,11 @@ pub trait Rpc { #[method(name = "getAccountBalance")] async fn get_account_balance(&self, account_id: AccountId) -> Result; - #[method(name = "getTransactionByHash")] - async fn get_transaction_by_hash( + #[method(name = "getTransaction")] + async fn get_transaction( &self, - hash: HashType, - ) -> Result; + tx_hash: HashType, + ) -> Result, ErrorObjectOwned>; #[method(name = "getAccountsNonces")] async fn get_accounts_nonces( diff --git a/sequencer/service/src/lib.rs b/sequencer/service/src/lib.rs index 547f58d2..8ab997d9 100644 --- a/sequencer/service/src/lib.rs +++ b/sequencer/service/src/lib.rs @@ -89,10 +89,19 @@ impl SequencerHandle { } #[must_use] - pub fn is_finished(&self) -> bool { - self.main_loop_handle.is_finished() - || self.retry_pending_blocks_loop_handle.is_finished() - || self.listen_for_bedrock_blocks_loop_handle.is_finished() + pub fn is_stopped(&self) -> bool { + let Self { + addr: _, + server_handle, + main_loop_handle, + retry_pending_blocks_loop_handle, + listen_for_bedrock_blocks_loop_handle, + } = self; + + server_handle.as_ref().is_none_or(ServerHandle::is_stopped) + || main_loop_handle.is_finished() + || retry_pending_blocks_loop_handle.is_finished() + || listen_for_bedrock_blocks_loop_handle.is_finished() } #[must_use] diff --git a/sequencer/service/src/service.rs b/sequencer/service/src/service.rs index d1b21f69..c8f17189 100644 --- a/sequencer/service/src/service.rs +++ b/sequencer/service/src/service.rs @@ -93,24 +93,37 @@ impl Result { + async fn get_block(&self, block_id: BlockId) -> Result, ErrorObjectOwned> { let sequencer = self.sequencer.lock().await; sequencer .block_store() .get_block_at_id(block_id) - .map_err(|err| db_error_to_rpc_error(&err)) + .map_err(|err| internal_error(&err)) } - async fn get_block_range_data( + async fn get_block_range( &self, start_block_id: BlockId, end_block_id: BlockId, ) -> Result, ErrorObjectOwned> { let sequencer = self.sequencer.lock().await; (start_block_id..=end_block_id) - .map(|block_id| sequencer.block_store().get_block_at_id(block_id)) + .map(|block_id| { + sequencer + .block_store() + .get_block_at_id(block_id) + .map_err(|err| internal_error(&err)) + .and_then(|opt| { + opt.ok_or_else(|| { + ErrorObjectOwned::owned( + NOT_FOUND_ERROR_CODE, + format!("Block with id {block_id} not found"), + None::<()>, + ) + }) + }) + }) .collect::, _>>() - .map_err(|err| db_error_to_rpc_error(&err)) } async fn get_last_block_id(&self) -> Result { @@ -124,17 +137,12 @@ impl Result { + ) -> Result, ErrorObjectOwned> { let sequencer = self.sequencer.lock().await; - sequencer - .block_store() - .get_transaction_by_hash(hash) - .ok_or_else(|| { - ErrorObjectOwned::owned(NOT_FOUND_ERROR_CODE, "Transaction not found", None::<()>) - }) + Ok(sequencer.block_store().get_transaction_by_hash(hash)) } async fn get_accounts_nonces( @@ -190,13 +198,6 @@ impl ErrorObjectOwned { - match err { - DbError::NotFound { entity } => ErrorObjectOwned::owned( - NOT_FOUND_ERROR_CODE, - format!("{entity} not found"), - None::<()>, - ), - _ => ErrorObjectOwned::owned(ErrorCode::InternalError.code(), err.to_string(), None::<()>), - } +fn internal_error(err: &DbError) -> ErrorObjectOwned { + ErrorObjectOwned::owned(ErrorCode::InternalError.code(), err.to_string(), None::<()>) } diff --git a/storage/src/error.rs b/storage/src/error.rs index 3e0f88ba..3056e09b 100644 --- a/storage/src/error.rs +++ b/storage/src/error.rs @@ -14,8 +14,6 @@ pub enum DbError { }, #[error("Logic Error: {additional_info}")] DbInteractionError { additional_info: String }, - #[error("{entity} not found")] - NotFound { entity: String }, } impl DbError { @@ -41,9 +39,4 @@ impl DbError { additional_info: message, } } - - #[must_use] - pub const fn not_found(entity: String) -> Self { - Self::NotFound { entity } - } } diff --git a/storage/src/indexer.rs b/storage/src/indexer.rs index c9b3b358..91c3d8d1 100644 --- a/storage/src/indexer.rs +++ b/storage/src/indexer.rs @@ -180,7 +180,9 @@ impl RocksDBIO { ) })?) } else { - Err(DbError::not_found("First block".to_owned())) + Err(DbError::db_interaction_error( + "First block not found".to_owned(), + )) } } @@ -207,7 +209,9 @@ impl RocksDBIO { ) })?) } else { - Err(DbError::not_found("Last block".to_owned())) + Err(DbError::db_interaction_error( + "Last block not found".to_owned(), + )) } } @@ -282,7 +286,9 @@ impl RocksDBIO { ) })?) } else { - Err(DbError::not_found("Last breakpoint id".to_owned())) + Err(DbError::db_interaction_error( + "Last breakpoint id not found".to_owned(), + )) } } @@ -508,7 +514,7 @@ impl RocksDBIO { Ok(()) } - pub fn get_block(&self, block_id: u64) -> DbResult { + pub fn get_block(&self, block_id: u64) -> DbResult> { let cf_block = self.block_column(); let res = self .db @@ -524,14 +530,14 @@ impl RocksDBIO { .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|serr| { + Ok(Some(borsh::from_slice::(&data).map_err(|serr| { DbError::borsh_cast_message( serr, Some("Failed to deserialize block data".to_owned()), ) - })?) + })?)) } else { - Err(DbError::not_found(format!("Block with id {block_id}"))) + Ok(None) } } @@ -659,7 +665,9 @@ impl RocksDBIO { }; for id in start..=block_id { - let block = self.get_block(id)?; + let block = self.get_block(id)?.ok_or_else(|| { + DbError::db_interaction_error(format!("Block with id {id} not found")) + })?; for transaction in block.body.transactions { transaction @@ -680,7 +688,9 @@ impl RocksDBIO { Ok(breakpoint) } else { - Err(DbError::not_found(format!("Block with id {block_id}"))) + Err(DbError::db_interaction_error(format!( + "Block with id {block_id} not found" + ))) } } @@ -712,7 +722,7 @@ impl RocksDBIO { // Mappings - pub fn get_block_id_by_hash(&self, hash: [u8; 32]) -> DbResult { + pub fn get_block_id_by_hash(&self, hash: [u8; 32]) -> DbResult> { let cf_hti = self.hash_to_id_column(); let res = self .db @@ -728,15 +738,15 @@ impl RocksDBIO { .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|serr| { + Ok(Some(borsh::from_slice::(&data).map_err(|serr| { DbError::borsh_cast_message(serr, Some("Failed to deserialize block id".to_owned())) - })?) + })?)) } else { - Err(DbError::not_found("Block with given hash".to_owned())) + Ok(None) } } - pub fn get_block_id_by_tx_hash(&self, tx_hash: [u8; 32]) -> DbResult { + pub fn get_block_id_by_tx_hash(&self, tx_hash: [u8; 32]) -> DbResult> { let cf_tti = self.tx_hash_to_id_column(); let res = self .db @@ -752,11 +762,11 @@ impl RocksDBIO { .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|serr| { + Ok(Some(borsh::from_slice::(&data).map_err(|serr| { DbError::borsh_cast_message(serr, Some("Failed to deserialize block id".to_owned())) - })?) + })?)) } else { - Err(DbError::not_found("Block for given tx hash".to_owned())) + Ok(None) } } @@ -909,8 +919,14 @@ impl RocksDBIO { let mut tx_batch = vec![]; for tx_hash in self.get_acc_transaction_hashes(acc_id, offset, limit)? { - let block_id = self.get_block_id_by_tx_hash(tx_hash)?; - let block = self.get_block(block_id)?; + let block_id = self.get_block_id_by_tx_hash(tx_hash)?.ok_or_else(|| { + DbError::db_interaction_error(format!( + "Block id not found for tx hash {tx_hash:#?}" + )) + })?; + let block = self.get_block(block_id)?.ok_or_else(|| { + DbError::db_interaction_error(format!("Block with id {block_id} not found")) + })?; let transaction = block .body @@ -1007,7 +1023,7 @@ mod tests { let first_id = dbio.get_meta_first_block_in_db().unwrap(); let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); - let last_block = dbio.get_block(1).unwrap(); + let last_block = dbio.get_block(1).unwrap().unwrap(); let breakpoint = dbio.get_breakpoint(0).unwrap(); let final_state = dbio.final_state().unwrap(); @@ -1044,7 +1060,7 @@ mod tests { let first_id = dbio.get_meta_first_block_in_db().unwrap(); let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let breakpoint = dbio.get_breakpoint(0).unwrap(); let final_state = dbio.final_state().unwrap(); @@ -1075,7 +1091,7 @@ mod tests { for i in 1..BREAKPOINT_INTERVAL { let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, u128::from(i - 1), true); @@ -1091,7 +1107,7 @@ mod tests { let first_id = dbio.get_meta_first_block_in_db().unwrap(); let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_breakpoint = dbio.get_breakpoint(0).unwrap(); let breakpoint = dbio.get_breakpoint(1).unwrap(); let final_state = dbio.final_state().unwrap(); @@ -1130,7 +1146,7 @@ mod tests { RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 0, true); @@ -1141,7 +1157,7 @@ mod tests { dbio.put_block(&block, [1; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 1, true); @@ -1152,7 +1168,7 @@ mod tests { dbio.put_block(&block, [2; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 2, true); @@ -1163,7 +1179,7 @@ mod tests { dbio.put_block(&block, [3; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 3, true); @@ -1173,10 +1189,16 @@ mod tests { let block = common::test_utils::produce_dummy_block(5, Some(prev_hash), vec![transfer_tx]); dbio.put_block(&block, [4; 32]).unwrap(); - let control_block_id1 = dbio.get_block_id_by_hash(control_hash1.0).unwrap(); - let control_block_id2 = dbio.get_block_id_by_hash(control_hash2.0).unwrap(); - let control_block_id3 = dbio.get_block_id_by_tx_hash(control_tx_hash1.0).unwrap(); - let control_block_id4 = dbio.get_block_id_by_tx_hash(control_tx_hash2.0).unwrap(); + let control_block_id1 = dbio.get_block_id_by_hash(control_hash1.0).unwrap().unwrap(); + let control_block_id2 = dbio.get_block_id_by_hash(control_hash2.0).unwrap().unwrap(); + let control_block_id3 = dbio + .get_block_id_by_tx_hash(control_tx_hash1.0) + .unwrap() + .unwrap(); + let control_block_id4 = dbio + .get_block_id_by_tx_hash(control_tx_hash2.0) + .unwrap() + .unwrap(); assert_eq!(control_block_id1, 2); assert_eq!(control_block_id2, 3); @@ -1195,7 +1217,7 @@ mod tests { RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 0, true); @@ -1205,7 +1227,7 @@ mod tests { dbio.put_block(&block, [1; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 1, true); @@ -1215,7 +1237,7 @@ mod tests { dbio.put_block(&block, [2; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 2, true); @@ -1225,7 +1247,7 @@ mod tests { dbio.put_block(&block, [3; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 3, true); @@ -1273,7 +1295,7 @@ mod tests { RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 0, true); @@ -1285,7 +1307,7 @@ mod tests { dbio.put_block(&block, [1; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 1, true); @@ -1297,7 +1319,7 @@ mod tests { dbio.put_block(&block, [2; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 2, true); @@ -1309,7 +1331,7 @@ mod tests { dbio.put_block(&block, [3; 32]).unwrap(); let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); let prev_hash = last_block.header.hash; let transfer_tx = transfer(1, 3, true); diff --git a/storage/src/sequencer.rs b/storage/src/sequencer.rs index 3d0190d6..143b96ce 100644 --- a/storage/src/sequencer.rs +++ b/storage/src/sequencer.rs @@ -142,7 +142,9 @@ impl RocksDBIO { ) })?) } else { - Err(DbError::not_found("First block".to_owned())) + Err(DbError::db_interaction_error( + "First block not found".to_owned(), + )) } } @@ -169,7 +171,9 @@ impl RocksDBIO { ) })?) } else { - Err(DbError::not_found("Last block".to_owned())) + Err(DbError::db_interaction_error( + "Last block not found".to_owned(), + )) } } @@ -395,7 +399,9 @@ impl RocksDBIO { ) })?) } else { - Err(DbError::not_found("Latest block meta".to_owned())) + Err(DbError::db_interaction_error( + "Latest block meta not found".to_owned(), + )) } } @@ -436,7 +442,7 @@ impl RocksDBIO { Ok(()) } - pub fn get_block(&self, block_id: u64) -> DbResult { + pub fn get_block(&self, block_id: u64) -> DbResult> { let cf_block = self.block_column(); let res = self .db @@ -452,14 +458,14 @@ impl RocksDBIO { .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|serr| { + Ok(Some(borsh::from_slice::(&data).map_err(|serr| { DbError::borsh_cast_message( serr, Some("Failed to deserialize block data".to_owned()), ) - })?) + })?)) } else { - Err(DbError::not_found(format!("Block with id {block_id}"))) + Ok(None) } } @@ -486,7 +492,9 @@ impl RocksDBIO { ) })?) } else { - Err(DbError::not_found("NSSA state".to_owned())) + Err(DbError::db_interaction_error( + "NSSA state not found".to_owned(), + )) } } @@ -502,7 +510,9 @@ impl RocksDBIO { .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))? .is_none() { - return Err(DbError::not_found(format!("Block with id {block_id}"))); + return Err(DbError::db_interaction_error(format!( + "Block with id {block_id} not found" + ))); } self.db @@ -513,7 +523,9 @@ impl RocksDBIO { } pub fn mark_block_as_finalized(&self, block_id: u64) -> DbResult<()> { - let mut block = self.get_block(block_id)?; + let mut block = self.get_block(block_id)?.ok_or_else(|| { + DbError::db_interaction_error(format!("Block with id {block_id} not found")) + })?; block.bedrock_status = BedrockStatus::Finalized; let cf_block = self.block_column(); diff --git a/wallet-ffi/Cargo.toml b/wallet-ffi/Cargo.toml index 93096e12..07169639 100644 --- a/wallet-ffi/Cargo.toml +++ b/wallet-ffi/Cargo.toml @@ -15,6 +15,7 @@ wallet.workspace = true nssa.workspace = true common.workspace = true nssa_core.workspace = true +sequencer_service_rpc = { workspace = true, features = ["client"] } tokio.workspace = true [build-dependencies] diff --git a/wallet-ffi/src/lib.rs b/wallet-ffi/src/lib.rs index c36b05e0..d84bf5a3 100644 --- a/wallet-ffi/src/lib.rs +++ b/wallet-ffi/src/lib.rs @@ -28,7 +28,7 @@ use std::sync::OnceLock; -use common::error::ExecutionFailureKind; +use ::wallet::ExecutionFailureKind; // Re-export public types for cbindgen pub use error::WalletFfiError as FfiError; use tokio::runtime::Handle; diff --git a/wallet-ffi/src/pinata.rs b/wallet-ffi/src/pinata.rs index 7c8e21d0..7ec2fc48 100644 --- a/wallet-ffi/src/pinata.rs +++ b/wallet-ffi/src/pinata.rs @@ -75,8 +75,8 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata( let pinata = Pinata(&wallet); match block_on(pinata.claim(pinata_id, winner_id, solution)) { - Ok(response) => { - let tx_hash = CString::new(response.tx_hash.to_string()) + Ok(tx_hash) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -182,8 +182,8 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_already_initializ pinata .claim_private_owned_account_already_initialized(pinata_id, winner_id, solution, proof), ) { - Ok((response, _shared_key)) => { - let tx_hash = CString::new(response.tx_hash.to_string()) + Ok((tx_hash, _shared_key)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -268,8 +268,8 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_not_initialized( let pinata = Pinata(&wallet); match block_on(pinata.claim_private_owned_account(pinata_id, winner_id, solution)) { - Ok((response, _shared_key)) => { - let tx_hash = CString::new(response.tx_hash.to_string()) + Ok((tx_hash, _shared_key)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); diff --git a/wallet-ffi/src/sync.rs b/wallet-ffi/src/sync.rs index c321feb0..41031d06 100644 --- a/wallet-ffi/src/sync.rs +++ b/wallet-ffi/src/sync.rs @@ -1,5 +1,7 @@ //! Block synchronization functions. +use sequencer_service_rpc::RpcClient as _; + use crate::{ block_on, error::{print_error, WalletFfiError}, @@ -134,10 +136,10 @@ pub unsafe extern "C" fn wallet_ffi_get_current_block_height( } }; - match block_on(wallet.sequencer_client.get_last_block()) { - Ok(response) => { + match block_on(wallet.sequencer_client.get_last_block_id()) { + Ok(last_block_id) => { unsafe { - *out_block_height = response.last_block; + *out_block_height = last_block_id; } WalletFfiError::Success } diff --git a/wallet-ffi/src/transfer.rs b/wallet-ffi/src/transfer.rs index da1892dd..5b1e27d2 100644 --- a/wallet-ffi/src/transfer.rs +++ b/wallet-ffi/src/transfer.rs @@ -73,8 +73,8 @@ pub unsafe extern "C" fn wallet_ffi_transfer_public( let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_public_transfer(from_id, to_id, amount)) { - Ok(response) => { - let tx_hash = CString::new(response.tx_hash.to_string()) + Ok(tx_hash) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -163,8 +163,8 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded( match block_on( transfer.send_shielded_transfer_to_outer_account(from_id, to_npk, to_vpk, amount), ) { - Ok((response, _shared_key)) => { - let tx_hash = CString::new(response.tx_hash) + Ok((tx_hash, _shared_key)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -244,8 +244,8 @@ pub unsafe extern "C" fn wallet_ffi_transfer_deshielded( let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_deshielded_transfer(from_id, to_id, amount)) { - Ok((response, _shared_key)) => { - let tx_hash = CString::new(response.tx_hash) + Ok((tx_hash, _shared_key)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -333,8 +333,8 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private( match block_on(transfer.send_private_transfer_to_outer_account(from_id, to_npk, to_vpk, amount)) { - Ok((response, _shared_key)) => { - let tx_hash = CString::new(response.tx_hash) + Ok((tx_hash, _shared_key)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -417,8 +417,8 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded_owned( let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_shielded_transfer(from_id, to_id, amount)) { - Ok((response, _shared_key)) => { - let tx_hash = CString::new(response.tx_hash) + Ok((tx_hash, _shared_key)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -501,8 +501,8 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private_owned( let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.send_private_transfer_to_owned_account(from_id, to_id, amount)) { - Ok((response, _shared_keys)) => { - let tx_hash = CString::new(response.tx_hash) + Ok((tx_hash, _shared_keys)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -573,8 +573,8 @@ pub unsafe extern "C" fn wallet_ffi_register_public_account( let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.register_account(account_id)) { - Ok(response) => { - let tx_hash = CString::new(response.tx_hash.to_string()) + Ok(tx_hash) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); @@ -645,8 +645,8 @@ pub unsafe extern "C" fn wallet_ffi_register_private_account( let transfer = NativeTokenTransfer(&wallet); match block_on(transfer.register_account_private(account_id)) { - Ok((res, _secret)) => { - let tx_hash = CString::new(res.tx_hash) + Ok((tx_hash, _secret)) => { + let tx_hash = CString::new(tx_hash.to_string()) .map(std::ffi::CString::into_raw) .unwrap_or(ptr::null_mut()); diff --git a/wallet/Cargo.toml b/wallet/Cargo.toml index 63e14bb6..e4eedd2e 100644 --- a/wallet/Cargo.toml +++ b/wallet/Cargo.toml @@ -12,10 +12,12 @@ nssa_core.workspace = true nssa.workspace = true common.workspace = true key_protocol.workspace = true +sequencer_service_rpc = { workspace = true, features = ["client"] } token_core.workspace = true amm_core.workspace = true anyhow.workspace = true +thiserror.workspace = true serde_json.workspace = true env_logger.workspace = true log.workspace = true diff --git a/wallet/src/cli/account.rs b/wallet/src/cli/account.rs index c7d76f24..7e90b1bd 100644 --- a/wallet/src/cli/account.rs +++ b/wallet/src/cli/account.rs @@ -3,6 +3,7 @@ use clap::Subcommand; use itertools::Itertools as _; use key_protocol::key_management::key_tree::chain_index::ChainIndex; use nssa::{Account, PublicKey, program::Program}; +use sequencer_service_rpc::RpcClient; use token_core::{TokenDefinition, TokenHolding}; use crate::{ @@ -244,11 +245,7 @@ impl WalletSubcommand for AccountSubcommand { } Self::New(new_subcommand) => new_subcommand.handle_subcommand(wallet_core).await, Self::SyncPrivate => { - let curr_last_block = wallet_core - .sequencer_client - .get_last_block() - .await? - .last_block; + let curr_last_block = wallet_core.sequencer_client.get_last_block_id().await?; if wallet_core .storage diff --git a/wallet/src/cli/chain.rs b/wallet/src/cli/chain.rs index 4beadbbc..dfb22eba 100644 --- a/wallet/src/cli/chain.rs +++ b/wallet/src/cli/chain.rs @@ -1,6 +1,7 @@ use anyhow::Result; use clap::Subcommand; use common::HashType; +use sequencer_service_rpc::RpcClient as _; use crate::{ WalletCore, @@ -32,22 +33,19 @@ impl WalletSubcommand for ChainSubcommand { ) -> Result { match self { Self::CurrentBlockId => { - let latest_block_res = wallet_core.sequencer_client.get_last_block().await?; + let latest_block_id = wallet_core.sequencer_client.get_last_block_id().await?; - println!("Last block id is {}", latest_block_res.last_block); + println!("Last block id is {latest_block_id}"); } Self::Block { id } => { - let block_res = wallet_core.sequencer_client.get_block(id).await?; + let block = wallet_core.sequencer_client.get_block(id).await?; - println!("Last block id is {:#?}", block_res.block); + println!("Last block id is {block:#?}"); } Self::Transaction { hash } => { - let tx_res = wallet_core - .sequencer_client - .get_transaction_by_hash(hash) - .await?; + let tx = wallet_core.sequencer_client.get_transaction(hash).await?; - println!("Last block id is {:#?}", tx_res.transaction); + println!("Transaction is {tx:#?}"); } } Ok(SubcommandReturnValue::Empty) diff --git a/wallet/src/cli/mod.rs b/wallet/src/cli/mod.rs index 58d77d6a..221f5539 100644 --- a/wallet/src/cli/mod.rs +++ b/wallet/src/cli/mod.rs @@ -1,9 +1,11 @@ -use std::{io::Write as _, path::PathBuf, sync::Arc}; +use std::{io::Write as _, path::PathBuf}; use anyhow::{Context as _, Result}; use clap::{Parser, Subcommand}; -use common::HashType; +use common::{HashType, transaction::NSSATransaction}; +use futures::TryFutureExt; use nssa::{ProgramDeploymentTransaction, program::Program}; +use sequencer_service_rpc::RpcClient as _; use crate::{ WalletCore, @@ -175,7 +177,7 @@ pub async fn execute_subcommand( let transaction = ProgramDeploymentTransaction::new(message); let _response = wallet_core .sequencer_client - .send_tx_program(transaction) + .send_transaction(NSSATransaction::ProgramDeployment(transaction)) .await .context("Transaction submission error")?; @@ -188,11 +190,7 @@ pub async fn execute_subcommand( pub async fn execute_continuous_run(wallet_core: &mut WalletCore) -> Result<()> { loop { - let latest_block_num = wallet_core - .sequencer_client - .get_last_block() - .await? - .last_block; + let latest_block_num = wallet_core.sequencer_client.get_last_block_id().await?; wallet_core.sync_to_block(latest_block_num).await?; tokio::time::sleep(wallet_core.config().seq_poll_timeout).await; @@ -230,16 +228,17 @@ pub async fn execute_keys_restoration(wallet_core: &mut WalletCore, depth: u32) .storage .user_data .public_key_tree - .cleanup_tree_remove_uninit_layered(depth, Arc::clone(&wallet_core.sequencer_client)) + .cleanup_tree_remove_uninit_layered(depth, |account_id| { + wallet_core + .sequencer_client + .get_account(account_id) + .map_err(Into::into) + }) .await?; println!("Public tree cleaned up"); - let last_block = wallet_core - .sequencer_client - .get_last_block() - .await? - .last_block; + let last_block = wallet_core.sequencer_client.get_last_block_id().await?; println!("Last block is {last_block}"); diff --git a/wallet/src/cli/programs/native_token_transfer.rs b/wallet/src/cli/programs/native_token_transfer.rs index 314f78ba..910c6914 100644 --- a/wallet/src/cli/programs/native_token_transfer.rs +++ b/wallet/src/cli/programs/native_token_transfer.rs @@ -58,14 +58,13 @@ impl WalletSubcommand for AuthTransferSubcommand { AccountPrivacyKind::Public => { let account_id = account_id.parse()?; - let res = NativeTokenTransfer(wallet_core) + let tx_hash = NativeTokenTransfer(wallet_core) .register_account(account_id) .await?; - println!("Results of tx send are {res:#?}"); + println!("Results of tx send are {tx_hash:#?}"); - let transfer_tx = - wallet_core.poll_native_token_transfer(res.tx_hash).await?; + let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; println!("Transaction data is {transfer_tx:?}"); @@ -74,13 +73,12 @@ impl WalletSubcommand for AuthTransferSubcommand { AccountPrivacyKind::Private => { let account_id = account_id.parse()?; - let (res, secret) = NativeTokenTransfer(wallet_core) + let (tx_hash, secret) = NativeTokenTransfer(wallet_core) .register_account_private(account_id) .await?; - println!("Results of tx send are {res:#?}"); + println!("Results of tx send are {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -311,13 +309,12 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandPrivate { let from: AccountId = from.parse().unwrap(); let to: AccountId = to.parse().unwrap(); - let (res, [secret_from, secret_to]) = NativeTokenTransfer(wallet_core) + let (tx_hash, [secret_from, secret_to]) = NativeTokenTransfer(wallet_core) .send_private_transfer_to_owned_account(from, to, amount) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -351,13 +348,12 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandPrivate { let to_vpk = nssa_core::encryption::shared_key_derivation::Secp256k1Point(to_vpk.to_vec()); - let (res, [secret_from, _]) = NativeTokenTransfer(wallet_core) + let (tx_hash, [secret_from, _]) = NativeTokenTransfer(wallet_core) .send_private_transfer_to_outer_account(from, to_npk, to_vpk, amount) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -387,13 +383,12 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandShielded { let from: AccountId = from.parse().unwrap(); let to: AccountId = to.parse().unwrap(); - let (res, secret) = NativeTokenTransfer(wallet_core) + let (tx_hash, secret) = NativeTokenTransfer(wallet_core) .send_shielded_transfer(from, to, amount) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -428,13 +423,11 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommandShielded { let to_vpk = nssa_core::encryption::shared_key_derivation::Secp256k1Point(to_vpk.to_vec()); - let (res, _) = NativeTokenTransfer(wallet_core) + let (tx_hash, _) = NativeTokenTransfer(wallet_core) .send_shielded_transfer_to_outer_account(from, to_npk, to_vpk, amount) .await?; - println!("Results of tx send are {res:#?}"); - - let tx_hash = res.tx_hash; + println!("Transaction hash is {tx_hash:#?}"); wallet_core.store_persistent_data().await?; @@ -460,13 +453,12 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommand { let from: AccountId = from.parse().unwrap(); let to: AccountId = to.parse().unwrap(); - let (res, secret) = NativeTokenTransfer(wallet_core) + let (tx_hash, secret) = NativeTokenTransfer(wallet_core) .send_deshielded_transfer(from, to, amount) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -486,13 +478,13 @@ impl WalletSubcommand for NativeTokenTransferProgramSubcommand { let from: AccountId = from.parse().unwrap(); let to: AccountId = to.parse().unwrap(); - let res = NativeTokenTransfer(wallet_core) + let tx_hash = NativeTokenTransfer(wallet_core) .send_public_transfer(from, to, amount) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let transfer_tx = wallet_core.poll_native_token_transfer(res.tx_hash).await?; + let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; println!("Transaction data is {transfer_tx:?}"); diff --git a/wallet/src/cli/programs/pinata.rs b/wallet/src/cli/programs/pinata.rs index 948da9c2..9dba482c 100644 --- a/wallet/src/cli/programs/pinata.rs +++ b/wallet/src/cli/programs/pinata.rs @@ -112,13 +112,12 @@ impl WalletSubcommand for PinataProgramSubcommandPublic { .await .context("failed to compute solution")?; - let res = Pinata(wallet_core) + let tx_hash = Pinata(wallet_core) .claim(pinata_account_id, winner_account_id, solution) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; println!("Transaction data is {transfer_tx:?}"); @@ -148,13 +147,12 @@ impl WalletSubcommand for PinataProgramSubcommandPrivate { .await .context("failed to compute solution")?; - let (res, secret_winner) = Pinata(wallet_core) + let (tx_hash, secret_winner) = Pinata(wallet_core) .claim_private_owned_account(pinata_account_id, winner_account_id, solution) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; println!("Transaction data is {transfer_tx:?}"); diff --git a/wallet/src/cli/programs/token.rs b/wallet/src/cli/programs/token.rs index 65a283dd..cd5ec4b2 100644 --- a/wallet/src/cli/programs/token.rs +++ b/wallet/src/cli/programs/token.rs @@ -713,7 +713,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { let sender_account_id: AccountId = sender_account_id.parse().unwrap(); let recipient_account_id: AccountId = recipient_account_id.parse().unwrap(); - let (res, [secret_sender, secret_recipient]) = Token(wallet_core) + let (tx_hash, [secret_sender, secret_recipient]) = Token(wallet_core) .send_transfer_transaction_private_owned_account( sender_account_id, recipient_account_id, @@ -721,9 +721,8 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -761,7 +760,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { recipient_vpk.to_vec(), ); - let (res, [secret_sender, _]) = Token(wallet_core) + let (tx_hash, [secret_sender, _]) = Token(wallet_core) .send_transfer_transaction_private_foreign_account( sender_account_id, recipient_npk, @@ -770,9 +769,8 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -796,7 +794,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let holder_account_id: AccountId = holder_account_id.parse().unwrap(); - let (res, [secret_definition, secret_holder]) = Token(wallet_core) + let (tx_hash, [secret_definition, secret_holder]) = Token(wallet_core) .send_burn_transaction_private_owned_account( definition_account_id, holder_account_id, @@ -804,9 +802,8 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -833,7 +830,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let holder_account_id: AccountId = holder_account_id.parse().unwrap(); - let (res, [secret_definition, secret_holder]) = Token(wallet_core) + let (tx_hash, [secret_definition, secret_holder]) = Token(wallet_core) .send_mint_transaction_private_owned_account( definition_account_id, holder_account_id, @@ -841,9 +838,8 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -882,7 +878,7 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { holder_vpk.to_vec(), ); - let (res, [secret_definition, _]) = Token(wallet_core) + let (tx_hash, [secret_definition, _]) = Token(wallet_core) .send_mint_transaction_private_foreign_account( definition_account_id, holder_npk, @@ -891,9 +887,8 @@ impl WalletSubcommand for TokenProgramSubcommandPrivate { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -927,7 +922,7 @@ impl WalletSubcommand for TokenProgramSubcommandDeshielded { let sender_account_id: AccountId = sender_account_id.parse().unwrap(); let recipient_account_id: AccountId = recipient_account_id.parse().unwrap(); - let (res, secret_sender) = Token(wallet_core) + let (tx_hash, secret_sender) = Token(wallet_core) .send_transfer_transaction_deshielded( sender_account_id, recipient_account_id, @@ -935,9 +930,8 @@ impl WalletSubcommand for TokenProgramSubcommandDeshielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -961,7 +955,7 @@ impl WalletSubcommand for TokenProgramSubcommandDeshielded { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let holder_account_id: AccountId = holder_account_id.parse().unwrap(); - let (res, secret_definition) = Token(wallet_core) + let (tx_hash, secret_definition) = Token(wallet_core) .send_burn_transaction_deshielded_owned_account( definition_account_id, holder_account_id, @@ -969,9 +963,8 @@ impl WalletSubcommand for TokenProgramSubcommandDeshielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -995,7 +988,7 @@ impl WalletSubcommand for TokenProgramSubcommandDeshielded { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let holder_account_id: AccountId = holder_account_id.parse().unwrap(); - let (res, secret_definition) = Token(wallet_core) + let (tx_hash, secret_definition) = Token(wallet_core) .send_mint_transaction_deshielded( definition_account_id, holder_account_id, @@ -1003,9 +996,8 @@ impl WalletSubcommand for TokenProgramSubcommandDeshielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1050,7 +1042,7 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { recipient_vpk.to_vec(), ); - let (res, _) = Token(wallet_core) + let (tx_hash, _) = Token(wallet_core) .send_transfer_transaction_shielded_foreign_account( sender_account_id, recipient_npk, @@ -1059,9 +1051,8 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1080,7 +1071,7 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { let sender_account_id: AccountId = sender_account_id.parse().unwrap(); let recipient_account_id: AccountId = recipient_account_id.parse().unwrap(); - let (res, secret_recipient) = Token(wallet_core) + let (tx_hash, secret_recipient) = Token(wallet_core) .send_transfer_transaction_shielded_owned_account( sender_account_id, recipient_account_id, @@ -1088,9 +1079,8 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1114,7 +1104,7 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let holder_account_id: AccountId = holder_account_id.parse().unwrap(); - let (res, secret_holder) = Token(wallet_core) + let (tx_hash, secret_holder) = Token(wallet_core) .send_burn_transaction_shielded( definition_account_id, holder_account_id, @@ -1122,9 +1112,8 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1148,7 +1137,7 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let holder_account_id: AccountId = holder_account_id.parse().unwrap(); - let (res, secret_holder) = Token(wallet_core) + let (tx_hash, secret_holder) = Token(wallet_core) .send_mint_transaction_shielded_owned_account( definition_account_id, holder_account_id, @@ -1156,9 +1145,8 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1194,7 +1182,7 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { holder_vpk.to_vec(), ); - let (res, _) = Token(wallet_core) + let (tx_hash, _) = Token(wallet_core) .send_mint_transaction_shielded_foreign_account( definition_account_id, holder_npk, @@ -1203,9 +1191,8 @@ impl WalletSubcommand for TokenProgramSubcommandShielded { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1235,7 +1222,7 @@ impl WalletSubcommand for CreateNewTokenProgramSubcommand { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let supply_account_id: AccountId = supply_account_id.parse().unwrap(); - let (res, [secret_definition, secret_supply]) = Token(wallet_core) + let (tx_hash, [secret_definition, secret_supply]) = Token(wallet_core) .send_new_definition_private_owned_definiton_and_supply( definition_account_id, supply_account_id, @@ -1244,9 +1231,8 @@ impl WalletSubcommand for CreateNewTokenProgramSubcommand { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1274,7 +1260,7 @@ impl WalletSubcommand for CreateNewTokenProgramSubcommand { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let supply_account_id: AccountId = supply_account_id.parse().unwrap(); - let (res, secret_definition) = Token(wallet_core) + let (tx_hash, secret_definition) = Token(wallet_core) .send_new_definition_private_owned_definiton( definition_account_id, supply_account_id, @@ -1283,9 +1269,8 @@ impl WalletSubcommand for CreateNewTokenProgramSubcommand { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { @@ -1310,7 +1295,7 @@ impl WalletSubcommand for CreateNewTokenProgramSubcommand { let definition_account_id: AccountId = definition_account_id.parse().unwrap(); let supply_account_id: AccountId = supply_account_id.parse().unwrap(); - let (res, secret_supply) = Token(wallet_core) + let (tx_hash, secret_supply) = Token(wallet_core) .send_new_definition_private_owned_supply( definition_account_id, supply_account_id, @@ -1319,9 +1304,8 @@ impl WalletSubcommand for CreateNewTokenProgramSubcommand { ) .await?; - println!("Results of tx send are {res:#?}"); + println!("Transaction hash is {tx_hash:#?}"); - let tx_hash = res.tx_hash; let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?; if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index d873b357..e2bb6a8a 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -8,15 +8,11 @@ reason = "Most of the shadows come from args parsing which is ok" )] -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; use anyhow::{Context as _, Result}; -use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64}; use chain_storage::WalletChainStore; -use common::{ - HashType, error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse, - sequencer_client::SequencerClient, transaction::NSSATransaction, -}; +use common::{HashType, transaction::NSSATransaction}; use config::WalletConfig; use key_protocol::key_management::key_tree::{chain_index::ChainIndex, traits::KeyNode as _}; use log::info; @@ -28,6 +24,7 @@ use nssa::{ }; use nssa_core::{Commitment, MembershipProof, SharedSecretKey, program::InstructionData}; pub use privacy_preserving_tx::PrivacyPreservingAccount; +use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder}; use tokio::io::AsyncWriteExt as _; use crate::{ @@ -59,8 +56,8 @@ pub enum ExecutionFailureKind { AmountMismatchError, #[error("Accounts key not found")] KeyNotFoundError, - #[error("Sequencer client error: {0:?}")] - SequencerClientError(#[from] SequencerClientError), + #[error("Sequencer client error")] + SequencerClientError(#[from] sequencer_service_rpc::ClientError), #[error("Can not pay for operation")] InsufficientFundsError, #[error("Account {0} data is invalid")] @@ -74,7 +71,7 @@ pub struct WalletCore { storage: WalletChainStore, storage_path: PathBuf, poller: TxPoller, - pub sequencer_client: Arc, + pub sequencer_client: SequencerClient, pub last_synced_block: u64, } @@ -145,11 +142,26 @@ impl WalletCore { config.apply_overrides(config_overrides); } - let sequencer_client = Arc::new(SequencerClient::new_with_auth( - config.sequencer_addr.clone(), - config.basic_auth.clone(), - )?); - let tx_poller = TxPoller::new(&config, Arc::clone(&sequencer_client)); + let sequencer_client = { + let mut builder = SequencerClientBuilder::default(); + if let Some(basic_auth) = &config.basic_auth { + builder = builder.set_headers( + [( + "Authorization".parse().expect("Header name is valid"), + format!("Basic {}", basic_auth) + .parse() + .context("Invalid basic auth format")?, + )] + .into_iter() + .collect(), + ); + } + builder + .build(config.sequencer_addr.clone()) + .context("Failed to create sequencer client")? + }; + + let tx_poller = TxPoller::new(&config, sequencer_client.clone()); let storage = storage_ctor(config)?; @@ -238,26 +250,17 @@ impl WalletCore { /// Get account balance. pub async fn get_account_balance(&self, acc: AccountId) -> Result { - Ok(self - .sequencer_client - .get_account_balance(acc) - .await? - .balance) + Ok(self.sequencer_client.get_account_balance(acc).await?) } /// Get accounts nonces. pub async fn get_accounts_nonces(&self, accs: Vec) -> Result> { - Ok(self - .sequencer_client - .get_accounts_nonces(accs) - .await? - .nonces) + Ok(self.sequencer_client.get_accounts_nonces(accs).await?) } /// Get account. pub async fn get_account_public(&self, account_id: AccountId) -> Result { - let response = self.sequencer_client.get_account(account_id).await?; - Ok(response.account) + Ok(self.sequencer_client.get_account(account_id).await?) } #[must_use] @@ -286,11 +289,7 @@ impl WalletCore { /// Poll transactions. pub async fn poll_native_token_transfer(&self, hash: HashType) -> Result { - let transaction_encoded = self.poller.poll_tx(hash).await?; - let tx_base64_decode = BASE64.decode(transaction_encoded)?; - let pub_tx = borsh::from_slice::(&tx_base64_decode).unwrap(); - - Ok(pub_tx) + self.poller.poll_tx(hash).await } pub async fn check_private_account_initialized( @@ -301,6 +300,7 @@ impl WalletCore { self.sequencer_client .get_proof_for_commitment(acc_comm) .await + .map(Some) .map_err(anyhow::Error::from) } else { Ok(None) @@ -351,7 +351,7 @@ impl WalletCore { accounts: Vec, instruction_data: InstructionData, program: &ProgramWithDependencies, - ) -> Result<(SendTxResponse, Vec), ExecutionFailureKind> { + ) -> Result<(HashType, Vec), ExecutionFailureKind> { self.send_privacy_preserving_tx_with_pre_check(accounts, instruction_data, program, |_| { Ok(()) }) @@ -364,7 +364,7 @@ impl WalletCore { instruction_data: InstructionData, program: &ProgramWithDependencies, tx_pre_check: impl FnOnce(&[&Account]) -> Result<(), ExecutionFailureKind>, - ) -> Result<(SendTxResponse, Vec), ExecutionFailureKind> { + ) -> Result<(HashType, Vec), ExecutionFailureKind> { let acc_manager = privacy_preserving_tx::AccountManager::new(self, accounts).await?; let pre_states = acc_manager.pre_states(); @@ -416,7 +416,9 @@ impl WalletCore { .collect(); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client + .send_transaction(NSSATransaction::PrivacyPreserving(tx)) + .await?, shared_secrets, )) } @@ -443,11 +445,11 @@ impl WalletCore { let bar = indicatif::ProgressBar::new(num_of_blocks); while let Some(block) = blocks.try_next().await? { - for tx in block.transactions { + for tx in block.body.transactions { self.sync_private_accounts_with_tx(tx); } - self.last_synced_block = block.block_id; + self.last_synced_block = block.header.block_id; self.store_persistent_data().await?; bar.inc(1); } diff --git a/wallet/src/pinata_interactions.rs b/wallet/src/pinata_interactions.rs index abcfcf6a..22595a89 100644 --- a/wallet/src/pinata_interactions.rs +++ b/wallet/src/pinata_interactions.rs @@ -1,10 +1,12 @@ -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{HashType, transaction::NSSATransaction}; +use sequencer_service_rpc::RpcClient as _; use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder; use nssa::{AccountId, privacy_preserving_transaction::circuit}; use nssa_core::{MembershipProof, SharedSecretKey, account::AccountWithMetadata}; use crate::{ - WalletCore, helperfunctions::produce_random_nonces, transaction_utils::AccountPreparedData, + ExecutionFailureKind, WalletCore, helperfunctions::produce_random_nonces, + transaction_utils::AccountPreparedData, }; impl WalletCore { @@ -13,7 +15,7 @@ impl WalletCore { pinata_account_id: AccountId, winner_account_id: AccountId, solution: u128, - ) -> Result { + ) -> Result { let account_ids = vec![pinata_account_id, winner_account_id]; let program_id = nssa::program::Program::pinata().id(); let message = @@ -23,7 +25,7 @@ impl WalletCore { let witness_set = nssa::public_transaction::WitnessSet::for_message(&message, &[]); let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.sequencer_client.send_tx_public(tx).await?) + Ok(self.sequencer_client.send_transaction(NSSATransaction::Public(tx)).await?) } pub async fn claim_pinata_private_owned_account_already_initialized( @@ -32,7 +34,7 @@ impl WalletCore { winner_account_id: AccountId, solution: u128, winner_proof: MembershipProof, - ) -> Result<(SendTxResponse, [SharedSecretKey; 1]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 1]), ExecutionFailureKind> { let AccountPreparedData { nsk: winner_nsk, npk: winner_npk, @@ -89,7 +91,7 @@ impl WalletCore { ); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret_winner], )) } @@ -99,7 +101,7 @@ impl WalletCore { pinata_account_id: AccountId, winner_account_id: AccountId, solution: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 1]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 1]), ExecutionFailureKind> { let AccountPreparedData { nsk: _, npk: winner_npk, @@ -156,7 +158,7 @@ impl WalletCore { ); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret_winner], )) } diff --git a/wallet/src/poller.rs b/wallet/src/poller.rs index 113f42ee..7dbd59c1 100644 --- a/wallet/src/poller.rs +++ b/wallet/src/poller.rs @@ -1,8 +1,9 @@ -use std::{sync::Arc, time::Duration}; +use std::time::Duration; use anyhow::Result; -use common::{HashType, block::HashableBlockData, sequencer_client::SequencerClient}; +use common::{HashType, block::Block, transaction::NSSATransaction}; use log::{info, warn}; +use sequencer_service_rpc::{RpcClient as _, SequencerClient}; use crate::config::WalletConfig; @@ -13,12 +14,12 @@ pub struct TxPoller { polling_max_error_attempts: u64, polling_delay: Duration, block_poll_max_amount: u64, - client: Arc, + client: SequencerClient, } impl TxPoller { #[must_use] - pub const fn new(config: &WalletConfig, client: Arc) -> Self { + pub const fn new(config: &WalletConfig, client: SequencerClient) -> Self { Self { polling_delay: config.seq_poll_timeout, polling_max_blocks_to_query: config.seq_tx_poll_max_blocks, @@ -29,7 +30,7 @@ impl TxPoller { } // TODO: this polling is not based on blocks, but on timeouts, need to fix this. - pub async fn poll_tx(&self, tx_hash: HashType) -> Result { + pub async fn poll_tx(&self, tx_hash: HashType) -> Result { let max_blocks_to_query = self.polling_max_blocks_to_query; info!("Starting poll for transaction {tx_hash}"); @@ -38,29 +39,22 @@ impl TxPoller { let mut try_error_counter = 0_u64; - let tx_obj = loop { - let tx_obj = self - .client - .get_transaction_by_hash(tx_hash) - .await - .inspect_err(|err| { + loop { + match self.client.get_transaction(tx_hash).await { + Ok(Some(tx)) => return Ok(tx), + Ok(None) => {} + Err(err) => { warn!("Failed to get transaction by hash {tx_hash} with error: {err:#?}"); - }); - - if let Ok(tx_obj) = tx_obj { - break tx_obj; + } } + try_error_counter = try_error_counter .checked_add(1) .expect("We check error counter in this loop"); if try_error_counter > self.polling_max_error_attempts { - anyhow::bail!("Number of retries exceeded"); + break; } - }; - - if let Some(tx) = tx_obj.transaction { - return Ok(tx); } tokio::time::sleep(self.polling_delay).await; @@ -72,16 +66,15 @@ impl TxPoller { pub fn poll_block_range( &self, range: std::ops::RangeInclusive, - ) -> impl futures::Stream> { + ) -> impl futures::Stream> { async_stream::stream! { let mut chunk_start = *range.start(); loop { let chunk_end = std::cmp::min(chunk_start.saturating_add(self.block_poll_max_amount).saturating_sub(1), *range.end()); - let blocks = self.client.get_block_range(chunk_start..=chunk_end).await?.blocks; + let blocks = self.client.get_block_range(chunk_start, chunk_end).await?; for block in blocks { - let block = borsh::from_slice::(&block)?; yield Ok(block); } diff --git a/wallet/src/privacy_preserving_tx.rs b/wallet/src/privacy_preserving_tx.rs index 0aaffa9a..04056111 100644 --- a/wallet/src/privacy_preserving_tx.rs +++ b/wallet/src/privacy_preserving_tx.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use common::error::ExecutionFailureKind; use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder; use nssa::{AccountId, PrivateKey}; use nssa_core::{ @@ -8,7 +7,7 @@ use nssa_core::{ encryption::{EphemeralPublicKey, ViewingPublicKey}, }; -use crate::WalletCore; +use crate::{ExecutionFailureKind, WalletCore}; #[derive(Clone)] pub enum PrivacyPreservingAccount { diff --git a/wallet/src/program_facades/amm.rs b/wallet/src/program_facades/amm.rs index 19a51f29..c430f29d 100644 --- a/wallet/src/program_facades/amm.rs +++ b/wallet/src/program_facades/amm.rs @@ -1,9 +1,10 @@ use amm_core::{compute_liquidity_token_pda, compute_pool_pda, compute_vault_pda}; -use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; +use common::{HashType, transaction::NSSATransaction}; use nssa::{AccountId, program::Program}; +use sequencer_service_rpc::RpcClient as _; use token_core::TokenHolding; -use crate::WalletCore; +use crate::{ExecutionFailureKind, WalletCore}; pub struct Amm<'wallet>(pub &'wallet WalletCore); impl Amm<'_> { @@ -14,7 +15,7 @@ impl Amm<'_> { user_holding_lp: AccountId, balance_a: u128, balance_b: u128, - ) -> Result { + ) -> Result { let program = Program::amm(); let amm_program_id = Program::amm().id(); let instruction = amm_core::Instruction::NewDefinition { @@ -95,7 +96,11 @@ impl Amm<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } pub async fn send_swap( @@ -105,7 +110,7 @@ impl Amm<'_> { swap_amount_in: u128, min_amount_out: u128, token_definition_id_in: AccountId, - ) -> Result { + ) -> Result { let instruction = amm_core::Instruction::Swap { swap_amount_in, min_amount_out, @@ -203,7 +208,11 @@ impl Amm<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } pub async fn send_add_liquidity( @@ -214,7 +223,7 @@ impl Amm<'_> { min_amount_liquidity: u128, max_amount_to_add_token_a: u128, max_amount_to_add_token_b: u128, - ) -> Result { + ) -> Result { let instruction = amm_core::Instruction::AddLiquidity { min_amount_liquidity, max_amount_to_add_token_a, @@ -295,7 +304,11 @@ impl Amm<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } pub async fn send_remove_liquidity( @@ -306,7 +319,7 @@ impl Amm<'_> { remove_liquidity_amount: u128, min_amount_to_remove_token_a: u128, min_amount_to_remove_token_b: u128, - ) -> Result { + ) -> Result { let instruction = amm_core::Instruction::RemoveLiquidity { remove_liquidity_amount, min_amount_to_remove_token_a, @@ -378,6 +391,10 @@ impl Amm<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } } diff --git a/wallet/src/program_facades/native_token_transfer/deshielded.rs b/wallet/src/program_facades/native_token_transfer/deshielded.rs index 7b774595..d51f15ce 100644 --- a/wallet/src/program_facades/native_token_transfer/deshielded.rs +++ b/wallet/src/program_facades/native_token_transfer/deshielded.rs @@ -1,8 +1,8 @@ -use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; +use common::HashType; use nssa::AccountId; use super::{NativeTokenTransfer, auth_transfer_preparation}; -use crate::PrivacyPreservingAccount; +use crate::{ExecutionFailureKind, PrivacyPreservingAccount}; impl NativeTokenTransfer<'_> { pub async fn send_deshielded_transfer( @@ -10,7 +10,7 @@ impl NativeTokenTransfer<'_> { from: AccountId, to: AccountId, balance_to_move: u128, - ) -> Result<(SendTxResponse, nssa_core::SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, nssa_core::SharedSecretKey), ExecutionFailureKind> { let (instruction_data, program, tx_pre_check) = auth_transfer_preparation(balance_to_move); self.0 diff --git a/wallet/src/program_facades/native_token_transfer/mod.rs b/wallet/src/program_facades/native_token_transfer/mod.rs index 1db864f6..72be949a 100644 --- a/wallet/src/program_facades/native_token_transfer/mod.rs +++ b/wallet/src/program_facades/native_token_transfer/mod.rs @@ -1,8 +1,7 @@ -use common::error::ExecutionFailureKind; use nssa::{Account, program::Program}; use nssa_core::program::InstructionData; -use crate::WalletCore; +use crate::{ExecutionFailureKind, WalletCore}; pub mod deshielded; pub mod private; diff --git a/wallet/src/program_facades/native_token_transfer/private.rs b/wallet/src/program_facades/native_token_transfer/private.rs index eb37ec94..c3a2125b 100644 --- a/wallet/src/program_facades/native_token_transfer/private.rs +++ b/wallet/src/program_facades/native_token_transfer/private.rs @@ -1,17 +1,17 @@ use std::vec; -use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; +use common::HashType; use nssa::{AccountId, program::Program}; use nssa_core::{NullifierPublicKey, SharedSecretKey, encryption::ViewingPublicKey}; use super::{NativeTokenTransfer, auth_transfer_preparation}; -use crate::PrivacyPreservingAccount; +use crate::{ExecutionFailureKind, PrivacyPreservingAccount}; impl NativeTokenTransfer<'_> { pub async fn register_account_private( &self, from: AccountId, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction: u128 = 0; self.0 @@ -34,7 +34,7 @@ impl NativeTokenTransfer<'_> { to_npk: NullifierPublicKey, to_vpk: ViewingPublicKey, balance_to_move: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let (instruction_data, program, tx_pre_check) = auth_transfer_preparation(balance_to_move); self.0 @@ -64,7 +64,7 @@ impl NativeTokenTransfer<'_> { from: AccountId, to: AccountId, balance_to_move: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let (instruction_data, program, tx_pre_check) = auth_transfer_preparation(balance_to_move); self.0 diff --git a/wallet/src/program_facades/native_token_transfer/public.rs b/wallet/src/program_facades/native_token_transfer/public.rs index eefaa1fe..a54a215f 100644 --- a/wallet/src/program_facades/native_token_transfer/public.rs +++ b/wallet/src/program_facades/native_token_transfer/public.rs @@ -1,11 +1,13 @@ -use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; +use common::{HashType, transaction::NSSATransaction}; use nssa::{ AccountId, PublicTransaction, program::Program, public_transaction::{Message, WitnessSet}, }; +use sequencer_service_rpc::RpcClient as _; use super::NativeTokenTransfer; +use crate::ExecutionFailureKind; impl NativeTokenTransfer<'_> { pub async fn send_public_transfer( @@ -13,7 +15,7 @@ impl NativeTokenTransfer<'_> { from: AccountId, to: AccountId, balance_to_move: u128, - ) -> Result { + ) -> Result { let balance = self .0 .get_account_balance(from) @@ -50,7 +52,11 @@ impl NativeTokenTransfer<'_> { let tx = PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } else { Err(ExecutionFailureKind::InsufficientFundsError) } @@ -59,7 +65,7 @@ impl NativeTokenTransfer<'_> { pub async fn register_account( &self, from: AccountId, - ) -> Result { + ) -> Result { let nonces = self .0 .get_accounts_nonces(vec![from]) @@ -90,6 +96,10 @@ impl NativeTokenTransfer<'_> { let tx = PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } } diff --git a/wallet/src/program_facades/native_token_transfer/shielded.rs b/wallet/src/program_facades/native_token_transfer/shielded.rs index 22897502..625e1a8b 100644 --- a/wallet/src/program_facades/native_token_transfer/shielded.rs +++ b/wallet/src/program_facades/native_token_transfer/shielded.rs @@ -1,9 +1,9 @@ -use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; +use common::HashType; use nssa::AccountId; use nssa_core::{NullifierPublicKey, SharedSecretKey, encryption::ViewingPublicKey}; use super::{NativeTokenTransfer, auth_transfer_preparation}; -use crate::PrivacyPreservingAccount; +use crate::{ExecutionFailureKind, PrivacyPreservingAccount}; impl NativeTokenTransfer<'_> { pub async fn send_shielded_transfer( @@ -11,7 +11,7 @@ impl NativeTokenTransfer<'_> { from: AccountId, to: AccountId, balance_to_move: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let (instruction_data, program, tx_pre_check) = auth_transfer_preparation(balance_to_move); self.0 @@ -40,7 +40,7 @@ impl NativeTokenTransfer<'_> { to_npk: NullifierPublicKey, to_vpk: ViewingPublicKey, balance_to_move: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let (instruction_data, program, tx_pre_check) = auth_transfer_preparation(balance_to_move); self.0 diff --git a/wallet/src/program_facades/pinata.rs b/wallet/src/program_facades/pinata.rs index c68fa658..97118ecd 100644 --- a/wallet/src/program_facades/pinata.rs +++ b/wallet/src/program_facades/pinata.rs @@ -1,8 +1,9 @@ -use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; +use common::{HashType, transaction::NSSATransaction}; use nssa::AccountId; use nssa_core::{MembershipProof, SharedSecretKey}; +use sequencer_service_rpc::RpcClient as _; -use crate::{PrivacyPreservingAccount, WalletCore}; +use crate::{ExecutionFailureKind, PrivacyPreservingAccount, WalletCore}; pub struct Pinata<'wallet>(pub &'wallet WalletCore); @@ -12,7 +13,7 @@ impl Pinata<'_> { pinata_account_id: AccountId, winner_account_id: AccountId, solution: u128, - ) -> Result { + ) -> Result { let account_ids = vec![pinata_account_id, winner_account_id]; let program_id = nssa::program::Program::pinata().id(); let message = @@ -22,7 +23,11 @@ impl Pinata<'_> { let witness_set = nssa::public_transaction::WitnessSet::for_message(&message, &[]); let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } /// Claim a pinata reward using a privacy-preserving transaction for an already-initialized @@ -36,7 +41,7 @@ impl Pinata<'_> { winner_account_id: AccountId, solution: u128, _winner_proof: MembershipProof, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { self.claim_private_owned_account(pinata_account_id, winner_account_id, solution) .await } @@ -46,7 +51,7 @@ impl Pinata<'_> { pinata_account_id: AccountId, winner_account_id: AccountId, solution: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { self.0 .send_privacy_preserving_tx( vec![ diff --git a/wallet/src/program_facades/token.rs b/wallet/src/program_facades/token.rs index bdacae37..ca36e723 100644 --- a/wallet/src/program_facades/token.rs +++ b/wallet/src/program_facades/token.rs @@ -1,9 +1,10 @@ -use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; +use common::{HashType, transaction::NSSATransaction}; use nssa::{AccountId, program::Program}; use nssa_core::{NullifierPublicKey, SharedSecretKey, encryption::ViewingPublicKey}; +use sequencer_service_rpc::RpcClient as _; use token_core::Instruction; -use crate::{PrivacyPreservingAccount, WalletCore}; +use crate::{ExecutionFailureKind, PrivacyPreservingAccount, WalletCore}; pub struct Token<'wallet>(pub &'wallet WalletCore); @@ -14,7 +15,7 @@ impl Token<'_> { supply_account_id: AccountId, name: String, total_supply: u128, - ) -> Result { + ) -> Result { let account_ids = vec![definition_account_id, supply_account_id]; let program_id = nssa::program::Program::token().id(); let instruction = Instruction::NewFungibleDefinition { name, total_supply }; @@ -30,7 +31,11 @@ impl Token<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } pub async fn send_new_definition_private_owned_supply( @@ -39,7 +44,7 @@ impl Token<'_> { supply_account_id: AccountId, name: String, total_supply: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::NewFungibleDefinition { name, total_supply }; let instruction_data = Program::serialize_instruction(instruction).expect("Instruction should serialize"); @@ -69,7 +74,7 @@ impl Token<'_> { supply_account_id: AccountId, name: String, total_supply: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::NewFungibleDefinition { name, total_supply }; let instruction_data = Program::serialize_instruction(instruction).expect("Instruction should serialize"); @@ -99,7 +104,7 @@ impl Token<'_> { supply_account_id: AccountId, name: String, total_supply: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let instruction = Instruction::NewFungibleDefinition { name, total_supply }; let instruction_data = Program::serialize_instruction(instruction).expect("Instruction should serialize"); @@ -127,7 +132,7 @@ impl Token<'_> { sender_account_id: AccountId, recipient_account_id: AccountId, amount: u128, - ) -> Result { + ) -> Result { let account_ids = vec![sender_account_id, recipient_account_id]; let program_id = nssa::program::Program::token().id(); let instruction = Instruction::Transfer { @@ -162,7 +167,11 @@ impl Token<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } pub async fn send_transfer_transaction_private_owned_account( @@ -170,7 +179,7 @@ impl Token<'_> { sender_account_id: AccountId, recipient_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let instruction = Instruction::Transfer { amount_to_transfer: amount, }; @@ -201,7 +210,7 @@ impl Token<'_> { recipient_npk: NullifierPublicKey, recipient_vpk: ViewingPublicKey, amount: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let instruction = Instruction::Transfer { amount_to_transfer: amount, }; @@ -234,7 +243,7 @@ impl Token<'_> { sender_account_id: AccountId, recipient_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Transfer { amount_to_transfer: amount, }; @@ -265,7 +274,7 @@ impl Token<'_> { sender_account_id: AccountId, recipient_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Transfer { amount_to_transfer: amount, }; @@ -297,7 +306,7 @@ impl Token<'_> { recipient_npk: NullifierPublicKey, recipient_vpk: ViewingPublicKey, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Transfer { amount_to_transfer: amount, }; @@ -331,7 +340,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result { + ) -> Result { let account_ids = vec![definition_account_id, holder_account_id]; let instruction = Instruction::Burn { amount_to_burn: amount, @@ -364,7 +373,11 @@ impl Token<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } pub async fn send_burn_transaction_private_owned_account( @@ -372,7 +385,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let instruction = Instruction::Burn { amount_to_burn: amount, }; @@ -402,7 +415,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Burn { amount_to_burn: amount, }; @@ -433,7 +446,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Burn { amount_to_burn: amount, }; @@ -464,7 +477,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result { + ) -> Result { let account_ids = vec![definition_account_id, holder_account_id]; let instruction = Instruction::Mint { amount_to_mint: amount, @@ -499,7 +512,11 @@ impl Token<'_> { let tx = nssa::PublicTransaction::new(message, witness_set); - Ok(self.0.sequencer_client.send_tx_public(tx).await?) + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) } pub async fn send_mint_transaction_private_owned_account( @@ -507,7 +524,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let instruction = Instruction::Mint { amount_to_mint: amount, }; @@ -538,7 +555,7 @@ impl Token<'_> { holder_npk: NullifierPublicKey, holder_vpk: ViewingPublicKey, amount: u128, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let instruction = Instruction::Mint { amount_to_mint: amount, }; @@ -571,7 +588,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Mint { amount_to_mint: amount, }; @@ -602,7 +619,7 @@ impl Token<'_> { definition_account_id: AccountId, holder_account_id: AccountId, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Mint { amount_to_mint: amount, }; @@ -634,7 +651,7 @@ impl Token<'_> { holder_npk: NullifierPublicKey, holder_vpk: ViewingPublicKey, amount: u128, - ) -> Result<(SendTxResponse, SharedSecretKey), ExecutionFailureKind> { + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { let instruction = Instruction::Mint { amount_to_mint: amount, }; diff --git a/wallet/src/transaction_utils.rs b/wallet/src/transaction_utils.rs index 2adc3033..ab64ab28 100644 --- a/wallet/src/transaction_utils.rs +++ b/wallet/src/transaction_utils.rs @@ -1,4 +1,5 @@ -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{HashType, transaction::NSSATransaction}; +use sequencer_service_rpc::RpcClient as _; use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder; use nssa::{ Account, AccountId, PrivacyPreservingTransaction, @@ -10,7 +11,7 @@ use nssa_core::{ account::AccountWithMetadata, encryption::ViewingPublicKey, program::InstructionData, }; -use crate::{WalletCore, helperfunctions::produce_random_nonces}; +use crate::{ExecutionFailureKind, WalletCore, helperfunctions::produce_random_nonces}; pub(crate) struct AccountPreparedData { pub nsk: Option, @@ -51,11 +52,12 @@ impl WalletCore { } if needs_proof { - proof = self - .sequencer_client - .get_proof_for_commitment(sender_commitment) - .await - .unwrap(); + proof = Some( + self.sequencer_client + .get_proof_for_commitment(sender_commitment) + .await + .unwrap(), + ); } Ok(AccountPreparedData { @@ -75,7 +77,7 @@ impl WalletCore { tx_pre_check: impl FnOnce(&Account, &Account) -> Result<(), ExecutionFailureKind>, program: Program, to_proof: MembershipProof, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let AccountPreparedData { nsk: from_nsk, npk: from_npk, @@ -140,7 +142,7 @@ impl WalletCore { let tx = PrivacyPreservingTransaction::new(message, witness_set); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret_from, shared_secret_to], )) } @@ -152,7 +154,7 @@ impl WalletCore { instruction_data: InstructionData, tx_pre_check: impl FnOnce(&Account, &Account) -> Result<(), ExecutionFailureKind>, program: Program, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let AccountPreparedData { nsk: from_nsk, npk: from_npk, @@ -214,7 +216,7 @@ impl WalletCore { let tx = PrivacyPreservingTransaction::new(message, witness_set); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret_from, shared_secret_to], )) } @@ -227,7 +229,7 @@ impl WalletCore { instruction_data: InstructionData, tx_pre_check: impl FnOnce(&Account, &Account) -> Result<(), ExecutionFailureKind>, program: Program, - ) -> Result<(SendTxResponse, [SharedSecretKey; 2]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 2]), ExecutionFailureKind> { let AccountPreparedData { nsk: from_nsk, npk: from_npk, @@ -285,7 +287,7 @@ impl WalletCore { let tx = PrivacyPreservingTransaction::new(message, witness_set); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret_from, shared_secret_to], )) } @@ -297,7 +299,7 @@ impl WalletCore { instruction_data: InstructionData, tx_pre_check: impl FnOnce(&Account, &Account) -> Result<(), ExecutionFailureKind>, program: Program, - ) -> Result<(SendTxResponse, [nssa_core::SharedSecretKey; 1]), ExecutionFailureKind> { + ) -> Result<(HashType, [nssa_core::SharedSecretKey; 1]), ExecutionFailureKind> { let AccountPreparedData { nsk: from_nsk, npk: from_npk, @@ -345,7 +347,7 @@ impl WalletCore { let tx = PrivacyPreservingTransaction::new(message, witness_set); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret], )) } @@ -358,7 +360,7 @@ impl WalletCore { tx_pre_check: impl FnOnce(&Account, &Account) -> Result<(), ExecutionFailureKind>, program: Program, to_proof: MembershipProof, - ) -> Result<(SendTxResponse, [SharedSecretKey; 1]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 1]), ExecutionFailureKind> { let Ok(from_acc) = self.get_account_public(from).await else { return Err(ExecutionFailureKind::KeyNotFoundError); }; @@ -412,7 +414,7 @@ impl WalletCore { let tx = PrivacyPreservingTransaction::new(message, witness_set); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret], )) } @@ -424,7 +426,7 @@ impl WalletCore { instruction_data: InstructionData, tx_pre_check: impl FnOnce(&Account, &Account) -> Result<(), ExecutionFailureKind>, program: Program, - ) -> Result<(SendTxResponse, [SharedSecretKey; 1]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 1]), ExecutionFailureKind> { let Ok(from_acc) = self.get_account_public(from).await else { return Err(ExecutionFailureKind::KeyNotFoundError); }; @@ -478,7 +480,7 @@ impl WalletCore { let tx = PrivacyPreservingTransaction::new(message, witness_set); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret], )) } @@ -491,7 +493,7 @@ impl WalletCore { instruction_data: InstructionData, tx_pre_check: impl FnOnce(&Account, &Account) -> Result<(), ExecutionFailureKind>, program: Program, - ) -> Result { + ) -> Result { let Ok(from_acc) = self.get_account_public(from).await else { return Err(ExecutionFailureKind::KeyNotFoundError); }; @@ -538,13 +540,13 @@ impl WalletCore { let witness_set = WitnessSet::for_message(&message, proof, &[signing_key]); let tx = PrivacyPreservingTransaction::new(message, witness_set); - Ok(self.sequencer_client.send_tx_private(tx).await?) + Ok(self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?) } pub async fn register_account_under_authenticated_transfers_programs_private( &self, from: AccountId, - ) -> Result<(SendTxResponse, [SharedSecretKey; 1]), ExecutionFailureKind> { + ) -> Result<(HashType, [SharedSecretKey; 1]), ExecutionFailureKind> { let AccountPreparedData { nsk: _, npk: from_npk, @@ -585,7 +587,7 @@ impl WalletCore { let tx = PrivacyPreservingTransaction::new(message, witness_set); Ok(( - self.sequencer_client.send_tx_private(tx).await?, + self.sequencer_client.send_transaction(NSSATransaction::PrivacyPreserving(tx)).await?, [shared_secret_from], )) }