Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions rs/canonical_state/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ rust_test(
deps = [
# Keep sorted.
"//rs/crypto/sha2",
"//rs/monitoring/logger",
"//rs/monitoring/metrics",
"//rs/registry/subnet_features",
"//rs/sys",
"//rs/test_utilities/state",
Expand Down
2 changes: 2 additions & 0 deletions rs/canonical_state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ assert_matches = { workspace = true }
hex = { workspace = true }
ic-canonical-state-tree-hash-test-utils = { path = "tree_hash/test_utils" }
ic-crypto-sha2 = { path = "../crypto/sha2/" }
ic-logger = { path = "../monitoring/logger" }
ic-management-canister-types-private = { path = "../types/management_canister_types" }
ic-metrics = { path = "../monitoring/metrics" }
ic-registry-subnet-features = { path = "../registry/subnet_features" }
ic-sys = { path = "../sys" }
ic-test-utilities-state = { path = "../test_utilities/state" }
Expand Down
12 changes: 8 additions & 4 deletions rs/canonical_state/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ mod tests {
CertificationVersion::{self, *},
all_supported_versions,
};
use ic_logger::replica_logger::no_op_logger;
use ic_management_canister_types_private::Global;
use ic_metrics::MetricsRegistry;
use ic_registry_routing_table::{CanisterIdRange, RoutingTable};
use ic_registry_subnet_features::SubnetFeatures;
use ic_registry_subnet_type::SubnetType;
Expand Down Expand Up @@ -1234,12 +1236,14 @@ mod tests {
INITIAL_CYCLES,
NumSeconds::from(100_000),
);
canister_state
.system_state
.consume_cycles(CompoundCycles::<Instructions>::new(
canister_state.system_state.consume_cycles(
CompoundCycles::<Instructions>::new(
Cycles::new(123_456),
CanisterCyclesCostSchedule::Normal,
));
),
&no_op_logger(),
&MetricsRegistry::new().int_counter("error_counter", "Test error counter"),
);
let consumed_by_canisters = canister_state
.system_state
.canister_metrics()
Expand Down
54 changes: 51 additions & 3 deletions rs/cycles_account_manager/src/cycles_account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ impl CyclesAccountManager {
cycles: Cycles,
subnet_cycles_config: CyclesAccountManagerSubnetConfig,
reveal_top_up: bool,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<(), CanisterOutOfCyclesError> {
let threshold = self.freeze_threshold_cycles(
canister.system_state.freeze_threshold,
Expand Down Expand Up @@ -346,6 +348,8 @@ impl CyclesAccountManager {
CompoundCycles::new(cycles, subnet_cycles_config.cost_schedule),
threshold,
reveal_top_up,
log,
charging_error,
)
}
}
Expand All @@ -372,6 +376,8 @@ impl CyclesAccountManager {
cycles: CompoundCycles<T>,
subnet_cycles_config: CyclesAccountManagerSubnetConfig,
reveal_top_up: bool,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<(), CanisterOutOfCyclesError> {
self.consume_cycles_impl(
system_state,
Expand All @@ -380,6 +386,8 @@ impl CyclesAccountManager {
cycles,
subnet_cycles_config,
reveal_top_up,
log,
charging_error,
)
}

Expand All @@ -394,6 +402,8 @@ impl CyclesAccountManager {
cycles: CompoundCycles<T>,
subnet_cycles_config: CyclesAccountManagerSubnetConfig,
reveal_top_up: bool,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<(), CanisterOutOfCyclesError> {
let threshold = self.freeze_threshold_cycles(
system_state.freeze_threshold,
Expand All @@ -404,7 +414,14 @@ impl CyclesAccountManager {
subnet_cycles_config,
system_state.reserved_balance(),
);
self.consume_with_threshold_impl(system_state, cycles, threshold, reveal_top_up)
self.consume_with_threshold_impl(
system_state,
cycles,
threshold,
reveal_top_up,
log,
charging_error,
)
}

/// Consumes a direct, final `Instructions` charge (e.g. the cost of
Expand All @@ -423,6 +440,8 @@ impl CyclesAccountManager {
cycles: CompoundCycles<Instructions>,
subnet_cycles_config: CyclesAccountManagerSubnetConfig,
reveal_top_up: bool,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<(), CanisterOutOfCyclesError> {
self.consume_cycles_impl(
system_state,
Expand All @@ -431,6 +450,8 @@ impl CyclesAccountManager {
cycles,
subnet_cycles_config,
reveal_top_up,
log,
charging_error,
)?;
let zero_refund =
CompoundCycles::<Instructions>::new(Cycles::zero(), subnet_cycles_config.cost_schedule);
Expand All @@ -446,6 +467,8 @@ impl CyclesAccountManager {
canister: &mut CanisterState,
amount: NumInstructions,
subnet_cycles_config: CyclesAccountManagerSubnetConfig,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<(), CanisterOutOfCyclesError> {
let memory_usage = canister.memory_usage();
let message_memory = canister.message_memory_usage();
Expand All @@ -458,6 +481,8 @@ impl CyclesAccountManager {
cycles,
subnet_cycles_config,
reveal_top_up,
log,
charging_error,
)
}

Expand All @@ -481,6 +506,8 @@ impl CyclesAccountManager {
subnet_cycles_config: CyclesAccountManagerSubnetConfig,
reveal_top_up: bool,
execution_mode: WasmExecutionMode,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<CompoundCycles<Instructions>, CanisterOutOfCyclesError> {
let cost = self.execution_cost(num_instructions, subnet_cycles_config, execution_mode);
self.consume_with_threshold_impl(
Expand All @@ -496,6 +523,8 @@ impl CyclesAccountManager {
system_state.reserved_balance(),
),
reveal_top_up,
log,
charging_error,
)
.map(|_| cost)
}
Expand Down Expand Up @@ -952,8 +981,17 @@ impl CyclesAccountManager {
cycles: CompoundCycles<T>,
threshold: Cycles,
reveal_top_up: bool,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<(), CanisterOutOfCyclesError> {
self.consume_with_threshold_impl(system_state, cycles, threshold, reveal_top_up)
self.consume_with_threshold_impl(
system_state,
cycles,
threshold,
reveal_top_up,
log,
charging_error,
)
}

/// Same as `consume_with_threshold` but without the restriction to
Expand All @@ -965,6 +1003,8 @@ impl CyclesAccountManager {
cycles: CompoundCycles<T>,
threshold: Cycles,
reveal_top_up: bool,
log: &ReplicaLogger,
charging_error: &IntCounter,
) -> Result<(), CanisterOutOfCyclesError> {
let use_case = T::cycles_use_case();

Expand Down Expand Up @@ -995,7 +1035,8 @@ impl CyclesAccountManager {
reveal_top_up,
)?;

system_state.consume_cycles(cycles);
// The balance was verified against the threshold above.
system_state.consume_cycles(cycles, log, charging_error);
Ok(())
}

Expand Down Expand Up @@ -1195,6 +1236,7 @@ impl CyclesAccountManager {
&self,
rate: CompoundCycles<T>,
log: &ReplicaLogger,
charging_error: &IntCounter,
canister: &mut CanisterState,
duration_since_last_charge: Duration,
) -> Result<(), CanisterOutOfCyclesError> {
Expand All @@ -1206,6 +1248,8 @@ impl CyclesAccountManager {
cycles,
Cycles::zero(),
false, // caller is system => no need to reveal top up balance
log,
charging_error,
) {
info!(
log,
Expand All @@ -1225,6 +1269,7 @@ impl CyclesAccountManager {
pub fn charge_canister_for_resource_allocation_and_usage(
&self,
log: &ReplicaLogger,
charging_error: &IntCounter,
canister: &mut CanisterState,
duration_since_last_charge: Duration,
subnet_cycles_config: CyclesAccountManagerSubnetConfig,
Expand All @@ -1244,18 +1289,21 @@ impl CyclesAccountManager {
self.charge_canister_for_single_resource(
memory,
log,
charging_error,
canister,
duration_since_last_charge,
)?;
self.charge_canister_for_single_resource(
message_memory,
log,
charging_error,
canister,
duration_since_last_charge,
)?;
self.charge_canister_for_single_resource(
compute_allocation,
log,
charging_error,
canister,
duration_since_last_charge,
)?;
Expand Down
Loading
Loading