modified verification to loop until success or failure

This commit is contained in:
Robert Kelly 2018-05-02 10:15:08 -04:00
parent cb362e9052
commit b992a84d67
1 changed files with 19 additions and 18 deletions

View File

@ -24,7 +24,6 @@ pub const MAX_ENTRY_IDS: usize = 1024 * 4;
#[derive(Debug, PartialEq, Eq)]
pub enum AccountingError {
AccountNotFound,
BalanceUpdatedBeforeTransactionCompleted,
InsufficientFunds,
InvalidTransferSignature,
}
@ -142,25 +141,27 @@ impl Accountant {
return Err(AccountingError::InvalidTransferSignature);
}
let bal = option.unwrap();
let current = bal.load(Ordering::Relaxed) as i64;
loop {
let bal = option.unwrap();
let current = bal.load(Ordering::Relaxed) as i64;
if current < tr.data.tokens {
self.forget_signature_with_last_id(&tr.sig, &tr.data.last_id);
return Err(AccountingError::InsufficientFunds);
if current < tr.data.tokens {
self.forget_signature_with_last_id(&tr.sig, &tr.data.last_id);
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) {