Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return early in PosBase::transfer if an attempt is made to transfer zero tokens #856

Merged
merged 4 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/856-amount-is-zero.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Return early in PosBase::transfer if an attempt is made to transfer zero
tokens ([#856](https://github.com/anoma/namada/pull/856))
3 changes: 3 additions & 0 deletions core/src/ledger/storage_api/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub fn transfer<S>(
where
S: StorageRead + StorageWrite,
{
if amount.is_zero() {
return Ok(());
}
let src_key = token::balance_key(token, src);
let src_balance = read_balance(storage, token, src)?;
match src_balance.checked_sub(amount) {
Expand Down
14 changes: 14 additions & 0 deletions core/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub const MAX_AMOUNT: Amount = Amount { micro: u64::MAX };
pub type Change = i128;

impl Amount {
/// Returns whether an amount is zero.
pub fn is_zero(&self) -> bool {
self.micro == 0
}

/// Get the amount as a [`Change`]
pub fn change(&self) -> Change {
self.micro as Change
Expand Down Expand Up @@ -550,6 +555,15 @@ mod tests {
assert_eq!(max.checked_add(one), None);
assert_eq!(max.checked_add(max), None);
}

#[test]
fn test_amount_is_zero() {
let zero = Amount::from(0);
assert!(zero.is_zero());

let non_zero = Amount::from(1);
assert!(!non_zero.is_zero());
}
}

/// Helpers for testing with addresses.
Expand Down
10 changes: 5 additions & 5 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"tx_bond.wasm": "tx_bond.3277d0c7eb81db738c3b77963c546e385e11bd0c87ff5213e49d0d4a96f4b7e1.wasm",
"tx_bond.wasm": "tx_bond.8915d1bc2e50a39113eda4b84dbafcef55634bda3c7c517687112952353bdc40.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.8da9bfc181836d1715e3e35f99bf3577c157f619ceb573b2911e9ebd2b497a9b.wasm",
"tx_ibc.wasm": "tx_ibc.7f35d82f6ba216f6ecffe0c7ca52e641a9b7763dde03d533a88bc20a88c14737.wasm",
"tx_ibc.wasm": "tx_ibc.d8d856437804b82374051fb15c441c9b50715e51aca06b4f7d1ffe67fd75dd13.wasm",
"tx_init_account.wasm": "tx_init_account.9cc792ba8535b0e29643184afbd51c6b5e7153a4276a7acc23079ed9e802fb2b.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.26d8b551e609dddc6dc6e608d36994edde00b49a14c20fa0c8074e94244d5674.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.84fa938093c4a5bd9d0113ac2684fa38ccd99cd56e8291da74026a9b85d278a4.wasm",
"tx_init_validator.wasm": "tx_init_validator.3f04b3bbeb17b493858ba96dc309021468b166ab01d594773cbf9744d1a8076b.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.7a645d6afbf8844e1aea83905eed26f9e7d550db456bdde169a4786c902b917f.wasm",
"tx_transfer.wasm": "tx_transfer.0db9fb8473c65b88800e2cd1e9a630245d01062cdc31fdb80b87da53fedaa5fd.wasm",
"tx_transfer.wasm": "tx_transfer.64d73335d0db816c56d21e5ef2af11f594337370d36ba28a8e1b329e1216692f.wasm",
"tx_unbond.wasm": "tx_unbond.c3391611d4a1f5818876a799efa87fee793eedcba193c636e294bf1dd4c6f340.wasm",
"tx_update_vp.wasm": "tx_update_vp.dbaf4fdacb12fba9fe1231afd0371a302a8242cbbf74565d8605a646fe5a00ab.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.704d0c40268c997ab185d9e5a9f02decf40f4c51c9684a25ef6a9be2768b180d.wasm",
"tx_withdraw.wasm": "tx_withdraw.d213ea0d916f7c962527fb2526df98479dff7c0ab984e776f523e61407ca3608.wasm",
"tx_withdraw.wasm": "tx_withdraw.70bea521c96d4a5e970a741c7b5af03c6407c4b7f9c27b3f8b9a4baa530f6c43.wasm",
"vp_implicit.wasm": "vp_implicit.e5aff165c7b3c43f0d6d0bc2848f2d171ce5599f01fa33d0572bea584a83903c.wasm",
"vp_masp.wasm": "vp_masp.813d4ec58e55255f0fe3e3d40ea723fca34dcea69de57f26e89c7737c56254db.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.25bcab2206bba4cf89dbf33cf133fd034878566d7e375856bbf53e4547db441f.wasm",
Expand Down