Skip to content

Commit

Permalink
Merge pull request #1886 from CosmWasm/1885-more-serialization
Browse files Browse the repository at this point in the history
Add `to_json_{vec,binary,string}` and `from_json` and deprecate old ones
  • Loading branch information
chipshort authored Sep 22, 2023
2 parents 6d3f9b0 + 2007e7a commit 09555ac
Show file tree
Hide file tree
Showing 56 changed files with 560 additions and 490 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ and this project adheres to
- cosmwasm-std: Add `is_negative` for `Int{64,128,256,512}` ([#1867]).
- cosmwasm-std: Add `TryFrom<Uint{256,512}> for Uint64` and
`TryFrom<Uint{A}> for Int{B}` where `A >= B` ([#1870]).
- cosmwasm-std: Add `to_json_{vec,binary,string}` and `from_json` and deprecate
`to_{vec,binary}` in favor of `to_json_{vec,binary}` and `from_{slice,binary}`
in favor of `from_json`. ([#1886])

[#1854]: https://github.com/CosmWasm/cosmwasm/pull/1854
[#1861]: https://github.com/CosmWasm/cosmwasm/pull/1861
[#1866]: https://github.com/CosmWasm/cosmwasm/pull/1866
[#1867]: https://github.com/CosmWasm/cosmwasm/pull/1867
[#1870]: https://github.com/CosmWasm/cosmwasm/pull/1870
[#1886]: https://github.com/CosmWasm/cosmwasm/pull/1886

## [1.4.0] - 2023-09-04

Expand Down
42 changes: 21 additions & 21 deletions contracts/crypto-verify/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, QueryResponse, Response,
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, QueryResponse, Response,
StdError, StdResult, Uint128,
};
use sha2::{Digest, Sha256};
Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
message,
signature,
public_key,
} => to_binary(&query_verify_cosmos(
} => to_json_binary(&query_verify_cosmos(
deps,
&message.0,
&signature.0,
Expand All @@ -42,7 +42,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
message,
signature,
signer_address,
} => to_binary(&query_verify_ethereum_text(
} => to_json_binary(&query_verify_ethereum_text(
deps,
&message,
&signature,
Expand All @@ -60,14 +60,14 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
r,
s,
v,
} => to_binary(&query_verify_ethereum_transaction(
} => to_json_binary(&query_verify_ethereum_transaction(
deps, from, to, nonce, gas_limit, gas_price, value, data, chain_id, r, s, v,
)?),
QueryMsg::VerifyTendermintSignature {
message,
signature,
public_key,
} => to_binary(&query_verify_tendermint(
} => to_json_binary(&query_verify_tendermint(
deps,
&message.0,
&signature.0,
Expand All @@ -77,13 +77,13 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
messages,
signatures,
public_keys,
} => to_binary(&query_verify_tendermint_batch(
} => to_json_binary(&query_verify_tendermint_batch(
deps,
&messages,
&signatures,
&public_keys,
)?),
QueryMsg::ListVerificationSchemes {} => to_binary(&query_list_verifications(deps)?),
QueryMsg::ListVerificationSchemes {} => to_json_binary(&query_list_verifications(deps)?),
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ mod tests {
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{
from_slice, Binary, OwnedDeps, RecoverPubkeyError, StdError, VerificationError,
from_json, Binary, OwnedDeps, RecoverPubkeyError, StdError, VerificationError,
};
use hex_literal::hex;

Expand Down Expand Up @@ -274,7 +274,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand All @@ -296,7 +296,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: false });
}
Expand Down Expand Up @@ -339,7 +339,7 @@ mod tests {
signer_address: signer_address.into(),
};
let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand All @@ -360,7 +360,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();
assert_eq!(res, VerifyResponse { verifies: false });
}

Expand All @@ -380,7 +380,7 @@ mod tests {
signer_address: signer_address.into(),
};
let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();
assert_eq!(res, VerifyResponse { verifies: false });

// Broken signature
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
v,
};
let raw = query(deps.as_ref(), mock_env(), msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();
assert_eq!(res, VerifyResponse { verifies: true });
}

Expand Down Expand Up @@ -475,7 +475,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand Down Expand Up @@ -508,7 +508,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand Down Expand Up @@ -542,7 +542,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand Down Expand Up @@ -573,7 +573,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: false });
}
Expand Down Expand Up @@ -626,7 +626,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand All @@ -648,7 +648,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw).unwrap();
let res: VerifyResponse = from_json(raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: false });
}
Expand Down Expand Up @@ -683,7 +683,7 @@ mod tests {
let query_msg = QueryMsg::ListVerificationSchemes {};

let raw = query(deps.as_ref(), mock_env(), query_msg).unwrap();
let res: ListVerificationsResponse = from_slice(&raw).unwrap();
let res: ListVerificationsResponse = from_json(raw).unwrap();

assert_eq!(
res,
Expand Down
20 changes: 10 additions & 10 deletions contracts/cyberpunk/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
entry_point, to_binary, Api, DenomMetadata, Deps, DepsMut, Empty, Env, MessageInfo,
entry_point, to_json_binary, Api, DenomMetadata, Deps, DepsMut, Empty, Env, MessageInfo,
PageRequest, QueryResponse, Response, StdError, StdResult, WasmMsg,
};

Expand Down Expand Up @@ -94,7 +94,7 @@ fn execute_memory_loop() -> Result<Response, ContractError> {
fn execute_message_loop(env: Env) -> Result<Response, ContractError> {
let resp = Response::new().add_message(WasmMsg::Execute {
contract_addr: env.contract.address.into(),
msg: to_binary(&ExecuteMsg::MessageLoop {})?,
msg: to_json_binary(&ExecuteMsg::MessageLoop {})?,
funds: vec![],
});
Ok(resp)
Expand Down Expand Up @@ -148,7 +148,7 @@ fn execute_unreachable() -> Result<Response, ContractError> {
}

fn execute_mirror_env(env: Env) -> Result<Response, ContractError> {
Ok(Response::new().set_data(to_binary(&env)?))
Ok(Response::new().set_data(to_json_binary(&env)?))
}

fn execute_debug(api: &dyn Api) -> Result<Response, ContractError> {
Expand Down Expand Up @@ -183,9 +183,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
use QueryMsg::*;

match msg {
MirrorEnv {} => to_binary(&query_mirror_env(env)),
Denoms {} => to_binary(&query_denoms(deps)?),
Denom { denom } => to_binary(&query_denom(deps, denom)?),
MirrorEnv {} => to_json_binary(&query_mirror_env(env)),
Denoms {} => to_json_binary(&query_denoms(deps)?),
Denom { denom } => to_json_binary(&query_denom(deps, denom)?),
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ mod tests {
use cosmwasm_std::testing::{
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{from_binary, DenomMetadata, DenomUnit, OwnedDeps};
use cosmwasm_std::{from_json, DenomMetadata, DenomUnit, OwnedDeps};

fn setup() -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
let mut deps = mock_dependencies();
Expand Down Expand Up @@ -274,12 +274,12 @@ mod tests {
);

let symbols: Vec<DenomMetadata> =
from_binary(&query(deps.as_ref(), mock_env(), QueryMsg::Denoms {}).unwrap()).unwrap();
from_json(query(deps.as_ref(), mock_env(), QueryMsg::Denoms {}).unwrap()).unwrap();

assert_eq!(symbols.len(), 98);

let denom: DenomMetadata = from_binary(
&query(
let denom: DenomMetadata = from_json(
query(
deps.as_ref(),
mock_env(),
QueryMsg::Denom {
Expand Down
6 changes: 3 additions & 3 deletions contracts/cyberpunk/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! });
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)
use cosmwasm_std::{from_binary, Empty, Env, Response};
use cosmwasm_std::{from_json, Empty, Env, Response};
use cosmwasm_vm::testing::{
execute, instantiate, mock_env, mock_info, mock_instance, mock_instance_with_gas_limit, query,
};
Expand Down Expand Up @@ -152,13 +152,13 @@ fn test_env() {
)
.unwrap();

let received_env: Env = from_binary(&res.data.unwrap()).unwrap();
let received_env: Env = from_json(res.data.unwrap()).unwrap();

assert_eq!(received_env, env);

let env = mock_env();
let received_env: Env =
from_binary(&query(&mut deps, env.clone(), QueryMsg::MirrorEnv {}).unwrap()).unwrap();
from_json(query(&mut deps, env.clone(), QueryMsg::MirrorEnv {}).unwrap()).unwrap();

assert_eq!(received_env, env);
}
18 changes: 9 additions & 9 deletions contracts/floaty/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
entry_point, from_slice, to_binary, to_vec, AllBalanceResponse, BankMsg, Deps, DepsMut, Env,
Event, MessageInfo, QueryResponse, Response, StdError, StdResult,
entry_point, from_json, to_json_binary, to_json_vec, AllBalanceResponse, BankMsg, Deps,
DepsMut, Env, Event, MessageInfo, QueryResponse, Response, StdError, StdResult,
};

use crate::errors::HackError;
Expand All @@ -18,7 +18,7 @@ pub fn instantiate(

deps.storage.set(
CONFIG_KEY,
&to_vec(&State {
&to_json_vec(&State {
verifier: deps.api.addr_validate(&msg.verifier)?,
beneficiary: deps.api.addr_validate(&msg.beneficiary)?,
funder: info.sender,
Expand All @@ -40,7 +40,7 @@ pub fn execute(
.storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("State"))?;
let state: State = from_slice(&data)?;
let state: State = from_json(data)?;

if info.sender == state.verifier {
let to_addr = state.beneficiary;
Expand All @@ -67,8 +67,8 @@ pub fn execute(

pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
match msg {
QueryMsg::Verifier {} => to_binary(&query_verifier(deps)?),
QueryMsg::OtherBalance { address } => to_binary(&query_other_balance(deps, address)?),
QueryMsg::Verifier {} => to_json_binary(&query_verifier(deps)?),
QueryMsg::OtherBalance { address } => to_json_binary(&query_other_balance(deps, address)?),
}
}

Expand All @@ -77,7 +77,7 @@ fn query_verifier(deps: Deps) -> StdResult<VerifierResponse> {
.storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("State"))?;
let state: State = from_slice(&data)?;
let state: State = from_json(data)?;
Ok(VerifierResponse {
verifier: state.verifier.into(),
})
Expand Down Expand Up @@ -124,7 +124,7 @@ mod tests {

// it worked, let's check the state
let data = deps.storage.get(CONFIG_KEY).expect("no data stored");
let state: State = from_slice(&data).unwrap();
let state: State = from_json(data).unwrap();
assert_eq!(state, expected_state);
}

Expand Down Expand Up @@ -246,7 +246,7 @@ mod tests {

// state should not change
let data = deps.storage.get(CONFIG_KEY).expect("no data stored");
let state: State = from_slice(&data).unwrap();
let state: State = from_json(data).unwrap();
assert_eq!(
state,
State {
Expand Down
Loading

0 comments on commit 09555ac

Please sign in to comment.