Skip to content

Commit

Permalink
feat(minor-interchain-token-service): fix scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcobb23 committed Nov 11, 2024
1 parent 19fa978 commit c935358
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ fn destination_token_decimals(
state::load_chain_config(storage, destination_chain).change_context(Error::State)?;

if source_chain_config
.truncation
.max_uint
.le(&destination_chain_config.max_uint)
.le(&destination_chain_config.truncation.max_uint)
{
source_chain_decimals
} else {
source_chain_config
.max_target_decimals
destination_chain_config
.truncation
.max_decimals_when_truncating
.min(source_chain_decimals)
}
.then(Result::Ok)
Expand Down Expand Up @@ -244,6 +246,7 @@ fn destination_amount(

let destination_max_uint = state::load_chain_config(storage, destination_chain)
.change_context(Error::State)?
.truncation
.max_uint;

// It's intentionally written in this way since the end result may still be fine even if
Expand Down Expand Up @@ -311,6 +314,7 @@ mod test {

use super::Error;
use crate::contract::execute::interceptors;
use crate::msg::TruncationConfig;
use crate::state::{self, TokenDeploymentType};
use crate::{msg, DeployInterchainToken, InterchainTransfer, TokenInstance};

Expand Down Expand Up @@ -347,8 +351,10 @@ mod test {
msg::ChainConfig {
chain: destination_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(1_000_000_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(1_000_000_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand Down Expand Up @@ -398,8 +404,10 @@ mod test {
msg::ChainConfig {
chain: destination_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand Down Expand Up @@ -449,8 +457,10 @@ mod test {
msg::ChainConfig {
chain: destination_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand Down Expand Up @@ -500,8 +510,10 @@ mod test {
msg::ChainConfig {
chain: destination_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(100_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(100_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand Down Expand Up @@ -551,8 +563,10 @@ mod test {
msg::ChainConfig {
chain: destination_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(100_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(100_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand Down Expand Up @@ -588,8 +602,10 @@ mod test {
msg::ChainConfig {
chain: source_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand All @@ -599,8 +615,10 @@ mod test {
msg::ChainConfig {
chain: destination_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(1_000_000_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(1_000_000_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand Down Expand Up @@ -648,8 +666,10 @@ mod test {
msg::ChainConfig {
chain: source_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(1_000_000_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(1_000_000_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand All @@ -659,8 +679,10 @@ mod test {
msg::ChainConfig {
chain: destination_chain.clone(),
its_edge_contract: "itsedgecontract".to_string().try_into().unwrap(),
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_target_decimals: 6,
truncation_config: TruncationConfig {
max_uint: Uint256::from(1_000_000_000_000_000u128).try_into().unwrap(),
max_decimals_when_truncating: 6,
},
},
)
.unwrap();
Expand Down
31 changes: 21 additions & 10 deletions contracts/interchain-token-service/src/contract/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ mod tests {
disable_execution, enable_execution, execute_message, freeze_chain, register_chain,
register_chains, unfreeze_chain, update_chain, Error,
};
use crate::msg::TruncationConfig;
use crate::state::{self, Config};
use crate::{msg, DeployInterchainToken, HubMessage, InterchainTransfer};

Expand Down Expand Up @@ -536,8 +537,10 @@ mod tests {
msg::ChainConfig {
chain: SOLANA.parse().unwrap(),
its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::one().try_into().unwrap(),
max_target_decimals: 16u8
truncation_config: TruncationConfig {
max_uint: Uint256::one().try_into().unwrap(),
max_decimals_when_truncating: 16u8
}
}
));
assert_err_contains!(
Expand All @@ -546,8 +549,10 @@ mod tests {
msg::ChainConfig {
chain: SOLANA.parse().unwrap(),
its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::one().try_into().unwrap(),
max_target_decimals: 16u8
truncation_config: TruncationConfig {
max_uint: Uint256::one().try_into().unwrap(),
max_decimals_when_truncating: 16u8
}
}
),
Error,
Expand All @@ -562,14 +567,18 @@ mod tests {
msg::ChainConfig {
chain: SOLANA.parse().unwrap(),
its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::MAX.try_into().unwrap(),
max_target_decimals: 16u8,
truncation_config: TruncationConfig {
max_uint: Uint256::MAX.try_into().unwrap(),
max_decimals_when_truncating: 16u8,
},
},
msg::ChainConfig {
chain: XRPL.parse().unwrap(),
its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::MAX.try_into().unwrap(),
max_target_decimals: 16u8,
truncation_config: TruncationConfig {
max_uint: Uint256::MAX.try_into().unwrap(),
max_decimals_when_truncating: 16u8,
},
},
];
assert_ok!(register_chains(deps.as_mut(), chains[0..1].to_vec()));
Expand Down Expand Up @@ -623,8 +632,10 @@ mod tests {
msg::ChainConfig {
chain: chain.clone(),
its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::one().try_into().unwrap(),
max_target_decimals: 16u8
truncation_config: TruncationConfig {
max_uint: Uint256::one().try_into().unwrap(),
max_decimals_when_truncating: 16u8
}
}
));
}
Expand Down
7 changes: 6 additions & 1 deletion contracts/interchain-token-service/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ pub enum ExecuteMsg {
pub struct ChainConfig {
pub chain: ChainNameRaw,
pub its_edge_contract: Address,
pub truncation_config: TruncationConfig,
}

#[cw_serde]
pub struct TruncationConfig {
pub max_uint: nonempty::Uint256, // The maximum uint value that is supported by the chain's token standard
pub max_target_decimals: u8, // The maximum number of decimals that is preserved when deploying a token to another chain where smaller uint values are used
pub max_decimals_when_truncating: u8, // The maximum number of decimals that is preserved when deploying from a chain with a larger max_uint
}

#[cw_serde]
Expand Down
27 changes: 19 additions & 8 deletions contracts/interchain-token-service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,24 @@ pub struct Config {

#[cw_serde]
pub struct ChainConfig {
pub max_uint: nonempty::Uint256,
pub max_target_decimals: u8,
pub truncation: TruncationConfig,
pub its_address: Address,
frozen: bool,
}

#[cw_serde]
pub struct TruncationConfig {
pub max_uint: nonempty::Uint256,
pub max_decimals_when_truncating: u8,
}

impl From<msg::ChainConfig> for ChainConfig {
fn from(value: msg::ChainConfig) -> Self {
Self {
max_uint: value.max_uint,
max_target_decimals: value.max_target_decimals,
truncation: TruncationConfig {
max_uint: value.truncation_config.max_uint,
max_decimals_when_truncating: value.truncation_config.max_decimals_when_truncating,
},
its_address: value.its_edge_contract,
frozen: false,
}
Expand Down Expand Up @@ -326,8 +333,10 @@ mod tests {
msg::ChainConfig {
chain: chain1.clone(),
its_edge_contract: address1.clone(),
max_uint: Uint256::MAX.try_into().unwrap(),
max_target_decimals: 16u8
truncation_config: msg::TruncationConfig {
max_uint: Uint256::MAX.try_into().unwrap(),
max_decimals_when_truncating: 16u8
}
}
));
assert_ok!(save_chain_config(
Expand All @@ -336,8 +345,10 @@ mod tests {
msg::ChainConfig {
chain: chain2.clone(),
its_edge_contract: address2.clone(),
max_uint: Uint256::MAX.try_into().unwrap(),
max_target_decimals: 16u8
truncation_config: msg::TruncationConfig {
max_uint: Uint256::MAX.try_into().unwrap(),
max_decimals_when_truncating: 16u8
}
}
));
assert_eq!(
Expand Down
14 changes: 9 additions & 5 deletions contracts/interchain-token-service/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{HexBinary, Uint256};
use interchain_token_service::contract::{self, ExecuteError};
use interchain_token_service::events::Event;
use interchain_token_service::msg::{self, ExecuteMsg};
use interchain_token_service::msg::{self, ExecuteMsg, TruncationConfig};
use interchain_token_service::{
DeployInterchainToken, HubMessage, InterchainTransfer, TokenId, TokenSupply,
};
Expand Down Expand Up @@ -111,8 +111,10 @@ fn register_multiple_chains_succeeds() {
.map(|i| msg::ChainConfig {
chain: i.to_string().parse().unwrap(),
its_edge_contract: i.to_string().parse().unwrap(),
max_target_decimals: 18u8,
max_uint: Uint256::MAX.try_into().unwrap(),
truncation_config: TruncationConfig {
max_decimals_when_truncating: 18u8,
max_uint: Uint256::MAX.try_into().unwrap(),
},
})
.collect();
assert_ok!(register_chains(deps.as_mut(), chains.clone()));
Expand All @@ -134,8 +136,10 @@ fn register_multiple_chains_fails_if_one_invalid() {
.map(|i| msg::ChainConfig {
chain: i.to_string().parse().unwrap(),
its_edge_contract: i.to_string().parse().unwrap(),
max_target_decimals: 18u8,
max_uint: Uint256::MAX.try_into().unwrap(),
truncation_config: TruncationConfig {
max_decimals_when_truncating: 18u8,
max_uint: Uint256::MAX.try_into().unwrap(),
},
})
.collect();
assert_ok!(register_chains(deps.as_mut(), chains[0..1].to_vec()));
Expand Down
10 changes: 6 additions & 4 deletions contracts/interchain-token-service/tests/utils/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cosmwasm_std::{
from_json, to_json_binary, Addr, DepsMut, HexBinary, MemoryStorage, OwnedDeps, Response,
Uint256, WasmQuery,
};
use interchain_token_service::msg::{self, ExecuteMsg};
use interchain_token_service::msg::{self, ExecuteMsg, TruncationConfig};
use interchain_token_service::{contract, HubMessage};
use router_api::{Address, ChainName, ChainNameRaw, CrossChainId};

Expand Down Expand Up @@ -88,15 +88,17 @@ pub fn register_chain(
chain: ChainNameRaw,
its_edge_contract: Address,
max_uint: nonempty::Uint256,
max_target_decimals: u8,
max_decimals_when_truncating: u8,
) -> Result<Response, ContractError> {
register_chains(
deps,
vec![msg::ChainConfig {
chain,
its_edge_contract,
max_uint,
max_target_decimals,
truncation_config: TruncationConfig {
max_uint,
max_decimals_when_truncating,
},
}],
)
}
Expand Down

0 comments on commit c935358

Please sign in to comment.