Skip to content

Commit

Permalink
Clean up naming
Browse files Browse the repository at this point in the history
  • Loading branch information
maplant committed Jul 26, 2023
1 parent 9306328 commit dd903fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 57 deletions.
40 changes: 22 additions & 18 deletions iot_packet_verifier/src/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use tokio::sync::Mutex;
/// Caches balances fetched from the solana chain and debits made by the
/// packet verifier.
pub struct BalanceCache<S> {
balances: BalanceStore,
payer_accounts: BalanceStore,
solana: S,
}

pub type BalanceStore = Arc<Mutex<HashMap<PublicKeyBinary, Balance>>>;
pub type BalanceStore = Arc<Mutex<HashMap<PublicKeyBinary, PayerAccount>>>;

impl<S> BalanceCache<S>
where
Expand All @@ -43,23 +43,23 @@ where
let balance = solana.payer_balance(&payer).await?;
balances.insert(
payer,
Balance {
PayerAccount {
burned: burn_amount as u64,
balance,
},
);
}

Ok(Self {
balances: Arc::new(Mutex::new(balances)),
payer_accounts: Arc::new(Mutex::new(balances)),
solana,
})
}
}

impl<S> BalanceCache<S> {
pub fn balances(&self) -> BalanceStore {
self.balances.clone()
self.payer_accounts.clone()
}
}

Expand All @@ -78,38 +78,42 @@ where
amount: u64,
trigger_balance_check_threshold: u64,
) -> Result<Option<u64>, S::Error> {
let mut balances = self.balances.lock().await;
let mut payer_accounts = self.payer_accounts.lock().await;

// Fetch the balance if we haven't seen the payer before
if let Entry::Vacant(balance) = balances.entry(payer.clone()) {
let balance = balance.insert(Balance::new(self.solana.payer_balance(payer).await?));
return Ok((balance.balance >= amount).then(|| {
balance.burned += amount;
balance.balance - amount
if let Entry::Vacant(payer_account) = payer_accounts.entry(payer.clone()) {
let payer_account =
payer_account.insert(PayerAccount::new(self.solana.payer_balance(payer).await?));
return Ok((payer_account.balance >= amount).then(|| {
payer_account.burned += amount;
payer_account.balance - amount
}));
}

let balance = balances.get_mut(payer).unwrap();
match balance.balance.checked_sub(amount + balance.burned) {
let payer_account = payer_accounts.get_mut(payer).unwrap();
match payer_account
.balance
.checked_sub(amount + payer_account.burned)
{
Some(remaining_balance) => {
if remaining_balance < trigger_balance_check_threshold {
balance.balance = self.solana.payer_balance(payer).await?;
payer_account.balance = self.solana.payer_balance(payer).await?;
}
balance.burned += amount;
Ok(Some(balance.balance - balance.burned))
payer_account.burned += amount;
Ok(Some(payer_account.balance - payer_account.burned))
}
None => Ok(None),
}
}
}

#[derive(Copy, Clone, Debug, Default)]
pub struct Balance {
pub struct PayerAccount {
pub balance: u64,
pub burned: u64,
}

impl Balance {
impl PayerAccount {
pub fn new(balance: u64) -> Self {
Self { balance, burned: 0 }
}
Expand Down
39 changes: 0 additions & 39 deletions iot_packet_verifier/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,43 +485,4 @@ async fn test_end_to_end() {
invalid_packets,
vec![invalid_packet(BYTES_PER_DC as u32, vec![5])]
);

// Add one DC to the balance:
*solana_network.lock().await.get_mut(&payer).unwrap() = 1;

valid_packets.clear();
invalid_packets.clear();

// First packet should be invalid since it is too large, second
// should clear
verifier
.verify(
1,
pending_burns.clone(),
stream::iter(vec![
packet_report(0, 5, 2 * BYTES_PER_DC as u32, vec![6]),
packet_report(0, 6, BYTES_PER_DC as u32, vec![7]),
]),
&mut valid_packets,
&mut invalid_packets,
)
.await
.unwrap();

assert_eq!(
invalid_packets,
vec![invalid_packet(2 * BYTES_PER_DC as u32, vec![6])]
);
assert_eq!(
valid_packets,
vec![valid_packet(6000, BYTES_PER_DC as u32, vec![7])]
);

let balance = {
let balances = verifier.debiter.balances();
let balances = balances.lock().await;
*balances.get(&payer).unwrap()
};
assert_eq!(balance.balance, 1);
assert_eq!(balance.burned, 1);
}

0 comments on commit dd903fe

Please sign in to comment.