Skip to content

Commit

Permalink
fix(minor-nexus-gateway)!: make msg id consistent with evm chains (#350)
Browse files Browse the repository at this point in the history
* fix(minor-nexus-gateway)!: make msg id consistent with evm chains
  • Loading branch information
cjcobb23 authored Apr 18, 2024
1 parent 3329113 commit d360ea4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
8 changes: 4 additions & 4 deletions contracts/nexus-gateway/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ where

let msgs: Vec<_> = msgs
.into_iter()
.map(connection_router_api::Message::from)
.collect();
.map(connection_router_api::Message::try_from)
.collect::<Result<Vec<_>>>()?;
if msgs.is_empty() {
return Ok(Response::default());
}
Expand Down Expand Up @@ -134,7 +134,7 @@ mod test {
.unwrap()
.try_into()
.unwrap(),
source_tx_id: vec![0x2f, 0xe4].try_into().unwrap(),
source_tx_id: vec![0x2f; 32].try_into().unwrap(),
source_tx_index: 100,
},
nexus::Message {
Expand All @@ -148,7 +148,7 @@ mod test {
.unwrap()
.try_into()
.unwrap(),
source_tx_id: vec![0x23, 0xf4].try_into().unwrap(),
source_tx_id: vec![0x23; 32].try_into().unwrap(),
source_tx_index: 1000,
},
];
Expand Down
6 changes: 6 additions & 0 deletions contracts/nexus-gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub enum ContractError {
#[error("invalid message id {0}")]
InvalidMessageId(String),

#[error("invalid source tx id {0}")]
InvalidSourceTxId(String),

#[error("invalid event index {0}")]
InvalidEventIndex(u64),

#[error("invalid payload hash {0}")]
InvalidMessagePayloadHash(HexBinary),

Expand Down
66 changes: 53 additions & 13 deletions contracts/nexus-gateway/src/nexus.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axelar_wasm_std::nonempty;
use axelar_wasm_std::{msg_id::tx_hash_event_index::HexTxHashAndEventIndex, nonempty};
use connection_router_api::{Address, ChainName, CrossChainId};
use cosmwasm_std::{CosmosMsg, CustomMsg};
use error_stack::{Result, ResultExt};
use error_stack::{Report, Result, ResultExt};
use hex::{FromHex, ToHex};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -68,24 +68,29 @@ impl From<connection_router_api::Message> for Message {
}
}

impl From<Message> for connection_router_api::Message {
fn from(msg: Message) -> Self {
Self {
impl TryFrom<Message> for connection_router_api::Message {
type Error = Report<ContractError>;

fn try_from(msg: Message) -> Result<Self, ContractError> {
let msg_id = HexTxHashAndEventIndex {
tx_hash: <[u8; 32]>::try_from(msg.source_tx_id.as_ref().as_slice()).map_err(|_| {
ContractError::InvalidSourceTxId(msg.source_tx_id.as_ref().encode_hex::<String>())
})?,
event_index: u32::try_from(msg.source_tx_index)
.map_err(|_| ContractError::InvalidEventIndex(msg.source_tx_index))?,
};

Ok(Self {
cc_id: CrossChainId {
chain: msg.source_chain,
id: format!(
"{}:{}",
msg.source_tx_id.as_ref().encode_hex::<String>(),
msg.source_tx_index
)
.try_into()
.expect("cannot be empty"),
id: nonempty::String::try_from(msg_id.to_string())
.change_context(ContractError::InvalidMessageId(msg_id.to_string()))?,
},
source_address: msg.source_address,
destination_chain: msg.destination_chain,
destination_address: msg.destination_address,
payload_hash: msg.payload_hash,
}
})
}
}

Expand All @@ -94,3 +99,38 @@ impl From<Message> for CosmosMsg<Message> {
CosmosMsg::Custom(msg)
}
}

#[cfg(test)]
mod test {
use std::vec;

use cosmwasm_std::HexBinary;

use super::Message;

#[test]
fn should_convert_nexus_message_to_router_message() {
let msg = Message {
source_chain: "ethereum".parse().unwrap(),
source_address: "something".parse().unwrap(),
destination_chain: "polygon".parse().unwrap(),
destination_address: "something else".parse().unwrap(),
payload_hash: [1; 32],
source_tx_id: vec![2; 32].try_into().unwrap(),
source_tx_index: 1,
};

let router_msg = connection_router_api::Message::try_from(msg.clone());
assert!(router_msg.is_ok());
let router_msg = router_msg.unwrap();
assert_eq!(router_msg.cc_id.chain, msg.source_chain);
assert_eq!(
router_msg.cc_id.id.to_string(),
format!(
"0x{}-{}",
HexBinary::from(msg.source_tx_id.as_ref().clone()).to_hex(),
msg.source_tx_index
)
);
}
}

0 comments on commit d360ea4

Please sign in to comment.