From 8b1d3b478c8a1875927610f8b22df307ef73b1a9 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 16 Jul 2025 10:04:23 -0300 Subject: [PATCH] minor refactor --- accounts/src/account_core/mod.rs | 2 +- accounts/src/key_management/mod.rs | 10 +++--- common/src/transaction.rs | 26 ++++++-------- node_core/src/lib.rs | 21 ++++-------- node_rpc/src/process.rs | 42 ----------------------- sequencer_core/src/lib.rs | 4 +-- sequencer_core/src/transaction_mempool.rs | 21 ------------ 7 files changed, 26 insertions(+), 100 deletions(-) diff --git a/accounts/src/account_core/mod.rs b/accounts/src/account_core/mod.rs index 192c158..66d143e 100644 --- a/accounts/src/account_core/mod.rs +++ b/accounts/src/account_core/mod.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use anyhow::Result; use common::{merkle_tree_public::TreeHashType, transaction::Tag}; -use k256::AffinePoint; +use k256::{ecdsa::VerifyingKey, AffinePoint}; use log::info; use serde::{Deserialize, Serialize}; use utxo::utxo_core::UTXO; diff --git a/accounts/src/key_management/mod.rs b/accounts/src/key_management/mod.rs index 2598294..e871a0f 100644 --- a/accounts/src/key_management/mod.rs +++ b/accounts/src/key_management/mod.rs @@ -10,7 +10,7 @@ use secret_holders::{SeedHolder, TopSecretKeyHolder, UTXOSecretKeyHolder}; use serde::{Deserialize, Serialize}; use crate::account_core::PublicKey; -pub type PublicAccountSecretKey = [u8; 32]; +pub type PublicAccountSigningKey = [u8; 32]; pub mod constants_types; pub mod ephemeral_key_holder; @@ -23,7 +23,7 @@ pub struct AddressKeyHolder { #[allow(dead_code)] top_secret_key_holder: TopSecretKeyHolder, pub utxo_secret_key_holder: UTXOSecretKeyHolder, - pub pub_account_secret_key: PublicAccountSecretKey, + pub_account_signing_key: PublicAccountSigningKey, pub address: TreeHashType, pub nullifer_public_key: PublicKey, pub viewing_public_key: PublicKey, @@ -54,12 +54,12 @@ impl AddressKeyHolder { address, nullifer_public_key, viewing_public_key, - pub_account_secret_key, + pub_account_signing_key: pub_account_secret_key, } } - pub fn pub_account_secret_key(&self) -> SigningKey { - let field_bytes = FieldBytes::from_slice(&self.pub_account_secret_key); + pub fn get_pub_account_signing_key(&self) -> SigningKey { + let field_bytes = FieldBytes::from_slice(&self.pub_account_signing_key); // TODO: remove unwrap SigningKey::from_bytes(&field_bytes).unwrap() } diff --git a/common/src/transaction.rs b/common/src/transaction.rs index d02ed41..68bf640 100644 --- a/common/src/transaction.rs +++ b/common/src/transaction.rs @@ -226,40 +226,40 @@ impl TransactionBody { } } -pub type SignaturePrivateKey = SigningKey; -pub type SignaturePublicKey = VerifyingKey; +type TransactionHash = [u8; 32]; pub type TransactionSignature = Signature; - -type TransactionHash = TreeHashType; +pub type SignaturePublicKey = VerifyingKey; +pub type SignaturePrivateKey = SigningKey; /// A transaction with a signature. /// Meant to be sent through the network to the sequencer #[derive(Debug, Serialize, Deserialize, Clone)] pub struct SignedTransaction { pub body: TransactionBody, - signature: (TransactionSignature, SignaturePublicKey), + signature: TransactionSignature, + public_key: VerifyingKey, } impl SignedTransaction { pub fn from_transaction_body( body: TransactionBody, - private_key: SignaturePrivateKey, + private_key: SigningKey, ) -> SignedTransaction { let hash = body.hash(); - let signature: Signature = private_key.sign(&hash); + let signature: TransactionSignature = private_key.sign(&hash); let public_key = VerifyingKey::from(&private_key); Self { body, - signature: (signature, public_key), + signature, + public_key, } } pub fn into_authenticated(self) -> Result { let hash = self.body.hash(); - let (signature, public_key) = &self.signature; - public_key - .verify(&hash, signature) + self.public_key + .verify(&hash, &self.signature) .map_err(|_| TransactionSignatureError::InvalidSignature)?; Ok(AuthenticatedTransaction { @@ -287,10 +287,6 @@ impl AuthenticatedTransaction { &self.signed_tx.body } - pub fn signature(&self) -> &TransactionSignature { - &self.signed_tx.signature.0 - } - pub fn hash(&self) -> &TransactionHash { &self.hash } diff --git a/node_core/src/lib.rs b/node_core/src/lib.rs index 7812614..5bed54a 100644 --- a/node_core/src/lib.rs +++ b/node_core/src/lib.rs @@ -264,8 +264,7 @@ impl NodeCore { sc_addr, state_changes, }; - // TODO: Change to the correct key once established. - let key_to_sign_transaction = account.key_holder.pub_account_secret_key(); + let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key(); Ok(( SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction), @@ -362,8 +361,7 @@ impl NodeCore { sc_addr, state_changes, }; - // TODO: Change to the correct key once established. - let key_to_sign_transaction = account.key_holder.pub_account_secret_key(); + let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key(); Ok(( SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction), @@ -480,8 +478,7 @@ impl NodeCore { state_changes, }; - // TODO: Change to the correct key once established. - let key_to_sign_transaction = account.key_holder.pub_account_secret_key(); + let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key(); Ok(( SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction), @@ -627,8 +624,7 @@ impl NodeCore { state_changes, }; - // TODO: Change to the correct key once established. - let key_to_sign_transaction = account.key_holder.pub_account_secret_key(); + let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key(); Ok(( SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction), @@ -758,8 +754,7 @@ impl NodeCore { state_changes, }; - // TODO: Change to the correct key once established. - let key_to_sign_transaction = account.key_holder.pub_account_secret_key(); + let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key(); Ok(( SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction), @@ -850,8 +845,7 @@ impl NodeCore { state_changes, }; - // TODO: Change to the correct key once established. - let key_to_sign_transaction = account.key_holder.pub_account_secret_key(); + let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key(); Ok(SignedTransaction::from_transaction_body( transaction_body, @@ -1493,8 +1487,7 @@ impl NodeCore { sc_addr, state_changes, }; - // TODO: Change to the correct key once established. - let key_to_sign_transaction = account.key_holder.pub_account_secret_key(); + let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key(); Ok(( SignedTransaction::from_transaction_body(transaction_body, key_to_sign_transaction), diff --git a/node_rpc/src/process.rs b/node_rpc/src/process.rs index 149121a..9f45b98 100644 --- a/node_rpc/src/process.rs +++ b/node_rpc/src/process.rs @@ -42,7 +42,6 @@ pub const EXECUTE_SCENARIO_MULTIPLE_SEND: &str = "execute_scenario_multiple_send pub const SHOW_ACCOUNT_PUBLIC_BALANCE: &str = "show_account_public_balance"; pub const SHOW_ACCOUNT_UTXO: &str = "show_account_utxo"; pub const SHOW_TRANSACTION: &str = "show_transaction"; -// pub const WRITE_DEPOSIT_PUBLIC_BALANCE: &str = "write_deposit_public_balance"; pub const WRITE_MINT_UTXO: &str = "write_mint_utxo"; pub const WRITE_MINT_UTXO_MULTIPLE_ASSETS: &str = "write_mint_utxo_multiple_assets"; pub const WRITE_SEND_UTXO_PRIVATE: &str = "write_send_utxo_private"; @@ -84,18 +83,10 @@ impl JsonHandler { .subscenario_1() .await .map_err(cast_common_execution_error_into_rpc_error)?, - // 2 => store - // .subscenario_2() - // .await - // .map_err(cast_common_execution_error_into_rpc_error)?, 3 => store .subscenario_3() .await .map_err(cast_common_execution_error_into_rpc_error)?, - // 4 => store - // .subscenario_4() - // .await - // .map_err(cast_common_execution_error_into_rpc_error)?, 5 => store .subscenario_5() .await @@ -359,36 +350,6 @@ impl JsonHandler { respond(helperstruct) } - // pub async fn process_write_deposit_public_balance( - // &self, - // request: Request, - // ) -> Result { - // let req = WriteDepositPublicBalanceRequest::parse(Some(request.params))?; - // - // let acc_addr_hex_dec = hex::decode(req.account_addr.clone()).map_err(|_| { - // RpcError::parse_error("Failed to decode account address from hex string".to_string()) - // })?; - // - // let acc_addr: [u8; 32] = acc_addr_hex_dec.try_into().map_err(|_| { - // RpcError::parse_error("Failed to parse account address from bytes".to_string()) - // })?; - // - // { - // let mut cover_guard = self.node_chain_store.lock().await; - // - // cover_guard - // .operate_account_deposit_public(acc_addr, req.amount as u128) - // .await - // .map_err(cast_common_execution_error_into_rpc_error)?; - // }; - // - // let helperstruct = WriteDepositPublicBalanceResponse { - // status: SUCCESS.to_string(), - // }; - // - // respond(helperstruct) - // } - pub async fn process_write_mint_utxo(&self, request: Request) -> Result { let req = WriteMintPrivateUTXORequest::parse(Some(request.params))?; @@ -781,9 +742,6 @@ impl JsonHandler { SHOW_ACCOUNT_PUBLIC_BALANCE => self.process_show_account_public_balance(request).await, SHOW_ACCOUNT_UTXO => self.process_show_account_utxo_request(request).await, SHOW_TRANSACTION => self.process_show_transaction(request).await, - // WRITE_DEPOSIT_PUBLIC_BALANCE => { - // self.process_write_deposit_public_balance(request).await - // } WRITE_MINT_UTXO => self.process_write_mint_utxo(request).await, WRITE_MINT_UTXO_MULTIPLE_ASSETS => { self.process_write_mint_utxo_multiple_assets(request).await diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index 188394d..174763e 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -363,7 +363,7 @@ mod tests { } #[test] - fn test_transaction_pre_check_fail_mempool_full() { + fn test_push_tx_into_mempool_fails_mempool_full() { let config = SequencerConfig { max_num_tx_in_block: 1, ..setup_sequencer_config() @@ -381,7 +381,7 @@ mod tests { }; sequencer.mempool.push_item(dummy_tx); - let result = sequencer.transaction_pre_check(tx, tx_roots); + let result = sequencer.push_tx_into_mempool_pre_check(tx, tx_roots); assert!(matches!( result, diff --git a/sequencer_core/src/transaction_mempool.rs b/sequencer_core/src/transaction_mempool.rs index cb40ad5..fc73fc9 100644 --- a/sequencer_core/src/transaction_mempool.rs +++ b/sequencer_core/src/transaction_mempool.rs @@ -12,27 +12,6 @@ impl From for MempoolTransaction { } } -// impl Serialize for TransactionMempool { -// fn serialize(&self, serializer: S) -> Result -// where -// S: serde::Serializer, -// { -// self.tx.serialize(serializer) -// } -// } -// -// impl<'de> Deserialize<'de> for TransactionMempool { -// fn deserialize(deserializer: D) -> Result -// where -// D: serde::Deserializer<'de>, -// { -// match TransactionBody::deserialize(deserializer) { -// Ok(tx) => Ok(TransactionMempool { tx }), -// Err(err) => Err(err), -// } -// } -// } - impl MemPoolItem for MempoolTransaction { type Identifier = TreeHashType;