wip: added nonces and test

This commit is contained in:
Sergio Chouhy 2025-07-28 16:09:03 -03:00
parent 484c22b649
commit f21c0df652
8 changed files with 45 additions and 1 deletions

View File

@ -62,6 +62,8 @@ pub struct TransactionBody {
///
/// First value represents vector of changes, second is new length of a state
pub state_changes: (serde_json::Value, usize),
pub nonce: u64,
}
#[derive(Debug, Serialize, Deserialize)]
@ -322,6 +324,7 @@ mod tests {
secret_r: [8; 32],
sc_addr: "someAddress".to_string(),
state_changes: (serde_json::Value::Null, 10),
nonce: 1,
}
}

View File

@ -337,6 +337,7 @@ mod tests {
secret_r: [0; 32],
sc_addr: "sc_addr".to_string(),
state_changes: (serde_json::Value::Null, 0),
nonce: 1,
};
Transaction::new(body, SignaturePrivateKey::random(&mut rng))
}

View File

@ -274,6 +274,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
nonce: 1,
};
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
@ -370,6 +371,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
nonce: 1,
};
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
@ -484,6 +486,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
nonce: 1
};
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
@ -629,6 +632,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
nonce: 1,
};
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
@ -757,6 +761,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
nonce: 1
};
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
@ -846,6 +851,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
nonce: 1
};
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
@ -992,6 +998,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
1,
);
tx.log();
@ -1548,6 +1555,7 @@ impl NodeCore {
secret_r,
sc_addr,
state_changes,
nonce: 1,
};
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();

View File

@ -15,6 +15,7 @@ pub fn create_public_transaction_payload(
secret_r: [u8; 32],
sc_addr: String,
state_changes: (serde_json::Value, usize),
nonce: u64
) -> TransactionBody {
TransactionBody {
tx_kind: TxKind::Public,
@ -31,6 +32,7 @@ pub fn create_public_transaction_payload(
secret_r,
sc_addr,
state_changes,
nonce,
}
}

View File

@ -41,6 +41,7 @@ pub enum TransactionMalformationErrorKind {
InvalidSignature,
IncorrectSender,
BalanceMismatch { tx: TreeHashType },
NonceMismatch { tx: TreeHashType },
FailedToDecode { tx: TreeHashType },
}
@ -223,11 +224,21 @@ impl SequencerCore {
ref utxo_commitments_created_hashes,
ref nullifier_created_hashes,
execution_input,
nonce,
..
} = mempool_tx.auth_tx.transaction().body();
let tx_hash = *mempool_tx.auth_tx.hash();
// Nonce check
let mut signer_addres = [0; 32];
let mut keccak_hasher = Keccak::v256();
keccak_hasher.update(&mempool_tx.auth_tx.transaction().public_key.to_sec1_bytes());
keccak_hasher.finalize(&mut signer_addres);
if self.store.acc_store.get_account_nonce(&signer_addres) != *nonce {
return Err(TransactionMalformationErrorKind::NonceMismatch { tx: tx_hash });
}
//Balance check
if let Ok(native_transfer_action) =
serde_json::from_slice::<PublicNativeTokenSend>(execution_input)
@ -250,6 +261,9 @@ impl SequencerCore {
&native_transfer_action.to,
to_balance + native_transfer_action.balance_to_move,
);
// Update nonce
let _new_nonce = self.store.acc_store.increase_nonce(&signer_addres);
} else {
return Err(TransactionMalformationErrorKind::BalanceMismatch { tx: tx_hash });
}
@ -287,7 +301,7 @@ impl SequencerCore {
.pop_size(self.sequencer_config.max_num_tx_in_block);
for tx in &transactions {
self.execute_check_transaction_on_state(tx)?;
self.execute_check_transaction_on_state(tx);
}
let prev_block_hash = self
@ -612,6 +626,7 @@ mod tests {
secret_r: [0; 32],
sc_addr: "sc_addr".to_string(),
state_changes: (serde_json::Value::Null, 0),
nonce: 0,
};
Transaction::new(body, SignaturePrivateKey::random(&mut rng))
}

View File

@ -74,6 +74,13 @@ impl SequencerAccountsStore {
.unwrap_or(0)
}
pub fn get_account_nonce(&self, account_addr: &AccountAddress) -> u64 {
self.accounts
.get(account_addr)
.map(|acc| acc.nonce)
.unwrap_or(0)
}
///Update `account_addr` balance,
///
/// returns 0, if account address not found, otherwise returns previous balance
@ -91,6 +98,12 @@ impl SequencerAccountsStore {
.unwrap_or(0)
}
pub fn increase_nonce(&mut self, account_addr: &AccountAddress) -> Option<u64> {
let acc_data = self.accounts.get_mut(account_addr)?;
acc_data.nonce += 1;
Some(acc_data.nonce)
}
///Remove account from storage
///
/// Fails, if `balance` is != 0

View File

@ -95,6 +95,7 @@ mod tests {
secret_r: Default::default(),
sc_addr: Default::default(),
state_changes: Default::default(),
nonce: 1
};
let tx = Transaction::new(body, SignaturePrivateKey::from_slice(&[1; 32]).unwrap());
(

View File

@ -506,6 +506,7 @@ mod tests {
secret_r: Default::default(),
sc_addr: Default::default(),
state_changes: Default::default(),
nonce: 1,
};
let tx = Transaction::new(tx_body, SignaturePrivateKey::from_slice(&[1; 32]).unwrap());