Skip to content

Commit

Permalink
Merge pull request #1162 from scrtlabs/fix-v010-env-block-time
Browse files Browse the repository at this point in the history
Fix v0.10 `env.block.time`
  • Loading branch information
assafmo authored Sep 11, 2022
2 parents 652d804 + 5506e41 commit 77952ad
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 17 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ ias_bin_sw.go
node_modules
/secretjs
/secret.js
tmp-swagger-gen
tmp-swagger-gen
x/compute/internal/keeper/testdata/contract_with_floats.wasm
x/compute/internal/keeper/testdata/contract.wasm
x/compute/internal/keeper/testdata/ibc.wasm
x/compute/internal/keeper/testdata/static-too-high-initial-memory.wasm
x/compute/internal/keeper/testdata/too-high-initial-memory.wasm
x/compute/internal/keeper/testdata/v1-contract.wasm
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"rust-analyzer.linkedProjects": [
"cosmwasm/Cargo.toml",
"cosmwasm/enclaves/Cargo.toml",
"x/compute/internal/keeper/testdata/v1-sanity-contract/Cargo.toml",
"x/compute/internal/keeper/testdata/test-contract/Cargo.toml",
"./x/compute/internal/keeper/testdata/ibc/Cargo.toml",
"cosmwasm/contracts/v1/compute-tests/test-compute-contract/Cargo.toml",
"cosmwasm/contracts/v1/compute-tests/ibc-test-contract/Cargo.toml",
"cosmwasm/contracts/v010/compute-tests/test-compute-contract/Cargo.toml",
"integration-tests/contract-v1/Cargo.toml",
"integration-tests/contract-v0.10/Cargo.toml",
"go-cosmwasm/Cargo.toml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub enum InitMsg {
to: HumanAddr,
code_hash: String,
},
GetEnv {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -318,6 +319,7 @@ pub enum HandleMsg {
},
CosmosMsgCustom {},
InitNewContract {},
GetEnv {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -546,7 +548,11 @@ pub fn init<S: Storage, A: Api, Q: Querier>(
messages: vec![CosmosMsg::Custom(Empty {})],
log: vec![],
}),
InitMsg::SendMultipleFundsToExecCallback { coins, to, code_hash } => Ok(InitResponse {
InitMsg::SendMultipleFundsToExecCallback {
coins,
to,
code_hash,
} => Ok(InitResponse {
messages: vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: to,
msg: Binary::from("{\"no_data\":{}}".as_bytes().to_vec()),
Expand All @@ -555,16 +561,24 @@ pub fn init<S: Storage, A: Api, Q: Querier>(
})],
log: vec![],
}),
InitMsg::SendMultipleFundsToInitCallback { coins, code_id, code_hash } => Ok(InitResponse {
InitMsg::SendMultipleFundsToInitCallback {
coins,
code_id,
code_hash,
} => Ok(InitResponse {
messages: vec![CosmosMsg::Wasm(WasmMsg::Instantiate {
code_id,
msg: Binary::from("{\"nop\":{}}".as_bytes().to_vec()),
callback_code_hash: code_hash,
send: coins,
label: "test".to_string()
label: "test".to_string(),
})],
log: vec![],
})
}),
InitMsg::GetEnv {} => Ok(InitResponse {
log: vec![log("env", serde_json_wasm::to_string(&env).unwrap())],
messages: vec![],
}),
}
}

Expand Down Expand Up @@ -1243,7 +1257,11 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
log: vec![],
data: None,
}),
HandleMsg::SendMultipleFundsToExecCallback { coins, to, code_hash } => Ok(HandleResponse {
HandleMsg::SendMultipleFundsToExecCallback {
coins,
to,
code_hash,
} => Ok(HandleResponse {
messages: vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: to,
msg: Binary::from("{\"no_data\":{}}".as_bytes().to_vec()),
Expand All @@ -1253,17 +1271,26 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
log: vec![],
data: None,
}),
HandleMsg::SendMultipleFundsToInitCallback { coins, code_id, code_hash } => Ok(HandleResponse {
HandleMsg::SendMultipleFundsToInitCallback {
coins,
code_id,
code_hash,
} => Ok(HandleResponse {
messages: vec![CosmosMsg::Wasm(WasmMsg::Instantiate {
code_id,
msg: Binary::from("{\"nop\":{}}".as_bytes().to_vec()),
callback_code_hash: code_hash,
send: coins,
label: "test".to_string()
label: "test".to_string(),
})],
log: vec![],
data: None,
})
}),
HandleMsg::GetEnv {} => Ok(HandleResponse {
log: vec![log("env", serde_json_wasm::to_string(&env).unwrap())],
data: None,
messages: vec![],
}),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::state::{count, count_read, expiration, expiration_read};
pub fn instantiate(
deps: DepsMut,
env: Env,
_info: MessageInfo,
info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
match msg {
Expand Down Expand Up @@ -321,6 +321,9 @@ pub fn instantiate(
funds: coins,
})),
),
InstantiateMsg::GetEnv {} => Ok(Response::new()
.add_attribute("env", serde_json_wasm::to_string(&env).unwrap())
.add_attribute("info", serde_json_wasm::to_string(&info).unwrap())),
}
}

Expand Down Expand Up @@ -1168,6 +1171,9 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S
Ok(a) => Ok(Response::new().set_data(a.as_bytes())),
Err(_) => Ok(Response::new().set_data(to_binary("Apple")?)),
},
ExecuteMsg::GetEnv {} => Ok(Response::new()
.add_attribute("env", serde_json_wasm::to_string(&env).unwrap())
.add_attribute("info", serde_json_wasm::to_string(&info).unwrap())),
}
}

Expand Down Expand Up @@ -1737,6 +1743,12 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
let answer: u8 = 1;
return Ok(to_binary(&answer)?);
}
QueryMsg::GetEnv {} => Ok(Binary::from(
serde_json_wasm::to_string(&env)
.unwrap()
.as_bytes()
.to_vec(),
)),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum InstantiateMsg {
to: String,
code_hash: String,
},
GetEnv {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -397,6 +398,7 @@ pub enum ExecuteMsg {
amount: Vec<Coin>,
},
CosmosMsgCustom {},
GetEnv {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -436,6 +438,7 @@ pub enum QueryMsg {
msg: String,
},
GetContractVersion {},
GetEnv {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down
34 changes: 32 additions & 2 deletions cosmwasm/enclaves/shared/cosmwasm-types/generic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,36 @@ impl BaseEnv {
}

fn into_v010(self) -> CwEnv {
CwEnv::V010Env { env: self.0 }
// Assaf: contract_key is irrelevant inside the contract,
// but existing v0.10 contracts might expect it to be populated :facepalm:,
// therefore we are going to leave it populated :shrug:.

// in secretd v1.3 the timestamp passed from Go was unix time in seconds
// from secretd v1.4 the timestamp passed from Go is unix time in nanoseconds
// v0.10 time is seconds since unix epoch
// v1 time is nanoseconds since unix epoch
// so we need to convert it from nanoseconds to seconds

CwEnv::V010Env {
env: V010Env {
block: v010types::BlockInfo {
height: self.0.block.height,
// v0.10 env.block.time is seconds since unix epoch
time: v1types::Timestamp::from_nanos(self.0.block.time).seconds(),
chain_id: self.0.block.chain_id,
},
message: v010types::MessageInfo {
sender: self.0.message.sender,
sent_funds: self.0.message.sent_funds,
},
contract: v010types::ContractInfo {
address: self.0.contract.address,
},
contract_key: self.0.contract_key,
contract_code_hash: self.0.contract_code_hash,
transaction: None,
},
}
}

/// This is the conversion function from the base to the new env. We assume that if there are
Expand All @@ -81,8 +110,9 @@ impl BaseEnv {
env: V1Env {
block: v1types::BlockInfo {
height: self.0.block.height,
// v1 env.block.time is nanoseconds since unix epoch
time: v1types::Timestamp::from_nanos(self.0.block.time),
chain_id: self.0.block.chain_id.clone(),
chain_id: self.0.block.chain_id,
},
contract: v1types::ContractInfo {
address: v1types::Addr::unchecked(self.0.contract.address.0),
Expand Down
2 changes: 1 addition & 1 deletion cosmwasm/enclaves/shared/cosmwasm-types/v0.10/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub struct TransactionInfo {
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq)]
pub struct BlockInfo {
pub height: u64,
// time is seconds since epoch begin (Jan. 1, 1970)
/// Absolute time of the block creation in seconds since the UNIX epoch (00:00:00 on 1970-01-01 UTC).
pub time: u64,
pub chain_id: String,
}
Expand Down
2 changes: 1 addition & 1 deletion cosmwasm/enclaves/shared/cosmwasm-types/v1.0/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Env {
pub struct BlockInfo {
/// The height of a block is the number of blocks preceding it in the blockchain.
pub height: u64,
/// Absolute time of the block creation in seconds since the UNIX epoch (00:00:00 on 1970-01-01 UTC).
/// Absolute time of the block creation in nanoseconds since the UNIX epoch (00:00:00 on 1970-01-01 UTC).
///
/// The source of this is the [BFT Time in Tendermint](https://docs.tendermint.com/master/spec/consensus/bft-time.html),
/// which has the same nanosecond precision as the `Timestamp` type.
Expand Down
Loading

0 comments on commit 77952ad

Please sign in to comment.