Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyizxx committed Oct 21, 2024
1 parent fbbd18d commit 03e8a22
Showing 1 changed file with 20 additions and 38 deletions.
58 changes: 20 additions & 38 deletions contracts/axelarnet-gateway/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ pub fn call_contract(
call_contract: CallContractData,
) -> Result<Response<nexus::execute::Message>> {
let Config {
router,
chain_name,
nexus,
..
} = state::load_config(storage);

let client: nexus::Client = client::CosmosClient::new(querier).into();
Expand All @@ -119,24 +118,7 @@ pub fn call_contract(
token: token.clone(),
};

let res = match determine_routing_destination(
storage,
&info.sender,
&client,
&msg.destination_chain,
)? {
RoutingDestination::Nexus => {
Response::new().add_messages(route_to_nexus(&client, &nexus, msg, token)?)
}
RoutingDestination::Router if token.is_none() => {
let (messages, events) = route_to_router(&Router::new(router), vec![msg])?;
Response::new().add_messages(messages).add_events(events)
}
_ => bail!(Error::InvalidRoutingDestination),
}
.add_event(event.into());

Ok(res)
route_messages(storage, querier, info.sender, vec![msg]).map(|res| res.add_event(event.into()))
}

pub fn route_messages(
Expand Down Expand Up @@ -170,16 +152,21 @@ pub fn route_messages(
.group_by(|msg| msg.destination_chain.to_owned())
.into_iter()
.try_fold(Response::new(), |acc, (dest_chain, msgs)| {
let (messages, events) =
match determine_routing_destination(storage, &sender, &client, &dest_chain)? {
RoutingDestination::This => {
prepare_msgs_for_execution(storage, chain_name.clone(), msgs.collect())
}
RoutingDestination::Nexus => {
route_messages_to_nexus(&client, &nexus, msgs.collect())
}
RoutingDestination::Router => route_to_router(&router, msgs.collect()),
}?;
let (messages, events) = match determine_routing_destination(
&sender,
&client,
&dest_chain,
&router.address,
&chain_name,
)? {
RoutingDestination::This => {
prepare_msgs_for_execution(storage, chain_name.clone(), msgs.collect())
}
RoutingDestination::Nexus => {
route_messages_to_nexus(&client, &nexus, msgs.collect())
}
RoutingDestination::Router => route_to_router(&router, msgs.collect()),
}?;

Ok(acc.add_messages(messages).add_events(events))
})
Expand Down Expand Up @@ -323,24 +310,19 @@ fn unique_cross_chain_id(client: &nexus::Client, chain_name: ChainName) -> Resul

/// Query Nexus module in core to decide should route message to core
fn determine_routing_destination(
storage: &dyn Storage,
sender: &Addr,
client: &nexus::Client,
dest_chain: &ChainName,
router: &Addr,
this_chain: &ChainName,
) -> Result<RoutingDestination> {
let Config {
chain_name: this_chain,
router,
..
} = state::load_config(storage);

if client
.is_chain_registered(dest_chain)
.change_context(Error::Nexus)?
{
RoutingDestination::Nexus
} else if sender == router {
ensure!(*dest_chain == this_chain, Error::InvalidRoutingDestination);
ensure!(dest_chain == this_chain, Error::InvalidRoutingDestination);
RoutingDestination::This
} else {
RoutingDestination::Router
Expand Down

0 comments on commit 03e8a22

Please sign in to comment.