modified verification to loop until success or failure
This commit is contained in:
@ -24,7 +24,6 @@ pub const MAX_ENTRY_IDS: usize = 1024 * 4;
|
|||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum AccountingError {
|
pub enum AccountingError {
|
||||||
AccountNotFound,
|
AccountNotFound,
|
||||||
BalanceUpdatedBeforeTransactionCompleted,
|
|
||||||
InsufficientFunds,
|
InsufficientFunds,
|
||||||
InvalidTransferSignature,
|
InvalidTransferSignature,
|
||||||
}
|
}
|
||||||
@ -142,25 +141,27 @@ impl Accountant {
|
|||||||
return Err(AccountingError::InvalidTransferSignature);
|
return Err(AccountingError::InvalidTransferSignature);
|
||||||
}
|
}
|
||||||
|
|
||||||
let bal = option.unwrap();
|
loop {
|
||||||
let current = bal.load(Ordering::Relaxed) as i64;
|
let bal = option.unwrap();
|
||||||
|
let current = bal.load(Ordering::Relaxed) as i64;
|
||||||
|
|
||||||
if current < tr.data.tokens {
|
if current < tr.data.tokens {
|
||||||
self.forget_signature_with_last_id(&tr.sig, &tr.data.last_id);
|
self.forget_signature_with_last_id(&tr.sig, &tr.data.last_id);
|
||||||
return Err(AccountingError::InsufficientFunds);
|
return Err(AccountingError::InsufficientFunds);
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = bal.compare_exchange(
|
||||||
|
current as isize,
|
||||||
|
(current - tr.data.tokens) as isize,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_) => return Ok(()),
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = bal.compare_exchange(
|
|
||||||
current as isize,
|
|
||||||
(current - tr.data.tokens) as isize,
|
|
||||||
Ordering::Relaxed,
|
|
||||||
Ordering::Relaxed,
|
|
||||||
);
|
|
||||||
|
|
||||||
return match result {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(_) => Err(AccountingError::BalanceUpdatedBeforeTransactionCompleted),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_verified_transaction_credits(&self, tr: &Transaction) {
|
pub fn process_verified_transaction_credits(&self, tr: &Transaction) {
|
||||||
|
Reference in New Issue
Block a user