Skip to content

Commit

Permalink
fix(minor-interchain-token-service): skip invariant checks for p2p
Browse files Browse the repository at this point in the history
* ITS tokens deployed between core chains are deployed in p2p mode,
and the hub has no awareness of their deployment. Therefore, we don't
track balances for the tokens when the source or destination chain is
a core chain, and we don't check whether the token is deployed.
  • Loading branch information
cjcobb23 committed Nov 12, 2024
1 parent 421b1ea commit 10bed31
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 10 deletions.
44 changes: 35 additions & 9 deletions contracts/interchain-token-service/src/contract/execute/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axelar_core_std::nexus;
use axelar_wasm_std::{killswitch, nonempty, FnExt, IntoContractError};
use cosmwasm_std::{DepsMut, HexBinary, QuerierWrapper, Response, Storage};
use error_stack::{bail, ensure, report, Result, ResultExt};
Expand Down Expand Up @@ -72,6 +73,8 @@ pub enum Error {
token_id: TokenId,
chain: ChainNameRaw,
},
#[error("error querying nexus module for chain registration for chain {0}")]
Nexus(ChainNameRaw),
}

/// Executes an incoming ITS message.
Expand Down Expand Up @@ -111,6 +114,7 @@ fn execute_message_on_hub(

let message = apply_to_hub(
deps.storage,
deps.querier,
cc_id.source_chain.clone(),
destination_chain.clone(),
message,
Expand Down Expand Up @@ -141,6 +145,7 @@ fn execute_message_on_hub(

fn apply_to_hub(
storage: &mut dyn Storage,
querier: QuerierWrapper,
source_chain: ChainNameRaw,
destination_chain: ChainNameRaw,
message: Message,
Expand All @@ -150,7 +155,7 @@ fn apply_to_hub(

match message {
Message::InterchainTransfer(transfer) => {
apply_to_transfer(storage, source_chain, destination_chain, transfer)
apply_to_transfer(storage, querier, source_chain, destination_chain, transfer)
.map(Message::InterchainTransfer)?
}
Message::DeployInterchainToken(deploy_token) => {
Expand All @@ -163,18 +168,39 @@ fn apply_to_hub(

fn apply_to_transfer(
storage: &mut dyn Storage,
querier: QuerierWrapper,
source_chain: ChainNameRaw,
destination_chain: ChainNameRaw,
transfer: InterchainTransfer,
) -> Result<InterchainTransfer, Error> {
interceptors::subtract_supply_amount(storage, &source_chain, &transfer)?;
let transfer = interceptors::apply_scaling_factor_to_amount(
storage,
&source_chain,
&destination_chain,
transfer,
)?;
interceptors::add_supply_amount(storage, &destination_chain, &transfer)?;
// we only do balance tracking for amplifier chains
let client: nexus::Client = client::CosmosClient::new(querier).into();
let source_is_amplifier_chain = !client
.is_chain_registered(&source_chain.normalize())
.change_context(Error::Nexus(source_chain.clone()))?;
let destination_is_amplifier_chain = !client
.is_chain_registered(&destination_chain.normalize())
.change_context(Error::Nexus(destination_chain.clone()))?;

if source_is_amplifier_chain {
interceptors::subtract_supply_amount(storage, &source_chain, &transfer)?;
}

// we only possibly scale if both source chain and destination chain are amplifier chains
let transfer = if source_is_amplifier_chain && destination_is_amplifier_chain {
interceptors::apply_scaling_factor_to_amount(
storage,
&source_chain,
&destination_chain,
transfer,
)?
} else {
transfer
};

if destination_is_amplifier_chain {
interceptors::add_supply_amount(storage, &destination_chain, &transfer)?;
}

Check warning on line 203 in contracts/interchain-token-service/src/contract/execute/mod.rs

View check run for this annotation

Codecov / codecov/patch

contracts/interchain-token-service/src/contract/execute/mod.rs#L203

Added line #L203 was not covered by tests

Ok(transfer)
}
Expand Down
59 changes: 59 additions & 0 deletions contracts/interchain-token-service/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use interchain_token_service::{
};
use router_api::{Address, ChainName, ChainNameRaw, CrossChainId};
use serde_json::json;
use utils::params::{CORE_CHAIN, CORE_ITS_CONTRACT};
use utils::{params, register_chains, TestMessage};

mod utils;
Expand Down Expand Up @@ -950,6 +951,64 @@ fn deploy_interchain_token_tracks_supply() {
);
}

#[test]
fn transfer_interchain_token_from_core_does_not_check_balance_or_deployment() {
let (
mut deps,
TestMessage {
router_message,
source_its_chain: _,
source_its_contract,
destination_its_chain,
destination_its_contract: _,
hub_message,
},
) = utils::setup();

let token_id = hub_message.token_id();
let amount = nonempty::Uint256::try_from(400u64).unwrap();

// this deploys the token from source_its_chain to destination_its_chain
assert_ok!(utils::execute_hub_message(
deps.as_mut(),
router_message.cc_id.clone(),
source_its_contract.clone(),
hub_message,
));

let msg = HubMessage::SendToHub {
destination_chain: destination_its_chain.clone(),
message: InterchainTransfer {
token_id,
source_address: HexBinary::from([1; 32]).try_into().unwrap(),
destination_address: HexBinary::from([2; 32]).try_into().unwrap(),
amount,
data: None,
}
.into(),
};
assert_ok!(utils::execute_hub_message(
deps.as_mut(),
CrossChainId {
source_chain: CORE_CHAIN.parse().unwrap(),
message_id: router_message.cc_id.message_id.clone()
},
CORE_ITS_CONTRACT.parse().unwrap(),
msg,
));

assert_eq!(
assert_ok!(utils::query_token_instance(
deps.as_ref(),
destination_its_chain.clone(),
token_id
))
.unwrap()
.supply,
TokenSupply::Tracked(amount.into())
);
}

#[test]
fn deploy_interchain_token_with_minter_does_not_track_supply() {
let (
Expand Down
12 changes: 11 additions & 1 deletion contracts/interchain-token-service/tests/utils/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use interchain_token_service::msg::{self, ExecuteMsg, TruncationConfig};
use interchain_token_service::{contract, HubMessage};
use router_api::{Address, ChainName, ChainNameRaw, CrossChainId};

use super::params::{CORE_CHAIN, CORE_ITS_CONTRACT};
use super::{instantiate_contract, TestMessage};
use crate::utils::params;

Expand Down Expand Up @@ -70,7 +71,7 @@ pub fn make_deps() -> OwnedDeps<MemoryStorage, MockApi, MockQuerier<AxelarQueryM
AxelarQueryMsg::Nexus(nexus::query::QueryMsg::IsChainRegistered { chain }) => {
Ok(to_json_binary(
&(IsChainRegisteredResponse {
is_registered: chain == "ethereum",
is_registered: chain == CORE_CHAIN,
}),
)
.into())
Expand Down Expand Up @@ -204,5 +205,14 @@ pub fn setup() -> (
)
.unwrap();

register_chain(
deps.as_mut(),
CORE_CHAIN.parse().unwrap(),
CORE_ITS_CONTRACT.parse().unwrap(),
Uint256::MAX.try_into().unwrap(),
u8::MAX,
)
.unwrap();

(deps, TestMessage::dummy())
}
2 changes: 2 additions & 0 deletions contracts/interchain-token-service/tests/utils/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub const ROUTER: &str = "router";
pub const GATEWAY: &str = "gateway";
pub const GOVERNANCE: &str = "governance";
pub const ADMIN: &str = "admin";
pub const CORE_CHAIN: &str = "core-ethereum";
pub const CORE_ITS_CONTRACT: &str = "core-its-contract";

0 comments on commit 10bed31

Please sign in to comment.