From f5bfe35c9a7ab16e0895147e1bfeb05c82fafa57 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Sun, 26 May 2024 16:31:34 -0400 Subject: [PATCH] added ability to voice to specify chain address length for proxies --- contracts/main/voice/src/contract.rs | 38 +++++++++++++++---- contracts/main/voice/src/msg.rs | 8 ++++ contracts/main/voice/src/state.rs | 14 +++++++ contracts/main/voice/src/suite_tests/suite.rs | 1 + 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/contracts/main/voice/src/contract.rs b/contracts/main/voice/src/contract.rs index 498f58a..c8cfeee 100644 --- a/contracts/main/voice/src/contract.rs +++ b/contracts/main/voice/src/contract.rs @@ -1,9 +1,9 @@ #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - from_json, instantiate2_address, to_json_binary, to_json_vec, Binary, CodeInfoResponse, - ContractResult, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg, SystemResult, - Uint64, WasmMsg, + from_json, instantiate2_address, to_json_binary, to_json_vec, Binary, CanonicalAddr, + CodeInfoResponse, ContractResult, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg, + SystemResult, Uint64, WasmMsg, }; use cw2::set_contract_version; @@ -13,7 +13,9 @@ use polytone::ibc::{Msg, Packet}; use crate::error::ContractError; use crate::ibc::{ACK_GAS_NEEDED, REPLY_FORWARD_DATA}; use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}; -use crate::state::{BLOCK_MAX_GAS, PROXY_CODE_ID, SENDER_TO_PROXY}; +use crate::state::{ + SenderInfo, BLOCK_MAX_GAS, CONTRACT_ADDR_LEN, PROXY_CODE_ID, PROXY_TO_SENDER, SENDER_TO_PROXY, +}; const CONTRACT_NAME: &str = "crates.io:polytone-voice"; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -37,6 +39,7 @@ pub fn instantiate( PROXY_CODE_ID.save(deps.storage, &msg.proxy_code_id.u64())?; BLOCK_MAX_GAS.save(deps.storage, &msg.block_max_gas.u64())?; + CONTRACT_ADDR_LEN.save(deps.storage, &msg.contract_addr_len.unwrap_or(32))?; Ok(Response::default() .add_attribute("method", "instantiate") @@ -104,17 +107,33 @@ pub fn execute( let contract = deps.api.addr_canonicalize(env.contract.address.as_str())?; let code_id = PROXY_CODE_ID.load(deps.storage)?; + let addr_len = CONTRACT_ADDR_LEN.load(deps.storage)?; let CodeInfoResponse { checksum, .. } = deps.querier.query_wasm_code_info(code_id)?; let salt = salt(&connection_id, &counterparty_port, &sender); - let proxy = deps.api.addr_humanize(&instantiate2_address( - &checksum, &contract, &salt, - )?)?; + let init2_addr_data: CanonicalAddr = + instantiate2_address(&checksum, &contract, &salt)?.to_vec() + [0..addr_len as usize] + .into(); + let proxy = deps.api.addr_humanize(&init2_addr_data)?; SENDER_TO_PROXY.save( deps.storage, - (connection_id, counterparty_port, sender.clone()), + ( + connection_id.clone(), + counterparty_port.clone(), + sender.clone(), + ), &proxy, )?; + PROXY_TO_SENDER.save( + deps.storage, + proxy.clone(), + &SenderInfo { + connection_id, + remote_port: counterparty_port, + remote_sender: sender.clone(), + }, + )?; ( Some(WasmMsg::Instantiate2 { admin: None, @@ -171,6 +190,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { QueryMsg::BlockMaxGas => to_json_binary(&BLOCK_MAX_GAS.load(deps.storage)?), QueryMsg::ProxyCodeId => to_json_binary(&PROXY_CODE_ID.load(deps.storage)?), + QueryMsg::SenderInfoForProxy { proxy } => { + to_json_binary(&PROXY_TO_SENDER.load(deps.storage, deps.api.addr_validate(&proxy)?)?) + } } } diff --git a/contracts/main/voice/src/msg.rs b/contracts/main/voice/src/msg.rs index a3da41b..6026a8e 100644 --- a/contracts/main/voice/src/msg.rs +++ b/contracts/main/voice/src/msg.rs @@ -1,12 +1,17 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; use cosmwasm_std::{Binary, Uint64}; +use crate::state::SenderInfo; + #[cw_serde] pub struct InstantiateMsg { /// Code ID to use for instantiating proxy contracts. pub proxy_code_id: Uint64, /// The max gas allowed in a single block. pub block_max_gas: Uint64, + /// The contract address length used by the chain. Defaults to 32. Some + /// chains use other lengths, such as Injective which uses 20. + pub contract_addr_len: Option, } #[cw_serde] @@ -35,6 +40,9 @@ pub enum QueryMsg { /// `"proxy_code_id"`. #[returns(Uint64)] ProxyCodeId, + /// Queries the sender information for a given proxy. + #[returns(SenderInfo)] + SenderInfoForProxy { proxy: String }, } #[cw_serde] diff --git a/contracts/main/voice/src/state.rs b/contracts/main/voice/src/state.rs index 89ffa8b..476b352 100644 --- a/contracts/main/voice/src/state.rs +++ b/contracts/main/voice/src/state.rs @@ -1,9 +1,13 @@ +use cosmwasm_schema::cw_serde; use cosmwasm_std::Addr; use cw_storage_plus::{Item, Map}; /// (connection_id, remote_port, remote_sender) -> proxy pub(crate) const SENDER_TO_PROXY: Map<(String, String, String), Addr> = Map::new("c2p"); +/// proxy -> { connection_id, remote_port, remote_sender } +pub(crate) const PROXY_TO_SENDER: Map = Map::new("p2c"); + /// (channel_id) -> connection_id pub(crate) const CHANNEL_TO_CONNECTION: Map = Map::new("c2c"); @@ -12,3 +16,13 @@ pub(crate) const PROXY_CODE_ID: Item = Item::new("pci"); /// Max gas usable in a single block. pub(crate) const BLOCK_MAX_GAS: Item = Item::new("bmg"); + +/// Contract address length used by the chain. +pub(crate) const CONTRACT_ADDR_LEN: Item = Item::new("cal"); + +#[cw_serde] +pub struct SenderInfo { + pub connection_id: String, + pub remote_port: String, + pub remote_sender: String, +} diff --git a/contracts/main/voice/src/suite_tests/suite.rs b/contracts/main/voice/src/suite_tests/suite.rs index c40abab..de8fa41 100644 --- a/contracts/main/voice/src/suite_tests/suite.rs +++ b/contracts/main/voice/src/suite_tests/suite.rs @@ -33,6 +33,7 @@ impl Default for SuiteBuilder { instantiate: InstantiateMsg { proxy_code_id: Uint64::new(9999), block_max_gas: Uint64::new(110_000), + contract_addr_len: None, }, } }