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

test: pox-4 delegation integration tests #1937

Merged
merged 11 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/docker-compose.dev.stacks-blockchain.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.7'
services:
stacks-blockchain:
image: 'hirosystems/stacks-api-e2e:stacks3.0-1360a17'
image: 'hirosystems/stacks-api-e2e:stacks3.0-4d11d85'
restart: on-failure
environment:
STACKS_EVENT_OBSERVER: host.docker.internal:3700
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.dev.stacks-krypton.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.7'
services:
stacks-blockchain:
image: 'hirosystems/stacks-api-e2e:stacks3.0-1360a17'
image: 'hirosystems/stacks-api-e2e:stacks3.0-4d11d85'
ports:
- '18443:18443' # bitcoin regtest JSON-RPC interface
- '18444:18444' # bitcoin regtest p2p
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"@stacks/common": "6.10.0",
"@stacks/encryption": "6.13.1",
"@stacks/network": "6.11.3",
"@stacks/stacking": "6.13.1",
"@stacks/stacking": "6.13.2",
"@stacks/transactions": "6.11.3",
"@types/express-list-endpoints": "4.0.1",
"@types/lru-cache": "5.1.1",
Expand Down
6 changes: 3 additions & 3 deletions src/tests-2.5/pox-4-btc-address-formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe.each([P2SH_P2WPKH, P2WPKH, P2WSH, P2TR])(
let stackingClient: StackingClient;
let signerPrivKey: StacksPrivateKey;
let signerPubKey: string;
const cycleCount = 2;
const cycleCount = 1;

const { btcAddr, btcAddrDecoded, btcAddrRegtest, btcDescriptor } = addressSetup();

Expand Down Expand Up @@ -184,8 +184,8 @@ describe.each([P2SH_P2WPKH, P2WPKH, P2WSH, P2TR])(

test('stx unlocked - RPC balance', async () => {
// Wait until account has unlocked (finished Stacking cycles)
const rpcAccount = await testEnv.client.getAccount(account.stacksAddress);
await standByUntilBurnBlock(rpcAccount.unlock_height + poxInfo.reward_phase_block_length);
await standByForPoxCycle();
await standByForPoxCycle();

// Check that STX are no longer reported as locked by the RPC endpoints:
await timeout(200); // make sure unlock was processed
Expand Down
4 changes: 2 additions & 2 deletions src/tests-2.5/pox-4-burnchain-delegate-stx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ describe('PoX-4 - Stack using Bitcoin-chain delegate ops', () => {
'(define-public (delegate-stx (amount-ustx uint) (delegate-to principal) (until-burn-ht (optional uint)) (pox-addr (optional (tuple (hashbytes (buff 32)) (version (buff 1)))))))',
function_args: [
{
hex: '0x010000000000000000000ffd1e7ab22000',
repr: 'u4500432000000000',
hex: '0x01000000000000000000066541116e8800',
repr: 'u1800180000000000',
name: 'amount-ustx',
type: 'uint',
},
Expand Down
105 changes: 75 additions & 30 deletions src/tests-2.5/pox-4-delegate-aggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {
import { stxToMicroStx } from '../helpers';
import {
AnchorMode,
StacksPrivateKey,
bufferCV,
makeContractCall,
makeRandomPrivKey,
makeSTXTokenTransfer,
noneCV,
randomBytes,
someCV,
standardPrincipalCV,
uintCV,
Expand All @@ -35,6 +36,9 @@ import {
} from 'stacks-encoding-native-js';
import { AddressStxBalanceResponse } from '@stacks/stacks-blockchain-api-types';
import * as assert from 'assert';
import { hexToBytes } from '@stacks/common';
import { StackingClient } from '@stacks/stacking';
import { getPublicKeyFromPrivate } from '@stacks/encryption';

describe('PoX-4 - Delegate aggregation increase operations', () => {
const seedKey = testnetKeys[4].secretKey;
Expand All @@ -51,12 +55,20 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {

let poxCycleAddressIndex: bigint;

let stackingClient: StackingClient;
let signerPrivKey: StacksPrivateKey;
let signerPubKey: string;

beforeAll(() => {
seedAccount = accountFromKey(seedKey);
// delegatorKey = ECPair.makeRandom({ compressed: true }).privateKey!.toString('hex');
// delegateeKey = ECPair.makeRandom({ compressed: true }).privateKey!.toString('hex');
delegatorAccount = accountFromKey(delegatorKey);
delegateeAccount = accountFromKey(delegateeKey);

stackingClient = new StackingClient(delegatorAccount.stxAddr, testEnv.stacksNetwork);
signerPrivKey = makeRandomPrivKey();
signerPubKey = getPublicKeyFromPrivate(signerPrivKey.data);
});

test('Import testing accounts to bitcoind', async () => {
Expand Down Expand Up @@ -116,7 +128,8 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
});

test('Get pox-info', async () => {
// wait until the start of the next cycle so we have enough blocks within the cycle to perform the various txs
// wait until the start of the next cycle so we have enough blocks within the cycle to perform
// the various txs
poxInfo = await standByForNextPoxCycle();
[contractAddress, contractName] = poxInfo.contract_id.split('.');
expect(contractName).toBe('pox-4');
Expand Down Expand Up @@ -175,6 +188,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
let amountDelegated: bigint;
let amountStackedInitial: bigint;
test('Perform delegate-stack-stx operation', async () => {
await standByForPoxCycle();
// get amount delegated
const getDelegationInfo1 = await readOnlyFnCall<
ClarityValueTuple<{ 'amount-ustx': ClarityValueUInt }>
Expand All @@ -190,7 +204,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
const poxInfo2 = await testEnv.client.getPox();
const startBurnHt = poxInfo2.current_burnchain_block_height as number;

amountStackedInitial = amountDelegated - 2000n;
amountStackedInitial = amountDelegated - 20000n;

const txFee = 10000n;
const delegateStackStxTx = await makeContractCall({
Expand All @@ -203,7 +217,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
uintCV(amountStackedInitial), // amount-ustx
delegateeAccount.poxAddrClar, // pox-addr
uintCV(startBurnHt), // start-burn-ht
uintCV(1), // lock-period,
uintCV(6), // lock-period,
],
network: testEnv.stacksNetwork,
anchorMode: AnchorMode.OnChainOnly,
Expand Down Expand Up @@ -236,7 +250,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
);
expect(res.results[0].data).toEqual(
expect.objectContaining({
lock_period: '1',
lock_period: '6',
lock_amount: amountStackedInitial.toString(),
})
);
Expand All @@ -250,8 +264,20 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
});

test('Perform stack-aggregation-commit-indexed - delegator commit to stacking operation', async () => {
await standByForPoxCycle();
const poxInfo2 = await testEnv.client.getPox();
const rewardCycle = BigInt(poxInfo2.next_cycle.id);
const signerSig = hexToBytes(
stackingClient.signPoxSignature({
topic: 'agg-commit',
poxAddress: delegateeAccount.btcAddr,
rewardCycle: Number(rewardCycle),
period: 1,
signerPrivateKey: signerPrivKey,
maxAmount: amountStackedInitial,
authId: 0,
})
);
const stackAggrCommitTx = await makeContractCall({
senderKey: delegatorAccount.secretKey,
contractAddress,
Expand All @@ -260,7 +286,10 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
functionArgs: [
delegateeAccount.poxAddrClar, // pox-addr
uintCV(rewardCycle), // reward-cycle
bufferCV(randomBytes(33)), // signer-key
someCV(bufferCV(signerSig)), // signer-sig
bufferCV(hexToBytes(signerPubKey)), // signer-key
uintCV(amountStackedInitial.toString()), // max-amount
uintCV(0), // auth-id
],
network: testEnv.stacksNetwork,
anchorMode: AnchorMode.OnChainOnly,
Expand Down Expand Up @@ -319,29 +348,6 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
Buffer.from(delegateStackIncreaseTx.serialize())
);

// then commit to increased amount with call to `stack-aggregation-increase`
const poxInfo2 = await testEnv.client.getPox();
const rewardCycle = BigInt(poxInfo2.next_cycle.id);
const stackAggrIncreaseTx = await makeContractCall({
senderKey: delegatorAccount.secretKey,
contractAddress,
contractName,
functionName: 'stack-aggregation-increase',
functionArgs: [
delegateeAccount.poxAddrClar, // pox-addr
uintCV(rewardCycle), // reward-cycle
uintCV(poxCycleAddressIndex), // reward-cycle-index
],
network: testEnv.stacksNetwork,
anchorMode: AnchorMode.OnChainOnly,
fee: txFee,
validateWithAbi: false,
nonce: delegateStackIncreaseTx.auth.spendingCondition.nonce + 1n,
});
const { txId: stackAggrIncreaseTxId } = await testEnv.client.sendTransaction(
Buffer.from(stackAggrIncreaseTx.serialize())
);

const delegateStackIncreaseDbTx = await standByForTxSuccess(delegateStackIncreaseTxId);
const delegateStackIncreaseResult = decodeClarityValue<
ClarityValueResponseOk<
Expand Down Expand Up @@ -392,6 +398,45 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
})
);

// then commit to increased amount with call to `stack-aggregation-increase`
const poxInfo2 = await testEnv.client.getPox();
const maxAmount = amountStackedInitial + stxToDelegateIncrease;
const rewardCycle = BigInt(poxInfo2.next_cycle.id);
const signerSig = hexToBytes(
stackingClient.signPoxSignature({
topic: 'agg-increase',
poxAddress: delegateeAccount.btcAddr,
rewardCycle: Number(rewardCycle),
period: 1,
signerPrivateKey: signerPrivKey,
maxAmount: maxAmount,
authId: 1,
})
);
const stackAggrIncreaseTx = await makeContractCall({
senderKey: delegatorAccount.secretKey,
contractAddress,
contractName,
functionName: 'stack-aggregation-increase',
functionArgs: [
delegateeAccount.poxAddrClar, // pox-addr
uintCV(rewardCycle), // reward-cycle
uintCV(poxCycleAddressIndex), // reward-cycle-index
someCV(bufferCV(signerSig)), // signer-sig
bufferCV(hexToBytes(signerPubKey)), // signer-key
uintCV(maxAmount.toString()), // max-amount
uintCV(1), // auth-id
],
network: testEnv.stacksNetwork,
anchorMode: AnchorMode.OnChainOnly,
fee: txFee,
validateWithAbi: false,
nonce: delegateStackIncreaseTx.auth.spendingCondition.nonce + 1n,
});
const { txId: stackAggrIncreaseTxId } = await testEnv.client.sendTransaction(
Buffer.from(stackAggrIncreaseTx.serialize())
);

// validate API endpoint balance state for account
const apiBalance = await fetchGet<AddressStxBalanceResponse>(
`/extended/v1/address/${delegateeAccount.stxAddr}/stx`
Expand Down Expand Up @@ -421,7 +466,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
});

test('Wait for current pox cycle to complete', async () => {
const poxStatus1 = await standByForPoxCycleEnd();
const poxStatus1 = await standByForPoxCycle();
const poxStatus2 = await standByForPoxCycle();
console.log('___Wait for current pox cycle to complete___', {
pox1: { height: poxStatus1.current_burnchain_block_height, ...poxStatus1.next_cycle },
Expand Down
36 changes: 30 additions & 6 deletions src/tests-2.5/pox-4-delegate-revoked-stacking.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { hexToBytes } from '@stacks/common';
import { poxAddressToTuple } from '@stacks/stacking';
import { StackingClient, poxAddressToTuple } from '@stacks/stacking';
import { AddressStxBalanceResponse } from '@stacks/stacks-blockchain-api-types';
import {
AnchorMode,
Cl,
StacksPrivateKey,
bufferCV,
makeContractCall,
makeRandomPrivKey,
makeSTXTokenTransfer,
noneCV,
randomBytes,
Expand All @@ -29,6 +30,8 @@ import {
standByForTxSuccess,
testEnv,
} from '../test-utils/test-helpers';
import { hexToBytes } from '@stacks/common';
import { getPublicKeyFromPrivate } from '@stacks/encryption';

describe('PoX-4 - Delegate Revoked Stacking', () => {
const seedKey = testnetKeys[4].secretKey;
Expand All @@ -47,10 +50,18 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
let contractAddress: string;
let contractName: string;

let stackingClient: StackingClient;
let signerPrivKey: StacksPrivateKey;
let signerPubKey: string;

beforeAll(() => {
seedAccount = accountFromKey(seedKey);
POOL = accountFromKey(delegatorKey);
STACKER = accountFromKey(delegateeKey);

stackingClient = new StackingClient(POOL.stxAddr, testEnv.stacksNetwork);
signerPrivKey = makeRandomPrivKey();
signerPubKey = getPublicKeyFromPrivate(signerPrivKey.data);
});

test('Seed delegate accounts', async () => {
Expand Down Expand Up @@ -292,7 +303,7 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
expect.objectContaining({
name: 'revoke-delegate-stx',
stacker: STACKER.stxAddr,
data: { delegate_to: POOL.stxAddr },
data: expect.objectContaining({ delegate_to: POOL.stxAddr }),
})
);

Expand All @@ -303,6 +314,7 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
});

test('Try to perform delegate-stack-stx - while revoked', async () => {
await standByForPoxCycle();
poxInfo = await testEnv.client.getPox();
const startBurnHt = poxInfo.current_burnchain_block_height as number;

Expand Down Expand Up @@ -413,7 +425,17 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
test('Perform stack-aggregation-commit - delegator commit to stacking operation', async () => {
poxInfo = await testEnv.client.getPox();
const rewardCycle = BigInt(poxInfo.next_cycle.id);

const signerSig = hexToBytes(
stackingClient.signPoxSignature({
topic: 'agg-commit',
poxAddress: STACKER.btcAddr,
rewardCycle: Number(rewardCycle),
period: 1,
signerPrivateKey: signerPrivKey,
maxAmount: DELEGATE_HALF_AMOUNT,
authId: 0,
})
);
const stackAggrCommitTx = await makeContractCall({
senderKey: POOL.secretKey,
contractAddress,
Expand All @@ -422,7 +444,10 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
functionArgs: [
STACKER.poxAddrClar, // pox-addr
uintCV(rewardCycle), // reward-cycle
bufferCV(randomBytes(33)), // signer-key
someCV(bufferCV(signerSig)), // signer-sig
bufferCV(hexToBytes(signerPubKey)), // signer-key
uintCV(DELEGATE_HALF_AMOUNT.toString()), // max-amount
uintCV(0), // auth-id
],
network: testEnv.stacksNetwork,
anchorMode: AnchorMode.OnChainOnly,
Expand All @@ -447,7 +472,6 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
});

test('Wait for current two pox cycles to complete', async () => {
await standByForPoxCycleEnd();
await standByForPoxCycle();
await standByForPoxCycle();
});
Expand Down
Loading
Loading