Skip to content

Commit

Permalink
Merge branch 'main' into stacks_ampd
Browse files Browse the repository at this point in the history
  • Loading branch information
raress96 committed Nov 8, 2024
2 parents 1ef87f0 + 08bec3c commit 66f2d90
Show file tree
Hide file tree
Showing 20 changed files with 1,207 additions and 738 deletions.
138 changes: 9 additions & 129 deletions contracts/interchain-token-service/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sol! {
enum MessageType {
InterchainTransfer,
DeployInterchainToken,
DeployTokenManager,
DeployTokenManager, // note, this case is not supported by the ITS hub
SendToHub,
ReceiveFromHub,
}
Expand All @@ -39,13 +39,6 @@ sol! {
bytes minter;
}

struct DeployTokenManager {
uint256 messageType;
bytes32 tokenId;
uint256 tokenManagerType;
bytes params;
}

struct SendToHub {
uint256 messageType;
/// True destination chain name when sending a message from ITS edge source contract -> ITS Hub
Expand Down Expand Up @@ -110,17 +103,6 @@ impl Message {
minter: into_vec(minter).into(),
}
.abi_encode_params(),
Message::DeployTokenManager(primitives::DeployTokenManager {
token_id,
token_manager_type,
params,
}) => DeployTokenManager {
messageType: MessageType::DeployTokenManager.into(),
tokenId: FixedBytes::<32>::new(token_id.into()),
tokenManagerType: token_manager_type.into(),
params: Vec::<u8>::from(params).into(),
}
.abi_encode_params(),
}
.into()
}
Expand Down Expand Up @@ -164,24 +146,6 @@ impl Message {
}
.into()
}
MessageType::DeployTokenManager => {
let decoded = DeployTokenManager::abi_decode_params(payload, true)
.map_err(Error::AbiDecodeFailed)?;

let token_manager_type = u8::try_from(decoded.tokenManagerType)
.change_context(Error::InvalidTokenManagerType)?
.then(TokenManagerType::from_repr)
.ok_or_else(|| Error::InvalidTokenManagerType)?;

primitives::DeployTokenManager {
token_id: TokenId::new(decoded.tokenId.into()),
token_manager_type,
params: Vec::<u8>::from(decoded.params)
.try_into()
.map_err(Error::NonEmpty)?,
}
.into()
}
_ => bail!(Error::InvalidMessageType),
};

Expand Down Expand Up @@ -286,8 +250,8 @@ mod tests {
use router_api::ChainNameRaw;

use super::{DeployInterchainToken, InterchainTransfer};
use crate::abi::{DeployTokenManager, Error, MessageType, SendToHub};
use crate::{primitives, HubMessage, TokenManagerType};
use crate::abi::{Error, MessageType, SendToHub};
use crate::{primitives, HubMessage};

fn from_hex(hex: &str) -> nonempty::HexBinary {
HexBinary::from_hex(hex).unwrap().try_into().unwrap()
Expand Down Expand Up @@ -406,13 +370,6 @@ mod tests {
minter: vec![].into(),
}
.abi_encode_params(),
DeployTokenManager {
messageType: MessageType::DeployTokenManager.into(),
tokenId: FixedBytes::<32>::new([1u8; 32]),
tokenManagerType: TokenManagerType::NativeInterchainToken.into(),
params: vec![].into(),
}
.abi_encode_params(),
];

for message in test_cases {
Expand Down Expand Up @@ -515,70 +472,12 @@ mod tests {
}
}

#[test]
fn deploy_token_manager_encode_decode() {
let remote_chain = ChainNameRaw::from_str("chain").unwrap();

let cases = vec![
HubMessage::SendToHub {
destination_chain: remote_chain.clone(),
message: primitives::DeployTokenManager {
token_id: [0u8; 32].into(),
token_manager_type: TokenManagerType::NativeInterchainToken,
params: from_hex("00"),
}
.into(),
},
HubMessage::SendToHub {
destination_chain: remote_chain.clone(),
message: primitives::DeployTokenManager {
token_id: [1u8; 32].into(),
token_manager_type: TokenManagerType::Gateway,
params: from_hex("1234"),
}
.into(),
},
HubMessage::ReceiveFromHub {
source_chain: remote_chain.clone(),
message: primitives::DeployTokenManager {
token_id: [0u8; 32].into(),
token_manager_type: TokenManagerType::NativeInterchainToken,
params: from_hex("00"),
}
.into(),
},
HubMessage::ReceiveFromHub {
source_chain: remote_chain.clone(),
message: primitives::DeployTokenManager {
token_id: [1u8; 32].into(),
token_manager_type: TokenManagerType::Gateway,
params: from_hex("1234"),
}
.into(),
},
];

let encoded: Vec<_> = cases
.iter()
.map(|original| original.clone().abi_encode().to_hex())
.collect();

goldie::assert_json!(encoded);

for original in cases {
let encoded = original.clone().abi_encode();
let decoded = assert_ok!(HubMessage::abi_decode(&encoded));
assert_eq!(original, decoded);
}
}

#[test]
fn invalid_hub_message_type() {
let invalid_message_types = vec![
u8::MIN,
MessageType::InterchainTransfer as u8,
MessageType::DeployInterchainToken as u8,
MessageType::DeployTokenManager as u8,
MessageType::ReceiveFromHub as u8 + 1,
u8::MAX,
];
Expand All @@ -601,7 +500,6 @@ mod tests {
let invalid_message_types = vec![
MessageType::SendToHub as u8,
MessageType::ReceiveFromHub as u8,
MessageType::DeployTokenManager as u8 + 1,
u8::MAX,
];

Expand All @@ -620,11 +518,13 @@ mod tests {

#[test]
fn invalid_destination_chain() {
let message = DeployTokenManager {
messageType: MessageType::DeployTokenManager.into(),
let message = DeployInterchainToken {
messageType: MessageType::DeployInterchainToken.into(),
tokenId: FixedBytes::<32>::new([0u8; 32]),
tokenManagerType: TokenManagerType::NativeInterchainToken.into(),
params: vec![].into(),
name: "Test Token".into(),
symbol: "TST".into(),
decimals: 18,
minter: vec![].into(),
};

let payload = SendToHub {
Expand All @@ -638,26 +538,6 @@ mod tests {
assert_err_contains!(result, Error, Error::InvalidChainName);
}

#[test]
fn invalid_token_manager_type() {
let message = DeployTokenManager {
messageType: MessageType::DeployTokenManager.into(),
tokenId: FixedBytes::<32>::new([0u8; 32]),
tokenManagerType: U256::from(TokenManagerType::Gateway as u8 + 1),
params: vec![].into(),
};

let payload = SendToHub {
messageType: MessageType::SendToHub.into(),
destination_chain: "chain".into(),
message: message.abi_encode_params().into(),
}
.abi_encode_params();

let result = HubMessage::abi_decode(&payload);
assert_err_contains!(result, Error, Error::InvalidTokenManagerType);
}

#[test]
fn encode_decode_large_data() {
let large_data = vec![0u8; 1024 * 1024]; // 1MB of data
Expand Down
38 changes: 12 additions & 26 deletions contracts/interchain-token-service/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use cosmwasm_std::{Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Respons
use error_stack::{Report, ResultExt};
use execute::{freeze_chain, unfreeze_chain};

use crate::events::Event;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state;
use crate::state::Config;
Expand All @@ -26,10 +25,10 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub enum Error {
#[error("failed to execute a cross-chain message")]
Execute,
#[error("failed to register an its edge contract")]
RegisterItsContract,
#[error("failed to deregsiter an its edge contract")]
DeregisterItsContract,
#[error("failed to register chains")]
RegisterChains,
#[error("failed to update chain")]
UpdateChain,
#[error("failed to freeze chain")]
FreezeChain,
#[error("failed to unfreeze chain")]
Expand Down Expand Up @@ -79,17 +78,9 @@ pub fn instantiate(

state::save_config(deps.storage, &Config { axelarnet_gateway })?;

for (chain, address) in msg.its_contracts.iter() {
state::save_its_contract(deps.storage, chain, address)?;
}

killswitch::init(deps.storage, killswitch::State::Disengaged)?;

Ok(Response::new().add_events(
msg.its_contracts
.into_iter()
.map(|(chain, address)| Event::ItsContractRegistered { chain, address }.into()),
))
Ok(Response::new())
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -106,13 +97,14 @@ pub fn execute(
payload,
}) => execute::execute_message(deps, cc_id, source_address, payload)
.change_context(Error::Execute),
ExecuteMsg::RegisterItsContract { chain, address } => {
execute::register_its_contract(deps, chain, address)
.change_context(Error::RegisterItsContract)
ExecuteMsg::RegisterChains { chains } => {
execute::register_chains(deps, chains).change_context(Error::RegisterChains)
}
ExecuteMsg::DeregisterItsContract { chain } => {
execute::deregister_its_contract(deps, chain)
.change_context(Error::DeregisterItsContract)
ExecuteMsg::UpdateChain {
chain,
its_edge_contract,
} => {
execute::update_chain(deps, chain, its_edge_contract).change_context(Error::UpdateChain)
}
ExecuteMsg::FreezeChain { chain } => {
freeze_chain(deps, chain).change_context(Error::FreezeChain)
Expand All @@ -126,12 +118,6 @@ pub fn execute(
ExecuteMsg::EnableExecution => {
execute::enable_execution(deps).change_context(Error::EnableExecution)
}
ExecuteMsg::SetChainConfig {
chain,
max_uint,
max_target_decimals,
} => execute::set_chain_config(deps, chain, max_uint, max_target_decimals)
.change_context(Error::SetChainConfig),
}?
.then(Ok)
}
Expand Down
Loading

0 comments on commit 66f2d90

Please sign in to comment.