From b8bceeda28f761e4f1776a1102daa7813ebd7edf Mon Sep 17 00:00:00 2001 From: sherpalden Date: Fri, 6 Dec 2024 17:07:28 +0545 Subject: [PATCH] fix: remove dstNetwork in recv_message cluster connection --- Cargo.lock | 3 +- .../cw-cluster-connection/Cargo.toml | 1 + .../cw-cluster-connection/src/contract.rs | 3 +- .../cw-cluster-connection/src/helper.rs | 26 ++++++++++++-- .../cw-cluster-connection/src/lib.rs | 11 +----- .../cw-cluster-connection/src/msg.rs | 1 - .../cw-cluster-connection/tests/test.rs | 34 ++++++++++++++----- 7 files changed, 53 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d2c5b1d..5a2775ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -206,6 +206,7 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 1.1.0", + "cw-xcall 0.2.1", "cw-xcall-lib 0.1.0", "cw2 1.0.1", "getrandom", @@ -217,8 +218,6 @@ dependencies = [ "sha2 0.10.8", "sha3", "thiserror", - "soroban-rlp", - "soroban-sdk", ] [[package]] diff --git a/contracts/cosmwasm-vm/cw-cluster-connection/Cargo.toml b/contracts/cosmwasm-vm/cw-cluster-connection/Cargo.toml index 471972fc..f118f924 100644 --- a/contracts/cosmwasm-vm/cw-cluster-connection/Cargo.toml +++ b/contracts/cosmwasm-vm/cw-cluster-connection/Cargo.toml @@ -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 } diff --git a/contracts/cosmwasm-vm/cw-cluster-connection/src/contract.rs b/contracts/cosmwasm-vm/cw-cluster-connection/src/contract.rs index 92fb46c1..89b18f08 100644 --- a/contracts/cosmwasm-vm/cw-cluster-connection/src/contract.rs +++ b/contracts/cosmwasm-vm/cw-cluster-connection/src/contract.rs @@ -153,7 +153,6 @@ impl<'a> ClusterConnection<'a> { src_network: NetId, conn_sn: u128, msg: String, - dst_network: NetId, signatures: Vec>, ) -> Result { self.ensure_relayer(deps.storage, info.sender)?; @@ -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 = self.hex_decode(msg)?; let signed_msg = SignableMsg { diff --git a/contracts/cosmwasm-vm/cw-cluster-connection/src/helper.rs b/contracts/cosmwasm-vm/cw-cluster-connection/src/helper.rs index 550d0dcf..a7d7b15d 100644 --- a/contracts/cosmwasm-vm/cw-cluster-connection/src/helper.rs +++ b/contracts/cosmwasm-vm/cw-cluster-connection/src/helper.rs @@ -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; @@ -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 { + 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, diff --git a/contracts/cosmwasm-vm/cw-cluster-connection/src/lib.rs b/contracts/cosmwasm-vm/cw-cluster-connection/src/lib.rs index d0d95a06..913d5bbe 100644 --- a/contracts/cosmwasm-vm/cw-cluster-connection/src/lib.rs +++ b/contracts/cosmwasm-vm/cw-cluster-connection/src/lib.rs @@ -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), } } diff --git a/contracts/cosmwasm-vm/cw-cluster-connection/src/msg.rs b/contracts/cosmwasm-vm/cw-cluster-connection/src/msg.rs index e9289813..1b4f2895 100644 --- a/contracts/cosmwasm-vm/cw-cluster-connection/src/msg.rs +++ b/contracts/cosmwasm-vm/cw-cluster-connection/src/msg.rs @@ -39,7 +39,6 @@ pub enum ExecuteMsg { src_network: NetId, conn_sn: u128, msg: String, - dst_network: NetId, signatures: Vec>, }, } diff --git a/contracts/cosmwasm-vm/cw-cluster-connection/tests/test.rs b/contracts/cosmwasm-vm/cw-cluster-connection/tests/test.rs index 6dd311bc..06d5167e 100644 --- a/contracts/cosmwasm-vm/cw-cluster-connection/tests/test.rs +++ b/contracts/cosmwasm-vm/cw-cluster-connection/tests/test.rs @@ -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"; @@ -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; @@ -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( @@ -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; @@ -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(), };