Skip to content

Commit

Permalink
add ibc params
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Feb 21, 2024
1 parent f05e3a2 commit ab508c0
Show file tree
Hide file tree
Showing 14 changed files with 438 additions and 146 deletions.
14 changes: 14 additions & 0 deletions crates/apps/src/lib/config/genesis/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,17 @@ impl Finalized {
}
}

pub fn get_ibc_params(&self) -> namada::ibc::parameters::IbcParameters {
let templates::IbcParams {
default_mint_limit,
default_per_epoch_throughput_limit,
} = self.parameters.ibc_params.clone();
namada::ibc::parameters::IbcParameters {
default_mint_limit,
default_per_epoch_throughput_limit,
}
}

pub fn get_token_address(&self, alias: &Alias) -> Option<&Address> {
self.tokens.token.get(alias).map(|token| &token.address)
}
Expand Down Expand Up @@ -687,6 +698,7 @@ pub struct FinalizedParameters {
pub gov_params: templates::GovernanceParams,
pub pgf_params: namada::governance::pgf::parameters::PgfParameters,
pub eth_bridge_params: Option<templates::EthBridgeParams>,
pub ibc_params: templates::IbcParams,
}

impl FinalizedParameters {
Expand All @@ -697,6 +709,7 @@ impl FinalizedParameters {
gov_params,
pgf_params,
eth_bridge_params,
ibc_params,
}: templates::Parameters<Validated>,
) -> Self {
use namada::governance::pgf::parameters::PgfParameters;
Expand All @@ -711,6 +724,7 @@ impl FinalizedParameters {
gov_params,
pgf_params: finalized_pgf_params,
eth_bridge_params,
ibc_params,
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions crates/apps/src/lib/config/genesis/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub struct Parameters<T: TemplateValidation> {
pub gov_params: GovernanceParams,
pub pgf_params: PgfParams<T>,
pub eth_bridge_params: Option<EthBridgeParams>,
pub ibc_params: IbcParams,
}

#[derive(
Expand Down Expand Up @@ -485,6 +486,23 @@ pub struct EthBridgeParams {
pub contracts: Contracts,
}

#[derive(
Clone,
Debug,
Deserialize,
Serialize,
BorshDeserialize,
BorshSerialize,
PartialEq,
Eq,
)]
pub struct IbcParams {
/// Default supply limit of each token
pub default_mint_limit: token::Amount,
/// Default per-epoch throughput limit of each token
pub default_per_epoch_throughput_limit: token::Amount,
}

impl TokenBalances {
pub fn get(&self, addr: &GenesisAddress) -> Option<token::Amount> {
self.0.get(addr).map(|amt| amt.amount())
Expand Down Expand Up @@ -856,6 +874,7 @@ pub fn validate_parameters(
gov_params,
pgf_params,
eth_bridge_params,
ibc_params,
} = parameters;
match parameters.denominate(tokens) {
Err(e) => {
Expand All @@ -873,6 +892,7 @@ pub fn validate_parameters(
valid: Default::default(),
},
eth_bridge_params,
ibc_params,
}),
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use masp_primitives::sapling::Node;
use namada::governance::pgf::inflation as pgf_inflation;
use namada::ledger::events::EventType;
use namada::ledger::gas::{GasMetering, TxGasMeter};
use namada::ledger::ibc;
use namada::ledger::pos::namada_proof_of_stake;
use namada::ledger::protocol::{self, WrapperArgs};
use namada::proof_of_stake::storage::{
Expand Down Expand Up @@ -111,6 +112,8 @@ where
&mut self.wl_storage,
current_epoch,
)?;

ibc::clear_throughputs(&mut self.wl_storage)?;
}

// Get the actual votes from cometBFT in the preferred format
Expand Down
4 changes: 4 additions & 0 deletions crates/apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ where
.unwrap();
}

// Initialize IBC parameters
let ibc_params = genesis.get_ibc_params();
ibc_params.init_storage(&mut self.wl_storage).unwrap();

// Depends on parameters being initialized
self.wl_storage
.storage
Expand Down
44 changes: 4 additions & 40 deletions crates/ibc/src/context/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,54 +725,21 @@ pub trait IbcCommonContext: IbcStorageContext {
Ok(amount == Some(Amount::from_u64(1)))
}

/// Read the mint limit of the given token
fn mint_limit(&self, token: &Address) -> Result<Amount> {
let key = storage::mint_limit_key(token).map_err(|e| {
ChannelError::Other {
description: format!(
"Getting the deposit limit key failed: {e}"
),
}
})?;
Ok(self.read::<Amount>(&key)?.unwrap_or_default())
}

/// Read the per-epoch throughput limit of the given token
fn throughput_limit(&self, token: &Address) -> Result<Amount> {
let key = storage::throughput_limit_key(token).map_err(|e| {
ChannelError::Other {
description: format!(
"Getting the throughput limit key failed: {e}"
),
}
})?;
Ok(self.read::<Amount>(&key)?.unwrap_or_default())
}

/// Read the per-epoch deposit of the given token
fn deposit(&self, token: &Address) -> Result<Amount> {
let key =
storage::deposit_key(token).map_err(|e| ChannelError::Other {
description: format!("Getting the deposit key failed: {e}"),
})?;
let key = storage::deposit_key(token);
Ok(self.read::<Amount>(&key)?.unwrap_or_default())
}

/// Write the per-epoch deposit of the given token
fn store_deposit(&mut self, token: &Address, amount: Amount) -> Result<()> {
let key =
storage::deposit_key(token).map_err(|e| ChannelError::Other {
description: format!("Getting the deposit key failed: {e}"),
})?;
let key = storage::deposit_key(token);
self.write(&key, amount).map_err(ContextError::from)
}

/// Read the per-epoch withdraw of the given token
fn withdraw(&self, token: &Address) -> Result<Amount> {
let key =
storage::withdraw_key(token).map_err(|e| ChannelError::Other {
description: format!("Getting the withdraw key failed: {e}"),
})?;
let key = storage::withdraw_key(token);
Ok(self.read::<Amount>(&key)?.unwrap_or_default())
}

Expand All @@ -782,10 +749,7 @@ pub trait IbcCommonContext: IbcStorageContext {
token: &Address,
amount: Amount,
) -> Result<()> {
let key =
storage::withdraw_key(token).map_err(|e| ChannelError::Other {
description: format!("Getting the withdraw key failed: {e}"),
})?;
let key = storage::withdraw_key(token);
self.write(&key, amount).map_err(ContextError::from)
}
}
1 change: 1 addition & 0 deletions crates/ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod actions;
pub mod context;
pub mod parameters;
pub mod storage;

use std::cell::RefCell;
Expand Down
34 changes: 34 additions & 0 deletions crates/ibc/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! IBC system parameters

use namada_core::borsh::{BorshDeserialize, BorshSerialize};
use namada_core::types::token::Amount;
use namada_state::{StorageRead, StorageResult, StorageWrite};

#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
/// Governance parameter structure
pub struct IbcParameters {
/// Default supply limit of each token
pub default_mint_limit: Amount,
/// Default per-epoch throughput limit of each token
pub default_per_epoch_throughput_limit: Amount,
}

impl Default for IbcParameters {
fn default() -> Self {
Self {
default_mint_limit: Amount::zero(),
default_per_epoch_throughput_limit: Amount::zero(),
}
}
}

impl IbcParameters {
/// Initialize IBC parameters into storage
pub fn init_storage<S>(&self, storage: &mut S) -> StorageResult<()>
where
S: StorageRead + StorageWrite,
{
let key = crate::storage::params_key();
storage.write(&key, self)
}
}
112 changes: 46 additions & 66 deletions crates/ibc/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const COUNTER_SEG: &str = "counter";
const TRACE: &str = "ibc_trace";
const NFT_CLASS: &str = "nft_class";
const NFT_METADATA: &str = "nft_meta";
const PARAMS: &str = "params";
const MINT_LIMIT: &str = "mint_limit";
const THROUGHPUT_LIMIT: &str = "throughput_limit";
const DEPOSIT: &str = "deposit";
Expand All @@ -41,8 +42,6 @@ pub enum Error {
InvalidKey(String),
#[error("Port capability error: {0}")]
InvalidPortCapability(String),
#[error("Invalid address error: {0}")]
Address(String),
}

/// IBC storage functions result
Expand Down Expand Up @@ -490,78 +489,59 @@ pub fn is_ibc_counter_key(key: &Key) -> bool {
)
}

/// Returns a key of IBC parameters
pub fn params_key() -> Key {
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&PARAMS.to_string().to_db_key())
.expect("Cannot obtain a storage key")
}

/// Returns a key of the deposit limit for the token
pub fn mint_limit_key(token: &Address) -> Result<Key> {
let hash = match token {
Address::Internal(InternalAddress::IbcToken(hash)) => hash,
_ => {
return Err(Error::Address(format!(
"token is not an IbcToken: {token}"
)));
}
};
Ok(
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&MINT_LIMIT.to_string().to_db_key())
.expect("Cannot obtain a storage key")
.push(&hash.to_string().to_db_key())
.expect("Cannot obtain a storage key"),
)
pub fn mint_limit_key(token: &Address) -> Key {
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&MINT_LIMIT.to_string().to_db_key())
.expect("Cannot obtain a storage key")
// Set as String to avoid checking the token address
.push(&token.to_string().to_db_key())
.expect("Cannot obtain a storage key")
}

/// Returns a key of the per-epoch throughput limit for the token
pub fn throughput_limit_key(token: &Address) -> Result<Key> {
let hash = match token {
Address::Internal(InternalAddress::IbcToken(hash)) => hash,
_ => {
return Err(Error::Address(format!(
"token is not an IbcToken: {token}"
)));
}
};
Ok(
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&THROUGHPUT_LIMIT.to_string().to_db_key())
.expect("Cannot obtain a storage key")
.push(&hash.to_string().to_db_key())
.expect("Cannot obtain a storage key"),
)
pub fn throughput_limit_key(token: &Address) -> Key {
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&THROUGHPUT_LIMIT.to_string().to_db_key())
.expect("Cannot obtain a storage key")
// Set as String to avoid checking the token address
.push(&token.to_string().to_db_key())
.expect("Cannot obtain a storage key")
}

/// Returns a prefix of the per-epoch deposit
pub fn deposit_prefix() -> Key {
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&DEPOSIT.to_string().to_db_key())
.expect("Cannot obtain a storage key")
}

/// Returns a key of the per-epoch deposit for the token
pub fn deposit_key(token: &Address) -> Result<Key> {
let hash = match token {
Address::Internal(InternalAddress::IbcToken(hash)) => hash,
_ => {
return Err(Error::Address(format!(
"token is not an IbcToken: {token}"
)));
}
};
Ok(
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&DEPOSIT.to_string().to_db_key())
.expect("Cannot obtain a storage key")
.push(&hash.to_string().to_db_key())
.expect("Cannot obtain a storage key"),
)
pub fn deposit_key(token: &Address) -> Key {
deposit_prefix()
// Set as String to avoid checking the token address
.push(&token.to_string().to_db_key())
.expect("Cannot obtain a storage key")
}

/// Returns a prefix of the per-epoch withdraw
pub fn withdraw_prefix() -> Key {
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&WITHDRAW.to_string().to_db_key())
.expect("Cannot obtain a storage key")
}

/// Returns a key of the per-epoch withdraw for the token
pub fn withdraw_key(token: &Address) -> Result<Key> {
let hash = match token {
Address::Internal(InternalAddress::IbcToken(hash)) => hash,
_ => {
return Err(Error::Address(format!(
"token is not an IbcToken: {token}"
)));
}
};
Ok(
Key::from(Address::Internal(InternalAddress::Ibc).to_db_key())
.push(&WITHDRAW.to_string().to_db_key())
.expect("Cannot obtain a storage key")
.push(&hash.to_string().to_db_key())
.expect("Cannot obtain a storage key"),
)
pub fn withdraw_key(token: &Address) -> Key {
withdraw_prefix()
// Set as String to avoid checking the token address
.push(&token.to_string().to_db_key())
.expect("Cannot obtain a storage key")
}
Loading

0 comments on commit ab508c0

Please sign in to comment.