Skip to content

Commit

Permalink
refactor(pox-4): Dynamic unlockBurnHt and cleaner state
Browse files Browse the repository at this point in the history
- Moved `network` from `Stub` (Model) to `Real`, streamlining state management.
- `unlockBurnHt` now dynamically calculated, passed via constructor, ensuring
  alignment with generated `period`.
- Utilized `fast-check`'s `chain` for dynamic `unlockBurnHt` calculation,
  improving test accuracy.
- Enforces discipline in constructor use and `check` method validation,
  highlighting the importance of explicit state and parameter handling for
  reliable testing.
  • Loading branch information
moodmosaic committed Mar 26, 2024
1 parent 58f7dfd commit d55fc62
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
Expand Up @@ -28,7 +28,6 @@ describe("PoX-4 invariant tests", () => {
const model: Stub = {
stackingMinimum: 0,
wallets: new Map<StxAddress, Wallet>(),
network: sut.network
};

const wallets = [
Expand Down
Expand Up @@ -9,7 +9,6 @@ export type StxAddress = string;
export type Stub = {
stackingMinimum: number;
wallets: Map<StxAddress, Wallet>;
network: Simnet;
};

export type Real = {
Expand Down
22 changes: 12 additions & 10 deletions contrib/core-contract-tests/tests/pox-4/pox_Commands.ts
Expand Up @@ -90,21 +90,23 @@ export function PoxCommands(
}),
period: fc.integer({ min: 1, max: 12 }),
amount: fc.bigInt({ min:0n, max: 100_000_000_000_000n }),
}).map((
r: {
operator: Wallet;
stacker: Wallet;
startBurnHt: number;
period: number;
amount: bigint;
},
) =>
}).chain((r) =>
fc.record({
unlockBurnHt: fc.constant(
currentCycleFirstBlock(network) + 1050 * (r.period + 1),
),
}).map((unlockBurnHtRecord) => ({
...r,
...unlockBurnHtRecord,
}))
).map((r) =>
new DelegateStackStxCommand(
r.operator,
r.stacker,
r.startBurnHt,
r.period,
r.amount
r.amount,
r.unlockBurnHt,
)
),
// AllowContractCallerCommand
Expand Down
Expand Up @@ -2,7 +2,6 @@ import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { poxAddressToTuple } from "@stacks/stacking";
import { assert, expect } from "vitest";
import { Cl, ClarityType, isClarityType } from "@stacks/transactions";
import { currentCycleFirstBlock } from "./pox_Commands.ts";

/**
* The `DelegateStackStxCommand` locks STX for stacking within PoX-4 on behalf of a delegator.
Expand All @@ -29,6 +28,7 @@ export class DelegateStackStxCommand implements PoxCommand {
readonly startBurnHt: number;
readonly period: number;
readonly amountUstx: bigint;
readonly unlockBurnHt: number;

/**
* Constructs a `DelegateStackStxCommand` to lock uSTX as a Pool Operator
Expand All @@ -40,19 +40,22 @@ export class DelegateStackStxCommand implements PoxCommand {
* @param period - Number of reward cycles to lock uSTX.
* @param amountUstx - The uSTX amount stacked by the Operator on behalf
* of the Stacker
* @param unlockBurnHt - The burn height at which the uSTX is unlocked.
*/
constructor(
operator: Wallet,
stacker: Wallet,
startBurnHt: number,
period: number,
amountUstx: bigint,
unlockBurnHt: number,
) {
this.operator = operator;
this.stacker = stacker;
this.startBurnHt = startBurnHt;
this.period = period;
this.amountUstx = amountUstx;
this.unlockBurnHt = unlockBurnHt;
}

check(model: Readonly<Stub>): boolean {
Expand All @@ -73,7 +76,6 @@ export class DelegateStackStxCommand implements PoxCommand {

const operatorWallet = model.wallets.get(this.operator.stxAddress)!;
const stackerWallet = model.wallets.get(this.stacker.stxAddress)!;
const unlockBurnHt = currentCycleFirstBlock(model.network) + 1050 * (this.period + 1)

return (
model.stackingMinimum > 0 &&
Expand All @@ -83,7 +85,7 @@ export class DelegateStackStxCommand implements PoxCommand {
Number(this.amountUstx) <= stackerWallet.ustxBalance &&
Number(this.amountUstx) >= model.stackingMinimum &&
operatorWallet.hasPoolMembers.includes(stackerWallet.stxAddress) &&
unlockBurnHt <= stackerWallet.delegatedUntilBurnHt
this.unlockBurnHt <= stackerWallet.delegatedUntilBurnHt
);
}

Expand Down

0 comments on commit d55fc62

Please sign in to comment.