Skip to content

Commit dfc584e

Browse files
authored
Merge pull request #887 from CosmWasm/add-Timepoint
Add Timestamp type
2 parents e83b995 + 1242d87 commit dfc584e

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

contracts/ibc-reflect-send/src/contract.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cosmwasm_std::{
2-
attr, entry_point, to_binary, CosmosMsg, Deps, DepsMut, Env, IbcMsg, IbcTimeout, MessageInfo,
3-
Order, QueryResponse, Response, StdError, StdResult,
2+
attr, entry_point, to_binary, CosmosMsg, Deps, DepsMut, Env, IbcMsg, MessageInfo, Order,
3+
QueryResponse, Response, StdError, StdResult,
44
};
55

66
use crate::ibc::PACKET_LIFETIME;
@@ -91,7 +91,7 @@ pub fn handle_send_msgs(
9191
let msg = IbcMsg::SendPacket {
9292
channel_id,
9393
data: to_binary(&packet)?,
94-
timeout: IbcTimeout::in_secs(&env.block, PACKET_LIFETIME),
94+
timeout: env.block.timestamp().plus_seconds(PACKET_LIFETIME).into(),
9595
};
9696

9797
Ok(Response {
@@ -121,7 +121,7 @@ pub fn handle_check_remote_balance(
121121
let msg = IbcMsg::SendPacket {
122122
channel_id,
123123
data: to_binary(&packet)?,
124-
timeout: IbcTimeout::in_secs(&env.block, PACKET_LIFETIME),
124+
timeout: env.block.timestamp().plus_seconds(PACKET_LIFETIME).into(),
125125
};
126126

127127
Ok(Response {
@@ -171,7 +171,7 @@ pub fn handle_send_funds(
171171
channel_id: transfer_channel_id,
172172
to_address: remote_addr,
173173
amount,
174-
timeout: IbcTimeout::in_secs(&env.block, PACKET_LIFETIME),
174+
timeout: env.block.timestamp().plus_seconds(PACKET_LIFETIME).into(),
175175
};
176176

177177
Ok(Response {

contracts/ibc-reflect-send/src/ibc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cosmwasm_std::{
22
attr, entry_point, from_slice, to_binary, DepsMut, Env, IbcAcknowledgement, IbcBasicResponse,
3-
IbcChannel, IbcMsg, IbcOrder, IbcPacket, IbcReceiveResponse, IbcTimeout, StdError, StdResult,
3+
IbcChannel, IbcMsg, IbcOrder, IbcPacket, IbcReceiveResponse, StdError, StdResult,
44
};
55

66
use crate::ibc_msg::{
@@ -58,7 +58,7 @@ pub fn ibc_channel_connect(
5858
let msg = IbcMsg::SendPacket {
5959
channel_id: channel_id.clone(),
6060
data: to_binary(&packet)?,
61-
timeout: IbcTimeout::in_secs(&env.block, PACKET_LIFETIME),
61+
timeout: env.block.timestamp().plus_seconds(PACKET_LIFETIME).into(),
6262
};
6363

6464
Ok(IbcBasicResponse {
@@ -255,7 +255,7 @@ mod tests {
255255
mock_dependencies, mock_env, mock_ibc_channel, mock_ibc_packet_ack, mock_info, MockApi,
256256
MockQuerier, MockStorage,
257257
};
258-
use cosmwasm_std::{coin, coins, BankMsg, CosmosMsg, OwnedDeps};
258+
use cosmwasm_std::{coin, coins, BankMsg, CosmosMsg, IbcTimeout, OwnedDeps};
259259

260260
const CREATOR: &str = "creator";
261261

packages/std/src/ibc.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::fmt;
1010
use crate::binary::Binary;
1111
use crate::coins::Coin;
1212
use crate::results::{Attribute, CosmosMsg, Empty, SubMsg};
13-
use crate::types::BlockInfo;
13+
use crate::timestamp::Timestamp;
1414

1515
/// These are messages in the IBC lifecycle. Only usable by IBC-enabled contracts
1616
/// (contracts that directly speak the IBC protocol via 6 entry points)
@@ -69,11 +69,9 @@ pub enum IbcTimeout {
6969
},
7070
}
7171

72-
impl IbcTimeout {
73-
pub fn in_secs(block: &BlockInfo, secs: u64) -> Self {
74-
let secs = block.time + secs;
75-
let nanos = secs * 1_000_000_000 + block.time_nanos;
76-
IbcTimeout::TimestampNanos(nanos)
72+
impl From<Timestamp> for IbcTimeout {
73+
fn from(time: Timestamp) -> IbcTimeout {
74+
IbcTimeout::TimestampNanos(time.seconds * 1_000_000_000 + time.nanos)
7775
}
7876
}
7977

packages/std/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod results;
1919
mod sections;
2020
mod serde;
2121
mod storage;
22+
mod timestamp;
2223
mod traits;
2324
mod types;
2425

@@ -60,6 +61,7 @@ pub use crate::results::{Context, HandleResponse, InitResponse, MigrateResponse}
6061
pub use crate::results::{DistributionMsg, StakingMsg};
6162
pub use crate::serde::{from_binary, from_slice, to_binary, to_vec};
6263
pub use crate::storage::MemoryStorage;
64+
pub use crate::timestamp::Timestamp;
6365
pub use crate::traits::{Api, Querier, QuerierResult, QuerierWrapper, Storage};
6466
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo};
6567

packages/std/src/timestamp.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// A point in time in nanosecond precision.
2+
///
3+
/// This type cannot represent any time before the UNIX epoch because both fields are unsigned.
4+
pub struct Timestamp {
5+
/// Absolute time in seconds since the UNIX epoch (00:00:00 on 1970-01-01 UTC).
6+
pub seconds: u64,
7+
/// The fractional part time in nanoseconds since `time` (0 to 999999999).
8+
pub nanos: u64,
9+
}
10+
11+
impl Timestamp {
12+
pub fn plus_seconds(&self, addition: u64) -> Timestamp {
13+
Timestamp {
14+
seconds: self.seconds + addition,
15+
nanos: self.nanos,
16+
}
17+
}
18+
}

packages/std/src/types.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
22

33
use crate::addresses::Addr;
44
use crate::coins::Coin;
5+
use crate::timestamp::Timestamp;
56

67
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
78
pub struct Env {
@@ -63,6 +64,16 @@ pub struct BlockInfo {
6364
pub chain_id: String,
6465
}
6566

67+
impl BlockInfo {
68+
/// Returns the block creation time as a Timestamp in nanosecond precision
69+
pub fn timestamp(&self) -> Timestamp {
70+
Timestamp {
71+
seconds: self.time,
72+
nanos: self.time_nanos,
73+
}
74+
}
75+
}
76+
6677
/// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed
6778
/// along with the contract execution message into the `instantiate` and `execute` entry points.
6879
///

0 commit comments

Comments
 (0)