Skip to content

Commit

Permalink
Merge pull request #1561 from CosmWasm/code-query
Browse files Browse the repository at this point in the history
Add WasmQuery::CodeInfo to get checksum for code ID
  • Loading branch information
webmaster128 authored Jan 5, 2023
2 parents d6f5dfc + 224b80c commit f4d5053
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to
as the error type for `ibc_packet_receive` or `ibc_packet_ack` to gain
confidence that the implementations never errors and the transaction does not
get reverted. ([#1513])
- cosmwasm-std: Add new `WasmQuery::CodeInfo` to get the checksum of a code ID
([#1561]).

[#1436]: https://github.com/CosmWasm/cosmwasm/issues/1436
[#1437]: https://github.com/CosmWasm/cosmwasm/issues/1437
Expand All @@ -40,6 +42,7 @@ and this project adheres to
[#1552]: https://github.com/CosmWasm/cosmwasm/pull/1552
[#1554]: https://github.com/CosmWasm/cosmwasm/pull/1554
[#1560]: https://github.com/CosmWasm/cosmwasm/pull/1560
[#1561]: https://github.com/CosmWasm/cosmwasm/pull/1561

### Changed

Expand Down
4 changes: 3 additions & 1 deletion devtools/check_workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ cargo fmt
(cd packages/derive && cargo check && cargo clippy --all-targets -- -D warnings)
(
cd packages/std
# default, min, all
cargo check
cargo check --features iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2
cargo check --no-default-features
cargo check --features abort,iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2
cargo wasm-debug
cargo wasm-debug --features iterator,staking,stargate
cargo clippy --all-targets --features iterator,staking,stargate -- -D warnings
Expand Down
2 changes: 1 addition & 1 deletion devtools/test_workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ command -v shellcheck >/dev/null && shellcheck "$0"

cargo fmt
(cd packages/crypto && cargo test)
(cd packages/std && cargo test --features iterator)
(cd packages/std && cargo test --features iterator,cosmwasm_1_1,cosmwasm_1_2)
(cd packages/storage && cargo test --features iterator)
(cd packages/schema && cargo test)
(cd packages/schema-derive && cargo test)
Expand Down
6 changes: 6 additions & 0 deletions packages/std/src/errors/system_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ pub enum SystemError {
/// The address that was attempted to query
addr: String,
},
/// A Wasm code was not found.
NoSuchCode {
/// The code ID that is missing
code_id: u64,
},
Unknown {},
UnsupportedRequest {
kind: String,
Expand All @@ -52,6 +57,7 @@ impl std::fmt::Display for SystemError {
String::from_utf8_lossy(response)
),
SystemError::NoSuchContract { addr } => write!(f, "No such contract: {}", addr),
SystemError::NoSuchCode { code_id } => write!(f, "No such code: {}", code_id),
SystemError::Unknown {} => write!(f, "Unknown system error"),
SystemError::UnsupportedRequest { kind } => {
write!(f, "Unsupported query type: {}", kind)
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub use crate::math::{
Uint256, Uint512, Uint64,
};
pub use crate::never::Never;
#[cfg(feature = "cosmwasm_1_2")]
pub use crate::query::CodeInfoResponse;
#[cfg(feature = "cosmwasm_1_1")]
pub use crate::query::SupplyResponse;
pub use crate::query::{
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub use staking::{
AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, Delegation,
DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorResponse,
};
#[cfg(feature = "cosmwasm_1_2")]
pub use wasm::CodeInfoResponse;
pub use wasm::{ContractInfoResponse, WasmQuery};

#[non_exhaustive]
Expand Down
26 changes: 26 additions & 0 deletions packages/std/src/query/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::Binary;
#[cfg(feature = "cosmwasm_1_2")]
use crate::HexBinary;

use super::query_response::QueryResponseType;

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

#[non_exhaustive]
Expand Down Expand Up @@ -61,3 +66,24 @@ impl ContractInfoResponse {
}
}
}

/// The essential data from wasmd's [CodeInfo]/[CodeInfoResponse].
///
/// `code_hash`/`data_hash` was renamed to `checksum` to follow the CosmWasm
/// convention and naming in `instantiate2_address`.
///
/// [CodeInfo]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/types.proto#L62-L72
/// [CodeInfoResponse]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L184-L199
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
#[cfg(feature = "cosmwasm_1_2")]
pub struct CodeInfoResponse {
pub code_id: u64,
/// The address that initially stored the code
pub creator: String,
/// The hash of the Wasm blob
pub checksum: HexBinary,
}

#[cfg(feature = "cosmwasm_1_2")]
impl QueryResponseType for CodeInfoResponse {}
72 changes: 62 additions & 10 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,22 @@ impl WasmQuerier {
impl Default for WasmQuerier {
fn default() -> Self {
let handler = Box::from(|request: &WasmQuery| -> QuerierResult {
let addr = match request {
WasmQuery::Smart { contract_addr, .. } => contract_addr,
WasmQuery::Raw { contract_addr, .. } => contract_addr,
WasmQuery::ContractInfo { contract_addr, .. } => contract_addr,
}
.clone();
SystemResult::Err(SystemError::NoSuchContract { addr })
let err = match request {
WasmQuery::Smart { contract_addr, .. } => SystemError::NoSuchContract {
addr: contract_addr.clone(),
},
WasmQuery::Raw { contract_addr, .. } => SystemError::NoSuchContract {
addr: contract_addr.clone(),
},
WasmQuery::ContractInfo { contract_addr, .. } => SystemError::NoSuchContract {
addr: contract_addr.clone(),
},
#[cfg(feature = "cosmwasm_1_2")]
WasmQuery::CodeInfo { code_id, .. } => {
SystemError::NoSuchCode { code_id: *code_id }
}
};
SystemResult::Err(err)
});
Self::new(handler)
}
Expand Down Expand Up @@ -1381,7 +1390,7 @@ mod tests {

let any_addr = "foo".to_string();

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

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

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

#[cfg(feature = "cosmwasm_1_2")]
{
// By default, querier errors for WasmQuery::CodeInfo
let system_err = querier
.query(&WasmQuery::CodeInfo { code_id: 4 })
.unwrap_err();
match system_err {
SystemError::NoSuchCode { code_id } => assert_eq!(code_id, 4),
err => panic!("Unexpected error: {:?}", err),
}
}

querier.update_handler(|request| {
let constract1 = Addr::unchecked("contract1");
let mut storage1 = HashMap::<Binary, Binary>::default();
Expand Down Expand Up @@ -1469,6 +1490,24 @@ mod tests {
})
}
}
#[cfg(feature = "cosmwasm_1_2")]
WasmQuery::CodeInfo { code_id } => {
use crate::{CodeInfoResponse, HexBinary};
let code_id = *code_id;
if code_id == 4 {
let response = CodeInfoResponse {
code_id,
creator: "lalala".into(),
checksum: HexBinary::from_hex(
"84cf20810fd429caf58898c3210fcb71759a27becddae08dbde8668ea2f4725d",
)
.unwrap(),
};
SystemResult::Ok(ContractResult::Ok(to_binary(&response).unwrap()))
} else {
SystemResult::Err(SystemError::NoSuchCode { code_id })
}
}
}
});

Expand Down Expand Up @@ -1525,6 +1564,19 @@ mod tests {
),
res => panic!("Unexpected result: {:?}", res),
}

// WasmQuery::ContractInfo
#[cfg(feature = "cosmwasm_1_2")]
{
let result = querier.query(&WasmQuery::CodeInfo { code_id: 4 });
match result {
SystemResult::Ok(ContractResult::Ok(value)) => assert_eq!(
value,
br#"{"code_id":4,"creator":"lalala","checksum":"84cf20810fd429caf58898c3210fcb71759a27becddae08dbde8668ea2f4725d"}"#
),
res => panic!("Unexpected result: {:?}", res),
}
}
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::coin::Coin;
use crate::errors::{RecoverPubkeyError, StdError, StdResult, VerificationError};
#[cfg(feature = "iterator")]
use crate::iterator::{Order, Record};
#[cfg(feature = "cosmwasm_1_2")]
use crate::query::CodeInfoResponse;
#[cfg(feature = "cosmwasm_1_1")]
use crate::query::SupplyResponse;
use crate::query::{
Expand Down Expand Up @@ -304,6 +306,13 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
self.query(&request)
}

/// Given a code ID, query information about that code.
#[cfg(feature = "cosmwasm_1_2")]
pub fn query_wasm_code_info(&self, code_id: u64) -> StdResult<CodeInfoResponse> {
let request = WasmQuery::CodeInfo { code_id }.into();
self.query(&request)
}

#[cfg(feature = "staking")]
pub fn query_all_validators(&self) -> StdResult<Vec<Validator>> {
let request = StakingQuery::AllValidators {}.into();
Expand Down

0 comments on commit f4d5053

Please sign in to comment.