Skip to content

Commit 6a90008

Browse files
authored
test: add integration tests for cycle draining (#198)
(XC-416) Test that the canister balance decrease after calls with insufficient cycles is small.
1 parent a2b993e commit 6a90008

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

integration_tests/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ impl Setup {
125125
.unwrap_or_else(|err| panic!("Upgrade canister failed: {:?}", err));
126126
}
127127

128+
pub async fn get_canister_cycle_balance(&self) -> u128 {
129+
self.env.cycle_balance(self.sol_rpc_canister_id).await
130+
}
131+
128132
pub async fn with_mock_api_keys(self) -> Self {
129133
let client = self.client().build();
130134
let providers = client.get_providers().await;

integration_tests/tests/tests.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,55 @@ mod metrics_tests {
23262326
}
23272327
}
23282328

2329+
#[tokio::test]
2330+
async fn should_not_drain_canister_balance_when_insufficient_cycles_attached() {
2331+
let setup = Setup::new().await.with_mock_api_keys().await;
2332+
2333+
let client = setup
2334+
.client()
2335+
.with_rpc_sources(RpcSources::Custom(vec![RpcSource::Supported(
2336+
SupportedRpcProviderId::AnkrMainnet,
2337+
)]))
2338+
.build();
2339+
2340+
let required_cycles = client
2341+
.get_block(0)
2342+
.with_transaction_details(TransactionDetails::Signatures)
2343+
.request_cost()
2344+
.send()
2345+
.await
2346+
.unwrap();
2347+
2348+
for cycles in [0_u128, required_cycles - 1_000] {
2349+
let balance_before = setup.get_canister_cycle_balance().await;
2350+
let results = client
2351+
.get_block(0)
2352+
.with_transaction_details(TransactionDetails::Signatures)
2353+
.with_cycles(cycles)
2354+
.try_send()
2355+
.await;
2356+
2357+
assert!(
2358+
results.is_err()
2359+
|| matches!(
2360+
results,
2361+
Ok(MultiRpcResult::Consistent(Err(RpcError::ProviderError(
2362+
ProviderError::TooFewCycles { .. }
2363+
))))
2364+
)
2365+
);
2366+
2367+
let balance_after = setup.get_canister_cycle_balance().await;
2368+
2369+
// Rejecting requests with insufficient cycles attached still costs a small amount in execution costs
2370+
assert!(
2371+
balance_after >= balance_before - 30_000_000,
2372+
"Canister cycle balance decrease: {:?}",
2373+
balance_before - balance_after
2374+
);
2375+
}
2376+
}
2377+
23292378
#[tokio::test]
23302379
async fn should_log_request_and_response() {
23312380
let setup = Setup::new().await.with_mock_api_keys().await;

0 commit comments

Comments
 (0)