Skip to content

Commit

Permalink
Merge branch 'main' into route-message
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyizxx authored Oct 21, 2024
2 parents c7c3fad + a0527da commit fbbd18d
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 334 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ quote = "1.0.36"
rand = "0.8.5"
report = { version = "^1.0.0", path = "packages/report" }
rewards = { version = "^1.1.0", path = "contracts/rewards" }
router = { version = "^1.0.0", path = "contracts/router" }
router = { version = "^1.0.1", path = "contracts/router" }
router-api = { version = "^1.0.0", path = "packages/router-api" }
schemars = "0.8.10"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
Expand Down
81 changes: 33 additions & 48 deletions ampd/src/stellar/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;

use axelar_wasm_std::voting::Vote;
use stellar::WeightedSigners;
use stellar_xdr::curr::{ContractEventBody, ScAddress, ScSymbol, ScVal, StringM};
use stellar_xdr::curr::{BytesM, ContractEventBody, ScAddress, ScBytes, ScSymbol, ScVal, StringM};

use crate::handlers::stellar_verify_msg::Message;
use crate::handlers::stellar_verify_verifier_set::VerifierSetConfirmation;
Expand All @@ -15,67 +15,54 @@ impl PartialEq<ContractEventBody> for Message {
fn eq(&self, event: &ContractEventBody) -> bool {
let ContractEventBody::V0(body) = event;

if body.topics.len() != 3 {
if body.topics.len() != 5 {
return false;
}

let [symbol, source_address, payload_hash] = &body.topics[..] else {
let [symbol, source_address, destination_chain, destination_address, payload_hash] =
&body.topics[..]
else {
return false;
};

let expected_topic: ScVal =
ScSymbol(StringM::from_str(TOPIC_CALLED).expect("must convert str to ScSymbol")).into();

let (dest_chain, dest_address) = match &body.data {
ScVal::Vec(Some(data)) if data.len() == 3 => {
let [dest_chain, dest_address, _] = &data[..] else {
return false;
};
(dest_chain, dest_address)
}
_ => return false,
};

expected_topic == *symbol
&& (ScVal::Address(self.source_address.clone()) == *source_address)
&& (ScVal::Bytes(self.payload_hash.clone()) == *payload_hash)
&& (ScVal::String(self.destination_chain.clone()) == *dest_chain)
&& (ScVal::String(self.destination_address.clone()) == *dest_address)
&& (ScVal::String(self.destination_chain.clone()) == *destination_chain)
&& (ScVal::String(self.destination_address.clone()) == *destination_address)
}
}

impl PartialEq<ContractEventBody> for VerifierSetConfirmation {
fn eq(&self, event: &ContractEventBody) -> bool {
let ContractEventBody::V0(body) = event;

if body.topics.len() != 1 {
if body.topics.len() != 3 {
return false;
}

let [symbol] = &body.topics[..] else {
let [symbol, _, signers_hash] = &body.topics[..] else {
return false;
};

let expected_topic: ScVal =
ScSymbol(StringM::from_str(TOPIC_ROTATED).expect("must convert str to ScSymbol"))
.into();

let rotated_signers = match &body.data {
ScVal::Vec(Some(data)) if data.len() == 1 => {
let [rotated_signers] = &data[..] else {
return false;
};
rotated_signers.clone()
}
_ => return false,
let Some(weighted_signers_hash) = WeightedSigners::try_from(&self.verifier_set)
.ok()
.and_then(|weighted_signers| weighted_signers.hash().ok())
.and_then(|signers_hash| BytesM::try_from(signers_hash).ok())
.map(ScBytes)
.map(ScVal::Bytes)
else {
return false;
};

WeightedSigners::try_from(&self.verifier_set)
.ok()
.and_then(|signers| ScVal::try_from(signers).ok())
.map_or(false, |signers: ScVal| {
symbol == &expected_topic && signers == rotated_signers
})
&expected_topic == symbol && &weighted_signers_hash == signers_hash
}
}

Expand Down Expand Up @@ -147,7 +134,7 @@ mod test {
use stellar::WeightedSigners;
use stellar_xdr::curr::{
AccountId, BytesM, ContractEvent, ContractEventBody, ContractEventType, ContractEventV0,
PublicKey, ScAddress, ScBytes, ScString, ScSymbol, ScVal, ScVec, StringM, Uint256,
PublicKey, ScAddress, ScBytes, ScString, ScSymbol, ScVal, StringM, Uint256,
};

use crate::handlers::stellar_verify_msg::Message;
Expand Down Expand Up @@ -319,19 +306,13 @@ mod test {
topics: vec![
ScVal::Symbol(ScSymbol(StringM::from_str(TOPIC_CALLED).unwrap())),
ScVal::Address(msg.source_address.clone()),
ScVal::String(msg.destination_chain.clone()),
ScVal::String(msg.destination_address.clone()),
ScVal::Bytes(msg.payload_hash.clone()),
]
.try_into()
.unwrap(),
data: ScVal::Vec(Some(
vec![
ScVal::String(msg.destination_chain.clone()),
ScVal::String(msg.destination_address.clone()),
ScVal::String(StringM::from_str("payload").unwrap().into()),
]
.try_into()
.unwrap(),
)),
data: ScVal::Bytes(BytesM::try_from("payload".as_bytes()).unwrap().into()),
});

let event = ContractEvent {
Expand Down Expand Up @@ -372,19 +353,23 @@ mod test {
},
};

let weighted_signers: ScVal =
let weighted_signers_hash = BytesM::try_from(
WeightedSigners::try_from(&verifier_set_confirmation.verifier_set)
.unwrap()
.try_into()
.unwrap();
.hash()
.unwrap(),
)
.unwrap();

let event_body = ContractEventBody::V0(ContractEventV0 {
topics: vec![ScVal::Symbol(ScSymbol(
StringM::from_str(TOPIC_ROTATED).unwrap(),
))]
topics: vec![
ScVal::Symbol(ScSymbol(StringM::from_str(TOPIC_ROTATED).unwrap())),
ScVal::U64(1),
ScVal::Bytes(ScBytes(weighted_signers_hash)),
]
.try_into()
.unwrap(),
data: ScVal::Vec(Some(ScVec::try_from(vec![weighted_signers]).unwrap())),
data: ().into(),
});

let event = ContractEvent {
Expand Down
3 changes: 2 additions & 1 deletion contracts/router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "router"
version = "1.0.0"
version = "1.0.1"
rust-version = { workspace = true }
edition = { workspace = true }
description = "Router contract"
Expand Down 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
Loading

0 comments on commit fbbd18d

Please sign in to comment.