Skip to content

Commit

Permalink
feat(minor-router): route to axelarnet gateway (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyizxx authored Oct 21, 2024
1 parent 60a0ff2 commit a0527da
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 279 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ serde_json = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
assert_ok = { workspace = true }
axelar-core-std = { workspace = true, features = ["test"] }
cw-multi-test = "0.15.1"
hex = { version = "0.4.3", default-features = false }
Expand Down
35 changes: 20 additions & 15 deletions contracts/router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use axelar_wasm_std::{address, killswitch, permission_control, FnExt};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, Storage,
to_json_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, Storage,
};
use router_api::error::Error;

use crate::contract::migrations::v0_3_3;
use crate::contract::migrations::v1_0_1;
use crate::events::RouterInstantiated;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state;
use crate::state::{load_chain_by_gateway, load_config, Config};

Expand All @@ -18,13 +18,18 @@ mod query;

pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
const BASE_VERSION: &str = "1.0.1";

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(
deps: DepsMut,
_env: Env,
_msg: Empty,
msg: MigrateMsg,
) -> Result<Response, axelar_wasm_std::error::ContractError> {
v0_3_3::migrate(deps.storage)?;
cw2::assert_contract_version(deps.storage, CONTRACT_NAME, BASE_VERSION)?;

let axelarnet_gateway = address::validate_cosmwasm_address(deps.api, &msg.axelarnet_gateway)?;
v1_0_1::migrate(deps.storage, axelarnet_gateway)?;

// this needs to be the last thing to do during migration,
// because previous migration steps should check the old version
Expand All @@ -44,13 +49,13 @@ pub fn instantiate(

let admin = address::validate_cosmwasm_address(deps.api, &msg.admin_address)?;
let governance = address::validate_cosmwasm_address(deps.api, &msg.governance_address)?;
let nexus_gateway = address::validate_cosmwasm_address(deps.api, &msg.nexus_gateway)?;
let axelarnet_gateway = address::validate_cosmwasm_address(deps.api, &msg.axelarnet_gateway)?;

permission_control::set_admin(deps.storage, &admin)?;
permission_control::set_governance(deps.storage, &governance)?;

let config = Config {
nexus_gateway: nexus_gateway.clone(),
axelarnet_gateway: axelarnet_gateway.clone(),
};

state::save_config(deps.storage, &config)?;
Expand All @@ -60,7 +65,7 @@ pub fn instantiate(
RouterInstantiated {
admin,
governance,
nexus_gateway,
axelarnet_gateway,
}
.into(),
))
Expand Down Expand Up @@ -114,9 +119,9 @@ fn find_gateway_address(
sender: &Addr,
) -> impl FnOnce(&dyn Storage, &ExecuteMsg) -> error_stack::Result<Addr, Error> + '_ {
move |storage, _| {
let nexus_gateway = load_config(storage)?.nexus_gateway;
if nexus_gateway == sender {
Ok(nexus_gateway)
let axelarnet_gateway = load_config(storage)?.axelarnet_gateway;
if axelarnet_gateway == sender {
Ok(axelarnet_gateway)
} else {
load_chain_by_gateway(storage, sender)?
.gateway
Expand Down Expand Up @@ -166,7 +171,7 @@ mod test {

const ADMIN_ADDRESS: &str = "admin";
const GOVERNANCE_ADDRESS: &str = "governance";
const NEXUS_GATEWAY_ADDRESS: &str = "nexus_gateway";
const AXELARNET_GATEWAY_ADDRESS: &str = "axelarnet_gateway";
const UNAUTHORIZED_ADDRESS: &str = "unauthorized";

fn setup() -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {
Expand All @@ -182,7 +187,7 @@ mod test {
InstantiateMsg {
admin_address: ADMIN_ADDRESS.to_string(),
governance_address: GOVERNANCE_ADDRESS.to_string(),
nexus_gateway: NEXUS_GATEWAY_ADDRESS.to_string(),
axelarnet_gateway: AXELARNET_GATEWAY_ADDRESS.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -368,7 +373,7 @@ mod test {
let result = execute(
deps.as_mut(),
mock_env(),
mock_info(NEXUS_GATEWAY_ADDRESS, &[]),
mock_info(AXELARNET_GATEWAY_ADDRESS, &[]),
ExecuteMsg::RouteMessages(messages.clone()),
);
assert!(result.is_ok());
Expand Down Expand Up @@ -1805,7 +1810,7 @@ mod test {
assert!(execute(
deps.as_mut(),
mock_env(),
mock_info(NEXUS_GATEWAY_ADDRESS, &[]),
mock_info(AXELARNET_GATEWAY_ADDRESS, &[]),
ExecuteMsg::RouteMessages(generate_messages(&eth, &polygon, &mut 0, 10)),
)
.is_ok());
Expand Down
36 changes: 19 additions & 17 deletions contracts/router/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn validate_msgs(
// because the source chain is registered in the core nexus module.
// All messages received from the nexus gateway must adhere to the
// HexTxHashAndEventIndex message ID format.
if sender == config.nexus_gateway {
if sender == config.axelarnet_gateway {
verify_msg_ids(&msgs, &MessageIdFormat::HexTxHashAndEventIndex)?;
return Ok(msgs);
}
Expand Down Expand Up @@ -235,9 +235,9 @@ pub fn route_messages(
}
Some(destination_chain) => destination_chain.gateway.address,
// messages with unknown destination chains are routed to
// the nexus gateway if the sender is not the nexus gateway
// the axelarnet gateway if the sender is not the nexus gateway
// itself
None if sender != config.nexus_gateway => config.nexus_gateway.clone(),
None if sender != config.axelarnet_gateway => config.axelarnet_gateway.clone(),
_ => return Err(report!(Error::ChainNotFound)),
};

Expand Down Expand Up @@ -278,6 +278,8 @@ mod test {
use crate::msg::InstantiateMsg;
use crate::state::chain_endpoints;

const AXELARNET_GATEWAY: &str = "axelarnet_gateway";

fn rand_message(source_chain: ChainName, destination_chain: ChainName) -> Message {
let mut bytes = [0; 32];
rand::thread_rng().fill_bytes(&mut bytes);
Expand Down Expand Up @@ -322,7 +324,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand All @@ -349,7 +351,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -390,7 +392,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -429,7 +431,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -484,7 +486,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -513,7 +515,7 @@ mod test {

#[test]
fn route_messages_from_nexus_with_invalid_message_id() {
let sender = Addr::unchecked("nexus_gateway");
let sender = Addr::unchecked(AXELARNET_GATEWAY);
let source_chain: ChainName = "ethereum".parse().unwrap();
let destination_chain: ChainName = "bitcoin".parse().unwrap();

Expand All @@ -525,7 +527,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand All @@ -550,7 +552,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -602,7 +604,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -668,7 +670,7 @@ mod test {

#[test]
fn route_messages_from_nexus_to_registered_chains() {
let sender = Addr::unchecked("nexus_gateway");
let sender = Addr::unchecked(AXELARNET_GATEWAY);
let source_chain: ChainName = "ethereum".parse().unwrap();
let destination_chain_1: ChainName = "bitcoin".parse().unwrap();
let destination_chain_2: ChainName = "polygon".parse().unwrap();
Expand All @@ -681,7 +683,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -732,7 +734,7 @@ mod test {

#[test]
fn route_messages_from_nexus_to_non_registered_chains() {
let sender = Addr::unchecked("nexus_gateway");
let sender = Addr::unchecked(AXELARNET_GATEWAY);
let source_chain: ChainName = "ethereum".parse().unwrap();
let destination_chain: ChainName = "bitcoin".parse().unwrap();

Expand All @@ -744,7 +746,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -774,7 +776,7 @@ mod test {
InstantiateMsg {
admin_address: "admin".to_string(),
governance_address: "governance".to_string(),
nexus_gateway: "nexus_gateway".to_string(),
axelarnet_gateway: AXELARNET_GATEWAY.to_string(),
},
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion contracts/router/src/contract/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod v0_3_3;
pub mod v1_0_1;
Loading

0 comments on commit a0527da

Please sign in to comment.