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

dfn_core #5543

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG-Nns-Dapp-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ proposal is successful, the changes it released will be moved from this file to
* Reduce calls to `sns-governance` canister by getting `nervous_system_parameters` from the aggregator instead.
* Move theme toggle from account menu to sidebar.
* The `Markdown` UI component was migrated to `@dfinity/gix-components`.
* Migrate canister code from `dfn_core` to `ic_cdk`.

#### Deprecated

Expand Down
52 changes: 25 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion rs/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ tar = "0.4.40"

cycles-minting-canister = { workspace = true }
dfn_candid = { workspace = true }
dfn_core = { workspace = true }
dfn_protobuf = { workspace = true }
ic-base-types = { workspace = true }
ic-cdk = { workspace = true }
Expand Down
20 changes: 3 additions & 17 deletions rs/backend/src/accounts_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::fmt;
use std::ops::RangeBounds;
use std::time::{Duration, SystemTime};
use std::time::Duration;

pub mod constructors;
pub mod histogram;
Expand Down Expand Up @@ -614,13 +614,7 @@ impl AccountsStore {
}

pub fn mark_ledger_sync_complete(&mut self) {
self.last_ledger_sync_timestamp_nanos = u64::try_from(
dfn_core::api::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|err| unreachable!("The current time is well after the Unix epoch. Error: {err}"))
.as_nanos(),
)
.unwrap_or_else(|_| unreachable!("Not impossible, but centuries in the future"));
self.last_ledger_sync_timestamp_nanos = ic_cdk::api::time();
}

/// Initializes the `block_height_synced_up_to` value.
Expand Down Expand Up @@ -827,15 +821,7 @@ impl AccountsStore {
}

pub fn get_stats(&self, stats: &mut Stats) {
let timestamp_now_nanos = u64::try_from(
dfn_core::api::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|err| unreachable!("Hey, we are back in the sixties! Seriously, if we get here, the system time is before the Unix epoch. This should be impossible. Error: {err}"))
.as_nanos(),
)
.unwrap_or_else(|_| {
unreachable!("Well, this could kill us if the code is still running in 500 years. Not impossible.")
});
let timestamp_now_nanos = ic_cdk::api::time();
let duration_since_last_sync =
Duration::from_nanos(timestamp_now_nanos - self.last_ledger_sync_timestamp_nanos);

Expand Down
17 changes: 7 additions & 10 deletions rs/backend/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::stats::encode_metrics;
use crate::StableState;
use base64::{engine::general_purpose::STANDARD as BASE64_ENGINE, Engine};
use candid::{CandidType, Decode, Encode};
use dfn_core::api::ic0::time;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use ic_cdk::api::time;
use ic_cdk::println;
use ic_certified_map::{labeled, labeled_hash, AsHashTree, Hash, RbTree};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -210,10 +210,7 @@ pub fn http_request(req: HttpRequest) -> HttpResponse {
let parts: Vec<&str> = req.url.split('?').collect();
match *parts.first().unwrap_or(&"") {
"/metrics" => {
let now;
unsafe {
now = time();
};
let now = time();
let mut writer = MetricsEncoder::new(vec![], now / 1_000_000);
match encode_metrics(&mut writer) {
Ok(()) => {
Expand Down Expand Up @@ -322,15 +319,15 @@ fn security_headers() -> Vec<HeaderField> {
}

fn make_asset_certificate_header(asset_hashes: &AssetHashes, asset_name: &str) -> (String, String) {
let certificate = dfn_core::api::data_certificate().unwrap_or_else(|| {
dfn_core::api::trap_with("data certificate is only available in query calls");
let certificate = ic_cdk::api::data_certificate().unwrap_or_else(|| {
ic_cdk::api::trap("data certificate is only available in query calls");
});
let witness = asset_hashes.0.witness(asset_name.as_bytes());
let tree = labeled(LABEL_ASSETS, witness);
let mut serializer = serde_cbor::ser::Serializer::new(vec![]);
serializer.self_describe().unwrap();
tree.serialize(&mut serializer)
.unwrap_or_else(|e| dfn_core::api::trap_with(&format!("failed to serialize a hash tree: {e}")));
.unwrap_or_else(|e| ic_cdk::api::trap(&format!("failed to serialize a hash tree: {e}")));
(
"IC-Certificate".to_string(),
format!(
Expand Down Expand Up @@ -419,7 +416,7 @@ pub fn insert_tar_xz(compressed: Vec<u8>) {
.to_vec();

let name = String::from_utf8(name_bytes.clone()).unwrap_or_else(|e| {
dfn_core::api::trap_with(&format!(
ic_cdk::api::trap(&format!(
"non-utf8 file name {}: {}",
String::from_utf8_lossy(&name_bytes),
e
Expand Down Expand Up @@ -463,7 +460,7 @@ impl StableState for Assets {

fn update_root_hash(a: &AssetHashes) {
let prefixed_root_hash = &labeled_hash(LABEL_ASSETS, &a.0.root_hash());
dfn_core::api::set_certified_data(&prefixed_root_hash[..]);
ic_cdk::api::set_certified_data(&prefixed_root_hash[..]);
}

#[test]
Expand Down
7 changes: 3 additions & 4 deletions rs/backend/src/canisters/cmc.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::Cycles;
use cycles_minting_canister::{NotifyCreateCanister, NotifyError, NotifyTopUp};
use dfn_candid::candid;
use dfn_core::CanisterId;
use ic_base_types::CanisterId;
use ic_nns_constants::CYCLES_MINTING_CANISTER_ID;

pub async fn notify_create_canister(request: NotifyCreateCanister) -> Result<Result<CanisterId, NotifyError>, String> {
dfn_core::call(CYCLES_MINTING_CANISTER_ID, "notify_create_canister", candid, (request,))
ic_cdk::call(CYCLES_MINTING_CANISTER_ID.into(), "notify_create_canister", (request,))
.await
.map_err(|e| e.1)
}

pub async fn notify_top_up_canister(request: NotifyTopUp) -> Result<Result<Cycles, NotifyError>, String> {
dfn_core::call(CYCLES_MINTING_CANISTER_ID, "notify_top_up", candid, (request,))
ic_cdk::call(CYCLES_MINTING_CANISTER_ID.into(), "notify_top_up", (request,))
.await
.map_err(|e| e.1)
}
2 changes: 1 addition & 1 deletion rs/backend/src/canisters/exchange_rate_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod prod {
use ic_nns_constants::EXCHANGE_RATE_CANISTER_ID;

pub async fn get_exchange_rate(request: GetExchangeRateRequest) -> Result<GetExchangeRateResult, String> {
dfn_core::call(EXCHANGE_RATE_CANISTER_ID, "get_exchange_rate", candid, (request,))
ic_cdk::call(EXCHANGE_RATE_CANISTER_ID, "get_exchange_rate", candid, (request,))
.await
.map_err(|e| e.1)
}
Expand Down
4 changes: 2 additions & 2 deletions rs/backend/src/canisters/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use ic_nns_governance::pb::v1::{
pub async fn claim_or_refresh_neuron_from_account(
request: ClaimOrRefreshNeuronFromAccount,
) -> Result<ClaimOrRefreshNeuronFromAccountResponse, String> {
dfn_core::call(
ic_cdk::call(
GOVERNANCE_CANISTER_ID,
"claim_or_refresh_neuron_from_account",
candid,
Expand All @@ -31,7 +31,7 @@ mod prod {
use super::{candid, GetMetricsCallResult, GOVERNANCE_CANISTER_ID};

pub async fn get_metrics() -> GetMetricsCallResult {
dfn_core::call(GOVERNANCE_CANISTER_ID, "get_metrics", candid, ())
ic_cdk::call(GOVERNANCE_CANISTER_ID, "get_metrics", candid, ())
.await
.map_err(|e| e.1)
}
Expand Down
8 changes: 4 additions & 4 deletions rs/backend/src/canisters/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dfn_core::CanisterId;
use dfn_protobuf::protobuf;
use ic_base_types::CanisterId;
use ic_ledger_core::block::EncodedBlock;
use ic_nns_constants::LEDGER_CANISTER_ID;
use icp_ledger::protobuf::get_blocks_response::GetBlocksContent;
Expand All @@ -11,21 +11,21 @@ use icp_ledger::{BlockIndex, GetBlocksArgs};

pub async fn tip_of_chain() -> Result<BlockIndex, String> {
let response: TipOfChainResponsePb =
dfn_core::call(LEDGER_CANISTER_ID, "tip_of_chain_pb", protobuf, TipOfChainRequestPb {})
ic_cdk::call(LEDGER_CANISTER_ID, "tip_of_chain_pb", protobuf, TipOfChainRequestPb {})
.await
.map_err(|e| e.1)?;

Ok(response.chain_length.map(|c| c.height).unwrap_or_default())
}

pub async fn get_archive_index() -> Result<ArchiveIndexResponsePb, String> {
dfn_core::call(LEDGER_CANISTER_ID, "get_archive_index_pb", protobuf, ())
ic_cdk::call(LEDGER_CANISTER_ID, "get_archive_index_pb", protobuf, ())
.await
.map_err(|e| e.1)
}

pub async fn get_blocks(canister_id: CanisterId, from: BlockIndex, length: u32) -> Result<Vec<EncodedBlock>, String> {
let response: GetBlocksResponsePb = dfn_core::call(
let response: GetBlocksResponsePb = ic_cdk::call(
canister_id,
"get_blocks_pb",
protobuf,
Expand Down
2 changes: 1 addition & 1 deletion rs/backend/src/ledger_sync.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::canisters::ledger;
use crate::state::{with_state, with_state_mut};
use candid::Principal;
use dfn_core::CanisterId;
use ic_base_types::CanisterId;
use ic_cdk::println;
use ic_ledger_core::block::BlockType;
use ic_ledger_core::Tokens;
Expand Down
Loading
Loading