fix: clean up racy tests

This commit is contained in:
Michael Vines 2020-12-24 10:43:45 -08:00
parent 87eb924d2a
commit 5ced2f75a6
9 changed files with 192 additions and 156 deletions

View File

@ -2919,7 +2919,10 @@ export class Connection {
const signData = transaction.serializeMessage();
const wireTransaction = transaction._serialize(signData);
const encodedTransaction = wireTransaction.toString('base64');
const config: any = {encoding: 'base64'};
const config: any = {
encoding: 'base64',
commitment: this.commitment,
};
const args = [encodedTransaction, config];
if (signers) {

View File

@ -128,7 +128,6 @@ export class Loader {
[payer, program],
{
commitment: 'singleGossip',
skipPreflight: true,
},
);
}
@ -170,7 +169,6 @@ export class Loader {
transactions.push(
sendAndConfirmTransaction(connection, transaction, [payer, program], {
commitment: 'singleGossip',
skipPreflight: true,
}),
);
@ -211,7 +209,6 @@ export class Loader {
[payer, program],
{
commitment: 'singleGossip',
skipPreflight: true,
},
);
}

View File

@ -16,7 +16,7 @@ import {BPF_LOADER_PROGRAM_ID} from '../src/bpf-loader';
if (!mockRpcEnabled) {
// The default of 5 seconds is too slow for live testing sometimes
jest.setTimeout(120000);
jest.setTimeout(240000);
}
test('load BPF C program', async () => {
@ -54,7 +54,7 @@ test('load BPF C program', async () => {
});
await sendAndConfirmTransaction(connection, transaction, [from], {
commitment: 'singleGossip',
skipPreflight: true,
preflightCommitment: 'singleGossip',
});
});
@ -67,7 +67,6 @@ describe('load BPF Rust program', () => {
const connection = new Connection(url, 'singleGossip');
let program: Account;
let signature: string;
let payerAccount: Account;
let programData: Buffer;
@ -116,7 +115,9 @@ describe('load BPF Rust program', () => {
programData,
BPF_LOADER_PROGRAM_ID,
);
});
test('get confirmed transaction', async () => {
const transaction = new Transaction().add({
keys: [
{pubkey: payerAccount.publicKey, isSigner: true, isWritable: true},
@ -124,18 +125,16 @@ describe('load BPF Rust program', () => {
programId: program.publicKey,
});
signature = await sendAndConfirmTransaction(
const signature = await sendAndConfirmTransaction(
connection,
transaction,
[payerAccount],
{
commitment: 'max',
skipPreflight: true,
commitment: 'max', // `getParsedConfirmedTransaction` requires max commitment
preflightCommitment: connection.commitment || 'max',
},
);
});
test('get confirmed transaction', async () => {
const parsedTx = await connection.getParsedConfirmedTransaction(signature);
if (parsedTx === null) {
expect(parsedTx).not.toBeNull();

View File

@ -26,7 +26,7 @@ import {mockConfirmTransaction} from './mockrpc/confirm-transaction';
import {mockRpcSocket} from './__mocks__/rpc-websockets';
// Testing tokens and blockhash cache each take around 30s to complete
jest.setTimeout(40000);
jest.setTimeout(90000);
const errorMessage = 'Invalid';
const errorResponse = {
@ -91,7 +91,7 @@ test('get account info - not found', async () => {
});
test('get program accounts', async () => {
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const account0 = new Account();
const account1 = new Account();
const programId = new Account();
@ -107,6 +107,15 @@ test('get program accounts', async () => {
'2WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
mockConfirmTransaction(
'2WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
);
let signature = await connection.requestAirdrop(
account0.publicKey,
LAMPORTS_PER_SOL,
);
await connection.confirmTransaction(signature);
mockRpc.push([
url,
{
@ -119,8 +128,14 @@ test('get program accounts', async () => {
'2WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
await connection.requestAirdrop(account0.publicKey, LAMPORTS_PER_SOL);
await connection.requestAirdrop(account1.publicKey, 0.5 * LAMPORTS_PER_SOL);
mockConfirmTransaction(
'2WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
);
signature = await connection.requestAirdrop(
account1.publicKey,
0.5 * LAMPORTS_PER_SOL,
);
await connection.confirmTransaction(signature);
mockGetRecentBlockhash('max');
mockRpc.push([
@ -146,8 +161,7 @@ test('get program accounts', async () => {
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
);
await sendAndConfirmTransaction(connection, transaction, [account0], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
mockRpc.push([
@ -173,15 +187,14 @@ test('get program accounts', async () => {
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
);
await sendAndConfirmTransaction(connection, transaction, [account1], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
mockRpc.push([
url,
{
method: 'getFeeCalculatorForBlockhash',
params: [transaction.recentBlockhash, {commitment: 'recent'}],
params: [transaction.recentBlockhash, {commitment: 'singleGossip'}],
},
{
error: null,
@ -218,7 +231,7 @@ test('get program accounts', async () => {
method: 'getProgramAccounts',
params: [
programId.publicKey.toBase58(),
{commitment: 'recent', encoding: 'base64'},
{commitment: 'singleGossip', encoding: 'base64'},
],
},
{
@ -372,13 +385,13 @@ test('get inflation', async () => {
});
test('get epoch info', async () => {
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getEpochInfo',
params: [{commitment: 'recent'}],
params: [{commitment: 'singleGossip'}],
},
{
error: null,
@ -1246,7 +1259,7 @@ test('get recent blockhash', async () => {
const connection = new Connection(url);
for (const commitment of [
'max',
'recent',
'singleGossip',
'root',
'single',
'singleGossip',
@ -1264,14 +1277,14 @@ test('get recent blockhash', async () => {
test('get fee calculator', async () => {
const connection = new Connection(url);
mockGetRecentBlockhash('recent');
const {blockhash} = await connection.getRecentBlockhash('recent');
mockGetRecentBlockhash('singleGossip');
const {blockhash} = await connection.getRecentBlockhash('singleGossip');
mockRpc.push([
url,
{
method: 'getFeeCalculatorForBlockhash',
params: [blockhash, {commitment: 'recent'}],
params: [blockhash, {commitment: 'singleGossip'}],
},
{
error: null,
@ -1289,7 +1302,7 @@ test('get fee calculator', async () => {
]);
const feeCalculator = (
await connection.getFeeCalculatorForBlockhash(blockhash, 'recent')
await connection.getFeeCalculatorForBlockhash(blockhash, 'singleGossip')
).value;
if (feeCalculator === null) {
expect(feeCalculator).not.toBeNull();
@ -1462,7 +1475,7 @@ describe('token methods', () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const newAccount = new Account().publicKey;
let testToken: Token;
@ -1721,17 +1734,20 @@ test('stake activation should return activating for new accounts', async () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const voteAccounts = await connection.getVoteAccounts();
const voteAccount = voteAccounts.current.concat(voteAccounts.delinquent)[0];
const votePubkey = new PublicKey(voteAccount.votePubkey);
const authorized = new Account();
await connection.requestAirdrop(authorized.publicKey, 2 * LAMPORTS_PER_SOL);
let signature = await connection.requestAirdrop(
authorized.publicKey,
2 * LAMPORTS_PER_SOL,
);
await connection.confirmTransaction(signature);
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
StakeProgram.space,
'recent',
);
const newStakeAccount = new Account();
@ -1747,7 +1763,9 @@ test('stake activation should return activating for new accounts', async () => {
connection,
createAndInitialize,
[authorized, newStakeAccount],
{commitment: 'single', skipPreflight: true},
{
commitment: 'singleGossip',
},
);
let delegation = StakeProgram.delegate({
stakePubkey: newStakeAccount.publicKey,
@ -1755,15 +1773,14 @@ test('stake activation should return activating for new accounts', async () => {
votePubkey,
});
await sendAndConfirmTransaction(connection, delegation, [authorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
const LARGE_EPOCH = 4000;
await expect(
connection.getStakeActivation(
newStakeAccount.publicKey,
'recent',
'singleGossip',
LARGE_EPOCH,
),
).rejects.toThrow(
@ -1784,7 +1801,7 @@ test('stake activation should only accept state with valid string literals', asy
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const publicKey = new Account().publicKey;
const addStakeActivationMock = state => {
@ -1836,13 +1853,13 @@ test('getVersion', async () => {
test('request airdrop', async () => {
const account = new Account();
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [0, {commitment: 'recent'}],
params: [0, {commitment: 'singleGossip'}],
},
{
error: null,
@ -1852,7 +1869,7 @@ test('request airdrop', async () => {
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
0,
'recent',
'singleGossip',
);
mockRpc.push([
@ -1874,13 +1891,13 @@ test('request airdrop', async () => {
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'single');
await connection.confirmTransaction(signature);
mockRpc.push([
url,
{
method: 'getBalance',
params: [account.publicKey.toBase58(), {commitment: 'recent'}],
params: [account.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -1902,7 +1919,7 @@ test('request airdrop', async () => {
method: 'getAccountInfo',
params: [
account.publicKey.toBase58(),
{commitment: 'recent', encoding: 'base64'},
{commitment: 'singleGossip', encoding: 'base64'},
],
},
{
@ -1936,7 +1953,7 @@ test('request airdrop', async () => {
method: 'getAccountInfo',
params: [
account.publicKey.toBase58(),
{commitment: 'recent', encoding: 'jsonParsed'},
{commitment: 'singleGossip', encoding: 'jsonParsed'},
],
},
{
@ -1972,13 +1989,13 @@ test('request airdrop', async () => {
test('transaction failure', async () => {
const account = new Account();
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [0, {commitment: 'recent'}],
params: [0, {commitment: 'singleGossip'}],
},
{
error: null,
@ -1988,7 +2005,7 @@ test('transaction failure', async () => {
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
0,
'recent',
'singleGossip',
);
mockRpc.push([
@ -2009,13 +2026,13 @@ test('transaction failure', async () => {
);
mockConfirmTransaction(airdropSignature);
await connection.confirmTransaction(airdropSignature, 'single');
await connection.confirmTransaction(airdropSignature);
mockRpc.push([
url,
{
method: 'getBalance',
params: [account.publicKey.toBase58(), {commitment: 'recent'}],
params: [account.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -2062,7 +2079,7 @@ test('transaction failure', async () => {
connection,
transaction,
[account, newAccount],
{commitment: 'single', skipPreflight: true},
{commitment: 'singleGossip'},
);
mockRpc.push([
@ -2097,7 +2114,7 @@ test('transaction failure', async () => {
mockRpcSocket.push([
{
method: 'signatureSubscribe',
params: [signature, {commitment: 'single'}],
params: [signature, {commitment: 'singleGossip'}],
},
{
context: {
@ -2109,7 +2126,7 @@ test('transaction failure', async () => {
// Wait for one confirmation
const confirmResult = (
await connection.confirmTransaction(signature, 'single')
await connection.confirmTransaction(signature, 'singleGossip')
).value;
expect(confirmResult.err).toEqual(expectedErr);
@ -2148,13 +2165,13 @@ test('transaction failure', async () => {
test('transaction', async () => {
const accountFrom = new Account();
const accountTo = new Account();
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [0, {commitment: 'recent'}],
params: [0, {commitment: 'singleGossip'}],
},
{
error: null,
@ -2164,7 +2181,7 @@ test('transaction', async () => {
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
0,
'recent',
'singleGossip',
);
mockRpc.push([
@ -2185,13 +2202,13 @@ test('transaction', async () => {
);
mockConfirmTransaction(airdropFromSig);
await connection.confirmTransaction(airdropFromSig, 'single');
await connection.confirmTransaction(airdropFromSig);
mockRpc.push([
url,
{
method: 'getBalance',
params: [accountFrom.publicKey.toBase58(), {commitment: 'recent'}],
params: [accountFrom.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -2226,13 +2243,13 @@ test('transaction', async () => {
);
mockConfirmTransaction(airdropToSig);
await connection.confirmTransaction(airdropToSig, 'single');
await connection.confirmTransaction(airdropToSig);
mockRpc.push([
url,
{
method: 'getBalance',
params: [accountTo.publicKey.toBase58(), {commitment: 'recent'}],
params: [accountTo.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -2274,8 +2291,9 @@ test('transaction', async () => {
);
mockConfirmTransaction(signature);
let confirmResult = (await connection.confirmTransaction(signature, 'single'))
.value;
let confirmResult = (
await connection.confirmTransaction(signature, 'singleGossip')
).value;
expect(confirmResult.err).toBeNull();
mockGetRecentBlockhash('max');
@ -2309,7 +2327,7 @@ test('transaction', async () => {
expect(transaction.recentBlockhash).not.toEqual(transaction2.recentBlockhash);
mockConfirmTransaction(signature2);
await connection.confirmTransaction(signature2, 'single');
await connection.confirmTransaction(signature2, 'singleGossip');
mockRpc.push([
url,
@ -2341,7 +2359,7 @@ test('transaction', async () => {
expect(transaction2.recentBlockhash).toEqual(transaction3.recentBlockhash);
mockConfirmTransaction(signature3);
await connection.confirmTransaction(signature3, 'single');
await connection.confirmTransaction(signature3, 'singleGossip');
// Sleep until blockhash cache times out
await sleep(
@ -2377,7 +2395,7 @@ test('transaction', async () => {
},
);
mockConfirmTransaction(signature4);
await connection.confirmTransaction(signature4, 'single');
await connection.confirmTransaction(signature4, 'singleGossip');
expect(transaction4.recentBlockhash).not.toEqual(
transaction3.recentBlockhash,
@ -2387,7 +2405,7 @@ test('transaction', async () => {
url,
{
method: 'getBalance',
params: [accountFrom.publicKey.toBase58(), {commitment: 'recent'}],
params: [accountFrom.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -2409,7 +2427,7 @@ test('transaction', async () => {
url,
{
method: 'getBalance',
params: [accountTo.publicKey.toBase58(), {commitment: 'recent'}],
params: [accountTo.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -2434,27 +2452,27 @@ test('multi-instruction transaction', async () => {
const accountFrom = new Account();
const accountTo = new Account();
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
let signature = await connection.requestAirdrop(
accountFrom.publicKey,
LAMPORTS_PER_SOL,
);
await connection.confirmTransaction(signature, 'single');
await connection.confirmTransaction(signature, 'singleGossip');
expect(await connection.getBalance(accountFrom.publicKey)).toBe(
LAMPORTS_PER_SOL,
);
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
0,
'recent',
'singleGossip',
);
signature = await connection.requestAirdrop(
accountTo.publicKey,
minimumAmount + 21,
);
await connection.confirmTransaction(signature, 'single');
await connection.confirmTransaction(signature);
expect(await connection.getBalance(accountTo.publicKey)).toBe(
minimumAmount + 21,
);
@ -2482,7 +2500,7 @@ test('multi-instruction transaction', async () => {
{skipPreflight: true},
);
await connection.confirmTransaction(signature, 'single');
await connection.confirmTransaction(signature, 'singleGossip');
const response = (await connection.getSignatureStatus(signature)).value;
if (response !== null) {
@ -2509,7 +2527,7 @@ test('account change notification', async () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const owner = new Account();
const programAccount = new Account();
@ -2518,7 +2536,7 @@ test('account change notification', async () => {
const subscriptionId = connection.onAccountChange(
programAccount.publicKey,
mockCallback,
'recent',
'singleGossip',
);
const balanceNeeded = Math.max(
@ -2526,7 +2544,11 @@ test('account change notification', async () => {
1,
);
await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL);
let signature = await connection.requestAirdrop(
owner.publicKey,
LAMPORTS_PER_SOL,
);
await connection.confirmTransaction(signature);
try {
const transaction = new Transaction().add(
SystemProgram.transfer({
@ -2536,8 +2558,7 @@ test('account change notification', async () => {
}),
);
await sendAndConfirmTransaction(connection, transaction, [owner], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
} catch (err) {
await connection.removeAccountChangeListener(subscriptionId);
@ -2571,7 +2592,7 @@ test('program account change notification', async () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const owner = new Account();
const programAccount = new Account();
@ -2595,7 +2616,11 @@ test('program account change notification', async () => {
},
);
await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL);
let signature = await connection.requestAirdrop(
owner.publicKey,
LAMPORTS_PER_SOL,
);
await connection.confirmTransaction(signature);
try {
const transaction = new Transaction().add(
SystemProgram.transfer({
@ -2605,8 +2630,7 @@ test('program account change notification', async () => {
}),
);
await sendAndConfirmTransaction(connection, transaction, [owner], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
} catch (err) {
await connection.removeProgramAccountChangeListener(subscriptionId);
@ -2634,7 +2658,7 @@ test('slot notification', async () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
let notified = false;
const subscriptionId = connection.onSlotChange(slotInfo => {
@ -2666,7 +2690,7 @@ test('root notification', async () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
let roots = [];
const subscriptionId = connection.onRootChange(root => {

View File

@ -7,7 +7,7 @@ export function mockConfirmTransaction(signature: TransactionSignature) {
mockRpcSocket.push([
{
method: 'signatureSubscribe',
params: [signature, {commitment: 'single'}],
params: [signature, {commitment: 'singleGossip'}],
},
{
context: {

View File

@ -34,13 +34,13 @@ const expectedData = (authorizedPubkey: PublicKey): [string, string] => {
test('create and query nonce account', async () => {
const from = new Account();
const nonceAccount = new Account();
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [NONCE_ACCOUNT_LENGTH, {commitment: 'recent'}],
params: [NONCE_ACCOUNT_LENGTH, {commitment: 'singleGossip'}],
},
{
error: null,
@ -70,13 +70,13 @@ test('create and query nonce account', async () => {
minimumAmount * 2,
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'single');
await connection.confirmTransaction(signature, 'singleGossip');
mockRpc.push([
url,
{
method: 'getBalance',
params: [from.publicKey.toBase58(), {commitment: 'recent'}],
params: [from.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -121,7 +121,7 @@ test('create and query nonce account', async () => {
},
);
mockConfirmTransaction(nonceSignature);
await connection.confirmTransaction(nonceSignature, 'single');
await connection.confirmTransaction(nonceSignature, 'singleGossip');
mockRpc.push([
url,
@ -129,7 +129,7 @@ test('create and query nonce account', async () => {
method: 'getAccountInfo',
params: [
nonceAccount.publicKey.toBase58(),
{encoding: 'base64', commitment: 'recent'},
{encoding: 'base64', commitment: 'singleGossip'},
],
},
{
@ -165,13 +165,13 @@ test('create and query nonce account with seed', async () => {
seed,
SystemProgram.programId,
);
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [NONCE_ACCOUNT_LENGTH, {commitment: 'recent'}],
params: [NONCE_ACCOUNT_LENGTH, {commitment: 'singleGossip'}],
},
{
error: null,
@ -201,13 +201,13 @@ test('create and query nonce account with seed', async () => {
minimumAmount * 2,
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'single');
await connection.confirmTransaction(signature, 'singleGossip');
mockRpc.push([
url,
{
method: 'getBalance',
params: [from.publicKey.toBase58(), {commitment: 'recent'}],
params: [from.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -250,7 +250,7 @@ test('create and query nonce account with seed', async () => {
skipPreflight: true,
});
mockConfirmTransaction(nonceSignature);
await connection.confirmTransaction(nonceSignature, 'single');
await connection.confirmTransaction(nonceSignature, 'singleGossip');
mockRpc.push([
url,
@ -258,7 +258,7 @@ test('create and query nonce account with seed', async () => {
method: 'getAccountInfo',
params: [
noncePubkey.toBase58(),
{encoding: 'base64', commitment: 'recent'},
{encoding: 'base64', commitment: 'singleGossip'},
],
},
{

View File

@ -15,11 +15,12 @@ import {
Transaction,
} from '../src';
import {mockRpcEnabled} from './__mocks__/node-fetch';
import {newAccountWithLamports} from './new-account-with-lamports';
import {url} from './url';
if (!mockRpcEnabled) {
// Testing max commitment level takes around 20s to complete
jest.setTimeout(30000);
jest.setTimeout(60000);
}
test('createAccountWithSeed', async () => {
@ -265,19 +266,26 @@ test('live staking actions', async () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const voteAccounts = await connection.getVoteAccounts();
const voteAccount = voteAccounts.current.concat(voteAccounts.delinquent)[0];
const votePubkey = new PublicKey(voteAccount.votePubkey);
const from = new Account();
const authorized = new Account();
await connection.requestAirdrop(from.publicKey, 2 * LAMPORTS_PER_SOL);
await connection.requestAirdrop(authorized.publicKey, 2 * LAMPORTS_PER_SOL);
const from = await newAccountWithLamports(connection, 2 * LAMPORTS_PER_SOL);
const authorized = await newAccountWithLamports(
connection,
2 * LAMPORTS_PER_SOL,
);
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
StakeProgram.space,
'recent',
);
expect(await connection.getBalance(from.publicKey)).toEqual(
2 * LAMPORTS_PER_SOL,
);
expect(await connection.getBalance(authorized.publicKey)).toEqual(
2 * LAMPORTS_PER_SOL,
);
{
@ -295,7 +303,7 @@ test('live staking actions', async () => {
connection,
createAndInitialize,
[from, newStakeAccount],
{commitment: 'single', skipPreflight: true},
{commitment: 'singleGossip'},
);
expect(await connection.getBalance(newStakeAccount.publicKey)).toEqual(
minimumAmount + 42,
@ -307,8 +315,7 @@ test('live staking actions', async () => {
votePubkey,
});
await sendAndConfirmTransaction(connection, delegation, [authorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
}
@ -334,7 +341,7 @@ test('live staking actions', async () => {
connection,
createAndInitializeWithSeed,
[from],
{commitment: 'single', skipPreflight: true},
{commitment: 'singleGossip'},
);
let originalStakeBalance = await connection.getBalance(newAccountPubkey);
expect(originalStakeBalance).toEqual(3 * minimumAmount + 42);
@ -345,8 +352,7 @@ test('live staking actions', async () => {
votePubkey,
});
await sendAndConfirmTransaction(connection, delegation, [authorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
// Test that withdraw fails before deactivation
@ -359,8 +365,7 @@ test('live staking actions', async () => {
});
await expect(
sendAndConfirmTransaction(connection, withdraw, [authorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
}),
).rejects.toThrow();
@ -373,8 +378,7 @@ test('live staking actions', async () => {
lamports: minimumAmount + 20,
});
await sendAndConfirmTransaction(connection, split, [authorized, newStake], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
// Authorize to new account
@ -388,8 +392,7 @@ test('live staking actions', async () => {
stakeAuthorizationType: StakeAuthorizationLayout.Withdrawer,
});
await sendAndConfirmTransaction(connection, authorize, [authorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
authorize = StakeProgram.authorize({
stakePubkey: newAccountPubkey,
@ -398,8 +401,7 @@ test('live staking actions', async () => {
stakeAuthorizationType: StakeAuthorizationLayout.Staker,
});
await sendAndConfirmTransaction(connection, authorize, [authorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
// Test old authorized can't deactivate
@ -412,7 +414,7 @@ test('live staking actions', async () => {
connection,
deactivateNotAuthorized,
[authorized],
{commitment: 'single', skipPreflight: true},
{commitment: 'singleGossip', skipPreflight: true},
),
).rejects.toThrow();
@ -422,8 +424,7 @@ test('live staking actions', async () => {
authorizedPubkey: newAuthorized.publicKey,
});
await sendAndConfirmTransaction(connection, deactivate, [newAuthorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
// Test that withdraw succeeds after deactivation
@ -433,9 +434,9 @@ test('live staking actions', async () => {
toPubkey: recipient.publicKey,
lamports: minimumAmount + 20,
});
await sendAndConfirmTransaction(connection, withdraw, [newAuthorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
const balance = await connection.getBalance(newAccountPubkey);
expect(balance).toEqual(minimumAmount + 2);
@ -450,11 +451,10 @@ test('live staking actions', async () => {
stakeAuthorizationType: StakeAuthorizationLayout.Withdrawer,
});
await sendAndConfirmTransaction(connection, authorize, [newAuthorized], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
// Restore the previous authority using a dervied address
// Restore the previous authority using a derived address
authorize = StakeProgram.authorizeWithSeed({
stakePubkey: newAccountPubkey,
authorityBase: from.publicKey,
@ -464,7 +464,6 @@ test('live staking actions', async () => {
stakeAuthorizationType: StakeAuthorizationLayout.Withdrawer,
});
await sendAndConfirmTransaction(connection, authorize, [from], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
});
});

View File

@ -13,6 +13,7 @@ import {
} from '../src';
import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account';
import {mockRpcEnabled} from './__mocks__/node-fetch';
import {newAccountWithLamports} from './new-account-with-lamports';
import {sleep} from '../src/util/sleep';
import {url} from './url';
@ -277,19 +278,17 @@ test('live Nonce actions', async () => {
return;
}
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
const nonceAccount = new Account();
const from = new Account();
const from = await newAccountWithLamports(connection, 2 * LAMPORTS_PER_SOL);
const to = new Account();
const authority = new Account();
const newAuthority = new Account();
await connection.requestAirdrop(from.publicKey, 2 * LAMPORTS_PER_SOL);
await connection.requestAirdrop(authority.publicKey, LAMPORTS_PER_SOL);
await connection.requestAirdrop(newAuthority.publicKey, LAMPORTS_PER_SOL);
const newAuthority = await newAccountWithLamports(
connection,
LAMPORTS_PER_SOL,
);
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
NONCE_ACCOUNT_LENGTH,
'recent',
);
let createNonceAccount = new Transaction().add(
@ -304,7 +303,7 @@ test('live Nonce actions', async () => {
connection,
createNonceAccount,
[from, nonceAccount],
{commitment: 'single', skipPreflight: true},
{commitment: 'singleGossip', preflightCommitment: 'singleGossip'},
);
const nonceBalance = await connection.getBalance(nonceAccount.publicKey);
expect(nonceBalance).toEqual(minimumAmount);
@ -333,8 +332,8 @@ test('live Nonce actions', async () => {
}),
);
await sendAndConfirmTransaction(connection, advanceNonce, [from], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
preflightCommitment: 'singleGossip',
});
const nonceQuery3 = await connection.getNonce(nonceAccount.publicKey);
if (nonceQuery3 === null) {
@ -355,8 +354,8 @@ test('live Nonce actions', async () => {
}),
);
await sendAndConfirmTransaction(connection, authorizeNonce, [from], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
preflightCommitment: 'singleGossip',
});
let transfer = new Transaction().add(
@ -375,8 +374,8 @@ test('live Nonce actions', async () => {
};
await sendAndConfirmTransaction(connection, transfer, [from, newAuthority], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
preflightCommitment: 'singleGossip',
});
const toBalance = await connection.getBalance(to.publicKey);
expect(toBalance).toEqual(minimumAmount);
@ -394,8 +393,8 @@ test('live Nonce actions', async () => {
}),
);
await sendAndConfirmTransaction(connection, withdrawNonce, [newAuthority], {
commitment: 'single',
skipPreflight: true,
commitment: 'singleGossip',
preflightCommitment: 'singleGossip',
});
expect(await connection.getBalance(nonceAccount.publicKey)).toEqual(0);
const withdrawBalance = await connection.getBalance(

View File

@ -20,13 +20,13 @@ test('transaction-payer', async () => {
const accountPayer = new Account();
const accountFrom = new Account();
const accountTo = new Account();
const connection = new Connection(url, 'recent');
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [0, {commitment: 'recent'}],
params: [0, {commitment: 'singleGossip'}],
},
{
error: null,
@ -36,7 +36,7 @@ test('transaction-payer', async () => {
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
0,
'recent',
'singleGossip',
);
mockRpc.push([
@ -48,10 +48,15 @@ test('transaction-payer', async () => {
{
error: null,
result:
'0WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
'8WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
await connection.requestAirdrop(accountPayer.publicKey, LAMPORTS_PER_SOL);
let signature = await connection.requestAirdrop(
accountPayer.publicKey,
LAMPORTS_PER_SOL,
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'singleGossip');
mockRpc.push([
url,
@ -62,10 +67,15 @@ test('transaction-payer', async () => {
{
error: null,
result:
'0WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
'8WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
await connection.requestAirdrop(accountFrom.publicKey, minimumAmount + 12);
signature = await connection.requestAirdrop(
accountFrom.publicKey,
minimumAmount + 12,
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'singleGossip');
mockRpc.push([
url,
@ -79,7 +89,12 @@ test('transaction-payer', async () => {
'8WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
await connection.requestAirdrop(accountTo.publicKey, minimumAmount + 21);
signature = await connection.requestAirdrop(
accountTo.publicKey,
minimumAmount + 21,
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'singleGossip');
mockGetRecentBlockhash('max');
mockRpc.push([
@ -102,14 +117,14 @@ test('transaction-payer', async () => {
}),
);
const signature = await connection.sendTransaction(
signature = await connection.sendTransaction(
transaction,
[accountPayer, accountFrom],
{skipPreflight: true},
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'single');
await connection.confirmTransaction(signature, 'singleGossip');
mockRpc.push([
url,
@ -150,7 +165,7 @@ test('transaction-payer', async () => {
url,
{
method: 'getBalance',
params: [accountPayer.publicKey.toBase58(), {commitment: 'recent'}],
params: [accountPayer.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
@ -174,7 +189,7 @@ test('transaction-payer', async () => {
url,
{
method: 'getBalance',
params: [accountFrom.publicKey.toBase58(), {commitment: 'recent'}],
params: [accountFrom.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,