Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cw-orch-fns-derive = { path = "packages/cw-orch-fns-derive", version = "0.18.0"
cw-orch-networks = { path = "packages/cw-orch-networks", version = "0.20.0" }

thiserror = { version = "1.0.21" }
sha256 = { version = "1" }
sha2 = { version = "0.10.8" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For using sha2 instead of sha256 - it's lighter(sha2 is dependency of sha256) and returns bytes, instead of String, which is easier to use in pair with HexBinary

serde_json = "1.0.79"
tonic = { version = "0.10.2" }
prost-types = "0.12.3"
Expand Down
2 changes: 1 addition & 1 deletion cw-orch-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ thiserror = { workspace = true }

prost-types = { workspace = true }
# Daemon deps
sha256 = { workspace = true }
sha2 = { workspace = true }
prost = { version = "0.12.3" }
bitcoin = { version = "0.30.0" }
hex = { version = "0.4.3" }
Expand Down
11 changes: 5 additions & 6 deletions cw-orch-daemon/src/queriers/cosmwasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmrs::proto::cosmos::base::query::v1beta1::PageRequest;
use cosmrs::AccountId;
use cosmwasm_std::{
from_json, instantiate2_address, to_json_binary, CanonicalAddr, CodeInfoResponse,
ContractInfoResponse,
ContractInfoResponse, HexBinary,
};
use cw_orch_core::environment::{Querier, QuerierGetter, WasmQuerier};
use tokio::runtime::Handle;
Expand Down Expand Up @@ -45,14 +45,13 @@ impl Querier for CosmWasm {

impl CosmWasm {
/// Query code_id by hash
pub async fn _code_id_hash(&self, code_id: u64) -> Result<String, DaemonError> {
pub async fn _code_id_hash(&self, code_id: u64) -> Result<HexBinary, DaemonError> {
use cosmos_modules::cosmwasm::{query_client::*, QueryCodeRequest};
let mut client: QueryClient<Channel> = QueryClient::new(self.channel.clone());
let request = QueryCodeRequest { code_id };
let resp = client.code(request).await?.into_inner();
let contract_hash = resp.code_info.unwrap().data_hash;
let on_chain_hash = base16::encode_lower(&contract_hash);
Ok(on_chain_hash)
Ok(contract_hash.into())
}

/// Query contract info
Expand Down Expand Up @@ -199,7 +198,7 @@ impl CosmWasm {
}

impl WasmQuerier for CosmWasm {
fn code_id_hash(&self, code_id: u64) -> Result<String, Self::Error> {
fn code_id_hash(&self, code_id: u64) -> Result<HexBinary, Self::Error> {
self.rt_handle
.as_ref()
.ok_or(DaemonError::QuerierNeedRuntime)?
Expand Down Expand Up @@ -287,7 +286,7 @@ impl WasmQuerier for CosmWasm {
let prefix = account_id.prefix();
let canon = account_id.to_bytes();
let checksum = self.code_id_hash(code_id)?;
let addr = instantiate2_address(checksum.as_bytes(), &CanonicalAddr(canon.into()), &salt)?;
let addr = instantiate2_address(checksum.as_slice(), &CanonicalAddr(canon.into()), &salt)?;

Ok(AccountId::new(prefix, &addr.0)?.to_string())
}
Expand Down
17 changes: 8 additions & 9 deletions cw-orch/src/osmosis_test_tube/queriers/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{cell::RefCell, rc::Rc, str::FromStr};
use cosmrs::AccountId;
use cosmwasm_std::{
from_json, instantiate2_address, to_json_vec, CanonicalAddr, CodeInfoResponse,
ContractInfoResponse,
ContractInfoResponse, HexBinary,
};
use cw_orch_core::{
environment::{Querier, QuerierGetter, StateInterface, WasmQuerier},
Expand Down Expand Up @@ -41,7 +41,7 @@ impl<S: StateInterface> QuerierGetter<OsmosisTestTubeWasmQuerier> for OsmosisTes
}

impl WasmQuerier for OsmosisTestTubeWasmQuerier {
fn code_id_hash(&self, code_id: u64) -> Result<String, Self::Error> {
fn code_id_hash(&self, code_id: u64) -> Result<HexBinary, Self::Error> {
let code_info_result: QueryCodeResponse = self
.app
.borrow()
Expand All @@ -51,12 +51,11 @@ impl WasmQuerier for OsmosisTestTubeWasmQuerier {
)
.map_err(map_err)?;

Ok(hex::encode(
code_info_result
.code_info
.ok_or(CwEnvError::CodeIdNotInStore(code_id.to_string()))?
.data_hash,
))
Ok(code_info_result
.code_info
.ok_or(CwEnvError::CodeIdNotInStore(code_id.to_string()))?
.data_hash
.into())
}

fn contract_info(
Expand Down Expand Up @@ -170,7 +169,7 @@ impl WasmQuerier for OsmosisTestTubeWasmQuerier {
let prefix = account_id.prefix();
let canon = account_id.to_bytes();
let addr =
instantiate2_address(checksum.as_bytes(), &CanonicalAddr(canon.into()), &salt).unwrap();
instantiate2_address(checksum.as_slice(), &CanonicalAddr(canon.into()), &salt).unwrap();

Ok(AccountId::new(prefix, &addr.0).unwrap().to_string())
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cw-orch-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde = { workspace = true }
cw-multi-test = { workspace = true }

log = { workspace = true }
sha256 = { workspace = true }
sha2 = { workspace = true }
anyhow = { workspace = true }
serde_json = { workspace = true }

Expand Down
18 changes: 12 additions & 6 deletions packages/cw-orch-core/src/contract/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ pub use wasm_path::WasmPath;

mod wasm_path {
use crate::error::CwEnvError;
use cosmwasm_std::ensure_eq;
use sha256::TrySha256Digest;
use std::path::{Path, PathBuf};
use cosmwasm_std::{ensure_eq, HexBinary};
use sha2::{Digest, Sha256};
use std::{
io::Read,
path::{Path, PathBuf},
};

/// Direct path to a `.wasm` file
/// Stored as `PathBuf` to avoid lifetimes.
Expand Down Expand Up @@ -48,9 +51,12 @@ mod wasm_path {
}

/// Calculate the checksum of the WASM file.
pub fn checksum(&self) -> Result<String, CwEnvError> {
let checksum = self.path().digest()?;
Ok(checksum)
pub fn checksum(&self) -> Result<HexBinary, CwEnvError> {
let mut file = std::fs::File::open(self.path())?;
let mut wasm = Vec::<u8>::new();
file.read_to_end(&mut wasm)?;
let checksum: [u8; 32] = Sha256::digest(wasm).into();
Ok(checksum.into())
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cw-orch-core/src/environment/queriers/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{CodeInfoResponse, ContractInfoResponse};
use cosmwasm_std::{CodeInfoResponse, ContractInfoResponse, HexBinary};
use serde::{de::DeserializeOwned, Serialize};

use crate::{
Expand All @@ -10,7 +10,7 @@ use crate::{
use super::{Querier, QueryHandler};

pub trait WasmQuerier: Querier {
fn code_id_hash(&self, code_id: u64) -> Result<String, Self::Error>;
fn code_id_hash(&self, code_id: u64) -> Result<HexBinary, Self::Error>;

/// Query contract info
fn contract_info(
Expand All @@ -37,7 +37,7 @@ pub trait WasmQuerier: Querier {
/// Returns the checksum of the WASM file if the env supports it. Will re-upload every time if not supported.
fn local_hash<Chain: TxHandler + QueryHandler, T: Uploadable + ContractInstance<Chain>>(
contract: &T,
) -> Result<String, CwEnvError> {
) -> Result<HexBinary, CwEnvError> {
contract.wasm().checksum()
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cw-orch-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cosmwasm-std = { workspace = true }
cw-multi-test = { workspace = true }
cw-utils = { workspace = true }
serde = { workspace = true }
sha256 = { workspace = true }
sha2 = { workspace = true }
log.workspace = true

[dev-dependencies]
Expand Down
19 changes: 12 additions & 7 deletions packages/cw-orch-mock/src/queriers/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cw_orch_core::{
CwEnvError,
};
use serde::{de::DeserializeOwned, Serialize};
use sha2::{Digest, Sha256};

use crate::{core::MockApp, MockBase};

Expand All @@ -34,9 +35,12 @@ impl<A: Api, S: StateInterface> QuerierGetter<MockWasmQuerier<A>> for MockBase<A
}
}

fn code_id_hash<A: Api>(querier: &MockWasmQuerier<A>, code_id: u64) -> Result<String, CwEnvError> {
fn code_id_hash<A: Api>(
querier: &MockWasmQuerier<A>,
code_id: u64,
) -> Result<HexBinary, CwEnvError> {
let code_info = querier.app.borrow().wrap().query_wasm_code_info(code_id)?;
Ok(code_info.checksum.to_hex())
Ok(code_info.checksum)
}

fn contract_info<A: Api>(
Expand All @@ -53,10 +57,11 @@ fn contract_info<A: Api>(

fn local_hash<Chain: TxHandler + QueryHandler, T: Uploadable + ContractInstance<Chain>>(
contract: &T,
) -> Result<String, CwEnvError> {
) -> Result<HexBinary, CwEnvError> {
// We return the hashed contract-id.
// This will cause the logic to never re-upload a contract if it has the same contract-id.
Ok(sha256::digest(contract.id().as_bytes()))
let hash: [u8; 32] = Sha256::digest(contract.id()).into();
Ok(hash.into())
}

fn raw_query<A: Api>(
Expand Down Expand Up @@ -112,7 +117,7 @@ fn code<A: Api>(

impl<A: Api> WasmQuerier for MockWasmQuerier<A> {
/// Returns the hex-encoded checksum of the code.
fn code_id_hash(&self, code_id: u64) -> Result<String, CwEnvError> {
fn code_id_hash(&self, code_id: u64) -> Result<HexBinary, CwEnvError> {
code_id_hash(self, code_id)
}

Expand All @@ -126,7 +131,7 @@ impl<A: Api> WasmQuerier for MockWasmQuerier<A> {

fn local_hash<Chain: TxHandler + QueryHandler, T: Uploadable + ContractInstance<Chain>>(
contract: &T,
) -> Result<String, CwEnvError> {
) -> Result<HexBinary, CwEnvError> {
local_hash(contract)
}

Expand Down Expand Up @@ -172,7 +177,7 @@ impl<A: Api> WasmQuerier for MockWasmQuerier<A> {
))
} else {
// if bech32 mock
let checksum = HexBinary::from_hex(&self.code_id_hash(code_id)?)?;
let checksum = self.code_id_hash(code_id)?;
let canon_creator = self.app.borrow().api().addr_canonicalize(&creator.into())?;
let canonical_addr = instantiate2_address(checksum.as_slice(), &canon_creator, &salt)?;
Ok(self
Expand Down