Skip to content

Commit

Permalink
fix: remove dstNetwork in recv_message cluster connection
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalden committed Dec 6, 2024
1 parent a5fa0dc commit b8bceed
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
3 changes: 1 addition & 2 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/cosmwasm-vm/cw-cluster-connection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ serde = { workspace=true}
thiserror = { workspace=true}
common ={ workspace=true}
cw-xcall-lib = { path="../cw-xcall-lib" }
cw-xcall = { path="../cw-xcall" }
hex = "0.4.3"
serde-json-wasm = {workspace=true}
sha2 = { version = "0.10.6", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion contracts/cosmwasm-vm/cw-cluster-connection/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ impl<'a> ClusterConnection<'a> {
src_network: NetId,
conn_sn: u128,
msg: String,
dst_network: NetId,
signatures: Vec<Vec<u8>>,
) -> Result<Response, ContractError> {
self.ensure_relayer(deps.storage, info.sender)?;
Expand All @@ -162,6 +161,8 @@ impl<'a> ClusterConnection<'a> {
return Err(ContractError::DuplicateMessage);
}

let dst_network = self.get_network_id(deps.as_ref())?;

let msg_vec: Vec<u8> = self.hex_decode(msg)?;

let signed_msg = SignableMsg {
Expand Down
26 changes: 23 additions & 3 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::{collections::HashMap, str::FromStr};

use cosmwasm_std::{ensure_eq, Addr, BalanceResponse, BankQuery, Coin};
use cw_xcall_lib::network_address::NetId;
use cosmwasm_std::{ensure_eq, Addr, BalanceResponse, BankQuery, Coin, QueryRequest};
use cw_xcall_lib::network_address::{NetId, NetworkAddress};
use sha2::Digest;
use sha3::Keccak256;

Expand Down Expand Up @@ -78,6 +78,26 @@ impl<'a> ClusterConnection<'a> {
.map_err(|e| ContractError::InvalidHexData { msg: e.to_string() })
}

pub fn get_network_id(&self, deps: Deps) -> Result<String, ContractError> {
let xcall_host = self.get_xcall(deps.storage)?;

let query_msg = cw_xcall::msg::QueryMsg::GetNetworkAddress {};

let query_request = QueryRequest::Wasm(cosmwasm_std::WasmQuery::Smart {
contract_addr: xcall_host.to_string(),
msg: to_json_binary(&query_msg).map_err(ContractError::Std)?,
});

let network_address: String = deps
.querier
.query(&query_request)
.map_err(ContractError::Std)?;

Ok(NetworkAddress::from_str(network_address.as_str())?
.nid()
.to_string())
}

pub fn call_xcall_handle_message(
&self,
store: &dyn Storage,
Expand Down
11 changes: 1 addition & 10 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,8 @@ pub fn execute(
src_network,
conn_sn,
msg,
dst_network,
signatures,
} => conn.recv_message(
deps,
info,
src_network,
conn_sn,
msg,
dst_network,
signatures,
),
} => conn.recv_message(deps, info, src_network, conn_sn, msg, signatures),
}
}

Expand Down
1 change: 0 additions & 1 deletion contracts/cosmwasm-vm/cw-cluster-connection/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub enum ExecuteMsg {
src_network: NetId,
conn_sn: u128,
msg: String,
dst_network: NetId,
signatures: Vec<Vec<u8>>,
},
}
Expand Down
34 changes: 25 additions & 9 deletions contracts/cosmwasm-vm/cw-cluster-connection/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ use cluster_connection::{
};
use cluster_connection::{keccak256, SignableMsg};
use common::rlp;
use cosmwasm_std::{testing::mock_env, Env};
use cosmwasm_std::{testing::mock_env, ContractResult, Env};
use cosmwasm_std::{
testing::{mock_dependencies, mock_info, MockApi, MockQuerier},
Addr, MemoryStorage, OwnedDeps, Uint128,
};
use cosmwasm_std::{Api, Coin, Event};
use cosmwasm_std::{to_json_binary, Coin, ContractInfoResponse, Event, SystemResult, WasmQuery};
use cw_xcall_lib::network_address::NetId;
use k256::ecdsa::signature::SignerMut;
use k256::elliptic_curve::sec1::ToEncodedPoint;
use k256::{
ecdsa::Signature, ecdsa::SigningKey, ecdsa::VerifyingKey, elliptic_curve::rand_core::OsRng,
};
use k256::{ecdsa::SigningKey, ecdsa::VerifyingKey, elliptic_curve::rand_core::OsRng};
use std::str::FromStr;

const XCALL: &str = "xcall";
Expand Down Expand Up @@ -402,6 +398,17 @@ pub fn test_send_message_unauthorized() {
pub fn test_recv_message() {
let (mut deps, env, ctx) = instantiate(ADMIN);

deps.querier.update_wasm(|r: &WasmQuery| match r {
WasmQuery::Smart {
contract_addr: _,
msg: _,
} => SystemResult::Ok(ContractResult::Ok(to_json_binary("archway/xcall").unwrap())),
WasmQuery::ContractInfo { contract_addr: _ } => SystemResult::Ok(ContractResult::Ok(
to_json_binary(&ContractInfoResponse::default()).unwrap(),
)),
_ => todo!(),
});

let src_network = NetId::from_str("0x2.icon").unwrap();
let dst_network = NetId::from_str("archway").unwrap();
let conn_sn: u128 = 456456;
Expand Down Expand Up @@ -446,7 +453,6 @@ pub fn test_recv_message() {
src_network: src_network.clone(),
conn_sn,
msg: msg,
dst_network,
signatures: signatures.clone(),
};
let res = execute(
Expand Down Expand Up @@ -476,6 +482,17 @@ pub fn test_recv_message() {
pub fn test_recv_message_signatures_insufficient() {
let (mut deps, env, ctx) = instantiate(ADMIN);

deps.querier.update_wasm(|r: &WasmQuery| match r {
WasmQuery::Smart {
contract_addr: _,
msg: _,
} => SystemResult::Ok(ContractResult::Ok(to_json_binary("archway/xcall").unwrap())),
WasmQuery::ContractInfo { contract_addr: _ } => SystemResult::Ok(ContractResult::Ok(
to_json_binary(&ContractInfoResponse::default()).unwrap(),
)),
_ => todo!(),
});

let src_network = NetId::from_str("0x2.icon").unwrap();
let dst_network = NetId::from_str("archway").unwrap();
let conn_sn: u128 = 456456;
Expand Down Expand Up @@ -520,7 +537,6 @@ pub fn test_recv_message_signatures_insufficient() {
src_network: src_network.clone(),
conn_sn,
msg: msg,
dst_network,
signatures: signatures.clone(),
};

Expand Down

0 comments on commit b8bceed

Please sign in to comment.