From eb95d20ac3876ee2234c4ddabbdd92eda38c6dcd Mon Sep 17 00:00:00 2001 From: BowTiedRadone Date: Tue, 16 Apr 2024 18:31:58 +0300 Subject: [PATCH] fix(pox-4-tests): Fix `DelegateStackStx`, `StackStx`, `DelegateStackExtend`, `RevokeDelegateStx` This commit fixes issues regarding values generated at the generator level that are not available after the reward cycle change. --- .../tests/pox-4/pox_CommandModel.ts | 20 ++++++++++++------- .../tests/pox-4/pox_Commands.ts | 4 ---- .../pox-4/pox_DelegateStackExtendCommand.ts | 5 +++++ .../pox-4/pox_DelegateStackStxCommand.ts | 14 ++++++------- .../pox-4/pox_RevokeDelegateStxCommand.ts | 1 + .../tests/pox-4/pox_StackStxCommand.ts | 15 +++++++------- 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/contrib/core-contract-tests/tests/pox-4/pox_CommandModel.ts b/contrib/core-contract-tests/tests/pox-4/pox_CommandModel.ts index 49a220537..f094f8fec 100644 --- a/contrib/core-contract-tests/tests/pox-4/pox_CommandModel.ts +++ b/contrib/core-contract-tests/tests/pox-4/pox_CommandModel.ts @@ -47,7 +47,9 @@ export class Stub { refreshStateForNextRewardCycle(real: Real) { const burnBlockHeightResult = real.network.runSnippet("burn-block-height"); - const burnBlockHeight = cvToValue(burnBlockHeightResult as ClarityValue); + const burnBlockHeight = Number( + cvToValue(burnBlockHeightResult as ClarityValue), + ); const lastRefreshedCycle = this.lastRefreshedCycle; const currentRewCycle = Math.floor((Number(burnBlockHeight) - 0) / 1050); @@ -57,13 +59,13 @@ export class Stub { this.wallets.forEach((w) => { const wallet = this.stackers.get(w.stxAddress)!; const expiredDelegators = wallet.poolMembers.filter((stackerAddress) => - this.stackers.get(stackerAddress)!.delegatedUntilBurnHt + 1 < - burnBlockHeight + this.stackers.get(stackerAddress)!.delegatedUntilBurnHt < + burnBlockHeight + 1 ); const expiredStackers = wallet.lockedAddresses.filter( (stackerAddress) => - this.stackers.get(stackerAddress)!.unlockHeight + 1 <= - burnBlockHeight, + this.stackers.get(stackerAddress)!.unlockHeight <= + burnBlockHeight + 1, ); expiredDelegators.forEach((expDelegator) => { @@ -79,7 +81,7 @@ export class Stub { }); if ( - wallet.unlockHeight > 0 && wallet.unlockHeight + 1 <= burnBlockHeight + wallet.unlockHeight > 0 && wallet.unlockHeight <= burnBlockHeight + 1 ) { wallet.isStacking = false; wallet.amountUnlocked += wallet.amountLocked; @@ -89,8 +91,12 @@ export class Stub { } wallet.committedRewCycleIndexes = []; }); + this.stackers.forEach((stacker) => + process.stdout.write(`${JSON.stringify(stacker)}\n`) + ); + + this.lastRefreshedCycle = currentRewCycle; } - this.lastRefreshedCycle = currentRewCycle; } } diff --git a/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts b/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts index a35991d85..8a0cd389d 100644 --- a/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts +++ b/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts @@ -42,14 +42,12 @@ export function PoxCommands( authId: fc.nat(), period: fc.integer({ min: 1, max: 12 }), margin: fc.integer({ min: 1, max: 9 }), - currentCycle: fc.constant(currentCycle(network)), }).map(( r: { wallet: Wallet; authId: number; period: number; margin: number; - currentCycle: number; }, ) => new StackStxCommand( @@ -57,7 +55,6 @@ export function PoxCommands( r.authId, r.period, r.margin, - r.currentCycle, ) ), // DelegateStxCommand @@ -241,7 +238,6 @@ export function PoxCommands( return new DelegateStackStxCommand( finalResult.operator, finalResult.stacker, - finalResult.startBurnHt, finalResult.period, finalResult.amount, finalResult.unlockBurnHt, diff --git a/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackExtendCommand.ts b/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackExtendCommand.ts index bcf4b15a8..2a0e5808a 100644 --- a/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackExtendCommand.ts +++ b/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackExtendCommand.ts @@ -73,11 +73,16 @@ export class DelegateStackExtendCommand implements PoxCommand { ); const lastExtendCycle = firstExtendCycle + this.extendCount - 1; const totalPeriod = lastExtendCycle - firstRewardCycle + 1; + const newUnlockHeight = (REWARD_CYCLE_LENGTH * (firstRewardCycle + totalPeriod - 1) + 0); + const stackedAmount = stackerWallet.amountLocked; return ( stackerWallet.amountLocked > 0 && stackerWallet.hasDelegated === true && stackerWallet.isStacking === true && + stackerWallet.delegatedTo === this.operator.stxAddress && + stackerWallet.delegatedUntilBurnHt >= newUnlockHeight && + stackerWallet.delegatedMaxAmount >= stackedAmount && operatorWallet.poolMembers.includes(this.stacker.stxAddress) && operatorWallet.lockedAddresses.includes(this.stacker.stxAddress) && totalPeriod <= 12 diff --git a/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackStxCommand.ts b/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackStxCommand.ts index c2d0cc466..acc29cf89 100644 --- a/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackStxCommand.ts +++ b/contrib/core-contract-tests/tests/pox-4/pox_DelegateStackStxCommand.ts @@ -7,7 +7,7 @@ import { } from "./pox_CommandModel.ts"; import { poxAddressToTuple } from "@stacks/stacking"; import { assert, expect } from "vitest"; -import { Cl, ClarityType, isClarityType } from "@stacks/transactions"; +import { Cl, ClarityType, ClarityValue, cvToValue, isClarityType } from "@stacks/transactions"; import { currentCycle } from "./pox_Commands.ts"; /** @@ -32,7 +32,6 @@ import { currentCycle } from "./pox_Commands.ts"; export class DelegateStackStxCommand implements PoxCommand { readonly operator: Wallet; readonly stacker: Wallet; - readonly startBurnHt: number; readonly period: number; readonly amountUstx: bigint; readonly unlockBurnHt: number; @@ -43,7 +42,6 @@ export class DelegateStackStxCommand implements PoxCommand { * * @param operator - Represents the Pool Operator's wallet. * @param stacker - Represents the STacker's wallet. - * @param startBurnHt - A burn height inside the current reward cycle. * @param period - Number of reward cycles to lock uSTX. * @param amountUstx - The uSTX amount stacked by the Operator on behalf * of the Stacker. @@ -52,14 +50,12 @@ export class DelegateStackStxCommand implements PoxCommand { 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; @@ -98,6 +94,8 @@ export class DelegateStackStxCommand implements PoxCommand { run(model: Stub, real: Real): void { model.trackCommandRun(this.constructor.name); + const burnBlockHeightCV = real.network.runSnippet("burn-block-height"); + const burnBlockHeight = Number(cvToValue(burnBlockHeightCV as ClarityValue)); // Act const delegateStackStx = real.network.callPublicFn( @@ -111,7 +109,7 @@ export class DelegateStackStxCommand implements PoxCommand { // (pox-addr { version: (buff 1), hashbytes: (buff 32) }) poxAddressToTuple(this.operator.btcAddress), // (start-burn-ht uint) - Cl.uint(this.startBurnHt), + Cl.uint(burnBlockHeight), // (lock-period uint) Cl.uint(this.period), ], @@ -120,7 +118,7 @@ export class DelegateStackStxCommand implements PoxCommand { const { result: rewardCycle } = real.network.callReadOnlyFn( "ST000000000000000000002AMW42H.pox-4", "burn-height-to-reward-cycle", - [Cl.uint(real.network.blockHeight)], + [Cl.uint(burnBlockHeight)], this.operator.stxAddress, ); assert(isClarityType(rewardCycle, ClarityType.UInt)); @@ -179,6 +177,6 @@ export class DelegateStackStxCommand 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.operator.label} delegate-stack-stx period ${this.period}`; + return `${this.operator.label} delegate-stack-stx stacker ${this.stacker.label} period ${this.period}`; } } diff --git a/contrib/core-contract-tests/tests/pox-4/pox_RevokeDelegateStxCommand.ts b/contrib/core-contract-tests/tests/pox-4/pox_RevokeDelegateStxCommand.ts index 2d23fcc73..f0758b568 100644 --- a/contrib/core-contract-tests/tests/pox-4/pox_RevokeDelegateStxCommand.ts +++ b/contrib/core-contract-tests/tests/pox-4/pox_RevokeDelegateStxCommand.ts @@ -77,6 +77,7 @@ export class RevokeDelegateStxCommand implements PoxCommand { wallet.delegatedTo = ""; wallet.delegatedUntilBurnHt = 0; wallet.delegatedMaxAmount = 0; + wallet.delegatedPoxAddress = ''; // Remove the Stacker from the Pool Operator's pool members list. const walletIndexInDelegatorsList = operatorWallet.poolMembers.indexOf( diff --git a/contrib/core-contract-tests/tests/pox-4/pox_StackStxCommand.ts b/contrib/core-contract-tests/tests/pox-4/pox_StackStxCommand.ts index aea6ee0c7..13d27a9c3 100644 --- a/contrib/core-contract-tests/tests/pox-4/pox_StackStxCommand.ts +++ b/contrib/core-contract-tests/tests/pox-4/pox_StackStxCommand.ts @@ -7,7 +7,8 @@ import { } from "./pox_CommandModel.ts"; import { Pox4SignatureTopic, poxAddressToTuple } from "@stacks/stacking"; import { assert, expect } from "vitest"; -import { Cl, ClarityType, isClarityType } from "@stacks/transactions"; +import { Cl, ClarityType, ClarityValue, cvToJSON, cvToValue, isClarityType } from "@stacks/transactions"; +import { currentCycle } from "./pox_Commands.ts"; /** * The `StackStxCommand` locks STX for stacking within PoX-4. This self-service @@ -26,7 +27,6 @@ export class StackStxCommand implements PoxCommand { readonly authId: number; readonly period: number; readonly margin: number; - readonly currentCycle: number; /** * Constructs a `StackStxCommand` to lock uSTX for stacking. @@ -42,13 +42,11 @@ export class StackStxCommand implements PoxCommand { authId: number, period: number, margin: number, - currentCycle: number, ) { this.wallet = wallet; this.authId = authId; this.period = period; this.margin = margin; - this.currentCycle = currentCycle; } check(model: Readonly): boolean { @@ -66,6 +64,9 @@ export class StackStxCommand implements PoxCommand { run(model: Stub, real: Real): void { model.trackCommandRun(this.constructor.name); + const burnBlockHeightCV = real.network.runSnippet("burn-block-height"); + const burnBlockHeight = Number(cvToValue(burnBlockHeightCV as ClarityValue)); + const currentRewCycle = currentCycle(real.network) // The maximum amount of uSTX that can be used (per tx) with this signer // key. For our tests, we will use the minimum amount of uSTX to be stacked @@ -80,7 +81,7 @@ export class StackStxCommand implements PoxCommand { // For `stack-stx` and `stack-extend`, this refers to the reward cycle // where the transaction is confirmed. For `stack-aggregation-commit`, // this refers to the reward cycle argument in that function. - rewardCycle: this.currentCycle, + rewardCycle: currentRewCycle, // For `stack-stx`, this refers to `lock-period`. For `stack-extend`, // this refers to `extend-count`. For `stack-aggregation-commit`, this is // `u1`. @@ -112,7 +113,7 @@ export class StackStxCommand implements PoxCommand { // (pox-addr (tuple (version (buff 1)) (hashbytes (buff 32)))) poxAddressToTuple(this.wallet.btcAddress), // (start-burn-ht uint) - Cl.uint(real.network.blockHeight), + Cl.uint(burnBlockHeight), // (lock-period uint) Cl.uint(this.period), // (signer-sig (optional (buff 65))) @@ -182,6 +183,6 @@ export class StackStxCommand 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.label} stack-stx auth-id ${this.authId} and period ${this.period} during reward cycle ${this.currentCycle}`; + return `${this.wallet.label} stack-stx auth-id ${this.authId} and period ${this.period}`; } }