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

Functions to update token balance and supply #3029

Merged
merged 7 commits into from
May 9, 2024
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
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::proof_of_stake::{unjail_validator, ADDRESS as pos_address};
use namada::replay_protection;
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::{Authorization, Code, Data};
use namada::vote_ext::ethereum_events;
Expand Down Expand Up @@ -842,14 +845,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 @@ -1143,14 +1146,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 @@ -3117,12 +3120,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 @@ -3178,8 +3181,12 @@ mod test_finalize_block {
assert_eq!(event.event_type.to_string(), String::from("applied"));
let code = event.attributes.get("code").expect("Test failed").as_str();
assert_eq!(code, String::from(ResultCode::InvalidTx).as_str());
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 @@ -3701,15 +3708,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 @@ -4858,14 +4862,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 @@ -4877,14 +4875,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
Loading