Skip to content

Commit

Permalink
Merge branch 'tomas/refactor-token' (#2493)
Browse files Browse the repository at this point in the history
* tomas/refactor-token:
  changelog: add #2493
  benches: fix the shell to update conversions on new epochs
  move shielded params from core into shielded_token
  move inflation from core to trans_token
  replace namada_state usage with namada_storage in token crates
  • Loading branch information
tzemanovic committed Feb 22, 2024
2 parents eb747c4 + 00cddd4 commit 4ff1b8b
Show file tree
Hide file tree
Showing 27 changed files with 250 additions and 200 deletions.
1 change: 1 addition & 0 deletions .changelog/unreleased/improvements/2493-refactor-token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Refactored token crates. ([\#2493](https://github.com/anoma/namada/pull/2493))
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions crates/apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ impl BenchShell {
current_epoch + params.pipeline_len,
)
.unwrap();

namada::token::conversion::update_allowed_conversions(
&mut self.wl_storage,
)
.unwrap();
}

pub fn init_ibc_client_state(&mut self, addr_key: Key) -> ClientId {
Expand Down Expand Up @@ -913,7 +918,7 @@ impl Client for BenchShell {

impl Default for BenchShieldedCtx {
fn default() -> Self {
let mut shell = BenchShell::default();
let shell = BenchShell::default();
let base_dir = shell.tempdir.as_ref().canonicalize().unwrap();

// Create a global config and an empty wallet in the chain dir - this is
Expand Down Expand Up @@ -983,10 +988,6 @@ impl Default for BenchShieldedCtx {
}

crate::wallet::save(&chain_ctx.wallet).unwrap();
namada::token::conversion::update_allowed_conversions(
&mut shell.wl_storage,
)
.unwrap();

Self {
shielded: ShieldedContext::default(),
Expand Down
5 changes: 3 additions & 2 deletions crates/apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ use namada::governance::pgf::parameters::PgfParameters;
use namada::ledger::eth_bridge::EthereumBridgeParams;
use namada::ledger::parameters::EpochDuration;
use namada::ledger::pos::{Dec, GenesisValidator, OwnedPosParams};
use namada::token;
use namada::types::address::{Address, EstablishedAddress};
use namada::types::chain::ProposalBytes;
use namada::types::key::*;
use namada::types::storage;
use namada::types::string_encoding::StringEncoded;
use namada::types::time::{DateTimeUtc, DurationSecs};
use namada::types::token::Denomination;
use namada::types::{storage, token};
use serde::{Deserialize, Serialize};

#[cfg(all(any(test, feature = "benches"), not(feature = "integration")))]
Expand Down Expand Up @@ -228,7 +229,7 @@ pub struct TokenAccount {
#[derivative(PartialOrd = "ignore", Ord = "ignore")]
pub balances: HashMap<Address, token::Amount>,
/// Token parameters
pub masp_params: Option<token::MaspParams>,
pub masp_params: Option<token::ShieldedParams>,
/// Token inflation from the last epoch (read + write for every epoch)
pub last_inflation: token::Amount,
/// Token shielded ratio from the last epoch (read + write for every epoch)
Expand Down
5 changes: 3 additions & 2 deletions crates/apps/src/lib/config/genesis/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use borsh::{BorshDeserialize, BorshSerialize};
use namada::eth_bridge::storage::parameters::{
Contracts, Erc20WhitelistEntry, MinimumConfirmations,
};
use namada::token;
use namada::types::address::Address;
use namada::types::chain::ProposalBytes;
use namada::types::dec::Dec;
use namada::types::ethereum_structs;
use namada::types::token::{
Amount, DenominatedAmount, Denomination, NATIVE_MAX_DECIMAL_PLACES,
};
use namada::types::{ethereum_structs, token};
use serde::{Deserialize, Serialize};

use super::transactions::{self, Transactions};
Expand Down Expand Up @@ -209,7 +210,7 @@ pub struct Tokens {
)]
pub struct TokenConfig {
pub denom: Denomination,
pub masp_params: Option<token::MaspParams>,
pub masp_params: Option<token::ShieldedParams>,
}

#[derive(
Expand Down
2 changes: 1 addition & 1 deletion crates/apps/src/lib/node/ledger/storage/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ use namada::state::{
BlockStateRead, BlockStateWrite, DBIter, DBWriteBatch, DbError as Error,
DbResult as Result, MerkleTreeStoresRead, StoreType, DB,
};
use namada::token::ConversionState;
use namada::types;
use namada::types::storage::{
BlockHeight, BlockResults, Epoch, EthEventsQueue, Header, Key, KeySeg,
KEY_SEGMENT_SEPARATOR,
};
use namada::types::time::DateTimeUtc;
use namada::types::token::ConversionState;
use namada::types::{ethereum_events, ethereum_structs};
use rayon::prelude::*;
use rocksdb::{
Expand Down
1 change: 0 additions & 1 deletion crates/core/src/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! The ledger modules
pub mod eth_bridge;
pub mod inflation;
pub mod replay_protection;
2 changes: 1 addition & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod hints;
pub mod ledger;
pub mod types;

pub use {ibc, tendermint, tendermint_proto};
pub use {ibc, masp_primitives, tendermint, tendermint_proto};
/// Borsh binary encoding (re-exported) from official crate with custom ext.
pub mod borsh {
pub use borsh::*;
Expand Down
67 changes: 1 addition & 66 deletions crates/core/src/types/token.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! A basic fungible token
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt::Display;
use std::iter::Sum;
use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
Expand All @@ -10,10 +9,6 @@ use std::str::FromStr;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::BASE32HEX_NOPAD;
use ethabi::ethereum_types::U256;
use masp_primitives::asset_type::AssetType;
use masp_primitives::convert::AllowedConversion;
use masp_primitives::merkle_tree::FrozenCommitmentTree;
use masp_primitives::sapling;
use serde::{Deserialize, Serialize};
use thiserror::Error;

Expand All @@ -22,31 +17,9 @@ use crate::types::address::Address;
use crate::types::dec::{Dec, POS_DECIMAL_PRECISION};
use crate::types::hash::Hash;
use crate::types::storage;
use crate::types::storage::{DbKeySeg, Epoch, KeySeg};
use crate::types::storage::{DbKeySeg, KeySeg};
use crate::types::uint::{self, Uint, I256};

/// A representation of the conversion state
#[derive(Debug, Default, BorshSerialize, BorshDeserialize)]
pub struct ConversionState {
/// The last amount of the native token distributed
pub normed_inflation: Option<u128>,
/// The tree currently containing all the conversions
pub tree: FrozenCommitmentTree<sapling::Node>,
/// A map from token alias to actual address.
pub tokens: BTreeMap<String, Address>,
/// Map assets to their latest conversion and position in Merkle tree
#[allow(clippy::type_complexity)]
pub assets: BTreeMap<
AssetType,
(
(Address, Denomination, MaspDigitPos),
Epoch,
AllowedConversion,
usize,
),
>,
}

/// Amount in micro units. For different granularity another representation
/// might be more appropriate.
#[derive(
Expand Down Expand Up @@ -1002,44 +975,6 @@ impl From<DenominatedAmount> for IbcAmount {
}
}

/// Token parameters for each kind of asset held on chain
#[derive(
Clone,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
BorshSerialize,
BorshDeserialize,
BorshSchema,
Deserialize,
Serialize,
)]
pub struct MaspParams {
/// Maximum reward rate
pub max_reward_rate: Dec,
/// Shielded Pool nominal derivative gain
pub kd_gain_nom: Dec,
/// Shielded Pool nominal proportional gain for the given token
pub kp_gain_nom: Dec,
/// Target amount for the given token that is locked in the shielded pool
/// TODO: should this be a Uint or DenominatedAmount???
pub locked_amount_target: u64,
}

impl Default for MaspParams {
fn default() -> Self {
Self {
max_reward_rate: Dec::from_str("0.1").unwrap(),
kp_gain_nom: Dec::from_str("0.25").unwrap(),
kd_gain_nom: Dec::from_str("0.25").unwrap(),
locked_amount_target: 10_000_u64,
}
}
}

/// A simple bilateral token transfer
#[derive(
Debug,
Expand Down
3 changes: 1 addition & 2 deletions crates/proof_of_stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::collections::{HashMap, HashSet};

use namada_core::ledger::inflation;
use namada_core::types::address::{self, Address};
use namada_core::types::dec::Dec;
use namada_core::types::storage::Epoch;
Expand All @@ -19,8 +18,8 @@ use crate::storage::{
rewards_accumulator_handle, validator_commission_rate_handle,
validator_rewards_products_handle, validator_state_handle,
};
use crate::token::credit_tokens;
use crate::token::storage_key::minted_balance_key;
use crate::token::{credit_tokens, inflation};
use crate::types::{into_tm_voting_power, BondId, ValidatorState, VoteInfo};
use crate::{
bond_amounts_for_rewards, get_total_consensus_stake, staking_token_address,
Expand Down
5 changes: 3 additions & 2 deletions crates/shielded_token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ testing = ["multicore", "namada_core/testing"]
[dependencies]
namada_core = { path = "../core" }
namada_parameters = { path = "../parameters" }
namada_state = { path = "../state" }
namada_storage = { path = "../storage" }
namada_trans_token = { path = "../trans_token" }

borsh.workspace = true
masp_primitives.workspace = true
rayon = { workspace = true, optional = true }
serde.workspace = true
tracing.workspace = true

[dev-dependencies]
namada_core = { path = "../core", features = ["testing"] }
namada_state = { path = "../state", features = ["testing"] }
namada_storage = { path = "../storage", features = ["testing"] }

proptest.workspace = true
rayon.workspace = true
Expand Down
Loading

0 comments on commit 4ff1b8b

Please sign in to comment.