Skip to content

Commit a4b98fc

Browse files
chore(ICP-Ledger): remove dfn_core from icp ledger lib (#4095)
Changes: - Remove `DfnRuntime`, move `CdkRuntime` to `ledger_canister_core`. - Use `CdkRuntime` in the ICP ledger. - Use cdk `time` and `trap` in `icp/ledger/src/lib`, fix related tests - `ic_cdk::api::time` cannot be called in tests, have to provide the time with `add_payment_with_timestamp`. - Import `CanisterId` from `ic_base_types` instead of `dfn_core` in `AccountIdentifier`.
1 parent f7539ed commit a4b98fc

File tree

11 files changed

+74
-112
lines changed

11 files changed

+74
-112
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rs/ledger_suite/common/ledger_canister_core/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rust_library(
2121
"//rs/types/management_canister_types",
2222
"//rs/utils",
2323
"@crate_index//:candid",
24+
"@crate_index//:ic-cdk",
2425
"@crate_index//:serde",
2526
],
2627
)

rs/ledger_suite/common/ledger_canister_core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async-trait = { workspace = true }
1111
candid = { workspace = true }
1212
ic-base-types = { path = "../../../types/base_types" }
1313
ic-canister-log = { path = "../../../rust_canisters/canister_log" }
14+
ic-cdk = { workspace = true }
1415
ic-limits = { path = "../../../limits" }
1516
ic-ledger-core = { path = "../ledger_core" }
1617
ic-ledger-hash-of = { path = "../../../../packages/ic-ledger-hash-of" }

rs/ledger_suite/common/ledger_canister_core/src/runtime.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use async_trait::async_trait;
22
use candid::utils::{ArgumentDecoder, ArgumentEncoder};
3-
use ic_base_types::CanisterId;
3+
use ic_base_types::{CanisterId, PrincipalId};
44

55
#[async_trait]
66
pub trait Runtime {
@@ -33,3 +33,33 @@ pub fn heap_memory_size_bytes() -> usize {
3333
pub fn heap_memory_size_bytes() -> usize {
3434
0
3535
}
36+
37+
#[derive(Debug)]
38+
pub struct CdkRuntime;
39+
40+
#[async_trait]
41+
impl Runtime for CdkRuntime {
42+
fn id() -> CanisterId {
43+
CanisterId::try_from(PrincipalId::from(ic_cdk::api::id())).unwrap()
44+
}
45+
46+
fn print(msg: impl AsRef<str>) {
47+
ic_cdk::api::print(msg)
48+
}
49+
50+
async fn call<In, Out>(
51+
id: CanisterId,
52+
method: &str,
53+
cycles: u64,
54+
args: In,
55+
) -> Result<Out, (i32, String)>
56+
where
57+
In: ArgumentEncoder + Send,
58+
Out: for<'a> ArgumentDecoder<'a>,
59+
{
60+
let principal_id = PrincipalId::from(id);
61+
ic_cdk::api::call::call_with_payment(principal_id.into(), method, args, cycles)
62+
.await
63+
.map_err(|(code, msg)| (code as i32, msg))
64+
}
65+
}

rs/ledger_suite/icp/ledger/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package(default_visibility = ["//visibility:public"])
88
rust_library(
99
name = "ledger" + name_suffix,
1010
srcs = [
11-
"src/dfn_runtime.rs",
1211
"src/lib.rs",
1312
"src/tests.rs",
1413
],
@@ -35,8 +34,6 @@ package(default_visibility = ["//visibility:public"])
3534
"//rs/ledger_suite/icp:icp_ledger",
3635
"//rs/ledger_suite/icrc1",
3736
"//rs/limits",
38-
"//rs/rust_canisters/dfn_candid",
39-
"//rs/rust_canisters/dfn_core",
4037
"//rs/types/base_types",
4138
"@crate_index//:candid",
4239
"@crate_index//:hex",

rs/ledger_suite/icp/ledger/src/dfn_runtime.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

rs/ledger_suite/icp/ledger/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use dfn_core::api::{now, trap_with};
21
use ic_base_types::{CanisterId, PrincipalId};
2+
use ic_cdk::{api::time, trap};
33
use ic_ledger_canister_core::archive::ArchiveCanisterWasm;
44
use ic_ledger_canister_core::blockchain::Blockchain;
55
use ic_ledger_canister_core::ledger::{
66
self as core_ledger, LedgerContext, LedgerData, TransactionInfo,
77
};
8+
use ic_ledger_canister_core::runtime::CdkRuntime;
89
use ic_ledger_core::balances::BalancesStore;
910
use ic_ledger_core::{
1011
approvals::{Allowance, AllowanceTable, AllowancesData},
@@ -32,8 +33,6 @@ use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
3233
use std::sync::RwLock;
3334
use std::time::Duration;
3435

35-
mod dfn_runtime;
36-
3736
#[cfg(test)]
3837
mod tests;
3938

@@ -193,7 +192,7 @@ pub struct Ledger {
193192
approvals: LedgerAllowances,
194193
#[serde(default)]
195194
stable_approvals: AllowanceTable<StableAllowancesData>,
196-
pub blockchain: Blockchain<dfn_runtime::DfnRuntime, IcpLedgerArchiveWasm>,
195+
pub blockchain: Blockchain<CdkRuntime, IcpLedgerArchiveWasm>,
197196
// DEPRECATED
198197
pub maximum_number_of_accounts: usize,
199198
// DEPRECATED
@@ -269,7 +268,7 @@ impl LedgerContext for Ledger {
269268
}
270269

271270
impl LedgerData for Ledger {
272-
type Runtime = dfn_runtime::DfnRuntime;
271+
type Runtime = CdkRuntime;
273272
type ArchiveWasm = IcpLedgerArchiveWasm;
274273
type Transaction = Transaction;
275274
type Block = Block;
@@ -373,7 +372,7 @@ impl Ledger {
373372
operation: Operation,
374373
created_at_time: Option<TimeStamp>,
375374
) -> Result<(BlockIndex, HashOf<EncodedBlock>), PaymentError> {
376-
let now = TimeStamp::from(dfn_core::api::now());
375+
let now = TimeStamp::from_nanos_since_unix_epoch(time());
377376
self.add_payment_with_timestamp(memo, operation, created_at_time, now)
378377
}
379378

@@ -554,7 +553,7 @@ impl Ledger {
554553
pub fn upgrade(&mut self, args: UpgradeArgs) {
555554
if let Some(icrc1_minting_account) = args.icrc1_minting_account {
556555
if Some(AccountIdentifier::from(icrc1_minting_account)) != self.minting_account_id {
557-
trap_with(
556+
trap(
558557
"The icrc1 minting account is not the same as the minting account set during initialization",
559558
);
560559
}
@@ -629,7 +628,7 @@ pub fn change_notification_state(
629628
height,
630629
block_timestamp,
631630
new_state,
632-
TimeStamp::from(now()),
631+
TimeStamp::from_nanos_since_unix_epoch(time()),
633632
)
634633
}
635634

rs/ledger_suite/icp/ledger/src/tests.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -278,41 +278,46 @@ fn bad_created_at_time() {
278278
amount: Tokens::from_e8s(1000),
279279
};
280280

281-
let now = dfn_core::api::now().into();
281+
let now = SystemTime::now().into();
282282

283283
assert_eq!(
284284
PaymentError::TransferError(TransferError::TxTooOld {
285285
allowed_window_nanos: Duration::from_secs(24 * 60 * 60).as_nanos() as u64,
286286
}),
287287
state
288-
.add_payment(
288+
.add_payment_with_timestamp(
289289
Memo(1),
290290
transfer.clone(),
291-
Some(now - state.transaction_window - Duration::from_secs(1))
291+
Some(now - state.transaction_window - Duration::from_secs(1)),
292+
now
292293
)
293294
.unwrap_err()
294295
);
295296

296297
state
297-
.add_payment(
298+
.add_payment_with_timestamp(
298299
Memo(2),
299300
transfer.clone(),
300301
Some(now - Duration::from_secs(1)),
302+
now,
301303
)
302304
.unwrap();
303305

304306
assert_eq!(
305307
PaymentError::TransferError(TransferError::TxCreatedInFuture),
306308
state
307-
.add_payment(
309+
.add_payment_with_timestamp(
308310
Memo(3),
309311
transfer.clone(),
310-
Some(now + Duration::from_secs(120))
312+
Some(now + Duration::from_secs(120)),
313+
now
311314
)
312315
.unwrap_err()
313316
);
314317

315-
state.add_payment(Memo(4), transfer, Some(now)).unwrap();
318+
state
319+
.add_payment_with_timestamp(Memo(4), transfer, Some(now), now)
320+
.unwrap();
316321
}
317322

318323
/// Check that block timestamps don't go backwards.
@@ -328,9 +333,15 @@ fn monotonic_timestamps() {
328333
amount: Tokens::from_e8s(1000),
329334
};
330335

331-
state.add_payment(Memo(1), transfer.clone(), None).unwrap();
336+
let now = TimeStamp::from_nanos_since_unix_epoch(1_000_000_000);
332337

333-
state.add_payment(Memo(2), transfer.clone(), None).unwrap();
338+
state
339+
.add_payment_with_timestamp(Memo(1), transfer.clone(), None, now)
340+
.unwrap();
341+
342+
state
343+
.add_payment_with_timestamp(Memo(2), transfer.clone(), None, now)
344+
.unwrap();
334345

335346
state
336347
.add_payment_with_timestamp(
@@ -366,30 +377,31 @@ fn duplicate_txns() {
366377
amount: Tokens::from_e8s(1000),
367378
};
368379

369-
let now = dfn_core::api::now().into();
380+
let now = SystemTime::now().into();
370381

371382
assert_eq!(
372383
state
373-
.add_payment(Memo::default(), transfer.clone(), Some(now))
384+
.add_payment_with_timestamp(Memo::default(), transfer.clone(), Some(now), now)
374385
.unwrap()
375386
.0,
376387
0
377388
);
378389

379390
assert_eq!(
380391
state
381-
.add_payment(Memo(123), transfer.clone(), Some(now))
392+
.add_payment_with_timestamp(Memo(123), transfer.clone(), Some(now), now)
382393
.unwrap()
383394
.0,
384395
1
385396
);
386397

387398
assert_eq!(
388399
state
389-
.add_payment(
400+
.add_payment_with_timestamp(
390401
Memo::default(),
391402
transfer.clone(),
392-
Some(now - Duration::from_secs(1))
403+
Some(now - Duration::from_secs(1)),
404+
now
393405
)
394406
.unwrap()
395407
.0,
@@ -412,7 +424,7 @@ fn duplicate_txns() {
412424
assert_eq!(
413425
PaymentError::TransferError(TransferError::TxDuplicate { duplicate_of: 0 }),
414426
state
415-
.add_payment(Memo::default(), transfer.clone(), Some(now))
427+
.add_payment_with_timestamp(Memo::default(), transfer.clone(), Some(now), now)
416428
.unwrap_err()
417429
);
418430

@@ -670,7 +682,7 @@ fn test_throttle_tx_per_second_nok() {
670682
amount: Tokens::from_e8s(1000),
671683
};
672684

673-
let now: TimeStamp = dfn_core::api::now().into();
685+
let now = TimeStamp::from_nanos_since_unix_epoch(1000000);
674686

675687
assert_eq!(apply_at(&mut ledger, &op, now + millis(1)), 0);
676688
assert_eq!(apply_at(&mut ledger, &op, now + millis(1002)), 1);
@@ -693,7 +705,7 @@ fn test_throttle_tx_per_second_ok() {
693705
to: PrincipalId::new_user_test_id(1).into(),
694706
amount: Tokens::from_e8s(1000),
695707
};
696-
let now: TimeStamp = dfn_core::api::now().into();
708+
let now = TimeStamp::from_nanos_since_unix_epoch(1000000);
697709

698710
assert_eq!(apply_at(&mut ledger, &op, now + millis(1)), 0);
699711
assert_eq!(apply_at(&mut ledger, &op, now + millis(1002)), 1);
@@ -714,7 +726,7 @@ fn test_throttle_two_tx_per_second_after_soft_limit_ok() {
714726
to: PrincipalId::new_user_test_id(1).into(),
715727
amount: Tokens::from_e8s(1000),
716728
};
717-
let now: TimeStamp = dfn_core::api::now().into();
729+
let now = TimeStamp::from_nanos_since_unix_epoch(1000000);
718730

719731
assert_eq!(apply_at(&mut ledger, &op, now + millis(1)), 0);
720732
assert_eq!(apply_at(&mut ledger, &op, now + millis(2)), 1);
@@ -741,7 +753,7 @@ fn test_throttle_two_tx_per_second_after_soft_limit_nok() {
741753
to: PrincipalId::new_user_test_id(1).into(),
742754
amount: Tokens::from_e8s(1000),
743755
};
744-
let now: TimeStamp = dfn_core::api::now().into();
756+
let now = TimeStamp::from_nanos_since_unix_epoch(1000000);
745757

746758
assert_eq!(apply_at(&mut ledger, &op, now + millis(1)), 0);
747759
assert_eq!(apply_at(&mut ledger, &op, now + millis(2)), 1);

rs/ledger_suite/icp/src/account_identifier.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use candid::{CandidType, Principal};
2-
use dfn_core::CanisterId;
3-
use ic_base_types::{CanisterIdError, PrincipalId, PrincipalIdError};
2+
use ic_base_types::{CanisterId, CanisterIdError, PrincipalId, PrincipalIdError};
43
use ic_crypto_sha2::Sha224;
54
use ic_stable_structures::{storable::Bound, Storable};
65
use icrc_ledger_types::icrc1::account::Account;

rs/ledger_suite/icrc1/ledger/src/cdk_runtime.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)