solana/web3.js/test/connection.test.js

767 lines
17 KiB
JavaScript
Raw Normal View History

2018-08-23 10:52:48 -07:00
// @flow
import {
Account,
Connection,
BpfLoader,
Loader,
SystemProgram,
sendAndConfirmTransaction,
} from '../src';
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../src/timing';
2018-10-10 14:00:59 -07:00
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch';
2019-03-04 08:06:33 -08:00
import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash';
import {url} from './url';
import {sleep} from '../src/util/sleep';
2018-08-23 10:52:48 -07:00
2018-10-10 14:00:59 -07:00
if (!mockRpcEnabled) {
// The default of 5 seconds is too slow for live testing sometimes
jest.setTimeout(30000);
2018-10-10 14:00:59 -07:00
}
2018-08-24 11:12:48 -07:00
const errorMessage = 'Invalid request';
const errorResponse = {
error: {
message: errorMessage,
},
result: undefined,
};
2018-09-20 15:08:52 -07:00
test('get account info - error', () => {
const account = new Account();
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getAccountInfo',
2018-09-30 18:42:45 -07:00
params: [account.publicKey.toBase58()],
2018-09-20 15:08:52 -07:00
},
errorResponse,
]);
return expect(connection.getAccountInfo(account.publicKey)).rejects.toThrow(
2018-11-04 11:41:21 -08:00
errorMessage,
);
2018-09-20 15:08:52 -07:00
});
test('get program accounts', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
return;
}
const connection = new Connection(url);
const account0 = new Account();
const account1 = new Account();
const programId = new Account();
await connection.requestAirdrop(account0.publicKey, 42);
await connection.requestAirdrop(account1.publicKey, 84);
let transaction = SystemProgram.assign(
account0.publicKey,
programId.publicKey,
);
await sendAndConfirmTransaction(connection, transaction, account0);
transaction = SystemProgram.assign(account1.publicKey, programId.publicKey);
await sendAndConfirmTransaction(connection, transaction, account1);
const [, feeCalculator] = await connection.getRecentBlockhash();
const programAccounts = await connection.getProgramAccounts(
programId.publicKey,
);
expect(programAccounts.length).toBe(2);
programAccounts.forEach(function(element) {
expect([
account0.publicKey.toBase58(),
account1.publicKey.toBase58(),
]).toEqual(expect.arrayContaining([element[0]]));
if (element[0] == account0.publicKey) {
expect(element[1].lamports).toBe(42 - feeCalculator.lamportsPerSignature);
} else {
expect(element[1].lamports).toBe(84 - feeCalculator.lamportsPerSignature);
}
});
});
2019-04-24 15:22:50 -07:00
test('fullnodeExit', async () => {
if (!mockRpcEnabled) {
console.log('fullnodeExit skipped on live node');
return;
}
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'fullnodeExit',
},
{
error: null,
result: false,
},
]);
const result = await connection.fullnodeExit();
expect(result).toBe(false);
});
2018-08-24 10:39:51 -07:00
test('get balance', async () => {
2018-08-23 10:52:48 -07:00
const account = new Account();
const connection = new Connection(url);
2018-08-24 10:39:51 -07:00
mockRpc.push([
url,
{
method: 'getBalance',
2018-09-30 18:42:45 -07:00
params: [account.publicKey.toBase58()],
2018-08-24 10:39:51 -07:00
},
{
error: null,
result: 0,
2018-11-04 11:41:21 -08:00
},
2018-08-24 10:39:51 -07:00
]);
2018-08-23 10:52:48 -07:00
const balance = await connection.getBalance(account.publicKey);
expect(balance).toBeGreaterThanOrEqual(0);
});
test('get inflation', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getInflation',
params: [],
},
{
error: null,
result: {
foundation: 0.05,
foundation_term: 7.0,
initial: 0.15,
storage: 0.1,
taper: 0.15,
terminal: 0.015,
},
},
]);
const inflation = await connection.getInflation();
for (const key of [
'initial',
'terminal',
'taper',
'foundation',
'foundation_term',
'storage',
]) {
expect(inflation).toHaveProperty(key);
expect(inflation[key]).toBeGreaterThan(0);
}
});
2019-08-02 16:06:54 -07:00
test('get slot', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getSlot',
},
{
error: null,
result: 123,
},
]);
const slotLeader = await connection.getSlot();
if (mockRpcEnabled) {
expect(slotLeader).toBe(123);
} else {
// No idea what the correct slot value should be on a live cluster, so
// just check the type
expect(typeof slotLeader).toBe('number');
}
});
test('get slot leader', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getSlotLeader',
},
{
error: null,
result: '11111111111111111111111111111111',
},
]);
const slotLeader = await connection.getSlotLeader();
if (mockRpcEnabled) {
expect(slotLeader).toBe('11111111111111111111111111111111');
} else {
// No idea what the correct slotLeader value should be on a live cluster, so
// just check the type
expect(typeof slotLeader).toBe('string');
}
});
test('get cluster nodes', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getClusterNodes',
},
{
error: null,
result: [
{
pubkey: '11111111111111111111111111111111',
gossip: '127.0.0.0:1234',
tpu: '127.0.0.0:1235',
rpc: null,
},
],
},
]);
const clusterNodes = await connection.getClusterNodes();
if (mockRpcEnabled) {
expect(clusterNodes).toHaveLength(1);
expect(clusterNodes[0].pubkey).toBe('11111111111111111111111111111111');
expect(typeof clusterNodes[0].gossip).toBe('string');
expect(typeof clusterNodes[0].tpu).toBe('string');
expect(clusterNodes[0].rpc).toBeNull();
} else {
// There should be at least one node (the node that we're talking to)
expect(clusterNodes.length).toBeGreaterThan(0);
}
});
test('getVoteAccounts', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
return;
}
const connection = new Connection(url);
const voteAccounts = await connection.getVoteAccounts();
2019-09-26 15:00:54 -07:00
expect(
voteAccounts.current.concat(voteAccounts.delinquent).length,
).toBeGreaterThan(0);
});
test('confirm transaction - error', async () => {
2018-08-23 10:52:48 -07:00
const connection = new Connection(url);
2018-08-24 10:39:51 -07:00
const badTransactionSignature = 'bad transaction signature';
mockRpc.push([
url,
{
method: 'confirmTransaction',
params: [badTransactionSignature],
},
2018-08-24 11:12:48 -07:00
errorResponse,
2018-11-04 11:41:21 -08:00
]);
2018-08-24 10:39:51 -07:00
await expect(
2018-11-04 11:41:21 -08:00
connection.confirmTransaction(badTransactionSignature),
).rejects.toThrow(errorMessage);
2018-09-26 19:16:17 -07:00
mockRpc.push([
url,
{
method: 'getSignatureStatus',
params: [badTransactionSignature],
},
errorResponse,
2018-11-04 11:41:21 -08:00
]);
2018-09-26 19:16:17 -07:00
await expect(
2018-11-04 11:41:21 -08:00
connection.getSignatureStatus(badTransactionSignature),
).rejects.toThrow(errorMessage);
2018-08-23 10:52:48 -07:00
});
2018-08-24 10:39:51 -07:00
test('get transaction count', async () => {
2018-08-23 10:52:48 -07:00
const connection = new Connection(url);
2018-08-24 10:39:51 -07:00
mockRpc.push([
url,
{
method: 'getTransactionCount',
params: [],
},
{
error: null,
result: 1000000,
2018-11-04 11:41:21 -08:00
},
]);
2018-08-24 10:39:51 -07:00
2018-08-23 10:52:48 -07:00
const count = await connection.getTransactionCount();
expect(count).toBeGreaterThanOrEqual(0);
});
test('get total supply', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getTotalSupply',
params: [],
},
{
error: null,
result: 1000000,
},
]);
const count = await connection.getTotalSupply();
expect(count).toBeGreaterThanOrEqual(0);
});
test('get minimum balance for rent exemption', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [512],
},
{
error: null,
result: 1000000,
},
]);
const count = await connection.getMinimumBalanceForRentExemption(512);
expect(count).toBeGreaterThanOrEqual(0);
});
test('get recent blockhash', async () => {
2018-08-23 10:52:48 -07:00
const connection = new Connection(url);
2019-03-04 08:06:33 -08:00
mockGetRecentBlockhash();
2018-08-24 10:39:51 -07:00
2019-06-12 14:36:05 -07:00
const [
recentBlockhash,
feeCalculator,
] = await connection.getRecentBlockhash();
2019-03-04 08:06:33 -08:00
expect(recentBlockhash.length).toBeGreaterThanOrEqual(43);
2019-06-12 14:36:05 -07:00
expect(feeCalculator.lamportsPerSignature).toBeGreaterThanOrEqual(0);
2018-08-23 10:52:48 -07:00
});
2018-08-24 10:39:51 -07:00
test('request airdrop', async () => {
2018-08-23 16:39:52 -07:00
const account = new Account();
const connection = new Connection(url);
2018-08-24 10:39:51 -07:00
mockRpc.push([
url,
{
method: 'requestAirdrop',
2018-09-30 18:42:45 -07:00
params: [account.publicKey.toBase58(), 40],
2018-08-24 10:39:51 -07:00
},
{
error: null,
2018-11-04 11:41:21 -08:00
result:
'1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
2018-08-24 10:39:51 -07:00
]);
mockRpc.push([
url,
{
method: 'requestAirdrop',
2018-09-30 18:42:45 -07:00
params: [account.publicKey.toBase58(), 2],
2018-08-24 10:39:51 -07:00
},
{
error: null,
2018-11-04 11:41:21 -08:00
result:
'2WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
2018-08-23 16:39:52 -07:00
]);
2018-08-24 10:39:51 -07:00
mockRpc.push([
url,
{
method: 'getBalance',
2018-09-30 18:42:45 -07:00
params: [account.publicKey.toBase58()],
2018-08-24 10:39:51 -07:00
},
{
error: null,
result: 42,
2018-11-04 11:41:21 -08:00
},
2018-08-24 10:39:51 -07:00
]);
await connection.requestAirdrop(account.publicKey, 40);
await connection.requestAirdrop(account.publicKey, 2);
2018-08-23 16:39:52 -07:00
const balance = await connection.getBalance(account.publicKey);
expect(balance).toBe(42);
2018-09-20 15:08:52 -07:00
mockRpc.push([
url,
{
method: 'getAccountInfo',
2018-09-30 18:42:45 -07:00
params: [account.publicKey.toBase58()],
2018-09-20 15:08:52 -07:00
},
{
error: null,
result: {
owner: [
2018-11-04 11:41:21 -08:00
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2018-09-20 15:08:52 -07:00
],
2019-03-05 17:52:13 -08:00
lamports: 42,
2019-03-14 13:27:47 -07:00
data: [],
executable: false,
2018-11-04 11:41:21 -08:00
},
},
2018-09-20 15:08:52 -07:00
]);
const accountInfo = await connection.getAccountInfo(account.publicKey);
2019-03-05 17:52:13 -08:00
expect(accountInfo.lamports).toBe(42);
2019-03-14 13:27:47 -07:00
expect(accountInfo.data).toHaveLength(0);
expect(accountInfo.owner).toEqual(SystemProgram.programId);
2018-08-24 11:12:48 -07:00
});
test('transaction', async () => {
const accountFrom = new Account();
const accountTo = new Account();
const connection = new Connection(url);
2018-08-24 10:39:51 -07:00
mockRpc.push([
url,
{
method: 'requestAirdrop',
params: [accountFrom.publicKey.toBase58(), 100010],
2018-08-24 10:39:51 -07:00
},
{
error: null,
2018-11-04 11:41:21 -08:00
result:
'0WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
2018-08-24 10:39:51 -07:00
]);
mockRpc.push([
url,
{
method: 'getBalance',
2018-09-30 18:42:45 -07:00
params: [accountFrom.publicKey.toBase58()],
},
{
error: null,
result: 100010,
2018-11-04 11:41:21 -08:00
},
]);
await connection.requestAirdrop(accountFrom.publicKey, 100010);
expect(await connection.getBalance(accountFrom.publicKey)).toBe(100010);
2018-08-24 10:39:51 -07:00
mockRpc.push([
url,
{
method: 'requestAirdrop',
2018-09-30 18:42:45 -07:00
params: [accountTo.publicKey.toBase58(), 21],
},
{
error: null,
2018-11-04 11:41:21 -08:00
result:
'8WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
mockRpc.push([
url,
{
method: 'getBalance',
2018-09-30 18:42:45 -07:00
params: [accountTo.publicKey.toBase58()],
},
{
error: null,
result: 21,
2018-11-04 11:41:21 -08:00
},
]);
await connection.requestAirdrop(accountTo.publicKey, 21);
expect(await connection.getBalance(accountTo.publicKey)).toBe(21);
2018-08-24 10:39:51 -07:00
2019-03-04 08:06:33 -08:00
mockGetRecentBlockhash();
mockRpc.push([
url,
{
method: 'sendTransaction',
},
{
error: null,
2018-11-04 11:41:21 -08:00
result:
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
2018-09-14 08:27:40 -07:00
const transaction = SystemProgram.transfer(
2018-09-14 08:27:40 -07:00
accountFrom.publicKey,
accountTo.publicKey,
2018-11-04 11:41:21 -08:00
10,
2018-09-14 08:27:40 -07:00
);
const signature = await connection.sendTransaction(transaction, accountFrom);
mockRpc.push([
url,
{
method: 'confirmTransaction',
params: [
2018-11-04 11:41:21 -08:00
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
],
},
{
error: null,
result: true,
2018-11-04 11:41:21 -08:00
},
]);
let i = 0;
for (;;) {
if (await connection.confirmTransaction(signature)) {
break;
}
console.log('not confirmed', signature);
expect(mockRpcEnabled).toBe(false);
expect(++i).toBeLessThan(10);
await sleep(500);
}
2018-09-26 19:16:17 -07:00
mockRpc.push([
url,
{
method: 'getSignatureStatus',
params: [
2018-11-04 11:41:21 -08:00
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
2018-09-26 19:16:17 -07:00
],
},
{
error: null,
2019-04-10 14:40:49 -07:00
result: {Ok: null},
2018-11-04 11:41:21 -08:00
},
]);
2019-04-10 14:40:49 -07:00
await expect(connection.getSignatureStatus(signature)).resolves.toEqual({
Ok: null,
});
2018-09-26 19:16:17 -07:00
mockRpc.push([
url,
{
method: 'getBalance',
2018-09-30 18:42:45 -07:00
params: [accountFrom.publicKey.toBase58()],
},
{
error: null,
result: 2,
2018-11-04 11:41:21 -08:00
},
]);
// accountFrom may have less than 100000 due to transaction fees
const balance = await connection.getBalance(accountFrom.publicKey);
expect(balance).toBeGreaterThan(0);
expect(balance).toBeLessThanOrEqual(100000);
2018-08-23 10:52:48 -07:00
mockRpc.push([
url,
{
method: 'getBalance',
2018-09-30 18:42:45 -07:00
params: [accountTo.publicKey.toBase58()],
},
{
error: null,
result: 31,
2018-11-04 11:41:21 -08:00
},
]);
if (!mockRpcEnabled) {
// Credit-only account credits are committed at the end of every slot;
// this sleep is to ensure a full slot has elapsed
await sleep((1000 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
}
expect(await connection.getBalance(accountTo.publicKey)).toBe(31);
});
test('multi-instruction transaction', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
return;
}
const accountFrom = new Account();
const accountTo = new Account();
const connection = new Connection(url);
await connection.requestAirdrop(accountFrom.publicKey, 100000);
expect(await connection.getBalance(accountFrom.publicKey)).toBe(100000);
await connection.requestAirdrop(accountTo.publicKey, 21);
expect(await connection.getBalance(accountTo.publicKey)).toBe(21);
// 1. Move(accountFrom, accountTo)
// 2. Move(accountTo, accountFrom)
const transaction = SystemProgram.transfer(
accountFrom.publicKey,
accountTo.publicKey,
100,
2019-06-12 14:36:05 -07:00
).add(
SystemProgram.transfer(accountTo.publicKey, accountFrom.publicKey, 100),
);
2018-11-26 15:53:27 -08:00
const signature = await connection.sendTransaction(
transaction,
accountFrom,
accountTo,
);
let i = 0;
for (;;) {
if (await connection.confirmTransaction(signature)) {
break;
}
expect(mockRpcEnabled).toBe(false);
expect(++i).toBeLessThan(10);
await sleep(500);
}
2019-04-10 14:40:49 -07:00
await expect(connection.getSignatureStatus(signature)).resolves.toEqual({
Ok: null,
});
// accountFrom may have less than 100000 due to transaction fees
expect(await connection.getBalance(accountFrom.publicKey)).toBeGreaterThan(0);
expect(
await connection.getBalance(accountFrom.publicKey),
).toBeLessThanOrEqual(100000);
expect(await connection.getBalance(accountTo.publicKey)).toBe(21);
});
2018-10-26 21:37:39 -07:00
test('account change notification', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
return;
}
const connection = new Connection(url);
const owner = new Account();
const programAccount = new Account();
const mockCallback = jest.fn();
2018-11-04 11:41:21 -08:00
const subscriptionId = connection.onAccountChange(
programAccount.publicKey,
mockCallback,
);
2018-10-26 21:37:39 -07:00
await connection.requestAirdrop(owner.publicKey, 100000);
await Loader.load(connection, owner, programAccount, BpfLoader.programId, [
1,
2,
2018-10-26 21:37:39 -07:00
3,
]);
2018-10-26 21:37:39 -07:00
// Wait for mockCallback to receive a call
let i = 0;
for (;;) {
if (mockCallback.mock.calls.length > 0) {
break;
}
if (++i === 30) {
throw new Error('Account change notification not observed');
}
// Sleep for a 1/4 of a slot, notifications only occur after a block is
// processed
await sleep((250 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
}
2018-10-26 21:37:39 -07:00
2018-11-16 19:29:14 -08:00
await connection.removeAccountChangeListener(subscriptionId);
expect(mockCallback.mock.calls[0][0].lamports).toBe(1);
expect(mockCallback.mock.calls[0][0].owner).toEqual(BpfLoader.programId);
2018-10-26 21:37:39 -07:00
});
2019-03-08 16:02:39 -08:00
test('program account change notification', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
return;
}
const connection = new Connection(url);
const owner = new Account();
const programAccount = new Account();
// const mockCallback = jest.fn();
2019-03-08 16:02:39 -08:00
let notified = false;
2019-03-08 16:02:39 -08:00
const subscriptionId = connection.onProgramAccountChange(
BpfLoader.programId,
keyedAccountInfo => {
if (keyedAccountInfo.accountId !== programAccount.publicKey.toString()) {
//console.log('Ignoring another account', keyedAccountInfo);
return;
}
expect(keyedAccountInfo.accountInfo.lamports).toBe(1);
expect(keyedAccountInfo.accountInfo.owner).toEqual(BpfLoader.programId);
notified = true;
},
2019-03-08 16:02:39 -08:00
);
await connection.requestAirdrop(owner.publicKey, 100000);
await Loader.load(connection, owner, programAccount, BpfLoader.programId, [
1,
2,
2019-03-08 16:02:39 -08:00
3,
]);
2019-03-08 16:02:39 -08:00
// Wait for mockCallback to receive a call
let i = 0;
while (!notified) {
//for (;;) {
if (++i === 30) {
throw new Error('Program change notification not observed');
2019-03-08 16:02:39 -08:00
}
// Sleep for a 1/4 of a slot, notifications only occur after a block is
// processed
await sleep((250 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
}
await connection.removeProgramAccountChangeListener(subscriptionId);
});