From 4f9ee9f19ff153ebbe50c3fcee1b8b5946cbb732 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Tue, 2 Sep 2025 16:29:56 -0300 Subject: [PATCH 01/20] remove unused dependencies --- common/Cargo.toml | 1 - integration_tests/Cargo.toml | 9 --------- key_protocol/Cargo.toml | 3 --- mempool/Cargo.toml | 5 ----- nssa/Cargo.toml | 1 - nssa/test_program_methods/guest/Cargo.toml | 1 - sequencer_core/Cargo.toml | 13 ------------- sequencer_rpc/Cargo.toml | 11 ----------- sequencer_runner/Cargo.toml | 5 ----- storage/Cargo.toml | 4 ---- wallet/Cargo.toml | 14 -------------- 11 files changed, 67 deletions(-) diff --git a/common/Cargo.toml b/common/Cargo.toml index 4e29b1e..8bfc317 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -10,7 +10,6 @@ serde_json.workspace = true serde.workspace = true reqwest.workspace = true k256.workspace = true -rand.workspace = true rs_merkle.workspace = true sha2.workspace = true diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index df2bad8..16e1b2e 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -5,15 +5,12 @@ edition = "2024" [dependencies] anyhow.workspace = true -serde_json.workspace = true env_logger.workspace = true log.workspace = true -serde.workspace = true actix.workspace = true actix-web.workspace = true tokio.workspace = true -toml.workspace = true hex.workspace = true tempfile.workspace = true @@ -21,9 +18,6 @@ tempfile.workspace = true features = ["derive", "env"] workspace = true -[dependencies.sequencer_rpc] -path = "../sequencer_rpc" - [dependencies.sequencer_core] path = "../sequencer_core" @@ -35,6 +29,3 @@ path = "../wallet" [dependencies.common] path = "../common" - -[dependencies.key_protocol] -path = "../key_protocol" diff --git a/key_protocol/Cargo.toml b/key_protocol/Cargo.toml index 2f2bb07..947bd7a 100644 --- a/key_protocol/Cargo.toml +++ b/key_protocol/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" [dependencies] anyhow.workspace = true serde_json.workspace = true -env_logger.workspace = true log.workspace = true serde.workspace = true k256.workspace = true @@ -16,8 +15,6 @@ elliptic-curve.workspace = true hex.workspace = true aes-gcm.workspace = true lazy_static.workspace = true -tiny-keccak.workspace = true -nssa-core = { path = "../nssa/core" } [dependencies.common] path = "../common" diff --git a/mempool/Cargo.toml b/mempool/Cargo.toml index ff8b855..0d1a317 100644 --- a/mempool/Cargo.toml +++ b/mempool/Cargo.toml @@ -4,8 +4,3 @@ version = "0.1.0" edition = "2021" [dependencies] -anyhow.workspace = true -serde_json.workspace = true -env_logger.workspace = true -log.workspace = true -serde.workspace = true diff --git a/nssa/Cargo.toml b/nssa/Cargo.toml index 98a0adf..3163902 100644 --- a/nssa/Cargo.toml +++ b/nssa/Cargo.toml @@ -13,7 +13,6 @@ sha2 = "0.10.9" secp256k1 = "0.31.1" rand = "0.8" borsh = "1.5.7" -bytemuck = "1.13" hex = "0.4.3" [dev-dependencies] diff --git a/nssa/test_program_methods/guest/Cargo.toml b/nssa/test_program_methods/guest/Cargo.toml index 0d47ccd..ea669c0 100644 --- a/nssa/test_program_methods/guest/Cargo.toml +++ b/nssa/test_program_methods/guest/Cargo.toml @@ -6,5 +6,4 @@ edition = "2021" [workspace] [dependencies] -risc0-zkvm = { version = "3.0.3", default-features = false, features = ['std'] } nssa-core = { path = "../../core" } diff --git a/sequencer_core/Cargo.toml b/sequencer_core/Cargo.toml index 2e3570b..0e85b71 100644 --- a/sequencer_core/Cargo.toml +++ b/sequencer_core/Cargo.toml @@ -6,14 +6,8 @@ edition = "2021" [dependencies] hex.workspace = true anyhow.workspace = true -serde_json.workspace = true -env_logger.workspace = true -log.workspace = true serde.workspace = true rand.workspace = true -elliptic-curve.workspace = true -k256.workspace = true -tiny-keccak.workspace = true tempfile.workspace = true [dependencies.storage] @@ -22,15 +16,8 @@ path = "../storage" [dependencies.mempool] path = "../mempool" -[dependencies.key_protocol] -path = "../key_protocol" - [dependencies.common] path = "../common" [dependencies.nssa] path = "../nssa" - -[dependencies.secp256k1-zkp] -workspace = true -features = ["std", "rand-std", "rand", "serde", "global-context"] diff --git a/sequencer_rpc/Cargo.toml b/sequencer_rpc/Cargo.toml index c6ce004..132c0f1 100644 --- a/sequencer_rpc/Cargo.toml +++ b/sequencer_rpc/Cargo.toml @@ -6,10 +6,8 @@ edition = "2021" [dependencies] anyhow.workspace = true serde_json.workspace = true -env_logger.workspace = true log.workspace = true serde.workspace = true -actix.workspace = true actix-cors.workspace = true futures.workspace = true hex.workspace = true @@ -19,18 +17,9 @@ base64.workspace = true actix-web.workspace = true tokio.workspace = true -[dependencies.mempool] -path = "../mempool" - -[dependencies.key_protocol] -path = "../key_protocol" - [dependencies.sequencer_core] path = "../sequencer_core" -[dependencies.storage] -path = "../storage" - [dependencies.common] path = "../common" diff --git a/sequencer_runner/Cargo.toml b/sequencer_runner/Cargo.toml index b42fcb6..2ca4e23 100644 --- a/sequencer_runner/Cargo.toml +++ b/sequencer_runner/Cargo.toml @@ -8,20 +8,15 @@ anyhow.workspace = true serde_json.workspace = true env_logger.workspace = true log.workspace = true -serde.workspace = true actix.workspace = true actix-web.workspace = true tokio.workspace = true -toml.workspace = true [dependencies.clap] features = ["derive", "env"] workspace = true -[dependencies.mempool] -path = "../mempool" - [dependencies.sequencer_rpc] path = "../sequencer_rpc" diff --git a/storage/Cargo.toml b/storage/Cargo.toml index c4fd3a9..047b20b 100644 --- a/storage/Cargo.toml +++ b/storage/Cargo.toml @@ -6,12 +6,8 @@ edition = "2021" [dependencies] anyhow.workspace = true serde_json.workspace = true -env_logger.workspace = true -log.workspace = true serde.workspace = true -lru.workspace = true thiserror.workspace = true -hex.workspace = true rocksdb.workspace = true diff --git a/wallet/Cargo.toml b/wallet/Cargo.toml index 56974c4..0ecb0b0 100644 --- a/wallet/Cargo.toml +++ b/wallet/Cargo.toml @@ -9,18 +9,8 @@ serde_json.workspace = true env_logger.workspace = true log.workspace = true serde.workspace = true -rand.workspace = true -k256.workspace = true -sha2.workspace = true -bincode.workspace = true -elliptic-curve.workspace = true -reqwest.workspace = true -thiserror.workspace = true tokio.workspace = true tempfile.workspace = true -risc0-zkvm = "3.0.3" -hex.workspace = true -actix-rt.workspace = true clap.workspace = true nssa-core = { path = "../nssa/core" } @@ -32,7 +22,3 @@ path = "../nssa" [dependencies.common] path = "../common" - -[dependencies.secp256k1-zkp] -workspace = true -features = ["std", "rand-std", "rand", "serde", "global-context"] From 1d627ce5b961a49a138881ee65a7b1ad5fc07fa7 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 4 Sep 2025 13:33:17 +0300 Subject: [PATCH 02/20] fix: suggestions 1 --- common/src/rpc_primitives/requests.rs | 14 ++++ sequencer_rpc/src/process.rs | 100 +++++++++++++++++++++++--- wallet/src/lib.rs | 68 ++++++++---------- wallet/src/poller.rs | 12 ++++ 4 files changed, 145 insertions(+), 49 deletions(-) diff --git a/common/src/rpc_primitives/requests.rs b/common/src/rpc_primitives/requests.rs index 674c077..a4c18d7 100644 --- a/common/src/rpc_primitives/requests.rs +++ b/common/src/rpc_primitives/requests.rs @@ -48,6 +48,11 @@ pub struct GetAccountsNoncesRequest { pub addresses: Vec, } +#[derive(Serialize, Deserialize, Debug)] +pub struct GetAccountDataRequest { + pub address: String, +} + parse_request!(HelloRequest); parse_request!(RegisterAccountRequest); parse_request!(SendTxRequest); @@ -58,6 +63,7 @@ parse_request!(GetInitialTestnetAccountsRequest); parse_request!(GetAccountBalanceRequest); parse_request!(GetTransactionByHashRequest); parse_request!(GetAccountsNoncesRequest); +parse_request!(GetAccountDataRequest); #[derive(Serialize, Deserialize, Debug)] pub struct HelloResponse { @@ -104,3 +110,11 @@ pub struct GetAccountsNoncesResponse { pub struct GetTransactionByHashResponse { pub transaction: Option, } + +#[derive(Serialize, Deserialize, Debug)] +pub struct GetAccountDataResponse { + pub balance: u128, + pub nonce: u128, + pub program_owner: [u32; 8], + pub data: Vec, +} diff --git a/sequencer_rpc/src/process.rs b/sequencer_rpc/src/process.rs index d994210..5d7b4be 100644 --- a/sequencer_rpc/src/process.rs +++ b/sequencer_rpc/src/process.rs @@ -12,9 +12,10 @@ use common::{ message::{Message, Request}, parser::RpcRequest, requests::{ - GetAccountBalanceRequest, GetAccountBalanceResponse, GetAccountsNoncesRequest, - GetAccountsNoncesResponse, GetInitialTestnetAccountsRequest, - GetTransactionByHashRequest, GetTransactionByHashResponse, + GetAccountBalanceRequest, GetAccountBalanceResponse, GetAccountDataRequest, + GetAccountDataResponse, GetAccountsNoncesRequest, GetAccountsNoncesResponse, + GetInitialTestnetAccountsRequest, GetTransactionByHashRequest, + GetTransactionByHashResponse, }, }, }; @@ -35,6 +36,7 @@ pub const GET_LAST_BLOCK: &str = "get_last_block"; pub const GET_ACCOUNT_BALANCE: &str = "get_account_balance"; pub const GET_TRANSACTION_BY_HASH: &str = "get_transaction_by_hash"; pub const GET_ACCOUNTS_NONCES: &str = "get_accounts_nonces"; +pub const GET_ACCOUNT_DATA: &str = "get_account_data"; pub const HELLO_FROM_SEQUENCER: &str = "HELLO_FROM_SEQUENCER"; @@ -181,14 +183,9 @@ impl JsonHandler { let get_account_nonces_req = GetAccountsNoncesRequest::parse(Some(request.params))?; let mut addresses = vec![]; for address_raw in get_account_nonces_req.addresses { - let address_bytes = hex::decode(address_raw) - .map_err(|_| RpcError::invalid_params("invalid hex".to_string()))?; - - let address = nssa::Address::new( - address_bytes - .try_into() - .map_err(|_| RpcError::invalid_params("invalid length".to_string()))?, - ); + let address = address_raw + .parse::() + .map_err(|e| RpcError::invalid_params(e.to_string()))?; addresses.push(address); } @@ -207,6 +204,32 @@ impl JsonHandler { respond(helperstruct) } + ///Returns account struct for give address. + /// Address must be a valid hex string of the correct length. + async fn process_get_account_data(&self, request: Request) -> Result { + let get_account_nonces_req = GetAccountDataRequest::parse(Some(request.params))?; + + let address = get_account_nonces_req + .address + .parse::() + .map_err(|e| RpcError::invalid_params(e.to_string()))?; + + let account = { + let state = self.sequencer_state.lock().await; + + state.store.state.get_account_by_address(&address) + }; + + let helperstruct = GetAccountDataResponse { + balance: account.balance, + nonce: account.nonce, + program_owner: account.program_owner, + data: account.data, + }; + + respond(helperstruct) + } + /// Returns the transaction corresponding to the given hash, if it exists in the blockchain. /// The hash must be a valid hex string of the correct length. async fn process_get_transaction_by_hash(&self, request: Request) -> Result { @@ -242,6 +265,7 @@ impl JsonHandler { GET_INITIAL_TESTNET_ACCOUNTS => self.get_initial_testnet_accounts(request).await, GET_ACCOUNT_BALANCE => self.process_get_account_balance(request).await, GET_ACCOUNTS_NONCES => self.process_get_accounts_nonces(request).await, + GET_ACCOUNT_DATA => self.process_get_account_data(request).await, GET_TRANSACTION_BY_HASH => self.process_get_transaction_by_hash(request).await, _ => Err(RpcErr(RpcError::method_not_found(request.method))), } @@ -505,6 +529,60 @@ mod tests { assert_eq!(response, expected_response); } + #[actix_web::test] + async fn test_get_account_data_for_non_existent_account() { + let (json_handler, _, _) = components_for_tests(); + let request = serde_json::json!({ + "jsonrpc": "2.0", + "method": "get_account_data", + "params": { "address": "efac".repeat(16) }, + "id": 1 + }); + let expected_response = serde_json::json!({ + "id": 1, + "jsonrpc": "2.0", + "result": { + "balance": 0, + "nonce": 0, + "program_owner": [ 0, 0, 0, 0, 0, 0, 0, 0], + "data": [], + } + }); + + let response = call_rpc_handler_with_json(json_handler, request).await; + + assert_eq!(response, expected_response); + } + + #[actix_web::test] + async fn test_get_account_data_for_existent_account() { + let (json_handler, initial_accounts, _) = components_for_tests(); + + let acc_1_addr = initial_accounts[0].addr.clone(); + + let request = serde_json::json!({ + "jsonrpc": "2.0", + "method": "get_account_data", + "params": { "address": acc_1_addr }, + "id": 1 + }); + + let expected_response = serde_json::json!({ + "id": 1, + "jsonrpc": "2.0", + "result": { + "balance": 9990, + "nonce": 1, + "program_owner": [ 1793544791, 852173979, 3315478100u32, 4158236927u32, 146723505, 3793635251u32, 999304864, 2535706995u32], + "data": [], + } + }); + + let response = call_rpc_handler_with_json(json_handler, request).await; + + assert_eq!(response, expected_response); + } + #[actix_web::test] async fn test_get_transaction_by_hash_for_non_existent_hash() { let (json_handler, _, _) = components_for_tests(); diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index 7ce7955..8d4efc8 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -39,13 +39,7 @@ pub struct WalletCore { impl WalletCore { pub fn start_from_config_update_chain(config: WalletConfig) -> Result { let client = Arc::new(SequencerClient::new(config.sequencer_addr.clone())?); - let tx_poller = TxPoller { - polling_delay_millis: config.seq_poll_timeout_millis, - polling_max_blocks_to_query: config.seq_poll_max_blocks, - polling_max_error_attempts: config.seq_poll_max_retries, - polling_error_delay_millis: config.seq_poll_retry_delay_millis, - client: client.clone(), - }; + let tx_poller = TxPoller::new(config.clone(), client.clone()); let mut storage = WalletChainStore::new(config)?; @@ -61,7 +55,7 @@ impl WalletCore { }) } - ///Stre persistent accounts at home + ///Store persistent accounts at home pub fn store_persistent_accounts(&self) -> Result { let home = get_home()?; let accs_path = home.join("curr_accounts.json"); @@ -96,41 +90,39 @@ impl WalletCore { to: Address, balance_to_move: u128, ) -> Result { - if let Ok(balance) = self.get_account_balance(from).await { - if balance >= balance_to_move { - if let Ok(nonces) = self.get_accounts_nonces(vec![from]).await { - let addresses = vec![from, to]; - let program_id = nssa::program::Program::authenticated_transfer_program().id(); - let message = nssa::public_transaction::Message::try_new( - program_id, - addresses, - nonces, - balance_to_move, - ) - .unwrap(); + let Ok(balance) = self.get_account_balance(from).await else { + return Err(ExecutionFailureKind::SequencerError); + }; - let signing_key = self.storage.user_data.get_account_signing_key(&from); + if balance >= balance_to_move { + let Ok(nonces) = self.get_accounts_nonces(vec![from]).await else { + return Err(ExecutionFailureKind::SequencerError); + }; - if let Some(signing_key) = signing_key { - let witness_set = nssa::public_transaction::WitnessSet::for_message( - &message, - &[signing_key], - ); + let addresses = vec![from, to]; + let program_id = nssa::program::Program::authenticated_transfer_program().id(); + let message = nssa::public_transaction::Message::try_new( + program_id, + addresses, + nonces, + balance_to_move, + ) + .unwrap(); - let tx = nssa::PublicTransaction::new(message, witness_set); + let signing_key = self.storage.user_data.get_account_signing_key(&from); - Ok(self.sequencer_client.send_tx(tx).await?) - } else { - Err(ExecutionFailureKind::KeyNotFoundError) - } - } else { - Err(ExecutionFailureKind::SequencerError) - } - } else { - Err(ExecutionFailureKind::InsufficientFundsError) - } + let Some(signing_key) = signing_key else { + return Err(ExecutionFailureKind::KeyNotFoundError); + }; + + let witness_set = + nssa::public_transaction::WitnessSet::for_message(&message, &[signing_key]); + + let tx = nssa::PublicTransaction::new(message, witness_set); + + Ok(self.sequencer_client.send_tx(tx).await?) } else { - Err(ExecutionFailureKind::SequencerError) + Err(ExecutionFailureKind::InsufficientFundsError) } } diff --git a/wallet/src/poller.rs b/wallet/src/poller.rs index 8fd219f..279197f 100644 --- a/wallet/src/poller.rs +++ b/wallet/src/poller.rs @@ -4,6 +4,8 @@ use anyhow::Result; use common::sequencer_client::SequencerClient; use log::{info, warn}; +use crate::config::WalletConfig; + #[derive(Clone)] ///Helperstruct to poll transactions pub struct TxPoller { @@ -15,6 +17,16 @@ pub struct TxPoller { } impl TxPoller { + pub fn new(config: WalletConfig, client: Arc) -> Self { + Self { + polling_delay_millis: config.seq_poll_timeout_millis, + polling_max_blocks_to_query: config.seq_poll_max_blocks, + polling_max_error_attempts: config.seq_poll_max_retries, + polling_error_delay_millis: config.seq_poll_retry_delay_millis, + client: client.clone(), + } + } + pub async fn poll_tx(&self, tx_hash: String) -> Result { let max_blocks_to_query = self.polling_max_blocks_to_query; From ee91d7d79c6d733a77caa4c38755f68a10a49c76 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 4 Sep 2025 14:18:44 +0300 Subject: [PATCH 03/20] fix: ci fix 1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2b98867..b2905de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ futures = "0.3" actix-rt = "*" lazy_static = "1.5.0" env_logger = "0.10" -log = "0.4" +log = "0.4.28" lru = "0.7.8" thiserror = "2.0.12" rs_merkle = "1.4" From 7052bccfbc965f5f3c4c8c58f6c9e428848bb12a Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 4 Sep 2025 14:38:41 +0300 Subject: [PATCH 04/20] fix: try edition 2024 --- common/Cargo.toml | 2 +- common/src/merkle_tree_public/hasher.rs | 2 +- common/src/merkle_tree_public/merkle_tree.rs | 4 ++-- common/src/rpc_primitives/errors.rs | 6 ++--- common/src/rpc_primitives/message.rs | 2 +- common/src/rpc_primitives/requests.rs | 2 +- common/src/transaction.rs | 22 ++++++++++--------- key_protocol/Cargo.toml | 2 +- .../key_management/ephemeral_key_holder.rs | 6 ++--- key_protocol/src/key_management/mod.rs | 4 ++-- .../src/key_management/secret_holders.rs | 4 ++-- key_protocol/src/key_protocol_core/mod.rs | 4 +++- mempool/Cargo.toml | 2 +- nssa/program_methods/Cargo.toml | 2 +- nssa/program_methods/guest/Cargo.toml | 2 +- nssa/test_program_methods/Cargo.toml | 2 +- nssa/test_program_methods/guest/Cargo.toml | 2 +- rustfmt.toml | 2 +- sequencer_core/Cargo.toml | 2 +- sequencer_core/src/sequencer_store/mod.rs | 2 +- sequencer_rpc/Cargo.toml | 2 +- sequencer_rpc/src/lib.rs | 2 +- sequencer_rpc/src/net_utils.rs | 4 ++-- sequencer_rpc/src/process.rs | 12 +++++----- sequencer_runner/Cargo.toml | 2 +- sequencer_runner/src/lib.rs | 6 +++-- storage/Cargo.toml | 2 +- storage/src/lib.rs | 2 +- storage/src/sc_db_utils.rs | 2 +- wallet/Cargo.toml | 2 +- wallet/src/helperfunctions.rs | 10 ++++++--- wallet/src/lib.rs | 2 +- wallet/src/main.rs | 2 +- 33 files changed, 68 insertions(+), 58 deletions(-) diff --git a/common/Cargo.toml b/common/Cargo.toml index 4e29b1e..54923a3 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "common" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] anyhow.workspace = true diff --git a/common/src/merkle_tree_public/hasher.rs b/common/src/merkle_tree_public/hasher.rs index b23388f..ed9443a 100644 --- a/common/src/merkle_tree_public/hasher.rs +++ b/common/src/merkle_tree_public/hasher.rs @@ -1,5 +1,5 @@ use rs_merkle::Hasher; -use sha2::{digest::FixedOutput, Digest, Sha256}; +use sha2::{Digest, Sha256, digest::FixedOutput}; use super::TreeHashType; diff --git a/common/src/merkle_tree_public/merkle_tree.rs b/common/src/merkle_tree_public/merkle_tree.rs index e2eb715..8911344 100644 --- a/common/src/merkle_tree_public/merkle_tree.rs +++ b/common/src/merkle_tree_public/merkle_tree.rs @@ -2,14 +2,14 @@ use std::{collections::HashMap, fmt, marker::PhantomData}; use rs_merkle::{MerkleProof, MerkleTree}; use serde::{ + Deserialize, Deserializer, Serialize, de::{SeqAccess, Visitor}, ser::SerializeSeq, - Deserialize, Deserializer, Serialize, }; use crate::{transaction::Transaction, utxo_commitment::UTXOCommitment}; -use super::{hasher::OwnHasher, tree_leav_item::TreeLeavItem, TreeHashType}; +use super::{TreeHashType, hasher::OwnHasher, tree_leav_item::TreeLeavItem}; #[derive(Clone)] pub struct HashStorageMerkleTree { diff --git a/common/src/rpc_primitives/errors.rs b/common/src/rpc_primitives/errors.rs index 6bbf207..fc52dd0 100644 --- a/common/src/rpc_primitives/errors.rs +++ b/common/src/rpc_primitives/errors.rs @@ -1,4 +1,4 @@ -use serde_json::{to_value, Value}; +use serde_json::{Value, to_value}; use std::fmt; #[derive(serde::Serialize)] @@ -65,7 +65,7 @@ impl RpcError { return Self::server_error(Some(format!( "Failed to serialize invalid parameters error: {:?}", err.to_string() - ))) + ))); } }; RpcError::new(-32_602, "Invalid params".to_owned(), Some(value)) @@ -178,7 +178,7 @@ impl From for RpcError { let error_data = match to_value(&e) { Ok(value) => value, Err(_err) => { - return RpcError::new_internal_error(None, "Failed to serialize ServerError") + return RpcError::new_internal_error(None, "Failed to serialize ServerError"); } }; RpcError::new_internal_error(Some(error_data), e.to_string().as_str()) diff --git a/common/src/rpc_primitives/message.rs b/common/src/rpc_primitives/message.rs index c6bdc58..e8e4186 100644 --- a/common/src/rpc_primitives/message.rs +++ b/common/src/rpc_primitives/message.rs @@ -315,10 +315,10 @@ impl From for Vec { #[cfg(test)] mod tests { + use serde_json::Value; use serde_json::de::from_slice; use serde_json::json; use serde_json::ser::to_vec; - use serde_json::Value; use super::*; diff --git a/common/src/rpc_primitives/requests.rs b/common/src/rpc_primitives/requests.rs index a4c18d7..a566ee2 100644 --- a/common/src/rpc_primitives/requests.rs +++ b/common/src/rpc_primitives/requests.rs @@ -1,8 +1,8 @@ use crate::parse_request; use super::errors::RpcParseError; -use super::parser::parse_params; use super::parser::RpcRequest; +use super::parser::parse_params; use serde::{Deserialize, Serialize}; use serde_json::Value; diff --git a/common/src/transaction.rs b/common/src/transaction.rs index afc4559..1d0ccec 100644 --- a/common/src/transaction.rs +++ b/common/src/transaction.rs @@ -1,12 +1,12 @@ use k256::ecdsa::{ - signature::{Signer, Verifier}, Signature, SigningKey, VerifyingKey, + signature::{Signer, Verifier}, }; use log::info; use secp256k1_zkp::{PedersenCommitment, Tweak}; use serde::{Deserialize, Serialize}; -use sha2::{digest::FixedOutput, Digest}; +use sha2::{Digest, digest::FixedOutput}; use crate::merkle_tree_public::TreeHashType; @@ -297,9 +297,9 @@ impl AuthenticatedTransaction { #[cfg(test)] mod tests { use super::*; - use k256::{ecdsa::signature::Signer, FieldBytes}; - use secp256k1_zkp::{constants::SECRET_KEY_SIZE, Tweak}; - use sha2::{digest::FixedOutput, Digest}; + use k256::{FieldBytes, ecdsa::signature::Signer}; + use secp256k1_zkp::{Tweak, constants::SECRET_KEY_SIZE}; + use sha2::{Digest, digest::FixedOutput}; use crate::{ merkle_tree_public::TreeHashType, @@ -378,11 +378,13 @@ mod tests { assert_eq!(authenticated_tx.transaction(), &transaction); assert_eq!(hash, &transaction.body.hash()); - assert!(authenticated_tx - .transaction() - .public_key - .verify(&transaction.body.to_bytes(), &signature) - .is_ok()); + assert!( + authenticated_tx + .transaction() + .public_key + .verify(&transaction.body.to_bytes(), &signature) + .is_ok() + ); } #[test] diff --git a/key_protocol/Cargo.toml b/key_protocol/Cargo.toml index 2f2bb07..3150dfe 100644 --- a/key_protocol/Cargo.toml +++ b/key_protocol/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "key_protocol" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] anyhow.workspace = true diff --git a/key_protocol/src/key_management/ephemeral_key_holder.rs b/key_protocol/src/key_management/ephemeral_key_holder.rs index ecfb09e..b304292 100644 --- a/key_protocol/src/key_management/ephemeral_key_holder.rs +++ b/key_protocol/src/key_management/ephemeral_key_holder.rs @@ -1,9 +1,9 @@ -use aes_gcm::{aead::Aead, AeadCore, Aes256Gcm, KeyInit}; -use elliptic_curve::point::AffineCoordinates; +use aes_gcm::{AeadCore, Aes256Gcm, KeyInit, aead::Aead}; use elliptic_curve::PrimeField; +use elliptic_curve::point::AffineCoordinates; use k256::{AffinePoint, FieldBytes, Scalar}; use log::info; -use rand::{rngs::OsRng, RngCore}; +use rand::{RngCore, rngs::OsRng}; use super::constants_types::{CipherText, Nonce}; diff --git a/key_protocol/src/key_management/mod.rs b/key_protocol/src/key_management/mod.rs index 2c38375..1a037ef 100644 --- a/key_protocol/src/key_management/mod.rs +++ b/key_protocol/src/key_management/mod.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit}; +use aes_gcm::{Aes256Gcm, KeyInit, aead::Aead}; use constants_types::{CipherText, Nonce}; use elliptic_curve::point::AffineCoordinates; use k256::AffinePoint; @@ -136,8 +136,8 @@ impl KeyChain { #[cfg(test)] mod tests { use aes_gcm::{ - aead::{Aead, KeyInit, OsRng}, Aes256Gcm, + aead::{Aead, KeyInit, OsRng}, }; use constants_types::{CipherText, Nonce}; use constants_types::{NULLIFIER_SECRET_CONST, VIEWING_SECRET_CONST}; diff --git a/key_protocol/src/key_management/secret_holders.rs b/key_protocol/src/key_management/secret_holders.rs index 47342ec..c6b2948 100644 --- a/key_protocol/src/key_management/secret_holders.rs +++ b/key_protocol/src/key_management/secret_holders.rs @@ -1,9 +1,9 @@ use common::merkle_tree_public::TreeHashType; use elliptic_curve::PrimeField; use k256::{AffinePoint, FieldBytes, Scalar}; -use rand::{rngs::OsRng, RngCore}; +use rand::{RngCore, rngs::OsRng}; use serde::{Deserialize, Serialize}; -use sha2::{digest::FixedOutput, Digest}; +use sha2::{Digest, digest::FixedOutput}; use super::constants_types::{NULLIFIER_SECRET_CONST, VIEWING_SECRET_CONST}; diff --git a/key_protocol/src/key_protocol_core/mod.rs b/key_protocol/src/key_protocol_core/mod.rs index 67cb52c..7757183 100644 --- a/key_protocol/src/key_protocol_core/mod.rs +++ b/key_protocol/src/key_protocol_core/mod.rs @@ -36,7 +36,9 @@ impl NSSAUserData { accounts_keys: HashMap, ) -> Result { if !Self::valid_key_transaction_pairing_check(&accounts_keys) { - anyhow::bail!("Key transaction pairing check not satisfied, there is addresses, which is not derived from keys"); + anyhow::bail!( + "Key transaction pairing check not satisfied, there is addresses, which is not derived from keys" + ); } let key_holder = KeyChain::new_os_random_with_accounts(accounts_keys); diff --git a/mempool/Cargo.toml b/mempool/Cargo.toml index ff8b855..16327ac 100644 --- a/mempool/Cargo.toml +++ b/mempool/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "mempool" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] anyhow.workspace = true diff --git a/nssa/program_methods/Cargo.toml b/nssa/program_methods/Cargo.toml index af52307..40dab21 100644 --- a/nssa/program_methods/Cargo.toml +++ b/nssa/program_methods/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "program-methods" version = "0.1.0" -edition = "2021" +edition = "2024" [build-dependencies] risc0-build = { version = "3.0.3" } diff --git a/nssa/program_methods/guest/Cargo.toml b/nssa/program_methods/guest/Cargo.toml index 0d47ccd..da4dbe8 100644 --- a/nssa/program_methods/guest/Cargo.toml +++ b/nssa/program_methods/guest/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "programs" version = "0.1.0" -edition = "2021" +edition = "2024" [workspace] diff --git a/nssa/test_program_methods/Cargo.toml b/nssa/test_program_methods/Cargo.toml index 50d6ca2..0317d2b 100644 --- a/nssa/test_program_methods/Cargo.toml +++ b/nssa/test_program_methods/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "test-program-methods" version = "0.1.0" -edition = "2021" +edition = "2024" [build-dependencies] risc0-build = { version = "3.0.3" } diff --git a/nssa/test_program_methods/guest/Cargo.toml b/nssa/test_program_methods/guest/Cargo.toml index 0d47ccd..da4dbe8 100644 --- a/nssa/test_program_methods/guest/Cargo.toml +++ b/nssa/test_program_methods/guest/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "programs" version = "0.1.0" -edition = "2021" +edition = "2024" [workspace] diff --git a/rustfmt.toml b/rustfmt.toml index 3a26366..f216078 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1 @@ -edition = "2021" +edition = "2024" diff --git a/sequencer_core/Cargo.toml b/sequencer_core/Cargo.toml index 2e3570b..ecfb323 100644 --- a/sequencer_core/Cargo.toml +++ b/sequencer_core/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sequencer_core" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] hex.workspace = true diff --git a/sequencer_core/src/sequencer_store/mod.rs b/sequencer_core/src/sequencer_store/mod.rs index 4254ed7..bb0a7c1 100644 --- a/sequencer_core/src/sequencer_store/mod.rs +++ b/sequencer_core/src/sequencer_store/mod.rs @@ -3,7 +3,7 @@ use std::path::Path; use block_store::SequecerBlockStore; use common::block::HashableBlockData; use nssa::{self, Address}; -use rand::{rngs::OsRng, RngCore}; +use rand::{RngCore, rngs::OsRng}; use crate::config::AccountInitialData; diff --git a/sequencer_rpc/Cargo.toml b/sequencer_rpc/Cargo.toml index c6ce004..a0111f6 100644 --- a/sequencer_rpc/Cargo.toml +++ b/sequencer_rpc/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sequencer_rpc" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] anyhow.workspace = true diff --git a/sequencer_rpc/src/lib.rs b/sequencer_rpc/src/lib.rs index 1112dbd..2973c42 100644 --- a/sequencer_rpc/src/lib.rs +++ b/sequencer_rpc/src/lib.rs @@ -5,8 +5,8 @@ pub mod types; use std::sync::Arc; use common::rpc_primitives::{ - errors::{RpcError, RpcErrorKind}, RpcPollingConfig, + errors::{RpcError, RpcErrorKind}, }; use sequencer_core::SequencerCore; use serde::Serialize; diff --git a/sequencer_rpc/src/net_utils.rs b/sequencer_rpc/src/net_utils.rs index c421f17..b373823 100644 --- a/sequencer_rpc/src/net_utils.rs +++ b/sequencer_rpc/src/net_utils.rs @@ -2,13 +2,13 @@ use std::io; use std::sync::Arc; use actix_cors::Cors; -use actix_web::{http, middleware, web, App, Error as HttpError, HttpResponse, HttpServer}; +use actix_web::{App, Error as HttpError, HttpResponse, HttpServer, http, middleware, web}; use futures::Future; use futures::FutureExt; use log::info; -use common::rpc_primitives::message::Message; use common::rpc_primitives::RpcConfig; +use common::rpc_primitives::message::Message; use sequencer_core::SequencerCore; use tokio::sync::Mutex; diff --git a/sequencer_rpc/src/process.rs b/sequencer_rpc/src/process.rs index 5d7b4be..ad05843 100644 --- a/sequencer_rpc/src/process.rs +++ b/sequencer_rpc/src/process.rs @@ -1,5 +1,5 @@ use actix_web::Error as HttpError; -use base64::{engine::general_purpose, Engine}; +use base64::{Engine, engine::general_purpose}; use nssa; use sequencer_core::config::AccountInitialData; use serde_json::Value; @@ -26,7 +26,7 @@ use common::rpc_primitives::requests::{ SendTxResponse, }; -use super::{respond, types::err_rpc::RpcErr, JsonHandler}; +use super::{JsonHandler, respond, types::err_rpc::RpcErr}; pub const HELLO: &str = "hello"; pub const SEND_TX: &str = "send_tx"; @@ -276,13 +276,13 @@ impl JsonHandler { mod tests { use std::sync::Arc; - use crate::{rpc_handler, JsonHandler}; - use base64::{engine::general_purpose, Engine}; + use crate::{JsonHandler, rpc_handler}; + use base64::{Engine, engine::general_purpose}; use common::rpc_primitives::RpcPollingConfig; use sequencer_core::{ - config::{AccountInitialData, SequencerConfig}, SequencerCore, + config::{AccountInitialData, SequencerConfig}, }; use serde_json::Value; use tempfile::tempdir; @@ -368,7 +368,7 @@ mod tests { } async fn call_rpc_handler_with_json(handler: JsonHandler, request_json: Value) -> Value { - use actix_web::{test, web, App}; + use actix_web::{App, test, web}; let app = test::init_service( App::new() diff --git a/sequencer_runner/Cargo.toml b/sequencer_runner/Cargo.toml index b42fcb6..a610f8d 100644 --- a/sequencer_runner/Cargo.toml +++ b/sequencer_runner/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sequencer_runner" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] anyhow.workspace = true diff --git a/sequencer_runner/src/lib.rs b/sequencer_runner/src/lib.rs index 625350b..b682a18 100644 --- a/sequencer_runner/src/lib.rs +++ b/sequencer_runner/src/lib.rs @@ -5,7 +5,7 @@ use anyhow::Result; use clap::Parser; use common::rpc_primitives::RpcConfig; use log::info; -use sequencer_core::{config::SequencerConfig, SequencerCore}; +use sequencer_core::{SequencerCore, config::SequencerConfig}; use sequencer_rpc::new_http_server; use tokio::{sync::Mutex, task::JoinHandle}; @@ -71,7 +71,9 @@ pub async fn main_runner() -> Result<()> { if let Some(ref rust_log) = app_config.override_rust_log { info!("RUST_LOG env var set to {rust_log:?}"); - std::env::set_var(RUST_LOG, rust_log); + unsafe { + std::env::set_var(RUST_LOG, rust_log); + } } //ToDo: Add restart on failures diff --git a/storage/Cargo.toml b/storage/Cargo.toml index c4fd3a9..d756dd3 100644 --- a/storage/Cargo.toml +++ b/storage/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "storage" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] anyhow.workspace = true diff --git a/storage/src/lib.rs b/storage/src/lib.rs index 0dd12b7..9ead317 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -5,7 +5,7 @@ use error::DbError; use rocksdb::{ BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options, }; -use sc_db_utils::{produce_blob_from_fit_vec, DataBlob, DataBlobChangeVariant}; +use sc_db_utils::{DataBlob, DataBlobChangeVariant, produce_blob_from_fit_vec}; pub mod error; pub mod sc_db_utils; diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index c86828e..af982cf 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -1,4 +1,4 @@ -use serde::{de::Error, Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error}; use crate::SC_DATA_BLOB_SIZE; diff --git a/wallet/Cargo.toml b/wallet/Cargo.toml index b973e1c..6078f68 100644 --- a/wallet/Cargo.toml +++ b/wallet/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "wallet" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] anyhow.workspace = true diff --git a/wallet/src/helperfunctions.rs b/wallet/src/helperfunctions.rs index 0ad9aed..8a7b7b6 100644 --- a/wallet/src/helperfunctions.rs +++ b/wallet/src/helperfunctions.rs @@ -5,8 +5,8 @@ use key_protocol::key_protocol_core::NSSAUserData; use nssa::Address; use crate::{ - config::{PersistentAccountData, WalletConfig}, HOME_DIR_ENV_VAR, + config::{PersistentAccountData, WalletConfig}, }; ///Get home dir for wallet. Env var `NSSA_WALLET_HOME_DIR` must be set before execution to succeed. @@ -69,12 +69,16 @@ mod tests { #[test] fn test_get_home_get_env_var() { - std::env::set_var(HOME_DIR_ENV_VAR, "/path/to/configs"); + unsafe { + std::env::set_var(HOME_DIR_ENV_VAR, "/path/to/configs"); + } let home = get_home().unwrap(); assert_eq!(PathBuf::from_str("/path/to/configs").unwrap(), home); - std::env::remove_var(HOME_DIR_ENV_VAR); + unsafe { + std::env::remove_var(HOME_DIR_ENV_VAR); + } } } diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index 8d4efc8..3814925 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -2,8 +2,8 @@ use std::{fs::File, io::Write, path::PathBuf, str::FromStr, sync::Arc}; use base64::Engine; use common::{ - sequencer_client::{json::SendTxResponse, SequencerClient}, ExecutionFailureKind, + sequencer_client::{SequencerClient, json::SendTxResponse}, }; use anyhow::Result; diff --git a/wallet/src/main.rs b/wallet/src/main.rs index f8c91f8..b296400 100644 --- a/wallet/src/main.rs +++ b/wallet/src/main.rs @@ -1,7 +1,7 @@ use anyhow::Result; use clap::Parser; use tokio::runtime::Builder; -use wallet::{execute_subcommand, Args}; +use wallet::{Args, execute_subcommand}; pub const NUM_THREADS: usize = 2; From 762a0d760e6915a9e04359a817e91af01da2a6b8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 4 Sep 2025 14:54:48 +0300 Subject: [PATCH 05/20] fix: resolver 3 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b2905de..02192ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -resolver = "2" +resolver = "3" members = [ "integration_tests", "sequencer_runner", From d402cfca52b1d562a3df0a8bc16dca0592995eb8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 4 Sep 2025 15:12:01 +0300 Subject: [PATCH 06/20] fix: direct log dependency --- sequencer_core/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencer_core/Cargo.toml b/sequencer_core/Cargo.toml index ecfb323..d818916 100644 --- a/sequencer_core/Cargo.toml +++ b/sequencer_core/Cargo.toml @@ -8,7 +8,7 @@ hex.workspace = true anyhow.workspace = true serde_json.workspace = true env_logger.workspace = true -log.workspace = true +log = "0.4.28" serde.workspace = true rand.workspace = true elliptic-curve.workspace = true From 57045fa8e1c2e2de19ccd0ad66a0bf2a6ff1e90c Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 4 Sep 2025 15:15:14 +0300 Subject: [PATCH 07/20] fix: merge upfix --- sequencer_core/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/sequencer_core/Cargo.toml b/sequencer_core/Cargo.toml index cf98855..1c56f11 100644 --- a/sequencer_core/Cargo.toml +++ b/sequencer_core/Cargo.toml @@ -9,6 +9,7 @@ anyhow.workspace = true serde.workspace = true rand.workspace = true tempfile.workspace = true +log.workspace = true [dependencies.storage] path = "../storage" From b1d3b71df734f8890d9ba93313048cbc7dec30ee Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 4 Sep 2025 16:46:20 +0300 Subject: [PATCH 08/20] fix: tests fix --- sequencer_rpc/src/process.rs | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/sequencer_rpc/src/process.rs b/sequencer_rpc/src/process.rs index ad05843..2f55dd2 100644 --- a/sequencer_rpc/src/process.rs +++ b/sequencer_rpc/src/process.rs @@ -554,35 +554,6 @@ mod tests { assert_eq!(response, expected_response); } - #[actix_web::test] - async fn test_get_account_data_for_existent_account() { - let (json_handler, initial_accounts, _) = components_for_tests(); - - let acc_1_addr = initial_accounts[0].addr.clone(); - - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_account_data", - "params": { "address": acc_1_addr }, - "id": 1 - }); - - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "balance": 9990, - "nonce": 1, - "program_owner": [ 1793544791, 852173979, 3315478100u32, 4158236927u32, 146723505, 3793635251u32, 999304864, 2535706995u32], - "data": [], - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - #[actix_web::test] async fn test_get_transaction_by_hash_for_non_existent_hash() { let (json_handler, _, _) = components_for_tests(); From 4e122abbcefc2f0aecc00652d957a95b1bcfdc96 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:03:49 -0400 Subject: [PATCH 09/20] added comments for Merkle tree --- nssa/src/merkle_tree/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 2306efd..a27501c 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -39,11 +39,11 @@ impl MerkleTree { if tree_depth == capacity_depth { 0 } else { + // 2^(capacity_depth - tree_depth) - 1 (1 << (capacity_depth - tree_depth)) - 1 } } - - /// Number of levels required to hold all values + /// Number of levels required to hold all nodes fn depth(&self) -> usize { self.length.next_power_of_two().trailing_zeros() as usize } @@ -57,6 +57,7 @@ impl MerkleTree { } pub fn with_capacity(capacity: usize) -> Self { + //adjust capacity to ensure power of two let capacity = capacity.next_power_of_two(); let total_depth = capacity.trailing_zeros() as usize; @@ -75,6 +76,8 @@ impl MerkleTree { } } + /// reallocate storage of Merkle tree for double capacity. + /// current tree is embedded into the new tree as a subtree fn reallocate_to_double_capacity(&mut self) { let old_capacity = self.capacity; let new_capacity = old_capacity << 1; @@ -102,9 +105,11 @@ impl MerkleTree { let mut node_index = new_index + self.capacity - 1; let mut node_hash = hash_value(&value); + //add new node to nodes self.set_node(node_index, node_hash); self.length += 1; + //updates Merkle path of the newly inserted node for _ in 0..self.depth() { let parent_index = (node_index - 1) >> 1; let left_child = self.get_node((parent_index << 1) + 1); @@ -129,6 +134,7 @@ impl MerkleTree { while node_index != root_index { let parent_index = (node_index - 1) >> 1; + //left children have odd indices, and right children have even indices let is_left_child = node_index & 1 == 1; let sibling_index = if is_left_child { node_index + 1 From 5eeb92538409c94bb3adb7346fa9e1dbbd2a9333 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:21:46 -0400 Subject: [PATCH 10/20] added comments related to commitments --- nssa/core/src/commitment.rs | 4 ++++ nssa/src/state.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/nssa/core/src/commitment.rs b/nssa/core/src/commitment.rs index bc22c8f..8e66941 100644 --- a/nssa/core/src/commitment.rs +++ b/nssa/core/src/commitment.rs @@ -8,6 +8,8 @@ use crate::{NullifierPublicKey, account::Account}; pub struct Commitment(pub(super) [u8; 32]); impl Commitment { + //generate new commitment to a private account owned by user with npk. + //hash(npk || program_owner || balance || nonce || data) pub fn new(npk: &NullifierPublicKey, account: &Account) -> Self { let mut bytes = Vec::new(); bytes.extend_from_slice(&npk.to_byte_array()); @@ -34,6 +36,8 @@ pub type CommitmentSetDigest = [u8; 32]; pub type MembershipProof = (usize, Vec<[u8; 32]>); +/// computes the resulting digest/root hash for a membership proof +/// and its corresponding commitment. pub fn compute_digest_for_path( commitment: &Commitment, proof: &MembershipProof, diff --git a/nssa/src/state.rs b/nssa/src/state.rs index f975804..e27b00c 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -21,6 +21,7 @@ impl CommitmentSet { self.merkle_tree.root() } + /// queries the CommitmentSet for membership proof of commitment pub fn get_proof_for(&self, commitment: &Commitment) -> Option { let index = *self.commitments.get(commitment)?; @@ -29,6 +30,7 @@ impl CommitmentSet { .map(|path| (index, path)) } + /// appends a list of commitments to the CommitmentSet. pub(crate) fn extend(&mut self, commitments: &[Commitment]) { for commitment in commitments.iter().cloned() { let index = self.merkle_tree.insert(commitment.to_byte_array()); @@ -41,6 +43,9 @@ impl CommitmentSet { self.commitments.contains_key(commitment) } + /// Initializes a CommitmentState with a given capacity. + /// if capacity is not a power_of_two, then capacity is taken + /// to be the next power_of_two. pub(crate) fn with_capacity(capacity: usize) -> CommitmentSet { Self { merkle_tree: MerkleTree::with_capacity(capacity), From 45b31d304c114660513415db69a4f89fcddf81a7 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:26:56 -0400 Subject: [PATCH 11/20] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index a27501c..ebc13e0 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -57,7 +57,7 @@ impl MerkleTree { } pub fn with_capacity(capacity: usize) -> Self { - //adjust capacity to ensure power of two + // Adjust capacity to ensure power of two let capacity = capacity.next_power_of_two(); let total_depth = capacity.trailing_zeros() as usize; From 6e7520bfeacfd0c5a9a98b0dd48db788d31214d0 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:27:03 -0400 Subject: [PATCH 12/20] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index ebc13e0..5944525 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -76,8 +76,8 @@ impl MerkleTree { } } - /// reallocate storage of Merkle tree for double capacity. - /// current tree is embedded into the new tree as a subtree + /// Reallocates storage of Merkle tree for double capacity. + /// The current tree is embedded into the new tree as a subtree fn reallocate_to_double_capacity(&mut self) { let old_capacity = self.capacity; let new_capacity = old_capacity << 1; From 73c11600ba2c26e6c5d1d1231cf5683f6a64d232 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:27:43 -0400 Subject: [PATCH 13/20] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 5944525..8ce8560 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -105,7 +105,7 @@ impl MerkleTree { let mut node_index = new_index + self.capacity - 1; let mut node_hash = hash_value(&value); - //add new node to nodes + // Insert the new node at the bottom layer self.set_node(node_index, node_hash); self.length += 1; From 1e7c3e155501af8b9f6fb58143a4f09ff18b605a Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:27:55 -0400 Subject: [PATCH 14/20] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 8ce8560..0caff55 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -134,7 +134,7 @@ impl MerkleTree { while node_index != root_index { let parent_index = (node_index - 1) >> 1; - //left children have odd indices, and right children have even indices + // Left children have odd indices, and right children have even indices let is_left_child = node_index & 1 == 1; let sibling_index = if is_left_child { node_index + 1 From 26acdbed6169a3a2b8a9cd4e8754f4b42b9ab56a Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:28:13 -0400 Subject: [PATCH 15/20] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 0caff55..7b30d78 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -109,7 +109,7 @@ impl MerkleTree { self.set_node(node_index, node_hash); self.length += 1; - //updates Merkle path of the newly inserted node + // Update upper levels for the newly inserted node for _ in 0..self.depth() { let parent_index = (node_index - 1) >> 1; let left_child = self.get_node((parent_index << 1) + 1); From 5dea03027aa83e3d7beea5fcc44bcc45dca4556f Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:38:34 -0400 Subject: [PATCH 16/20] minor corrections --- nssa/core/src/commitment.rs | 7 +++---- nssa/src/state.rs | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/nssa/core/src/commitment.rs b/nssa/core/src/commitment.rs index 8e66941..760aa91 100644 --- a/nssa/core/src/commitment.rs +++ b/nssa/core/src/commitment.rs @@ -8,8 +8,8 @@ use crate::{NullifierPublicKey, account::Account}; pub struct Commitment(pub(super) [u8; 32]); impl Commitment { - //generate new commitment to a private account owned by user with npk. - //hash(npk || program_owner || balance || nonce || data) + // Generates the commitment to a private account owned by user for npk: + // hash(npk || program_owner || balance || nonce || data) pub fn new(npk: &NullifierPublicKey, account: &Account) -> Self { let mut bytes = Vec::new(); bytes.extend_from_slice(&npk.to_byte_array()); @@ -36,8 +36,7 @@ pub type CommitmentSetDigest = [u8; 32]; pub type MembershipProof = (usize, Vec<[u8; 32]>); -/// computes the resulting digest/root hash for a membership proof -/// and its corresponding commitment. +/// Computes the resulting digest for the given membership proof and corresponding commitment pub fn compute_digest_for_path( commitment: &Commitment, proof: &MembershipProof, diff --git a/nssa/src/state.rs b/nssa/src/state.rs index e27b00c..77495e9 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -21,7 +21,7 @@ impl CommitmentSet { self.merkle_tree.root() } - /// queries the CommitmentSet for membership proof of commitment + /// Queries the CommitmentSet for a membership proof of commitment pub fn get_proof_for(&self, commitment: &Commitment) -> Option { let index = *self.commitments.get(commitment)?; @@ -30,7 +30,7 @@ impl CommitmentSet { .map(|path| (index, path)) } - /// appends a list of commitments to the CommitmentSet. + /// Inserts a list of commitments to the CommitmentSet. pub(crate) fn extend(&mut self, commitments: &[Commitment]) { for commitment in commitments.iter().cloned() { let index = self.merkle_tree.insert(commitment.to_byte_array()); @@ -44,7 +44,7 @@ impl CommitmentSet { } /// Initializes a CommitmentState with a given capacity. - /// if capacity is not a power_of_two, then capacity is taken + /// If the capacity is not a power_of_two, then capacity is taken /// to be the next power_of_two. pub(crate) fn with_capacity(capacity: usize) -> CommitmentSet { Self { From 49019eaee270e880f4ae98dfd85923fe59393f9c Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 20:48:11 -0400 Subject: [PATCH 17/20] Update nssa/core/src/commitment.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/core/src/commitment.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nssa/core/src/commitment.rs b/nssa/core/src/commitment.rs index 760aa91..1eb8c98 100644 --- a/nssa/core/src/commitment.rs +++ b/nssa/core/src/commitment.rs @@ -8,8 +8,8 @@ use crate::{NullifierPublicKey, account::Account}; pub struct Commitment(pub(super) [u8; 32]); impl Commitment { - // Generates the commitment to a private account owned by user for npk: - // hash(npk || program_owner || balance || nonce || data) + /// Generates the commitment to a private account owned by user for npk: + /// SHA256(npk || program_owner || balance || nonce || data) pub fn new(npk: &NullifierPublicKey, account: &Account) -> Self { let mut bytes = Vec::new(); bytes.extend_from_slice(&npk.to_byte_array()); From 4ae2af21a578f73ddc0fe7cca340ca89ca3792c6 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 20:48:26 -0400 Subject: [PATCH 18/20] Update nssa/src/state.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/state.rs b/nssa/src/state.rs index 77495e9..1817445 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -43,7 +43,7 @@ impl CommitmentSet { self.commitments.contains_key(commitment) } - /// Initializes a CommitmentState with a given capacity. + /// Initializes an empty `CommitmentSet` with a given capacity. /// If the capacity is not a power_of_two, then capacity is taken /// to be the next power_of_two. pub(crate) fn with_capacity(capacity: usize) -> CommitmentSet { From 4a0a99e26486b72493910ae933c06661aec48c8f Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 20:48:47 -0400 Subject: [PATCH 19/20] Update nssa/src/state.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/state.rs b/nssa/src/state.rs index 1817445..8641e14 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -30,7 +30,7 @@ impl CommitmentSet { .map(|path| (index, path)) } - /// Inserts a list of commitments to the CommitmentSet. + /// Inserts a list of commitments to the `CommitmentSet`. pub(crate) fn extend(&mut self, commitments: &[Commitment]) { for commitment in commitments.iter().cloned() { let index = self.merkle_tree.insert(commitment.to_byte_array()); From c055941738f8b9d9263c5e3262b16e7865e59c33 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 20:48:56 -0400 Subject: [PATCH 20/20] Update nssa/src/state.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/state.rs b/nssa/src/state.rs index 8641e14..f980370 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -21,7 +21,7 @@ impl CommitmentSet { self.merkle_tree.root() } - /// Queries the CommitmentSet for a membership proof of commitment + /// Queries the `CommitmentSet` for a membership proof of commitment pub fn get_proof_for(&self, commitment: &Commitment) -> Option { let index = *self.commitments.get(commitment)?;