File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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]
23302379async fn should_log_request_and_response ( ) {
23312380 let setup = Setup :: new ( ) . await . with_mock_api_keys ( ) . await ;
You can’t perform that action at this time.
0 commit comments