Skip to content
Open
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
51 changes: 39 additions & 12 deletions rs/execution_environment/src/execution/response/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,30 @@ fn execute_response_refunds_cycles() {

// Canister A calls canister B.
let cycles_sent = Cycles::new(1_000_000);
let b_callback = wasm()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by improvement: using actual callbacks and canister requests (instead of injecting a response) to make the test more robust.

.accept_cycles(cycles_sent / 2_u64)
.message_payload()
.append_and_reply()
.build();
let wasm_payload = wasm()
.call_with_cycles(b_id, "update", call_args(), cycles_sent)
.call_with_cycles(
b_id,
"update",
call_args().other_side(b_callback.clone()),
cycles_sent,
)
.build();

// Enqueue ingress message to canister A and execute it.
let msg_id = test.ingress_raw(a_id, "update", wasm_payload).0;
assert_matches!(test.ingress_state(&msg_id), IngressState::Received);
test.execute_message(a_id);

// Create response from canister B to canister A.
let response = ResponseBuilder::new()
.originator(a_id)
.respondent(b_id)
.originator_reply_callback(CallbackId::from(1))
.refund(cycles_sent / 2_u64)
.build();
let response_payload_size = response.payload_size_bytes();
// Execute message on B.
test.induct_messages();
test.execute_message(b_id);

// Execute response.
// Execute response on A.
let balance_before = test.canister_state(a_id).system_state.balance();
let consumed_cycles_before = *test
.canister_state(a_id)
Expand All @@ -125,8 +130,16 @@ fn execute_response_refunds_cycles() {
.consumed_cycles_by_use_cases()
.get(&CyclesUseCase::RequestAndResponseTransmission)
.unwrap();
let consumed_cycles_before_counter = *test
.canister_state(a_id)
.system_state
.canister_metrics()
.consumed_cycles_by_use_cases_as_counters()
.get(&CyclesUseCase::RequestAndResponseTransmission)
.unwrap();
let instructions_before = test.canister_executed_instructions(a_id);
test.execute_response(a_id, response);
test.induct_messages();
test.execute_message(a_id);
let instructions_after = test.canister_executed_instructions(a_id);
let instructions_executed = instructions_after - instructions_before;
let balance_after = test.canister_state(a_id).system_state.balance();
Expand All @@ -137,6 +150,13 @@ fn execute_response_refunds_cycles() {
.consumed_cycles_by_use_cases()
.get(&CyclesUseCase::RequestAndResponseTransmission)
.unwrap();
let consumed_cycles_after_counter = *test
.canister_state(a_id)
.system_state
.canister_metrics()
.consumed_cycles_by_use_cases_as_counters()
.get(&CyclesUseCase::RequestAndResponseTransmission)
.unwrap();

// The balance is equivalent to the amount of cycles before executing`execute_response`
// plus the unaccepted cycles (no more the cycles sent via request),
Expand All @@ -146,7 +166,7 @@ fn execute_response_refunds_cycles() {
let prepayment_for_response_transmission =
mgr.prepayment_for_response_transmission(test.subnet_size(), cost_schedule);
let actual_response_transmission_fee = mgr.xnet_call_bytes_transmitted_fee(
response_payload_size,
NumBytes::from(b_callback.len() as u64),
test.subnet_size(),
cost_schedule,
);
Expand Down Expand Up @@ -177,6 +197,13 @@ fn execute_response_refunds_cycles() {
consumed_cycles_after,
consumed_cycles_before - response_transmission_refund.nominal(),
);
assert_eq!(
consumed_cycles_after_counter,
consumed_cycles_before_counter
+ (test.call_fee("update", &b_callback) + actual_response_transmission_fee)
.nominal(),
);
assert_eq!(consumed_cycles_after, consumed_cycles_after_counter);
}
}

Expand Down
7 changes: 5 additions & 2 deletions rs/execution_environment/src/execution_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2145,8 +2145,11 @@ impl ExecutionEnvironment {
state.metadata.subnet_call_context_manager.push_context(
SubnetCallContext::CanisterHttpRequest(canister_http_request_context),
);
if let Some(canister_stats) = state.canister_state_make_mut(&request.sender) {
canister_stats
if let Some(canister_state) = state.canister_state_make_mut(&request.sender) {
canister_state
.system_state
.observe_consumed_cycles_for_https_outcall(nominal_http_request_fee);
canister_state
.system_state
.canister_metrics_mut()
.load_metrics_mut()
Expand Down
Loading
Loading