chore: fix lagging stake program live test (#25107)

* chore: fix lagging stake program live test

* chore: use min stake delegation in stake tests
This commit is contained in:
Justin Starry 2022-05-11 00:37:15 +08:00 committed by GitHub
parent e3bdc38f0a
commit 634b7c3b5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 34 deletions

View File

@ -3103,6 +3103,11 @@ describe('Connection', function () {
if (process.env.TEST_LIVE) { if (process.env.TEST_LIVE) {
it('stake activation should return activating for new accounts', async () => { it('stake activation should return activating for new accounts', async () => {
// todo: use `Connection.getMinimumStakeDelegation` when implemented
const MIN_STAKE_DELEGATION = LAMPORTS_PER_SOL;
const STAKE_ACCOUNT_MIN_BALANCE =
await connection.getMinimumBalanceForRentExemption(StakeProgram.space);
const voteAccounts = await connection.getVoteAccounts(); const voteAccounts = await connection.getVoteAccounts();
const voteAccount = voteAccounts.current.concat( const voteAccount = voteAccounts.current.concat(
voteAccounts.delinquent, voteAccounts.delinquent,
@ -3116,17 +3121,13 @@ describe('Connection', function () {
); );
await connection.confirmTransaction(signature, 'confirmed'); await connection.confirmTransaction(signature, 'confirmed');
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
StakeProgram.space,
);
const newStakeAccount = Keypair.generate(); const newStakeAccount = Keypair.generate();
let createAndInitialize = StakeProgram.createAccount({ let createAndInitialize = StakeProgram.createAccount({
fromPubkey: authorized.publicKey, fromPubkey: authorized.publicKey,
stakePubkey: newStakeAccount.publicKey, stakePubkey: newStakeAccount.publicKey,
authorized: new Authorized(authorized.publicKey, authorized.publicKey), authorized: new Authorized(authorized.publicKey, authorized.publicKey),
lockup: new Lockup(0, 0, new PublicKey(0)), lockup: new Lockup(0, 0, new PublicKey(0)),
lamports: minimumAmount + 42, lamports: STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
}); });
await sendAndConfirmTransaction( await sendAndConfirmTransaction(
@ -3164,7 +3165,7 @@ describe('Connection', function () {
'confirmed', 'confirmed',
); );
expect(activationState.state).to.eq('activating'); expect(activationState.state).to.eq('activating');
expect(activationState.inactive).to.eq(42); expect(activationState.inactive).to.eq(MIN_STAKE_DELEGATION);
expect(activationState.active).to.eq(0); expect(activationState.active).to.eq(0);
}); });
} }

View File

@ -380,6 +380,13 @@ describe('StakeProgram', () => {
if (process.env.TEST_LIVE) { if (process.env.TEST_LIVE) {
it('live staking actions', async () => { it('live staking actions', async () => {
const connection = new Connection(url, 'confirmed'); const connection = new Connection(url, 'confirmed');
const SYSTEM_ACCOUNT_MIN_BALANCE =
await connection.getMinimumBalanceForRentExemption(0);
const STAKE_ACCOUNT_MIN_BALANCE =
await connection.getMinimumBalanceForRentExemption(StakeProgram.space);
// todo: use `Connection.getMinimumStakeDelegation` when implemented
const MIN_STAKE_DELEGATION = LAMPORTS_PER_SOL;
const voteAccounts = await connection.getVoteAccounts(); const voteAccounts = await connection.getVoteAccounts();
const voteAccount = voteAccounts.current.concat( const voteAccount = voteAccounts.current.concat(
@ -391,7 +398,7 @@ describe('StakeProgram', () => {
await helpers.airdrop({ await helpers.airdrop({
connection, connection,
address: payer.publicKey, address: payer.publicKey,
amount: 2 * LAMPORTS_PER_SOL, amount: 10 * LAMPORTS_PER_SOL,
}); });
const authorized = Keypair.generate(); const authorized = Keypair.generate();
@ -401,16 +408,12 @@ describe('StakeProgram', () => {
amount: 2 * LAMPORTS_PER_SOL, amount: 2 * LAMPORTS_PER_SOL,
}); });
const minimumAmount = await connection.getMinimumBalanceForRentExemption( const recipient = Keypair.generate();
StakeProgram.space, await helpers.airdrop({
); connection,
address: recipient.publicKey,
expect(await connection.getBalance(payer.publicKey)).to.eq( amount: SYSTEM_ACCOUNT_MIN_BALANCE,
2 * LAMPORTS_PER_SOL, });
);
expect(await connection.getBalance(authorized.publicKey)).to.eq(
2 * LAMPORTS_PER_SOL,
);
{ {
// Create Stake account without seed // Create Stake account without seed
@ -422,7 +425,7 @@ describe('StakeProgram', () => {
authorized.publicKey, authorized.publicKey,
authorized.publicKey, authorized.publicKey,
), ),
lamports: minimumAmount + 42, lamports: STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
}); });
await sendAndConfirmTransaction( await sendAndConfirmTransaction(
@ -432,7 +435,7 @@ describe('StakeProgram', () => {
{preflightCommitment: 'confirmed'}, {preflightCommitment: 'confirmed'},
); );
expect(await connection.getBalance(newStakeAccount.publicKey)).to.eq( expect(await connection.getBalance(newStakeAccount.publicKey)).to.eq(
minimumAmount + 42, STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
); );
const delegation = StakeProgram.delegate({ const delegation = StakeProgram.delegate({
@ -453,6 +456,8 @@ describe('StakeProgram', () => {
StakeProgram.programId, StakeProgram.programId,
); );
const WITHDRAW_AMOUNT = 1;
const INITIAL_STAKE_DELEGATION = 5 * LAMPORTS_PER_SOL;
let createAndInitializeWithSeed = StakeProgram.createAccountWithSeed({ let createAndInitializeWithSeed = StakeProgram.createAccountWithSeed({
fromPubkey: payer.publicKey, fromPubkey: payer.publicKey,
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
@ -460,7 +465,7 @@ describe('StakeProgram', () => {
seed, seed,
authorized: new Authorized(authorized.publicKey, authorized.publicKey), authorized: new Authorized(authorized.publicKey, authorized.publicKey),
lockup: new Lockup(0, 0, new PublicKey(0)), lockup: new Lockup(0, 0, new PublicKey(0)),
lamports: 4 * minimumAmount + 62, lamports: STAKE_ACCOUNT_MIN_BALANCE + INITIAL_STAKE_DELEGATION,
}); });
await sendAndConfirmTransaction( await sendAndConfirmTransaction(
@ -470,7 +475,9 @@ describe('StakeProgram', () => {
{preflightCommitment: 'confirmed'}, {preflightCommitment: 'confirmed'},
); );
let originalStakeBalance = await connection.getBalance(newAccountPubkey); let originalStakeBalance = await connection.getBalance(newAccountPubkey);
expect(originalStakeBalance).to.eq(4 * minimumAmount + 62); expect(originalStakeBalance).to.eq(
STAKE_ACCOUNT_MIN_BALANCE + INITIAL_STAKE_DELEGATION,
);
let delegation = StakeProgram.delegate({ let delegation = StakeProgram.delegate({
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
@ -482,12 +489,11 @@ describe('StakeProgram', () => {
}); });
// Test that withdraw fails before deactivation // Test that withdraw fails before deactivation
const recipient = Keypair.generate();
let withdraw = StakeProgram.withdraw({ let withdraw = StakeProgram.withdraw({
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
authorizedPubkey: authorized.publicKey, authorizedPubkey: authorized.publicKey,
toPubkey: recipient.publicKey, toPubkey: recipient.publicKey,
lamports: 1000, lamports: WITHDRAW_AMOUNT,
}); });
await expect( await expect(
sendAndConfirmTransaction(connection, withdraw, [authorized], { sendAndConfirmTransaction(connection, withdraw, [authorized], {
@ -516,14 +522,16 @@ describe('StakeProgram', () => {
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
authorizedPubkey: authorized.publicKey, authorizedPubkey: authorized.publicKey,
toPubkey: recipient.publicKey, toPubkey: recipient.publicKey,
lamports: minimumAmount + 20, lamports: WITHDRAW_AMOUNT,
}); });
await sendAndConfirmTransaction(connection, withdraw, [authorized], { await sendAndConfirmTransaction(connection, withdraw, [authorized], {
preflightCommitment: 'confirmed', preflightCommitment: 'confirmed',
}); });
const recipientBalance = await connection.getBalance(recipient.publicKey); const recipientBalance = await connection.getBalance(recipient.publicKey);
expect(recipientBalance).to.eq(minimumAmount + 20); expect(recipientBalance).to.eq(
SYSTEM_ACCOUNT_MIN_BALANCE + WITHDRAW_AMOUNT,
);
// Split stake // Split stake
const newStake = Keypair.generate(); const newStake = Keypair.generate();
@ -531,7 +539,7 @@ describe('StakeProgram', () => {
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
authorizedPubkey: authorized.publicKey, authorizedPubkey: authorized.publicKey,
splitStakePubkey: newStake.publicKey, splitStakePubkey: newStake.publicKey,
lamports: minimumAmount + 20, lamports: STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
}); });
await sendAndConfirmTransaction( await sendAndConfirmTransaction(
connection, connection,
@ -541,8 +549,8 @@ describe('StakeProgram', () => {
preflightCommitment: 'confirmed', preflightCommitment: 'confirmed',
}, },
); );
const balance = await connection.getBalance(newAccountPubkey); const balance = await connection.getBalance(newStake.publicKey);
expect(balance).to.eq(2 * minimumAmount + 22); expect(balance).to.eq(STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION);
// Split stake with seed // Split stake with seed
const seed2 = 'test string 2'; const seed2 = 'test string 2';
@ -554,7 +562,7 @@ describe('StakeProgram', () => {
let splitWithSeed = StakeProgram.splitWithSeed({ let splitWithSeed = StakeProgram.splitWithSeed({
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
authorizedPubkey: authorized.publicKey, authorizedPubkey: authorized.publicKey,
lamports: minimumAmount + 20, lamports: STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
splitStakePubkey: newStake2, splitStakePubkey: newStake2,
basePubkey: payer.publicKey, basePubkey: payer.publicKey,
seed: seed2, seed: seed2,
@ -567,11 +575,12 @@ describe('StakeProgram', () => {
preflightCommitment: 'confirmed', preflightCommitment: 'confirmed',
}, },
); );
expect(await connection.getBalance(newAccountPubkey)).to.eq( expect(await connection.getBalance(newStake2)).to.eq(
minimumAmount + 2, STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
); );
// Merge stake // Merge stake
const preMergeBalance = await connection.getBalance(newAccountPubkey);
let merge = StakeProgram.merge({ let merge = StakeProgram.merge({
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
sourceStakePubKey: newStake.publicKey, sourceStakePubKey: newStake.publicKey,
@ -580,15 +589,19 @@ describe('StakeProgram', () => {
await sendAndConfirmTransaction(connection, merge, [authorized], { await sendAndConfirmTransaction(connection, merge, [authorized], {
preflightCommitment: 'confirmed', preflightCommitment: 'confirmed',
}); });
const mergedBalance = await connection.getBalance(newAccountPubkey); const postMergeBalance = await connection.getBalance(newAccountPubkey);
expect(mergedBalance).to.eq(2 * minimumAmount + 22); expect(postMergeBalance - preMergeBalance).to.eq(
STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
);
// Resplit // Resplit
split = StakeProgram.split({ split = StakeProgram.split({
stakePubkey: newAccountPubkey, stakePubkey: newAccountPubkey,
authorizedPubkey: authorized.publicKey, authorizedPubkey: authorized.publicKey,
splitStakePubkey: newStake.publicKey, splitStakePubkey: newStake.publicKey,
lamports: minimumAmount + 20, // use a different amount than the first split so that this
// transaction is different and won't require a fresh blockhash
lamports: STAKE_ACCOUNT_MIN_BALANCE + MIN_STAKE_DELEGATION,
}); });
await sendAndConfirmTransaction( await sendAndConfirmTransaction(
connection, connection,