Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cycles prop tests #2178

Merged
merged 15 commits into from
Nov 13, 2024
31 changes: 17 additions & 14 deletions tests/property/ic_api/cycles/src/cycles/index.ts
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,10 @@ function acceptCycles(
): CyclesResult {
const startingCanisterBalance = canisterBalance();
const initialAvailable = msgCyclesAvailable();

const effectiveNumChunks = numChunks ?? 1n;

const requestedAmount = receiveAmount ?? initialAvailable;

const chunkSize =
requestedAmount > 0n
? bigintMax(1n, requestedAmount / effectiveNumChunks)
: 0n;

const accepted = acceptCyclesRecursive(chunkSize, requestedAmount, 0n);

const accepted = acceptCyclesStrategy(
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
receiveAmount ?? initialAvailable,
numChunks
);
const finalAvailable = msgCyclesAvailable();
const endingCanisterBalance = canisterBalance();
const cyclesRefunded = 0n; // This will always be 0 in the cycles canister
Expand All @@ -62,7 +54,18 @@ function acceptCycles(
};
}

function acceptCyclesRecursive(
function acceptCyclesStrategy(
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
receiveAmount: bigint,
numChunks?: bigint
): bigint {
if (numChunks === undefined) {
return msgCyclesAccept(receiveAmount);
}
const chunkSize = bigintMax(1n, receiveAmount / numChunks);
return acceptCyclesChunk(chunkSize, receiveAmount, 0n);
}

function acceptCyclesChunk(
chunkSize: bigint,
totalToAccept: bigint,
accumulatedCycles: bigint
Expand All @@ -76,7 +79,7 @@ function acceptCyclesRecursive(
}

const newlyAccepted = msgCyclesAccept(chunkSize);
return acceptCyclesRecursive(
return acceptCyclesChunk(
chunkSize,
totalToAccept,
accumulatedCycles + newlyAccepted
Expand Down
12 changes: 6 additions & 6 deletions tests/property/ic_api/cycles/src/intermediary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export default class {
this.cyclesPrincipal,
'receiveVariableCycles',
{
returnIdlType: CyclesResult,
cycles: amountToSend,
paramIdlTypes: [IDL.Nat64],
args: [amountToAccept]
returnIdlType: CyclesResult,
args: [amountToAccept],
cycles: amountToSend
}
);
return { ...result, cyclesRefunded: msgCyclesRefunded() };
Expand All @@ -50,10 +50,10 @@ export default class {
this.cyclesPrincipal,
'receiveCyclesByChunk',
{
returnIdlType: CyclesResult,
cycles: amount,
paramIdlTypes: [IDL.Nat64],
args: [numChunks]
returnIdlType: CyclesResult,
args: [numChunks],
cycles: amount
}
);
return { ...result, cyclesRefunded: msgCyclesRefunded() };
Expand Down
10 changes: 5 additions & 5 deletions tests/property/ic_api/cycles/test/tests.ts
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { defaultPropTestParams, expect, it, Test } from 'azle/test';
import fc from 'fast-check';

import { CyclesResult } from '../src/types';
import { _SERVICE } from './dfx_generated/intermediary/intermediary.did';
import { _SERVICE as Actor } from './dfx_generated/intermediary/intermediary.did';

export function getTests(): Test {
return () => {
it('should send all cycles from intermediary to cycles canister and receive none back', async () => {
const intermediaryCanister =
await getCanisterActor<_SERVICE>('intermediary');
await getCanisterActor<Actor>('intermediary');
await fc.assert(
fc.asyncProperty(fc.bigInt(0n, 10_000_000n), async (amount) => {
const result =
Expand All @@ -22,7 +22,7 @@ export function getTests(): Test {

it('should send a portion of the cycles from intermediary to cycles canister and receive the rest back', async () => {
const intermediaryCanister =
await getCanisterActor<_SERVICE>('intermediary');
await getCanisterActor<Actor>('intermediary');
await fc.assert(
fc.asyncProperty(
fc.bigInt(0n, 10_000_000n),
Expand All @@ -47,7 +47,7 @@ export function getTests(): Test {

it('should receive all cycles back to the intermediary if none are received by the cycles canister', async () => {
const intermediaryCanister =
await getCanisterActor<_SERVICE>('intermediary');
await getCanisterActor<Actor>('intermediary');
await fc.assert(
fc.asyncProperty(fc.bigInt(0n, 10_000_000n), async (amount) => {
const result =
Expand All @@ -60,7 +60,7 @@ export function getTests(): Test {

it('should send cycles in chunks from intermediary to cycles canister', async () => {
const intermediaryCanister =
await getCanisterActor<_SERVICE>('intermediary');
await getCanisterActor<Actor>('intermediary');
await fc.assert(
fc.asyncProperty(
fc.bigInt(0n, 10_000_000n),
Expand Down