Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nanosecond precision for playground canister acquisition timestamps #3501

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ The `dfx cycles` command no longer needs nor accepts the `--cycles-ledger-canist

Updated to candid 0.10, ic-cdk 0.12, and ic-cdk-timers 0.6

### fix: store playground canister acquisition timestamps with nanosecond precision on all platforms

They've always been stored with nanosecond precisions on Linux and Macos.
Now they are stored with nanosecond precision on Windows too.

# 0.15.3

### fix: allow `http://localhost:*` as `connect-src` in the asset canister's CSP
Expand Down
19 changes: 11 additions & 8 deletions src/dfx-core/src/config/model/canister_id_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ pub type CanisterIds = BTreeMap<CanisterName, NetworkNametoCanisterId>;

pub type CanisterTimestamps = BTreeMap<CanisterName, NetworkNametoCanisterTimestamp>;

// OffsetDateTime has nanosecond precision, while SystemTime is OS-dependent (100ns on Windows)
pub type AcquisitionDateTime = OffsetDateTime;

#[derive(Debug, Clone, Default)]
pub struct NetworkNametoCanisterTimestamp(BTreeMap<NetworkName, SystemTime>);
pub struct NetworkNametoCanisterTimestamp(BTreeMap<NetworkName, AcquisitionDateTime>);

impl Deref for NetworkNametoCanisterTimestamp {
type Target = BTreeMap<NetworkName, SystemTime>;
type Target = BTreeMap<NetworkName, AcquisitionDateTime>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -48,7 +51,7 @@ impl Serialize for NetworkNametoCanisterTimestamp {
let out = self.0.iter().map(|(key, time)| {
(
key,
OffsetDateTime::from(*time)
AcquisitionDateTime::from(*time)
.format(&Rfc3339)
.expect("Failed to serialise timestamp"),
)
Expand All @@ -63,12 +66,12 @@ impl<'de> Deserialize<'de> for NetworkNametoCanisterTimestamp {
D: serde::Deserializer<'de>,
{
let map: BTreeMap<NetworkName, String> = Deserialize::deserialize(deserializer)?;
let btree: BTreeMap<NetworkName, SystemTime> = map
let btree: BTreeMap<NetworkName, AcquisitionDateTime> = map
.into_iter()
.map(|(key, timestamp)| (key, OffsetDateTime::parse(&timestamp, &Rfc3339)))
.map(|(key, timestamp)| (key, AcquisitionDateTime::parse(&timestamp, &Rfc3339)))
.try_fold(BTreeMap::new(), |mut map, (key, result)| match result {
Ok(value) => {
map.insert(key, SystemTime::from(value));
map.insert(key, value);
Ok(map)
}
Err(err) => Err(err),
Expand Down Expand Up @@ -178,7 +181,7 @@ impl CanisterIdStore {
Ok(store)
}

pub fn get_timestamp(&self, canister_name: &str) -> Option<&SystemTime> {
pub fn get_timestamp(&self, canister_name: &str) -> Option<&AcquisitionDateTime> {
self.acquisition_timestamps
.get(canister_name)
.and_then(|timestamp_map| timestamp_map.get(&self.network_descriptor.name))
Expand Down Expand Up @@ -277,7 +280,7 @@ impl CanisterIdStore {
&mut self,
canister_name: &str,
canister_id: &str,
timestamp: Option<SystemTime>,
timestamp: Option<AcquisitionDateTime>,
) -> Result<(), CanisterIdStoreError> {
let network_name = &self.network_descriptor.name;
match self.ids.get_mut(canister_name) {
Expand Down
43 changes: 20 additions & 23 deletions src/dfx/src/lib/operations/canister/motoko_playground.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::lib::{environment::Environment, error::DfxResult};
use anyhow::{bail, Context};
use candid::{encode_args, CandidType, Decode, Deserialize, Encode, Principal};
use dfx_core::config::model::canister_id_store::AcquisitionDateTime;
use dfx_core::config::model::network_descriptor::{
NetworkTypeDescriptor, MAINNET_MOTOKO_PLAYGROUND_CANISTER_ID,
};
use num_traits::ToPrimitive;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{bail, Context};
use candid::{encode_args, CandidType, Decode, Deserialize, Encode, Principal};
use fn_error_context::context;
use ic_utils::interfaces::management_canister::builders::InstallMode;
use num_traits::ToPrimitive;
use rand::Rng;
use slog::{debug, info};

use crate::lib::{environment::Environment, error::DfxResult};
use std::time::SystemTime;

/// Arguments for the `getCanisterId` call.
#[derive(CandidType)]
Expand All @@ -28,19 +27,17 @@ pub struct CanisterInfo {
}

impl CanisterInfo {
#[context("Failed to construct playground canister info.")]
pub fn from(id: Principal, timestamp: &SystemTime) -> DfxResult<Self> {
let timestamp = candid::Int::from(timestamp.duration_since(UNIX_EPOCH)?.as_nanos());
Ok(Self { id, timestamp })
pub fn from(id: Principal, timestamp: &AcquisitionDateTime) -> Self {
let timestamp = candid::Int::from(timestamp.unix_timestamp_nanos());
Self { id, timestamp }
}

#[context("Failed to turn CanisterInfo into SystemTime")]
pub fn get_timestamp(&self) -> DfxResult<SystemTime> {
UNIX_EPOCH
.checked_add(Duration::from_nanos(
self.timestamp.0.to_u64().context("u64 overflow")?,
))
.context("Failed to make absolute time from offset")
#[context("Failed to get timestamp from CanisterInfo")]
pub fn get_timestamp(&self) -> DfxResult<AcquisitionDateTime> {
AcquisitionDateTime::from_unix_timestamp_nanos(
self.timestamp.0.to_i128().context("i128 overflow")?,
)
.context("Failed to make unix timestamp from nanos")
}
}

Expand Down Expand Up @@ -122,7 +119,7 @@ pub async fn reserve_canister_with_playground(
pub async fn authorize_asset_uploader(
env: &dyn Environment,
canister_id: Principal,
canister_timestamp: &SystemTime,
canister_timestamp: &AcquisitionDateTime,
principal_to_authorize: &Principal,
) -> DfxResult {
let agent = env.get_agent();
Expand All @@ -135,7 +132,7 @@ pub async fn authorize_asset_uploader(
} else {
bail!("Trying to authorize asset uploader on non-playground network.")
};
let canister_info = CanisterInfo::from(canister_id, canister_timestamp)?;
let canister_info = CanisterInfo::from(canister_id, canister_timestamp);

let nested_arg = Encode!(&principal_to_authorize)?;
let call_arg = Encode!(&canister_info, &"authorize", &nested_arg)?;
Expand All @@ -152,13 +149,13 @@ pub async fn authorize_asset_uploader(
pub async fn playground_install_code(
env: &dyn Environment,
canister_id: Principal,
canister_timestamp: &SystemTime,
canister_timestamp: &AcquisitionDateTime,
arg: &[u8],
wasm_module: &[u8],
mode: InstallMode,
is_asset_canister: bool,
) -> DfxResult<SystemTime> {
let canister_info = CanisterInfo::from(canister_id, canister_timestamp)?;
) -> DfxResult<AcquisitionDateTime> {
let canister_info = CanisterInfo::from(canister_id, canister_timestamp);
let agent = env.get_agent();
let playground_canister = match env.get_network_descriptor().r#type {
NetworkTypeDescriptor::Playground {
Expand Down
Loading