Skip to content

Commit b0e5720

Browse files
authored
test: pox-4 delegation integration tests (#1937)
* fix: progress on delegate-aggregation * chore: upgrade krypton image * fix: revoked stacking tests * fix: upgrade to newer core * fix: delegate stacking * fix: btc address formats * fix: rosetta segwit * chore: attempt to fix delegate aggregation * fix: burnchain delegate stx * fix: delegate aggregation tests * fix: rosetta btc addr
1 parent 4ddcbfa commit b0e5720

12 files changed

+157
-65
lines changed

docker/docker-compose.dev.stacks-blockchain.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: '3.7'
22
services:
33
stacks-blockchain:
4-
image: 'hirosystems/stacks-api-e2e:stacks3.0-1360a17'
4+
image: 'hirosystems/stacks-api-e2e:stacks3.0-4d11d85'
55
restart: on-failure
66
environment:
77
STACKS_EVENT_OBSERVER: host.docker.internal:3700

docker/docker-compose.dev.stacks-krypton.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: '3.7'
22
services:
33
stacks-blockchain:
4-
image: 'hirosystems/stacks-api-e2e:stacks3.0-1360a17'
4+
image: 'hirosystems/stacks-api-e2e:stacks3.0-4d11d85'
55
ports:
66
- '18443:18443' # bitcoin regtest JSON-RPC interface
77
- '18444:18444' # bitcoin regtest p2p

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"@stacks/common": "6.10.0",
9595
"@stacks/encryption": "6.13.1",
9696
"@stacks/network": "6.11.3",
97-
"@stacks/stacking": "6.13.1",
97+
"@stacks/stacking": "6.13.2",
9898
"@stacks/transactions": "6.11.3",
9999
"@types/express-list-endpoints": "4.0.1",
100100
"@types/lru-cache": "5.1.1",

src/tests-2.5/pox-4-btc-address-formats.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe.each([P2SH_P2WPKH, P2WPKH, P2WSH, P2TR])(
4848
let stackingClient: StackingClient;
4949
let signerPrivKey: StacksPrivateKey;
5050
let signerPubKey: string;
51-
const cycleCount = 2;
51+
const cycleCount = 1;
5252

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

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

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

190190
// Check that STX are no longer reported as locked by the RPC endpoints:
191191
await timeout(200); // make sure unlock was processed

src/tests-2.5/pox-4-burnchain-delegate-stx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ describe('PoX-4 - Stack using Bitcoin-chain delegate ops', () => {
357357
'(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)))))))',
358358
function_args: [
359359
{
360-
hex: '0x010000000000000000000ffd1e7ab22000',
361-
repr: 'u4500432000000000',
360+
hex: '0x01000000000000000000066541116e8800',
361+
repr: 'u1800180000000000',
362362
name: 'amount-ustx',
363363
type: 'uint',
364364
},

src/tests-2.5/pox-4-delegate-aggregation.ts

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import {
1616
import { stxToMicroStx } from '../helpers';
1717
import {
1818
AnchorMode,
19+
StacksPrivateKey,
1920
bufferCV,
2021
makeContractCall,
22+
makeRandomPrivKey,
2123
makeSTXTokenTransfer,
2224
noneCV,
23-
randomBytes,
2425
someCV,
2526
standardPrincipalCV,
2627
uintCV,
@@ -35,6 +36,9 @@ import {
3536
} from 'stacks-encoding-native-js';
3637
import { AddressStxBalanceResponse } from '@stacks/stacks-blockchain-api-types';
3738
import * as assert from 'assert';
39+
import { hexToBytes } from '@stacks/common';
40+
import { StackingClient } from '@stacks/stacking';
41+
import { getPublicKeyFromPrivate } from '@stacks/encryption';
3842

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

5256
let poxCycleAddressIndex: bigint;
5357

58+
let stackingClient: StackingClient;
59+
let signerPrivKey: StacksPrivateKey;
60+
let signerPubKey: string;
61+
5462
beforeAll(() => {
5563
seedAccount = accountFromKey(seedKey);
5664
// delegatorKey = ECPair.makeRandom({ compressed: true }).privateKey!.toString('hex');
5765
// delegateeKey = ECPair.makeRandom({ compressed: true }).privateKey!.toString('hex');
5866
delegatorAccount = accountFromKey(delegatorKey);
5967
delegateeAccount = accountFromKey(delegateeKey);
68+
69+
stackingClient = new StackingClient(delegatorAccount.stxAddr, testEnv.stacksNetwork);
70+
signerPrivKey = makeRandomPrivKey();
71+
signerPubKey = getPublicKeyFromPrivate(signerPrivKey.data);
6072
});
6173

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

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

193-
amountStackedInitial = amountDelegated - 2000n;
207+
amountStackedInitial = amountDelegated - 20000n;
194208

195209
const txFee = 10000n;
196210
const delegateStackStxTx = await makeContractCall({
@@ -203,7 +217,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
203217
uintCV(amountStackedInitial), // amount-ustx
204218
delegateeAccount.poxAddrClar, // pox-addr
205219
uintCV(startBurnHt), // start-burn-ht
206-
uintCV(1), // lock-period,
220+
uintCV(6), // lock-period,
207221
],
208222
network: testEnv.stacksNetwork,
209223
anchorMode: AnchorMode.OnChainOnly,
@@ -236,7 +250,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
236250
);
237251
expect(res.results[0].data).toEqual(
238252
expect.objectContaining({
239-
lock_period: '1',
253+
lock_period: '6',
240254
lock_amount: amountStackedInitial.toString(),
241255
})
242256
);
@@ -250,8 +264,20 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
250264
});
251265

252266
test('Perform stack-aggregation-commit-indexed - delegator commit to stacking operation', async () => {
267+
await standByForPoxCycle();
253268
const poxInfo2 = await testEnv.client.getPox();
254269
const rewardCycle = BigInt(poxInfo2.next_cycle.id);
270+
const signerSig = hexToBytes(
271+
stackingClient.signPoxSignature({
272+
topic: 'agg-commit',
273+
poxAddress: delegateeAccount.btcAddr,
274+
rewardCycle: Number(rewardCycle),
275+
period: 1,
276+
signerPrivateKey: signerPrivKey,
277+
maxAmount: amountStackedInitial,
278+
authId: 0,
279+
})
280+
);
255281
const stackAggrCommitTx = await makeContractCall({
256282
senderKey: delegatorAccount.secretKey,
257283
contractAddress,
@@ -260,7 +286,10 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
260286
functionArgs: [
261287
delegateeAccount.poxAddrClar, // pox-addr
262288
uintCV(rewardCycle), // reward-cycle
263-
bufferCV(randomBytes(33)), // signer-key
289+
someCV(bufferCV(signerSig)), // signer-sig
290+
bufferCV(hexToBytes(signerPubKey)), // signer-key
291+
uintCV(amountStackedInitial.toString()), // max-amount
292+
uintCV(0), // auth-id
264293
],
265294
network: testEnv.stacksNetwork,
266295
anchorMode: AnchorMode.OnChainOnly,
@@ -319,29 +348,6 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
319348
Buffer.from(delegateStackIncreaseTx.serialize())
320349
);
321350

322-
// then commit to increased amount with call to `stack-aggregation-increase`
323-
const poxInfo2 = await testEnv.client.getPox();
324-
const rewardCycle = BigInt(poxInfo2.next_cycle.id);
325-
const stackAggrIncreaseTx = await makeContractCall({
326-
senderKey: delegatorAccount.secretKey,
327-
contractAddress,
328-
contractName,
329-
functionName: 'stack-aggregation-increase',
330-
functionArgs: [
331-
delegateeAccount.poxAddrClar, // pox-addr
332-
uintCV(rewardCycle), // reward-cycle
333-
uintCV(poxCycleAddressIndex), // reward-cycle-index
334-
],
335-
network: testEnv.stacksNetwork,
336-
anchorMode: AnchorMode.OnChainOnly,
337-
fee: txFee,
338-
validateWithAbi: false,
339-
nonce: delegateStackIncreaseTx.auth.spendingCondition.nonce + 1n,
340-
});
341-
const { txId: stackAggrIncreaseTxId } = await testEnv.client.sendTransaction(
342-
Buffer.from(stackAggrIncreaseTx.serialize())
343-
);
344-
345351
const delegateStackIncreaseDbTx = await standByForTxSuccess(delegateStackIncreaseTxId);
346352
const delegateStackIncreaseResult = decodeClarityValue<
347353
ClarityValueResponseOk<
@@ -392,6 +398,45 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
392398
})
393399
);
394400

401+
// then commit to increased amount with call to `stack-aggregation-increase`
402+
const poxInfo2 = await testEnv.client.getPox();
403+
const maxAmount = amountStackedInitial + stxToDelegateIncrease;
404+
const rewardCycle = BigInt(poxInfo2.next_cycle.id);
405+
const signerSig = hexToBytes(
406+
stackingClient.signPoxSignature({
407+
topic: 'agg-increase',
408+
poxAddress: delegateeAccount.btcAddr,
409+
rewardCycle: Number(rewardCycle),
410+
period: 1,
411+
signerPrivateKey: signerPrivKey,
412+
maxAmount: maxAmount,
413+
authId: 1,
414+
})
415+
);
416+
const stackAggrIncreaseTx = await makeContractCall({
417+
senderKey: delegatorAccount.secretKey,
418+
contractAddress,
419+
contractName,
420+
functionName: 'stack-aggregation-increase',
421+
functionArgs: [
422+
delegateeAccount.poxAddrClar, // pox-addr
423+
uintCV(rewardCycle), // reward-cycle
424+
uintCV(poxCycleAddressIndex), // reward-cycle-index
425+
someCV(bufferCV(signerSig)), // signer-sig
426+
bufferCV(hexToBytes(signerPubKey)), // signer-key
427+
uintCV(maxAmount.toString()), // max-amount
428+
uintCV(1), // auth-id
429+
],
430+
network: testEnv.stacksNetwork,
431+
anchorMode: AnchorMode.OnChainOnly,
432+
fee: txFee,
433+
validateWithAbi: false,
434+
nonce: delegateStackIncreaseTx.auth.spendingCondition.nonce + 1n,
435+
});
436+
const { txId: stackAggrIncreaseTxId } = await testEnv.client.sendTransaction(
437+
Buffer.from(stackAggrIncreaseTx.serialize())
438+
);
439+
395440
// validate API endpoint balance state for account
396441
const apiBalance = await fetchGet<AddressStxBalanceResponse>(
397442
`/extended/v1/address/${delegateeAccount.stxAddr}/stx`
@@ -421,7 +466,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
421466
});
422467

423468
test('Wait for current pox cycle to complete', async () => {
424-
const poxStatus1 = await standByForPoxCycleEnd();
469+
const poxStatus1 = await standByForPoxCycle();
425470
const poxStatus2 = await standByForPoxCycle();
426471
console.log('___Wait for current pox cycle to complete___', {
427472
pox1: { height: poxStatus1.current_burnchain_block_height, ...poxStatus1.next_cycle },

src/tests-2.5/pox-4-delegate-revoked-stacking.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { hexToBytes } from '@stacks/common';
2-
import { poxAddressToTuple } from '@stacks/stacking';
1+
import { StackingClient, poxAddressToTuple } from '@stacks/stacking';
32
import { AddressStxBalanceResponse } from '@stacks/stacks-blockchain-api-types';
43
import {
54
AnchorMode,
65
Cl,
6+
StacksPrivateKey,
77
bufferCV,
88
makeContractCall,
9+
makeRandomPrivKey,
910
makeSTXTokenTransfer,
1011
noneCV,
1112
randomBytes,
@@ -29,6 +30,8 @@ import {
2930
standByForTxSuccess,
3031
testEnv,
3132
} from '../test-utils/test-helpers';
33+
import { hexToBytes } from '@stacks/common';
34+
import { getPublicKeyFromPrivate } from '@stacks/encryption';
3235

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

53+
let stackingClient: StackingClient;
54+
let signerPrivKey: StacksPrivateKey;
55+
let signerPubKey: string;
56+
5057
beforeAll(() => {
5158
seedAccount = accountFromKey(seedKey);
5259
POOL = accountFromKey(delegatorKey);
5360
STACKER = accountFromKey(delegateeKey);
61+
62+
stackingClient = new StackingClient(POOL.stxAddr, testEnv.stacksNetwork);
63+
signerPrivKey = makeRandomPrivKey();
64+
signerPubKey = getPublicKeyFromPrivate(signerPrivKey.data);
5465
});
5566

5667
test('Seed delegate accounts', async () => {
@@ -292,7 +303,7 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
292303
expect.objectContaining({
293304
name: 'revoke-delegate-stx',
294305
stacker: STACKER.stxAddr,
295-
data: { delegate_to: POOL.stxAddr },
306+
data: expect.objectContaining({ delegate_to: POOL.stxAddr }),
296307
})
297308
);
298309

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

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

@@ -413,7 +425,17 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
413425
test('Perform stack-aggregation-commit - delegator commit to stacking operation', async () => {
414426
poxInfo = await testEnv.client.getPox();
415427
const rewardCycle = BigInt(poxInfo.next_cycle.id);
416-
428+
const signerSig = hexToBytes(
429+
stackingClient.signPoxSignature({
430+
topic: 'agg-commit',
431+
poxAddress: STACKER.btcAddr,
432+
rewardCycle: Number(rewardCycle),
433+
period: 1,
434+
signerPrivateKey: signerPrivKey,
435+
maxAmount: DELEGATE_HALF_AMOUNT,
436+
authId: 0,
437+
})
438+
);
417439
const stackAggrCommitTx = await makeContractCall({
418440
senderKey: POOL.secretKey,
419441
contractAddress,
@@ -422,7 +444,10 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
422444
functionArgs: [
423445
STACKER.poxAddrClar, // pox-addr
424446
uintCV(rewardCycle), // reward-cycle
425-
bufferCV(randomBytes(33)), // signer-key
447+
someCV(bufferCV(signerSig)), // signer-sig
448+
bufferCV(hexToBytes(signerPubKey)), // signer-key
449+
uintCV(DELEGATE_HALF_AMOUNT.toString()), // max-amount
450+
uintCV(0), // auth-id
426451
],
427452
network: testEnv.stacksNetwork,
428453
anchorMode: AnchorMode.OnChainOnly,
@@ -447,7 +472,6 @@ describe('PoX-4 - Delegate Revoked Stacking', () => {
447472
});
448473

449474
test('Wait for current two pox cycles to complete', async () => {
450-
await standByForPoxCycleEnd();
451475
await standByForPoxCycle();
452476
await standByForPoxCycle();
453477
});

0 commit comments

Comments
 (0)