Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(minor-axelarnet-gateway): route messages from nexus to router #659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions contracts/axelarnet-gateway/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use axelar_wasm_std::error::ContractError;
use axelar_wasm_std::{address, FnExt, IntoContractError};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response};
use error_stack::ResultExt;
use cosmwasm_std::{
to_json_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, Storage,
};
use error_stack::{Report, ResultExt};

use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{self, Config};
Expand Down Expand Up @@ -70,7 +72,7 @@ pub fn execute(
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response<nexus::execute::Message>, ContractError> {
match msg.ensure_permissions(deps.storage, &info.sender)? {
match msg.ensure_permissions(deps.storage, &info.sender, match_nexus)? {
ExecuteMsg::CallContract {
destination_chain,
destination_address,
Expand All @@ -91,6 +93,9 @@ pub fn execute(
ExecuteMsg::Execute { cc_id, payload } => {
execute::execute(deps, cc_id, payload).change_context(Error::Execute)
}
ExecuteMsg::RouteMessagesFromNexus(msgs) => {
Ok(execute::route_messages_from_nexus(deps.storage, msgs)?)
}
}?
.then(Ok)
}
Expand All @@ -111,6 +116,10 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractErr
.then(Ok)
}

fn match_nexus(storage: &dyn Storage, _: &ExecuteMsg) -> Result<Addr, Report<Error>> {
Ok(state::load_config(storage).nexus)
}

#[cfg(test)]
mod tests {
use assert_ok::assert_ok;
Expand Down
17 changes: 17 additions & 0 deletions contracts/axelarnet-gateway/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum Error {
InvalidToken,
#[error("invalid routing destination")]
InvalidRoutingDestination,
#[error("failed to convert the nexus message for the router")]
InvalidNexusMessageForRouter,
}

#[cw_serde]
Expand Down Expand Up @@ -173,6 +175,21 @@ pub fn execute(
.then(Ok)
}

pub fn route_messages_from_nexus(
haiyizxx marked this conversation as resolved.
Show resolved Hide resolved
storage: &dyn Storage,
msgs: Vec<nexus::execute::Message>,
) -> Result<Response<nexus::execute::Message>> {
let msgs: Vec<_> = msgs
.into_iter()
.map(router_api::Message::try_from)
.collect::<error_stack::Result<Vec<_>, _>>()
.change_context(Error::InvalidNexusMessageForRouter)?;

let router = Router::new(state::load_config(storage).router);

Ok(Response::new().add_messages(router.route(msgs)))
}

fn ensure_same_payload_hash(
payload_hash: &[u8; 32],
) -> impl FnOnce(&Message) -> core::result::Result<(), state::Error> + '_ {
Expand Down
5 changes: 5 additions & 0 deletions contracts/axelarnet-gateway/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axelar_core_std::nexus;
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::HexBinary;
use msgs_derive::EnsurePermissions;
Expand Down Expand Up @@ -41,6 +42,10 @@ pub enum ExecuteMsg {
destination_address: Address,
payload: HexBinary,
},

/// Forward the given nexus messages to the next step of the routing layer.
#[permission(Specific(nexus))]
RouteMessagesFromNexus(Vec<nexus::execute::Message>),
}

#[cw_serde]
Expand Down
18 changes: 18 additions & 0 deletions contracts/axelarnet-gateway/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,21 @@ fn contract_call_with_token_to_amplifier_chains_fails() {
ExecuteError::InvalidRoutingDestination,
);
}

#[test]
fn route_from_nexus_to_router() {
let mut deps = mock_dependencies();

utils::instantiate_contract(deps.as_mut()).unwrap();

let response = assert_ok!(utils::route_from_nexus(
deps.as_mut(),
vec![
messages::dummy_from_nexus(&vec![1, 2, 3]),
messages::dummy_from_nexus(&vec![4, 5, 6]),
]
));

let msg: RouterExecuteMsg = assert_ok!(inspect_response_msg(response));
goldie::assert_json!(msg)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"route_messages": [
{
"cc_id": {
"source_chain": "Ethereum",
"message_id": "source-chain-0"
},
"source_address": "source-address",
"destination_chain": "destination-chain",
"destination_address": "destination-address",
"payload_hash": "f1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239"
},
{
"cc_id": {
"source_chain": "Ethereum",
"message_id": "source-chain-0"
},
"source_address": "source-address",
"destination_chain": "destination-chain",
"destination_address": "destination-address",
"payload_hash": "2e6a5ebccc8810fee3324c0d445001188787d0d3ac8aba2cd89b77527d198bb0"
}
]
}
12 changes: 12 additions & 0 deletions contracts/axelarnet-gateway/tests/utils/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ pub fn route_to_router(
GatewayExecuteMsg::RouteMessages(msgs),
)
}

pub fn route_from_nexus(
deps: DepsMut,
msgs: Vec<nexus::execute::Message>,
) -> Result<Response<nexus::execute::Message>, ContractError> {
contract::execute(
deps,
mock_env(),
mock_info("nexus", &[]),
GatewayExecuteMsg::RouteMessagesFromNexus(msgs),
)
}
15 changes: 15 additions & 0 deletions contracts/axelarnet-gateway/tests/utils/messages.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axelar_core_std::nexus;
use router_api::{CrossChainId, Message};
use sha3::Digest;

Expand All @@ -22,3 +23,17 @@ pub fn dummy_to_router(payload: &impl AsRef<[u8]>) -> Message {
payload_hash: sha3::Keccak256::digest(payload).into(),
}
}

pub fn dummy_from_nexus(payload: &impl AsRef<[u8]>) -> nexus::execute::Message {
nexus::execute::Message {
source_chain: "Ethereum".parse().unwrap(),
source_address: "source-address".parse().unwrap(),
destination_chain: "destination-chain".parse().unwrap(),
destination_address: "destination-address".parse().unwrap(),
payload_hash: sha3::Keccak256::digest(payload).into(),
source_tx_id: "source-chain".as_bytes().to_vec().try_into().unwrap(),
source_tx_index: 0,
id: "source-chain-0".to_string(),
token: None,
}
}
Loading