Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Apr 30, 2024
1 parent 9c47caf commit 7615492
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ mod test_finalize_block {
&mut shell.state,
&native_token,
&Address::from(&keypair.ref_to()),
|_| Amount::native_whole(1000),
|_| Ok(Amount::native_whole(1000)),
)
.unwrap();

Expand Down Expand Up @@ -1151,7 +1151,7 @@ mod test_finalize_block {
&mut shell.state,
&native_token,
&bridge_pool::BRIDGE_POOL_ADDRESS,
|_| amt,
|_| Ok(amt),
)
.expect("Test failed");
}
Expand Down
28 changes: 20 additions & 8 deletions crates/trans_token/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use namada_core::address::{Address, InternalAddress};
use namada_core::hints;
use namada_core::token::{self, Amount, DenominatedAmount};
use namada_core::token::{self, Amount, AmountError, DenominatedAmount};
use namada_storage as storage;
use namada_storage::{StorageRead, StorageWrite};
use storage::ResultExt;

use crate::storage_key::*;

Expand Down Expand Up @@ -41,11 +42,11 @@ pub fn update_balance<S, F>(
) -> storage::Result<()>
where
S: StorageRead + StorageWrite,
F: FnOnce(token::Amount) -> token::Amount,
F: FnOnce(token::Amount) -> storage::Result<token::Amount>,
{
let key = balance_key(token, owner);
let balance = storage.read::<token::Amount>(&key)?.unwrap_or_default();
let new_balance = f(balance);
let new_balance = f(balance)?;
storage.write(&key, new_balance)
}

Expand All @@ -70,11 +71,11 @@ pub fn update_total_supply<S, F>(
) -> storage::Result<()>
where
S: StorageRead + StorageWrite,
F: FnOnce(token::Amount) -> token::Amount,
F: FnOnce(token::Amount) -> storage::Result<token::Amount>,
{
let key = minted_balance_key(token);
let total_supply = storage.read::<token::Amount>(&key)?.unwrap_or_default();
let new_supply = f(total_supply);
let new_supply = f(total_supply)?;
storage.write(&key, new_supply)
}

Expand Down Expand Up @@ -197,10 +198,20 @@ where
S: StorageRead + StorageWrite,
{
// Update the destination balance
update_balance(storage, token, dest, |cur_amount| cur_amount + amount)?;
update_balance(storage, token, dest, |cur_amount| {
cur_amount
.checked_add(amount)
.ok_or(AmountError::Overflow)
.into_storage_result()
})?;

// Update the total supply
update_total_supply(storage, token, |cur_supply| cur_supply + amount)
update_total_supply(storage, token, |cur_supply| {
cur_supply
.checked_add(amount)
.ok_or(AmountError::Overflow)
.into_storage_result()
})
}

/// Burn a specified amount of tokens from some address. If the burn amount is
Expand Down Expand Up @@ -231,7 +242,8 @@ where
update_total_supply(storage, token, |cur_supply| {
cur_supply
.checked_sub(amount_to_burn)
.expect("Total token supply underflowed")
.ok_or(AmountError::Insufficient)
.into_storage_result()
})
}

Expand Down

0 comments on commit 7615492

Please sign in to comment.