Skip to content

Commit

Permalink
Merge branch 'brent/token-balance-update-fns' (#3029)
Browse files Browse the repository at this point in the history
* origin/brent/token-balance-update-fns:
  changelog: add #3029
  update eth-bridge things
  better modularization
  better error handling
  use new fns in tests
  remove redundant genesis total supply initialization
  add functions in trans_token storage
  • Loading branch information
brentstone committed May 9, 2024
2 parents 8e64596 + 9f32d08 commit e1d6183
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 214 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Refactor and modularize the token balance and supply API.
([\#3029](https://github.com/anoma/namada/pull/3029))
80 changes: 36 additions & 44 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,10 @@ mod test_finalize_block {
use namada::replay_protection;
use namada::sdk::events::Event;
use namada::tendermint::abci::types::{Misbehavior, MisbehaviorKind};
use namada::token::{Amount, DenominatedAmount, NATIVE_MAX_DECIMAL_PLACES};
use namada::token::{
read_balance, update_balance, Amount, DenominatedAmount,
NATIVE_MAX_DECIMAL_PLACES,
};
use namada::tx::data::Fee;
use namada::tx::event::types::APPLIED as APPLIED_TX;
use namada::tx::event::Code as CodeAttr;
Expand Down Expand Up @@ -843,14 +846,14 @@ mod test_finalize_block {
let mut processed_txs = vec![];

// Add unshielded balance for fee payment
let balance_key = token::storage_key::balance_key(
&shell.state.in_mem().native_token,
let native_token = shell.state.in_mem().native_token.clone();
update_balance(
&mut shell.state,
&native_token,
&Address::from(&keypair.ref_to()),
);
shell
.state
.write(&balance_key, Amount::native_whole(1000))
.unwrap();
|_| Ok(Amount::native_whole(1000)),
)
.unwrap();

// create some wrapper txs
for i in 0u64..4 {
Expand Down Expand Up @@ -1147,14 +1150,14 @@ mod test_finalize_block {
// add bertha's gas fees the pool
{
let amt: Amount = 999_999_u64.into();
let pool_balance_key = token::storage_key::balance_key(
&shell.state.in_mem().native_token,
let native_token = shell.state.in_mem().native_token.clone();
update_balance(
&mut shell.state,
&native_token,
&bridge_pool::BRIDGE_POOL_ADDRESS,
);
shell
.state
.write(&pool_balance_key, amt)
.expect("Test failed");
|_| Ok(amt),
)
.expect("Test failed");
}
// write transfer to storage
let transfer = {
Expand Down Expand Up @@ -3109,12 +3112,12 @@ mod test_finalize_block {
initial_balance,
)
.unwrap();
let balance_key = token::storage_key::balance_key(
let balance = read_balance(
&shell.state,
&native_token,
&Address::from(&keypair.to_public()),
);
let balance: Amount =
shell.state.read(&balance_key).unwrap().unwrap_or_default();
)
.unwrap();
assert_eq!(balance, initial_balance);

let mut wrapper =
Expand Down Expand Up @@ -3169,8 +3172,12 @@ mod test_finalize_block {
assert_eq!(*event.kind(), APPLIED_TX);
let code = event.read_attribute::<CodeAttr>().expect("Test failed");
assert_eq!(code, ResultCode::InvalidTx);
let balance: Amount =
shell.state.read(&balance_key).unwrap().unwrap_or_default();
let balance = read_balance(
&shell.state,
&native_token,
&Address::from(&keypair.to_public()),
)
.unwrap();

assert_eq!(balance, 0.into())
}
Expand Down Expand Up @@ -3691,15 +3698,12 @@ mod test_finalize_block {

// Slash pool balance
let nam_address = shell.state.in_mem().native_token.clone();
let slash_balance_key = token::storage_key::balance_key(
let slash_pool_balance_init = read_balance(
&shell.state,
&nam_address,
&namada_proof_of_stake::SLASH_POOL_ADDRESS,
);
let slash_pool_balance_init: token::Amount = shell
.state
.read(&slash_balance_key)
.expect("must be able to read")
.unwrap_or_default();
)
.unwrap();
debug_assert_eq!(slash_pool_balance_init, token::Amount::zero());

let consensus_set: Vec<WeightedValidator> =
Expand Down Expand Up @@ -4848,14 +4852,8 @@ mod test_finalize_block {
// NOTE: assumed that the only change in pos address balance by
// advancing to the next epoch is minted inflation - no change occurs
// due to slashing
let pos_balance_pre = shell
.state
.read::<token::Amount>(&token::storage_key::balance_key(
&staking_token,
&pos_address,
))
.unwrap()
.unwrap_or_default();
let pos_balance_pre =
read_balance(&shell.state, &staking_token, &pos_address).unwrap();
loop {
next_block_for_inflation(
shell,
Expand All @@ -4867,14 +4865,8 @@ mod test_finalize_block {
break;
}
}
let pos_balance_post = shell
.state
.read::<token::Amount>(&token::storage_key::balance_key(
&staking_token,
&pos_address,
))
.unwrap()
.unwrap_or_default();
let pos_balance_post =
read_balance(&shell.state, &staking_token, &pos_address).unwrap();

(
shell.state.in_mem().block.epoch,
Expand Down
9 changes: 0 additions & 9 deletions crates/apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ where
continue;
};

let mut total_token_balance = token::Amount::zero();
for (owner, balance) in balances {
if let genesis::GenesisAddress::PublicKey(pk) = owner {
namada::account::init_account_storage(
Expand All @@ -535,15 +534,7 @@ where
balance.amount(),
)
.expect("Couldn't credit initial balance");
total_token_balance += balance.amount();
}
// Write the total amount of tokens for the ratio
self.state
.write(
&token::storage_key::minted_balance_key(token_address),
total_token_balance,
)
.unwrap();
}
self.proceed_with(())
}
Expand Down
Loading

0 comments on commit e1d6183

Please sign in to comment.