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 1 commit
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::{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 @@
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 @@
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)?)

Check warning on line 97 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L96-L97

Added lines #L96 - L97 were not covered by tests
}
}?
.then(Ok)
}
Expand All @@ -111,6 +116,10 @@
.then(Ok)
}

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

Check warning on line 121 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L119-L121

Added lines #L119 - L121 were not covered by tests

#[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 @@
InvalidToken,
#[error("invalid routing destination")]
InvalidRoutingDestination,
#[error("failed converting the nexus message for the router")]
haiyizxx marked this conversation as resolved.
Show resolved Hide resolved
InvalidNexusMessageForRouter,
}

#[cw_serde]
Expand Down Expand Up @@ -173,6 +175,21 @@
.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)?;

Check warning on line 186 in contracts/axelarnet-gateway/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract/execute.rs#L178-L186

Added lines #L178 - L186 were not covered by tests

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

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

Check warning on line 191 in contracts/axelarnet-gateway/src/contract/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract/execute.rs#L188-L191

Added lines #L188 - L191 were not covered by tests

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
Loading