Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion rs/cross-chain/proposal-cli/src/candid/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn should_parse_constructor_parameters() {
| (TargetCanister::CkEthLedger, "(LedgerArg)")
| (TargetCanister::CkEthMinter, "(MinterArg)")
| (TargetCanister::CyclesIndex, "(opt IndexArg)")
| (TargetCanister::IcpIndex, "(InitArg)")
| (TargetCanister::IcpIndex, "(opt IndexArg)")
| (TargetCanister::IcpLedger, "(LedgerCanisterPayload)")
| (TargetCanister::LedgerSuiteOrchestrator, "(OrchestratorArg)")
);
Expand Down
1 change: 1 addition & 0 deletions rs/ledger_suite/icp/index/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ DEV_DEPENDENCIES = [
"//rs/ledger_suite/tests/sm-tests:ic-ledger-suite-state-machine-tests",
"//rs/state_machine_tests",
"//rs/test_utilities/load_wasm",
"//rs/types/types",
"@crate_index//:candid_parser",
"@crate_index//:proptest",
]
Expand Down
1 change: 1 addition & 0 deletions rs/ledger_suite/icp/index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ ic-ledger-suite-state-machine-tests = { path = "../../tests/sm-tests" }
ic-ledger-test-utils = { path = "../test_utils" }
ic-state-machine-tests = { path = "../../../state_machine_tests" }
ic-test-utilities-load-wasm = { path = "../../../test_utilities/load_wasm" }
ic-types = { path = "../../../types/types" }
proptest = { workspace = true }
18 changes: 16 additions & 2 deletions rs/ledger_suite/icp/index/index.did
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,28 @@ type HttpResponse = record {
headers : vec record { text; text };
status_code : nat16
};
type InitArg = record { ledger_id : principal };

type InitArg = record {
ledger_id : principal;
// The interval in seconds in which to retrieve blocks from the ledger. A lower value makes the index more
// responsive in showing new blocks, but increases the consumption of cycles of both the index and ledger canisters.
// A higher values means that it takes longer for new blocks to show up in the index.
retrieve_blocks_from_ledger_interval_seconds : opt nat64
};

type UpgradeArg = record {
ledger_id : opt principal;
// The interval in seconds in which to retrieve blocks from the ledger. A lower value makes the index more
// responsive in showing new blocks, but increases the consumption of cycles of both the index and ledger canisters.
// A higher values means that it takes longer for new blocks to show up in the index.
retrieve_blocks_from_ledger_interval_seconds : opt nat64
};

type IndexArg = variant {
Init : InitArg;
Upgrade : UpgradeArg
};

type Operation = variant {
Approve : record {
fee : Tokens;
Expand Down Expand Up @@ -75,7 +89,7 @@ type Transaction = record {
timestamp : opt TimeStamp
};
type TransactionWithId = record { id : nat64; transaction : Transaction };
service : (InitArg) -> {
service : (index_arg : opt IndexArg) -> {
get_account_identifier_balance : (text) -> (nat64) query;
get_account_identifier_transactions : (
GetAccountIdentifierTransactionsArgs
Expand Down
8 changes: 8 additions & 0 deletions rs/ledger_suite/icp/index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ use serde_bytes::ByteBuf;

pub mod logs;

#[derive(Clone, Debug, CandidType, Deserialize)]
pub enum IndexArg {
Init(InitArg),
Upgrade(UpgradeArg),
}

#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct InitArg {
pub ledger_id: Principal,
pub retrieve_blocks_from_ledger_interval_seconds: Option<u64>,
}

#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct UpgradeArg {
pub ledger_id: Option<Principal>,
pub retrieve_blocks_from_ledger_interval_seconds: Option<u64>,
}

Expand Down
55 changes: 37 additions & 18 deletions rs/ledger_suite/icp/index/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use candid::Principal;
use ic_base_types::PrincipalId;
use ic_canister_log::{export as export_logs, log};
use ic_cdk::api::caller;
use ic_cdk::{init, post_upgrade, query};
use ic_cdk::{init, post_upgrade, query, trap};
use ic_cdk_timers::TimerId;
use ic_http_types::{HttpRequest, HttpResponse, HttpResponseBuilder};
use ic_icp_index::logs::{P0, P1};
use ic_icp_index::{
GetAccountIdentifierTransactionsArgs, GetAccountIdentifierTransactionsResponse,
GetAccountIdentifierTransactionsResult, GetAccountTransactionsResult, InitArg, Log, LogEntry,
Priority, SettledTransaction, SettledTransactionWithId, Status, UpgradeArg,
GetAccountIdentifierTransactionsResult, GetAccountTransactionsResult, IndexArg, InitArg, Log,
LogEntry, Priority, SettledTransaction, SettledTransactionWithId, Status, UpgradeArg,
};
use ic_icrc1_index_ng::GetAccountTransactionsArgs;
use ic_ledger_canister_core::runtime::heap_memory_size_bytes;
Expand Down Expand Up @@ -256,10 +256,19 @@ fn balance_key(account_identifier: AccountIdentifier) -> (AccountIdentifierDataT
}

#[init]
fn init(init_arg: InitArg) {
fn init(index_arg: Option<IndexArg>) {
let InitArg {
ledger_id,
retrieve_blocks_from_ledger_interval_seconds,
} = match index_arg {
Some(IndexArg::Init(arg)) => arg,
_ => trap("Index initialization must take in input an InitArg argument"),
};
// stable memory initialization
mutate_state(|state| {
state.ledger_id = init_arg.ledger_id;
state.ledger_id = ledger_id;
state.retrieve_blocks_from_ledger_interval =
retrieve_blocks_from_ledger_interval_seconds.map(Duration::from_secs);
});

// set the first build_index to be called after init
Expand All @@ -269,20 +278,30 @@ fn init(init_arg: InitArg) {
}

#[post_upgrade]
fn post_upgrade(upgrade_arg: Option<UpgradeArg>) {
if let Some(upgrade) = upgrade_arg {
log!(P1, "Possible upgrade configuration changes: {:#?}", upgrade);

let UpgradeArg {
retrieve_blocks_from_ledger_interval_seconds,
} = upgrade;
fn post_upgrade(index_arg: Option<IndexArg>) {
match index_arg {
Some(IndexArg::Upgrade(upgrade)) => {
log!(P1, "Possible upgrade configuration changes: {:#?}", upgrade,);

let UpgradeArg {
ledger_id,
retrieve_blocks_from_ledger_interval_seconds,
} = upgrade;

mutate_state(|state| {
if let Some(new_value) = ledger_id {
state.ledger_id = new_value;
}

mutate_state(|state| {
if let Some(new_value) = retrieve_blocks_from_ledger_interval_seconds {
state.retrieve_blocks_from_ledger_interval = Some(Duration::from_secs(new_value));
}
});
}
if let Some(new_value) = retrieve_blocks_from_ledger_interval_seconds {
state.retrieve_blocks_from_ledger_interval =
Some(Duration::from_secs(new_value));
}
});
}
Some(IndexArg::Init(..)) => trap("Index upgrade argument cannot be of variant Init"),
_ => (),
};

// set the first build_index to be called after upgrade
set_build_index_timer(with_state(|state| {
Expand Down
Loading
Loading