Skip to content

Commit

Permalink
refactor(pox-4): Integrate Clarity value generation
Browse files Browse the repository at this point in the history
This change optimizes the generation of Clarity values for the
allowUntilBurnHt property in the AllowContractCallerCommand tests. By
directly creating Clarity optional values (none or some(uint)) within the
fast-check generator, we remove the need for conditional post-processing.
This approach simplifies the test setup and aligns more closely with
the intended usage patterns of the Clarity and fast-check libraries.
  • Loading branch information
moodmosaic committed Mar 26, 2024
1 parent 6bb9a67 commit 58f7dfd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
@@ -1,6 +1,6 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { expect } from "vitest";
import { boolCV, Cl } from "@stacks/transactions";
import { boolCV, Cl, ClarityType, OptionalCV, UIntCV } from "@stacks/transactions";

/**
* The `AllowContractCallerComand` gives a `contract-caller` authorization to call stacking methods.
Expand All @@ -13,7 +13,7 @@ import { boolCV, Cl } from "@stacks/transactions";
export class AllowContractCallerCommand implements PoxCommand {
readonly wallet: Wallet;
readonly allowanceTo: Wallet;
readonly allowUntilBurnHt: number | undefined;
readonly allowUntilBurnHt: OptionalCV<UIntCV>;

/**
* Constructs an `AllowContractCallerComand` that authorizes a `contract-caller` to call
Expand All @@ -27,7 +27,7 @@ export class AllowContractCallerCommand implements PoxCommand {
constructor(
wallet: Wallet,
allowanceTo: Wallet,
allowUntilBurnHt: number | undefined,
allowUntilBurnHt: OptionalCV<UIntCV>,
) {
this.wallet = wallet;
this.allowanceTo = allowanceTo;
Expand All @@ -40,11 +40,6 @@ export class AllowContractCallerCommand implements PoxCommand {
}

run(model: Stub, real: Real): void {
// Arrange
const untilBurnHtOptionalCv = this.allowUntilBurnHt === undefined
? Cl.none()
: Cl.some(Cl.uint(this.allowUntilBurnHt));

// Act
const allowContractCaller = real.network.callPublicFn(
"ST000000000000000000002AMW42H.pox-4",
Expand All @@ -53,7 +48,7 @@ export class AllowContractCallerCommand implements PoxCommand {
// (caller principal)
Cl.principal(this.allowanceTo.stxAddress),
// (until-burn-ht (optional uint))
untilBurnHtOptionalCv,
this.allowUntilBurnHt,
],
this.wallet.stxAddress,
);
Expand Down Expand Up @@ -83,7 +78,7 @@ export class AllowContractCallerCommand implements PoxCommand {
" ",
)
} ${this.allowanceTo.label.padStart(12, " ")} ${"until".padStart(53)} ${
(this.allowUntilBurnHt || "none").toString().padStart(17)
optionalCVToString(this.allowUntilBurnHt).padStart(17)
}`,
);
}
Expand All @@ -92,6 +87,13 @@ export class AllowContractCallerCommand implements PoxCommand {
// fast-check will call toString() in case of errors, e.g. property failed.
// It will then make a minimal counterexample, a process called 'shrinking'
// https://github.com/dubzzz/fast-check/issues/2864#issuecomment-1098002642
return `${this.wallet.stxAddress} allow-contract-caller ${this.allowanceTo.stxAddress} until burn ht ${this.allowUntilBurnHt}`;
return `${this.wallet.stxAddress} allow-contract-caller ${this.allowanceTo.stxAddress} until burn ht ${
optionalCVToString(this.allowUntilBurnHt)
}`;
}
}

const optionalCVToString = (optional: OptionalCV): string =>
optional.type === ClarityType.OptionalSome
? (optional.value as UIntCV).value.toString()
: "none";
9 changes: 6 additions & 3 deletions contrib/core-contract-tests/tests/pox-4/pox_Commands.ts
Expand Up @@ -6,7 +6,7 @@ import { StackStxCommand } from "./pox_StackStxCommand";
import { DelegateStxCommand } from "./pox_DelegateStxCommand";
import { DelegateStackStxCommand } from "./pox_DelegateStackStxCommand";
import { Simnet } from "@hirosystems/clarinet-sdk";
import { Cl, cvToValue } from "@stacks/transactions";
import { Cl, cvToValue, OptionalCV, UIntCV } from "@stacks/transactions";
import { RevokeDelegateStxCommand } from "./pox_RevokeDelegateStxCommand";
import { AllowContractCallerCommand } from "./pox_AllowContractCallerCommand";

Expand Down Expand Up @@ -111,13 +111,16 @@ export function PoxCommands(
fc.record({
wallet: fc.constantFrom(...wallets.values()),
allowanceTo: fc.constantFrom(...wallets.values()),
alllowUntilBurnHt: fc.option(fc.integer({ min: 1 }), {nil: undefined}),
alllowUntilBurnHt: fc.oneof(
fc.constant(Cl.none()),
fc.integer({ min: 1 }).map((value) => Cl.some(Cl.uint(value))),
),
})
.map(
(r: {
wallet: Wallet;
allowanceTo: Wallet;
alllowUntilBurnHt: number | undefined;
alllowUntilBurnHt: OptionalCV<UIntCV>;
}) =>
new AllowContractCallerCommand(
r.wallet,
Expand Down

0 comments on commit 58f7dfd

Please sign in to comment.