Skip to content

Commit f4d5053

Browse files
authored
Merge pull request #1561 from CosmWasm/code-query
Add WasmQuery::CodeInfo to get checksum for code ID
2 parents d6f5dfc + 224b80c commit f4d5053

File tree

9 files changed

+114
-12
lines changed

9 files changed

+114
-12
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

devtools/check_workspace.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ cargo fmt
77
(cd packages/derive && cargo check && cargo clippy --all-targets -- -D warnings)
88
(
99
cd packages/std
10+
# default, min, all
1011
cargo check
11-
cargo check --features iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2
12+
cargo check --no-default-features
13+
cargo check --features abort,iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2
1214
cargo wasm-debug
1315
cargo wasm-debug --features iterator,staking,stargate
1416
cargo clippy --all-targets --features iterator,staking,stargate -- -D warnings

devtools/test_workspace.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ command -v shellcheck >/dev/null && shellcheck "$0"
44

55
cargo fmt
66
(cd packages/crypto && cargo test)
7-
(cd packages/std && cargo test --features iterator)
7+
(cd packages/std && cargo test --features iterator,cosmwasm_1_1,cosmwasm_1_2)
88
(cd packages/storage && cargo test --features iterator)
99
(cd packages/schema && cargo test)
1010
(cd packages/schema-derive && cargo test)

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

44
use crate::Binary;
5+
#[cfg(feature = "cosmwasm_1_2")]
6+
use crate::HexBinary;
57

68
use super::query_response::QueryResponseType;
79

@@ -26,6 +28,9 @@ pub enum WasmQuery {
2628
},
2729
/// Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime
2830
ContractInfo { contract_addr: String },
31+
/// Returns a [`CodeInfoResponse`] with metadata of the code
32+
#[cfg(feature = "cosmwasm_1_2")]
33+
CodeInfo { code_id: u64 },
2934
}
3035

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

packages/std/src/testing/mock.rs

Lines changed: 62 additions & 10 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
}
@@ -1381,7 +1390,7 @@ mod tests {
13811390

13821391
let any_addr = "foo".to_string();
13831392

1384-
// Query WasmQuery::Raw
1393+
// By default, querier errors for WasmQuery::Raw
13851394
let system_err = querier
13861395
.query(&WasmQuery::Raw {
13871396
contract_addr: any_addr.clone(),
@@ -1393,7 +1402,7 @@ mod tests {
13931402
err => panic!("Unexpected error: {:?}", err),
13941403
}
13951404

1396-
// Query WasmQuery::Smart
1405+
// By default, querier errors for WasmQuery::Smart
13971406
let system_err = querier
13981407
.query(&WasmQuery::Smart {
13991408
contract_addr: any_addr.clone(),
@@ -1405,7 +1414,7 @@ mod tests {
14051414
err => panic!("Unexpected error: {:?}", err),
14061415
}
14071416

1408-
// Query WasmQuery::ContractInfo
1417+
// By default, querier errors for WasmQuery::ContractInfo
14091418
let system_err = querier
14101419
.query(&WasmQuery::ContractInfo {
14111420
contract_addr: any_addr.clone(),
@@ -1416,6 +1425,18 @@ mod tests {
14161425
err => panic!("Unexpected error: {:?}", err),
14171426
}
14181427

1428+
#[cfg(feature = "cosmwasm_1_2")]
1429+
{
1430+
// By default, querier errors for WasmQuery::CodeInfo
1431+
let system_err = querier
1432+
.query(&WasmQuery::CodeInfo { code_id: 4 })
1433+
.unwrap_err();
1434+
match system_err {
1435+
SystemError::NoSuchCode { code_id } => assert_eq!(code_id, 4),
1436+
err => panic!("Unexpected error: {:?}", err),
1437+
}
1438+
}
1439+
14191440
querier.update_handler(|request| {
14201441
let constract1 = Addr::unchecked("contract1");
14211442
let mut storage1 = HashMap::<Binary, Binary>::default();
@@ -1469,6 +1490,24 @@ mod tests {
14691490
})
14701491
}
14711492
}
1493+
#[cfg(feature = "cosmwasm_1_2")]
1494+
WasmQuery::CodeInfo { code_id } => {
1495+
use crate::{CodeInfoResponse, HexBinary};
1496+
let code_id = *code_id;
1497+
if code_id == 4 {
1498+
let response = CodeInfoResponse {
1499+
code_id,
1500+
creator: "lalala".into(),
1501+
checksum: HexBinary::from_hex(
1502+
"84cf20810fd429caf58898c3210fcb71759a27becddae08dbde8668ea2f4725d",
1503+
)
1504+
.unwrap(),
1505+
};
1506+
SystemResult::Ok(ContractResult::Ok(to_binary(&response).unwrap()))
1507+
} else {
1508+
SystemResult::Err(SystemError::NoSuchCode { code_id })
1509+
}
1510+
}
14721511
}
14731512
});
14741513

@@ -1525,6 +1564,19 @@ mod tests {
15251564
),
15261565
res => panic!("Unexpected result: {:?}", res),
15271566
}
1567+
1568+
// WasmQuery::ContractInfo
1569+
#[cfg(feature = "cosmwasm_1_2")]
1570+
{
1571+
let result = querier.query(&WasmQuery::CodeInfo { code_id: 4 });
1572+
match result {
1573+
SystemResult::Ok(ContractResult::Ok(value)) => assert_eq!(
1574+
value,
1575+
br#"{"code_id":4,"creator":"lalala","checksum":"84cf20810fd429caf58898c3210fcb71759a27becddae08dbde8668ea2f4725d"}"#
1576+
),
1577+
res => panic!("Unexpected result: {:?}", res),
1578+
}
1579+
}
15281580
}
15291581

15301582
#[test]

packages/std/src/traits.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::coin::Coin;
88
use crate::errors::{RecoverPubkeyError, StdError, StdResult, VerificationError};
99
#[cfg(feature = "iterator")]
1010
use crate::iterator::{Order, Record};
11+
#[cfg(feature = "cosmwasm_1_2")]
12+
use crate::query::CodeInfoResponse;
1113
#[cfg(feature = "cosmwasm_1_1")]
1214
use crate::query::SupplyResponse;
1315
use crate::query::{
@@ -304,6 +306,13 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
304306
self.query(&request)
305307
}
306308

309+
/// Given a code ID, query information about that code.
310+
#[cfg(feature = "cosmwasm_1_2")]
311+
pub fn query_wasm_code_info(&self, code_id: u64) -> StdResult<CodeInfoResponse> {
312+
let request = WasmQuery::CodeInfo { code_id }.into();
313+
self.query(&request)
314+
}
315+
307316
#[cfg(feature = "staking")]
308317
pub fn query_all_validators(&self) -> StdResult<Vec<Validator>> {
309318
let request = StakingQuery::AllValidators {}.into();

0 commit comments

Comments
 (0)