Skip to content

Commit e2814e5

Browse files
committed
Add new WasmQuery::CodeInfo
1 parent 5a6fec1 commit e2814e5

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ and this project adheres to
2929
as the error type for `ibc_packet_receive` or `ibc_packet_ack` to gain
3030
confidence that the implementations never errors and the transaction does not
3131
get reverted. ([#1513])
32+
- cosmwasm-std: Add new `WasmQuery::CodeInfo` to get the checksum of a code ID
33+
([#1561]).
3234

3335
[#1436]: https://github.com/CosmWasm/cosmwasm/issues/1436
3436
[#1437]: https://github.com/CosmWasm/cosmwasm/issues/1437
@@ -40,6 +42,7 @@ and this project adheres to
4042
[#1552]: https://github.com/CosmWasm/cosmwasm/pull/1552
4143
[#1554]: https://github.com/CosmWasm/cosmwasm/pull/1554
4244
[#1560]: https://github.com/CosmWasm/cosmwasm/pull/1560
45+
[#1561]: https://github.com/CosmWasm/cosmwasm/pull/1561
4346

4447
### Changed
4548

packages/std/src/errors/system_error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ pub enum SystemError {
2828
/// The address that was attempted to query
2929
addr: String,
3030
},
31+
/// A Wasm code was not found.
32+
NoSuchCode {
33+
/// The code ID that is missing
34+
code_id: u64,
35+
},
3136
Unknown {},
3237
UnsupportedRequest {
3338
kind: String,
@@ -52,6 +57,7 @@ impl std::fmt::Display for SystemError {
5257
String::from_utf8_lossy(response)
5358
),
5459
SystemError::NoSuchContract { addr } => write!(f, "No such contract: {}", addr),
60+
SystemError::NoSuchCode { code_id } => write!(f, "No such code: {}", code_id),
5561
SystemError::Unknown {} => write!(f, "Unknown system error"),
5662
SystemError::UnsupportedRequest { kind } => {
5763
write!(f, "Unsupported query type: {}", kind)

packages/std/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub use crate::math::{
5050
Uint256, Uint512, Uint64,
5151
};
5252
pub use crate::never::Never;
53+
#[cfg(feature = "cosmwasm_1_2")]
54+
pub use crate::query::CodeInfoResponse;
5355
#[cfg(feature = "cosmwasm_1_1")]
5456
pub use crate::query::SupplyResponse;
5557
pub use crate::query::{

packages/std/src/query/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub use staking::{
2121
AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, Delegation,
2222
DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorResponse,
2323
};
24+
#[cfg(feature = "cosmwasm_1_2")]
25+
pub use wasm::CodeInfoResponse;
2426
pub use wasm::{ContractInfoResponse, WasmQuery};
2527

2628
#[non_exhaustive]

packages/std/src/query/wasm.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub enum WasmQuery {
2626
},
2727
/// Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime
2828
ContractInfo { contract_addr: String },
29+
/// Returns a [`CodeInfoResponse`] with metadata of the code
30+
#[cfg(feature = "cosmwasm_1_2")]
31+
CodeInfo { code_id: u64 },
2932
}
3033

3134
#[non_exhaustive]
@@ -61,3 +64,23 @@ impl ContractInfoResponse {
6164
}
6265
}
6366
}
67+
68+
/// The essential data from wasmd's [CodeInfo]/[CodeInfoResponse].
69+
///
70+
/// `code_hash`/`data_hash` was renamed to `checksum` to follow the CosmWasm
71+
/// convention and naming in `instantiate2_address`.
72+
///
73+
/// [CodeInfo]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/types.proto#L62-L72
74+
/// [CodeInfoResponse]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L184-L199
75+
#[non_exhaustive]
76+
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
77+
#[cfg(feature = "cosmwasm_1_2")]
78+
pub struct CodeInfoResponse {
79+
/// The address that initially stored the code
80+
pub creator: String,
81+
/// The hash of the Wasm blob
82+
pub checksum: Binary,
83+
}
84+
85+
#[cfg(feature = "cosmwasm_1_2")]
86+
impl QueryResponseType for CodeInfoResponse {}

packages/std/src/testing/mock.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,22 @@ impl WasmQuerier {
562562
impl Default for WasmQuerier {
563563
fn default() -> Self {
564564
let handler = Box::from(|request: &WasmQuery| -> QuerierResult {
565-
let addr = match request {
566-
WasmQuery::Smart { contract_addr, .. } => contract_addr,
567-
WasmQuery::Raw { contract_addr, .. } => contract_addr,
568-
WasmQuery::ContractInfo { contract_addr, .. } => contract_addr,
569-
}
570-
.clone();
571-
SystemResult::Err(SystemError::NoSuchContract { addr })
565+
let err = match request {
566+
WasmQuery::Smart { contract_addr, .. } => SystemError::NoSuchContract {
567+
addr: contract_addr.clone(),
568+
},
569+
WasmQuery::Raw { contract_addr, .. } => SystemError::NoSuchContract {
570+
addr: contract_addr.clone(),
571+
},
572+
WasmQuery::ContractInfo { contract_addr, .. } => SystemError::NoSuchContract {
573+
addr: contract_addr.clone(),
574+
},
575+
#[cfg(feature = "cosmwasm_1_2")]
576+
WasmQuery::CodeInfo { code_id, .. } => {
577+
SystemError::NoSuchCode { code_id: *code_id }
578+
}
579+
};
580+
SystemResult::Err(err)
572581
});
573582
Self::new(handler)
574583
}

0 commit comments

Comments
 (0)