From 41b65521f9d293f9d3d8937b3efb62ae65c8f4ac Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Thu, 24 Jul 2025 16:05:27 +0300 Subject: [PATCH] fix: comments fix 1 --- ...ransfer_receipts.rs => execution_input.rs} | 2 +- common/src/lib.rs | 2 +- node_core/src/chain_storage/mod.rs | 6 +- node_core/src/lib.rs | 6 +- sequencer_core/src/lib.rs | 59 ++++++++++--------- .../src/sequencer_store/accounts_store.rs | 6 +- 6 files changed, 42 insertions(+), 39 deletions(-) rename common/src/{public_transfer_receipts.rs => execution_input.rs} (88%) diff --git a/common/src/public_transfer_receipts.rs b/common/src/execution_input.rs similarity index 88% rename from common/src/public_transfer_receipts.rs rename to common/src/execution_input.rs index c865a03..d1f282a 100644 --- a/common/src/public_transfer_receipts.rs +++ b/common/src/execution_input.rs @@ -6,5 +6,5 @@ use crate::merkle_tree_public::TreeHashType; pub struct PublicNativeTokenSend { pub from: TreeHashType, pub to: TreeHashType, - pub moved_balance: u64, + pub balance_to_move: u64, } diff --git a/common/src/lib.rs b/common/src/lib.rs index fb364c7..1722218 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -3,9 +3,9 @@ use serde::Deserialize; pub mod block; pub mod commitment; +pub mod execution_input; pub mod merkle_tree_public; pub mod nullifier; -pub mod public_transfer_receipts; pub mod rpc_primitives; pub mod transaction; pub mod utxo_commitment; diff --git a/node_core/src/chain_storage/mod.rs b/node_core/src/chain_storage/mod.rs index 5fac66c..126574f 100644 --- a/node_core/src/chain_storage/mod.rs +++ b/node_core/src/chain_storage/mod.rs @@ -5,9 +5,9 @@ use anyhow::Result; use block_store::NodeBlockStore; use common::{ block::Block, + execution_input::PublicNativeTokenSend, merkle_tree_public::merkle_tree::{PublicTransactionMerkleTree, UTXOCommitmentsMerkleTree}, nullifier::UTXONullifier, - public_transfer_receipts::PublicNativeTokenSend, utxo_commitment::UTXOCommitment, }; use k256::AffinePoint; @@ -166,10 +166,10 @@ impl NodeChainStore { if let Ok(transfer) = native_transfer { if let Some(acc_sender) = self.acc_map.get_mut(&transfer.from) { //Can panic, we depend on sequencer maintaining chain consistency here - acc_sender.balance -= transfer.moved_balance; + acc_sender.balance -= transfer.balance_to_move; if let Some(acc_rec) = self.acc_map.get_mut(&transfer.to) { - acc_rec.balance += transfer.moved_balance; + acc_rec.balance += transfer.balance_to_move; } } } diff --git a/node_core/src/lib.rs b/node_core/src/lib.rs index 456e409..b914bcf 100644 --- a/node_core/src/lib.rs +++ b/node_core/src/lib.rs @@ -4,7 +4,7 @@ use std::sync::{ }; use common::{ - public_transfer_receipts::PublicNativeTokenSend, transaction::Transaction, ExecutionFailureKind, + execution_input::PublicNativeTokenSend, transaction::Transaction, ExecutionFailureKind, }; use accounts::{ @@ -955,7 +955,7 @@ impl NodeCore { &self, from: AccountAddress, to: AccountAddress, - moved_balance: u64, + balance_to_move: u64, ) -> Result { let tx_roots = self.get_roots().await; @@ -982,7 +982,7 @@ impl NodeCore { serde_json::to_vec(&PublicNativeTokenSend { from, to, - moved_balance, + balance_to_move, }) .unwrap(), commitment, diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index d99a782..d22841a 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -4,9 +4,9 @@ use accounts::account_core::AccountAddress; use anyhow::Result; use common::{ block::{Block, HashableBlockData}, + execution_input::PublicNativeTokenSend, merkle_tree_public::TreeHashType, nullifier::UTXONullifier, - public_transfer_receipts::PublicNativeTokenSend, transaction::{AuthenticatedTransaction, Transaction, TransactionBody, TxKind}, utxo_commitment::UTXOCommitment, }; @@ -177,33 +177,6 @@ impl SequencerCore { ); } - //Balance check - if let Ok(native_transfer_action) = - serde_json::from_slice::(execution_input) - { - let from_balance = self - .store - .acc_store - .get_account_balance(&native_transfer_action.from); - let to_balance = self - .store - .acc_store - .get_account_balance(&native_transfer_action.to); - - if from_balance >= native_transfer_action.moved_balance { - self.store.acc_store.set_account_balance( - &native_transfer_action.from, - from_balance - native_transfer_action.moved_balance, - ); - self.store.acc_store.set_account_balance( - &native_transfer_action.to, - to_balance + native_transfer_action.moved_balance, - ); - } else { - return Err(TransactionMalformationErrorKind::BalanceMismatch { tx: tx_hash }); - } - } - Ok(tx) } @@ -233,9 +206,12 @@ impl SequencerCore { let TransactionBody { ref utxo_commitments_created_hashes, ref nullifier_created_hashes, + execution_input, .. } = mempool_tx.auth_tx.transaction().body(); + let tx_hash = *mempool_tx.auth_tx.hash(); + for utxo_comm in utxo_commitments_created_hashes { self.store .utxo_commitments_store @@ -252,6 +228,33 @@ impl SequencerCore { .pub_tx_store .add_tx(mempool_tx.auth_tx.transaction()); + //Balance check + if let Ok(native_transfer_action) = + serde_json::from_slice::(execution_input) + { + let from_balance = self + .store + .acc_store + .get_account_balance(&native_transfer_action.from); + let to_balance = self + .store + .acc_store + .get_account_balance(&native_transfer_action.to); + + if from_balance >= native_transfer_action.balance_to_move { + self.store.acc_store.set_account_balance( + &native_transfer_action.from, + from_balance - native_transfer_action.balance_to_move, + ); + self.store.acc_store.set_account_balance( + &native_transfer_action.to, + to_balance + native_transfer_action.balance_to_move, + ); + } else { + return Err(TransactionMalformationErrorKind::BalanceMismatch { tx: tx_hash }); + } + } + Ok(()) } diff --git a/sequencer_core/src/sequencer_store/accounts_store.rs b/sequencer_core/src/sequencer_store/accounts_store.rs index f44b5fe..ae27302 100644 --- a/sequencer_core/src/sequencer_store/accounts_store.rs +++ b/sequencer_core/src/sequencer_store/accounts_store.rs @@ -66,17 +66,17 @@ impl SequencerAccountsStore { ///Update `account_addr` balance, /// - /// returns 0, if account address not found, othervise returns previous balance + /// returns 0, if account address not found, otherwise returns previous balance pub fn set_account_balance(&mut self, account_addr: &AccountAddress, new_balance: u64) -> u64 { let acc_data = self.accounts.get_mut(account_addr); acc_data .map(|data| { - let old_bal = data.balance; + let old_balance = data.balance; data.balance = new_balance; - old_bal + old_balance }) .unwrap_or(0) }