diff --git a/sequencer_core/src/sequencer_store/accounts_store.rs b/sequencer_core/src/sequencer_store/accounts_store.rs index 3e8b35c..2d9555f 100644 --- a/sequencer_core/src/sequencer_store/accounts_store.rs +++ b/sequencer_core/src/sequencer_store/accounts_store.rs @@ -98,10 +98,18 @@ impl SequencerAccountsStore { } } - pub fn increase_nonce(&mut self, account_addr: &AccountAddress) -> Option { - let acc_data = self.accounts.get_mut(account_addr)?; - acc_data.nonce += 1; - Some(acc_data.nonce) + ///Update `account_addr` nonce, + /// + /// Returns previous nonce + pub fn increase_nonce(&mut self, account_addr: &AccountAddress) -> u64 { + if let Some(acc_data) = self.accounts.get_mut(account_addr) { + let old_nonce = acc_data.nonce; + acc_data.nonce += 1; + old_nonce + } else { + self.register_account(*account_addr); + self.increase_nonce(account_addr) + } } ///Remove account from storage @@ -289,4 +297,14 @@ mod tests { assert!(seq_acc_store.contains_account(&[1; 32])); assert_eq!(seq_acc_store.get_account_balance(&[1; 32]), 0); } + + #[test] + fn test_increase_nonce() { + let mut account_store = SequencerAccountsStore::default(); + let address = [1; 32]; + let first_nonce = account_store.increase_nonce(&address); + assert_eq!(first_nonce, 0); + let second_nonce = account_store.increase_nonce(&address); + assert_eq!(second_nonce, 1); + } }