Repository navigation
feat: Store prepayment for call transmission - #9859
schneiderstefan merged 5 commits into
Conversation
338adb5 to
dd37c1d
Compare
I see some test failures that I missed, I'll push a fix in a bit. |
Pushed a fix. |
There was a problem hiding this comment.
Pull request overview
This PR extends callback bookkeeping to persist the total call transmission prepayment (request + response transmission) so refund-time accounting can compute “consumed” transmission cycles (including non-refunded portions like base fee + request bytes), enabling future counter-style metrics.
Changes:
- Add
prepayment_for_call_transmissiontoCallback+ protobuf, with backward-compatible decoding default. - Thread the new prepayment through request charging and response refund paths (including sandbox-safe system state).
- Minor refactor: compute/consume call transmission fees via
xnet_total_transmission_feeand addCompoundCycles::is_zero().
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| rs/types/types/src/methods.rs | Adds new callback field + protobuf (de)serialization for call transmission prepayment. |
| rs/types/cycles/src/compound_cycles.rs | Adds CompoundCycles::is_zero() helper used for backward-compat handling. |
| rs/test_utilities/state/src/lib.rs | Updates callback construction in test utilities to include new field. |
| rs/state_layout/src/state_layout/tests.rs | Updates encode/decode tests to include new callback field (compat coverage). |
| rs/replicated_state/src/canister_state/tests.rs | Updates callback round-trip tests for the new field. |
| rs/replicated_state/src/canister_state/system_state/call_context_manager/tests.rs | Updates CCM tests for new callback constructor parameter. |
| rs/replicated_state/src/canister_state/system_state.rs | Updates internal test helpers to include new callback field. |
| rs/protobuf/src/gen/state/state.canister_state_bits.v1.rs | Regenerated prost output adding the new callback field + comment tweaks. |
| rs/protobuf/def/state/canister_state_bits/v1/canister_state_bits.proto | Adds protobuf field prepayment_for_call_transmission (tag 13). |
| rs/execution_environment/src/scheduler/test_utilities.rs | Updates scheduler test utilities to populate new callback field. |
| rs/execution_environment/src/execution/response.rs | Uses call-transmission prepayment during refund to enable correct “consumed” computation. |
| rs/execution_environment/benches/lib/src/common.rs | Updates benchmark setup callback initialization for new field. |
| rs/embedders/tests/system_api.rs | Updates embedders system API tests to include new callback field. |
| rs/embedders/tests/sandbox_safe_system_state.rs | Refactors tests to use total transmission fee and updated refund semantics. |
| rs/embedders/src/wasmtime_embedder/system_api/sandbox_safe_system_state.rs | Updates request charging to accept call-transmission prepayment and records it as consumed. |
| rs/embedders/src/wasmtime_embedder/system_api/request_in_prep.rs | Computes/stores call-transmission prepayment when building outgoing requests/callbacks. |
| rs/embedders/src/wasmtime_embedder/system_api.rs | Threads new prepayment through push_output_request plumbing. |
| rs/cycles_account_manager/src/cycles_account_manager.rs | Refactors withdraw_request_cycles to take total call-transmission prepayment and simplifies fee calculation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Review dismissed by automation script.
…11490) `CanisterMetrics::consumed_cycles_monotonic` (added in dfinity#11465) is absent in checkpoints written before it, and decodes as zero. Left alone it would only start accruing from the round it was added in, so every canister that predates it under-reports its lifetime consumption forever. The `consumed_cycles` gauge predates it and holds the full history, so the monotonic value can be derived from it. `SystemState::outstanding_prepayments()` sums the prepayments that have been added to the gauge but whose refund has not been observed yet -- the ones held in an open `Callback` (`prepayment_for_response_execution` plus `prepayment_for_call_transmission`, falling back to `prepayment_for_response_transmission` for callbacks created before April 2026, as the refund path itself does) or in the `prepaid_execution_cycles` of an aborted execution or `install_code`. All of them are recorded in the replicated state, which yields the invariant ```text consumed_cycles - outstanding_prepayments() == consumed_cycles_monotonic ``` whenever no execution is in progress or paused, and makes `SystemState::migrate_consumed_cycles_to_monotonic()` exact. Because the invariant holds at every checkpoint rather than just the first one, the backfill is also idempotent: a no-op for a canister already backfilled, and self-healing if a downgrade dropped the field. A callback created before `prepayment_for_call_transmission` was stored in callbacks (dfinity#9859, April 2026) is no exception, even though the fallback accounts for the response transmission part of that prepayment only, leaving out the `xnet_call_performed_fee` plus the request bytes fee. That call fee is part of the gauge, so the first backfill credits it while the callback is still open -- the gauge being what the canister really consumed -- and executing the response then adds exactly the prepayments that `outstanding_prepayments()` reported, keeping the monotonic value in step with the gauge. Only a canister still short of the gauge, i.e. one not backfilled yet or whose field a downgrade dropped, lags by that fee, which is the case the backfill exists for anyway. The scheduler runs it on checkpoint rounds only, after `abort_all_paused_executions`: a paused execution generally holds its prepayment in memory rather than in the state, and backfilling from a gauge that covers it would overestimate, so `outstanding_prepayments()` returns `None` for any paused execution -- including a paused response execution, whose prepayments the callback carried by the task does record, since the only caller runs once there are none left. Aborting materializes those prepayments into the task queues. The pass takes a mutable reference to a canister only when it has something to write, so it stays read-only once the subnet has been backfilled. The three states this considers unreachable -- a canister whose outstanding prepayments cannot be derived, outstanding prepayments above the gauge, and a monotonic value already above the gauge net of them -- are reported through `debug_assert_or_critical_error!` and a new `scheduler_consumed_cycles_invariant_broken` error counter, since all of them would otherwise be silent in production. `debug_assert_or_critical_error!` now passes its message to the `debug_assert!` as well, so the panic it raises under debug assertions names the report that tripped rather than reading `assertion failed: false`; that is what lets the tests here expect one specific report. The tests span the three levels this touches: `outstanding_prepayments()` and the backfill are covered by unit tests in `rs/replicated_state`; scheduler tests drive real prepayments through checkpoint rounds, a response callback paused and then aborted among them, to assert that the abort leaves behind a task that prepays nothing of its own and a callback that is no longer registered; and an `ExecutionTest` executes the response of a legacy callback, tying the fallback of `outstanding_prepayments()` to the one that `ResponseHelper::apply_initial_refunds()` applies -- the two fall back independently of each other, in different crates -- and asserts that a backfill taken while that callback is still open credits its call fee, so that executing the response leaves the monotonic value in step with the gauge. Turning a callback into a legacy one needs a new `reset_prepayment_for_call_transmission` testing hook, as no production code writes such a callback any more. No metric or behaviour changes: nothing outside of tests reads `consumed_cycles_monotonic` yet. A follow-up (`mraszyk/consumed-cycles-as-counter`, dfinity#11416) exports the monotonic total under the existing `replicated_state_consumed_cycles_since_replica_started` gauge and is stacked on this branch. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Store the prepayment for call transmission so that it can be used to properly calculate the consumed amount for an inter-canister call when updating the metrics during the refund phase.
Problem
In the current state of the code, only the prepayment for response transmission is stored so that the refund can be calculated when the actual response is received. This is enough for now when wanting to credit the refund back to the canister's balance or updating the current metrics which act like gauges. However, if we want to have new metrics that will act as counters (which will happen in a follow-up) we cannot rely only on that amount for the consumed part as we would miss the parts that are not refunded -- specifically the base fee and request transmission parts.
Solution
The proposed solution here is to additionally store the total call transmission fee so that it can be passed into the function that refunds cycles to the canister -- the refund part to the balance remains the same but based on prepayment and refund one can calculate the amount to update the metrics with only at refund step.
The changes include the following parts:
CyclesAccountManagerthat felt reasonable given that we have the total transmission fee computed and we don't need to recompute stuff.