fix: update rpc methods to upstream changes

This commit is contained in:
Tyera Eulberg
2020-01-15 14:04:26 -07:00
committed by Michael Vines
parent bf6c2ed6f5
commit 369afecfeb
6 changed files with 98 additions and 146 deletions

View File

@ -6,7 +6,7 @@
function getTransactionFee(connection) { function getTransactionFee(connection) {
return connection.getRecentBlockhash().then(response => { return connection.getRecentBlockhash().then(response => {
return response[1]; return response.feeCalculator;
}); });
} }

View File

@ -1,6 +1,7 @@
// @flow // @flow
import assert from 'assert'; import assert from 'assert';
import bs58 from 'bs58';
import {parse as urlParse, format as urlFormat} from 'url'; import {parse as urlParse, format as urlFormat} from 'url';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import jayson from 'jayson/lib/client/browser'; import jayson from 'jayson/lib/client/browser';
@ -131,7 +132,7 @@ type VoteAccountStatus = {
*/ */
const GetInflationResult = struct({ const GetInflationResult = struct({
foundation: 'number', foundation: 'number',
foundation_term: 'number', foundationTerm: 'number',
initial: 'number', initial: 'number',
storage: 'number', storage: 'number',
taper: 'number', taper: 'number',
@ -167,11 +168,11 @@ const GetEpochInfoResult = struct({
* @property {number} first_normal_slot * @property {number} first_normal_slot
*/ */
const GetEpochScheduleResult = struct({ const GetEpochScheduleResult = struct({
slots_per_epoch: 'number', slotsPerEpoch: 'number',
leader_schedule_slot_offset: 'number', leaderScheduleSlotOffset: 'number',
warmup: 'boolean', warmup: 'boolean',
first_normal_epoch: 'number', firstNormalEpoch: 'number',
first_normal_slot: 'number', firstNormalSlot: 'number',
}); });
/** /**
@ -191,13 +192,21 @@ const Version = struct({
* @property {Blockhash} blockhash Blockhash of this block * @property {Blockhash} blockhash Blockhash of this block
* @property {Blockhash} previousBlockhash Blockhash of this block's parent * @property {Blockhash} previousBlockhash Blockhash of this block's parent
* @property {number} parentSlot Slot index of this block's parent * @property {number} parentSlot Slot index of this block's parent
* @property {Array<Array<object>>} transactions Vector of transactions paired with statuses * @property {Array<object>} transactions Vector of transactions and status metas
*/ */
type ConfirmedBlock = { type ConfirmedBlock = {
blockhash: Blockhash, blockhash: Blockhash,
previousBlockhash: Blockhash, previousBlockhash: Blockhash,
parentSlot: number, parentSlot: number,
transactions: Array<[Transaction, GetSignatureStatusRpcResult]>, transactions: Array<{
transaction: Transaction,
meta: {
fee: number,
preBalances: Array<number>,
postBalances: Array<number>,
status: SignatureStatusResult,
},
}>,
}; };
function createRpcRequest(url): RpcRequest { function createRpcRequest(url): RpcRequest {
@ -282,10 +291,10 @@ const GetVersionRpcResult = struct({
*/ */
const AccountInfoResult = struct({ const AccountInfoResult = struct({
executable: 'boolean', executable: 'boolean',
owner: 'array', owner: 'string',
lamports: 'number', lamports: 'number',
data: 'array', data: 'string',
rent_epoch: 'number?', rentEpoch: 'number?',
}); });
/** /**
@ -306,7 +315,10 @@ const AccountNotificationResult = struct({
/** /**
* @private * @private
*/ */
const ProgramAccountInfoResult = struct(['string', AccountInfoResult]); const ProgramAccountInfoResult = struct({
pubkey: 'string',
account: AccountInfoResult,
});
/*** /***
* Expected JSON RPC response for the "programNotification" message * Expected JSON RPC response for the "programNotification" message
@ -441,8 +453,8 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult(
previousBlockhash: 'string', previousBlockhash: 'string',
parentSlot: 'number', parentSlot: 'number',
transactions: struct.array([ transactions: struct.array([
struct.tuple([ struct({
struct({ transaction: struct({
signatures: struct.array(['string']), signatures: struct.array(['string']),
message: struct({ message: struct({
accountKeys: struct.array(['string']), accountKeys: struct.array(['string']),
@ -464,7 +476,7 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult(
recentBlockhash: 'string', recentBlockhash: 'string',
}), }),
}), }),
struct.union([ meta: struct.union([
'null', 'null',
struct({ struct({
status: SignatureStatusResult, status: SignatureStatusResult,
@ -473,7 +485,7 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult(
postBalances: struct.array(['number']), postBalances: struct.array(['number']),
}), }),
]), ]),
]), }),
]), ]),
}), }),
); );
@ -481,17 +493,19 @@ export const GetConfirmedBlockRpcResult = jsonRpcResult(
/** /**
* Expected JSON RPC response for the "getRecentBlockhash" message * Expected JSON RPC response for the "getRecentBlockhash" message
*/ */
const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext([ const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext(
'string',
struct({ struct({
burnPercent: 'number', blockhash: 'string',
lamportsPerSignature: 'number', feeCalculator: struct({
maxLamportsPerSignature: 'number', burnPercent: 'number',
minLamportsPerSignature: 'number', lamportsPerSignature: 'number',
targetLamportsPerSignature: 'number', maxLamportsPerSignature: 'number',
targetSignaturesPerSlot: 'number', minLamportsPerSignature: 'number',
targetLamportsPerSignature: 'number',
targetSignaturesPerSlot: 'number',
}),
}), }),
]); );
/** /**
* Expected JSON RPC response for the "requestAirdrop" message * Expected JSON RPC response for the "requestAirdrop" message
@ -595,12 +609,18 @@ export type TransactionError = {|
/** /**
* @ignore * @ignore
*/ */
type BlockhashAndFeeCalculator = [Blockhash, FeeCalculator]; // This type exists to workaround an esdoc parse error type BlockhashAndFeeCalculator = {
blockhash: Blockhash,
feeCalculator: FeeCalculator,
}; // This type exists to workaround an esdoc parse error
/** /**
* @ignore * @ignore
*/ */
type PublicKeyAndAccount = [PublicKey, AccountInfo]; // This type exists to workaround an esdoc parse error type PublicKeyAndAccount = {
pubkey: PublicKey,
account: AccountInfo,
}; // This type exists to workaround an esdoc parse error
/** /**
* A connection to a fullnode JSON RPC endpoint * A connection to a fullnode JSON RPC endpoint
@ -727,7 +747,7 @@ export class Connection {
executable, executable,
owner: new PublicKey(owner), owner: new PublicKey(owner),
lamports, lamports,
data: Buffer.from(data), data: bs58.decode(data),
}; };
return { return {
@ -770,15 +790,15 @@ export class Connection {
assert(typeof result !== 'undefined'); assert(typeof result !== 'undefined');
return result.map(result => { return result.map(result => {
return [ return {
result[0], pubkey: result.pubkey,
{ account: {
executable: result[1].executable, executable: result.account.executable,
owner: new PublicKey(result[1].owner), owner: new PublicKey(result.account.owner),
lamports: result[1].lamports, lamports: result.account.lamports,
data: Buffer.from(result[1].data), data: bs58.decode(result.account.data),
}, },
]; };
}); });
} }
@ -1039,7 +1059,10 @@ export class Connection {
).toString(), ).toString(),
parentSlot: result.result.parentSlot, parentSlot: result.result.parentSlot,
transactions: result.result.transactions.map(result => { transactions: result.result.transactions.map(result => {
return [Transaction.fromRpcResult(result[0]), result[1]]; return {
transaction: Transaction.fromRpcResult(result.transaction),
meta: result.meta,
};
}), }),
}; };
} }
@ -1066,7 +1089,7 @@ export class Connection {
} }
const value = NonceAccount.fromAccountData( const value = NonceAccount.fromAccountData(
Buffer.from(res.result.value.data), bs58.decode(res.result.value.data),
); );
return { return {
@ -1148,14 +1171,11 @@ export class Connection {
let attempts = 0; let attempts = 0;
const startTime = Date.now(); const startTime = Date.now();
for (;;) { for (;;) {
const [ const {blockhash} = await this.getRecentBlockhash();
recentBlockhash,
//feeCalculator,
] = await this.getRecentBlockhash();
if (this._blockhashInfo.recentBlockhash != recentBlockhash) { if (this._blockhashInfo.recentBlockhash != blockhash) {
this._blockhashInfo = { this._blockhashInfo = {
recentBlockhash, recentBlockhash: blockhash,
seconds: new Date().getSeconds(), seconds: new Date().getSeconds(),
transactionSignatures: [], transactionSignatures: [],
}; };
@ -1343,7 +1363,7 @@ export class Connection {
executable: result.executable, executable: result.executable,
owner: new PublicKey(result.owner), owner: new PublicKey(result.owner),
lamports: result.lamports, lamports: result.lamports,
data: Buffer.from(result.data), data: bs58.decode(result.data),
}); });
return true; return true;
} }
@ -1412,12 +1432,12 @@ export class Connection {
assert(typeof result !== 'undefined'); assert(typeof result !== 'undefined');
sub.callback({ sub.callback({
accountId: result[0], accountId: result.pubkey,
accountInfo: { accountInfo: {
executable: result[1].executable, executable: result.account.executable,
owner: new PublicKey(result[1].owner), owner: new PublicKey(result.account.owner),
lamports: result[1].lamports, lamports: result.account.lamports,
data: Buffer.from(result[1].data), data: bs58.decode(result.account.data),
}, },
}); });
return true; return true;

View File

@ -28,7 +28,7 @@ test('load BPF C program', async () => {
const data = await fs.readFile('test/fixtures/noop-c/noop.so'); const data = await fs.readFile('test/fixtures/noop-c/noop.so');
const connection = new Connection(url, 'recent'); const connection = new Connection(url, 'recent');
const [, feeCalculator] = await connection.getRecentBlockhash(); const {feeCalculator} = await connection.getRecentBlockhash();
const fees = const fees =
feeCalculator.lamportsPerSignature * feeCalculator.lamportsPerSignature *
(BpfLoader.getMinNumSignatures(data.length) + NUM_RETRIES); (BpfLoader.getMinNumSignatures(data.length) + NUM_RETRIES);
@ -56,7 +56,7 @@ test('load BPF Rust program', async () => {
); );
const connection = new Connection(url, 'recent'); const connection = new Connection(url, 'recent');
const [, feeCalculator] = await connection.getRecentBlockhash(); const {feeCalculator} = await connection.getRecentBlockhash();
const fees = const fees =
feeCalculator.lamportsPerSignature * feeCalculator.lamportsPerSignature *
(BpfLoader.getMinNumSignatures(data.length) + NUM_RETRIES); (BpfLoader.getMinNumSignatures(data.length) + NUM_RETRIES);

View File

@ -65,7 +65,7 @@ test('get program accounts', async () => {
transaction = SystemProgram.assign(account1.publicKey, programId.publicKey); transaction = SystemProgram.assign(account1.publicKey, programId.publicKey);
await sendAndConfirmTransaction(connection, transaction, account1); await sendAndConfirmTransaction(connection, transaction, account1);
const [, feeCalculator] = await connection.getRecentBlockhash(); const {feeCalculator} = await connection.getRecentBlockhash();
const programAccounts = await connection.getProgramAccounts( const programAccounts = await connection.getProgramAccounts(
programId.publicKey, programId.publicKey,
@ -76,13 +76,13 @@ test('get program accounts', async () => {
expect([ expect([
account0.publicKey.toBase58(), account0.publicKey.toBase58(),
account1.publicKey.toBase58(), account1.publicKey.toBase58(),
]).toEqual(expect.arrayContaining([element[0]])); ]).toEqual(expect.arrayContaining([element.pubkey]));
if (element[0] == account0.publicKey) { if (element.pubkey == account0.publicKey) {
expect(element[1].lamports).toBe( expect(element.account.lamports).toBe(
LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature, LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature,
); );
} else { } else {
expect(element[1].lamports).toBe( expect(element.account.lamports).toBe(
0.5 * LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature, 0.5 * LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature,
); );
} }
@ -149,7 +149,7 @@ test('get inflation', async () => {
error: null, error: null,
result: { result: {
foundation: 0.05, foundation: 0.05,
foundation_term: 7.0, foundationTerm: 7.0,
initial: 0.15, initial: 0.15,
storage: 0.1, storage: 0.1,
taper: 0.15, taper: 0.15,
@ -165,7 +165,7 @@ test('get inflation', async () => {
'terminal', 'terminal',
'taper', 'taper',
'foundation', 'foundation',
'foundation_term', 'foundationTerm',
'storage', 'storage',
]) { ]) {
expect(inflation).toHaveProperty(key); expect(inflation).toHaveProperty(key);
@ -213,10 +213,10 @@ test('get epoch schedule', async () => {
{ {
error: null, error: null,
result: { result: {
first_normal_epoch: 8, firstNormalEpoch: 8,
first_normal_slot: 8160, firstNormalSlot: 8160,
leader_schedule_slot_offset: 8192, leaderScheduleSlotOffset: 8192,
slots_per_epoch: 8192, slotsPerEpoch: 8192,
warmup: true, warmup: true,
}, },
}, },
@ -225,10 +225,10 @@ test('get epoch schedule', async () => {
const epochSchedule = await connection.getEpochSchedule(); const epochSchedule = await connection.getEpochSchedule();
for (const key of [ for (const key of [
'first_normal_epoch', 'firstNormalEpoch',
'first_normal_slot', 'firstNormalSlot',
'leader_schedule_slot_offset', 'leaderScheduleSlotOffset',
'slots_per_epoch', 'slotsPerEpoch',
]) { ]) {
expect(epochSchedule).toHaveProperty(key); expect(epochSchedule).toHaveProperty(key);
expect(epochSchedule[key]).toBeGreaterThan(0); expect(epochSchedule[key]).toBeGreaterThan(0);
@ -449,6 +449,7 @@ test('get confirmed block', async () => {
expect(block1.previousBlockhash).toBe(blockhash0); expect(block1.previousBlockhash).toBe(blockhash0);
expect(block1.blockhash).not.toBeNull(); expect(block1.blockhash).not.toBeNull();
expect(block1.parentSlot).toBe(0); expect(block1.parentSlot).toBe(0);
expect(block1.transactions[0].transaction).not.toBeNull();
break; break;
} }
x++; x++;
@ -460,11 +461,8 @@ test('get recent blockhash', async () => {
mockGetRecentBlockhash(); mockGetRecentBlockhash();
const [ const {blockhash, feeCalculator} = await connection.getRecentBlockhash();
recentBlockhash, expect(blockhash.length).toBeGreaterThanOrEqual(43);
feeCalculator,
] = await connection.getRecentBlockhash();
expect(recentBlockhash.length).toBeGreaterThanOrEqual(43);
expect(feeCalculator.lamportsPerSignature).toBeGreaterThanOrEqual(0); expect(feeCalculator.lamportsPerSignature).toBeGreaterThanOrEqual(0);
}); });
@ -572,42 +570,9 @@ test('request airdrop', async () => {
slot: 11, slot: 11,
}, },
value: { value: {
owner: [ owner: '11111111111111111111111111111111',
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,
],
lamports: minimumAmount + 42, lamports: minimumAmount + 42,
data: [], data: '',
executable: false, executable: false,
}, },
}, },

View File

@ -24,9 +24,9 @@ export function mockGetRecentBlockhash(commitment: ?Commitment) {
context: { context: {
slot: 11, slot: 11,
}, },
value: [ value: {
recentBlockhash.publicKey.toBase58(), blockhash: recentBlockhash.publicKey.toBase58(),
{ feeCalculator: {
lamportsPerSignature: 42, lamportsPerSignature: 42,
burnPercent: 50, burnPercent: 50,
maxLamportsPerSignature: 42, maxLamportsPerSignature: 42,
@ -34,7 +34,7 @@ export function mockGetRecentBlockhash(commitment: ?Commitment) {
targetLamportsPerSignature: 42, targetLamportsPerSignature: 42,
targetSignaturesPerSlot: 42, targetSignaturesPerSlot: 42,
}, },
], },
}, },
}, },
]); ]);

View File

@ -113,42 +113,9 @@ test('create and query nonce account', async () => {
slot: 11, slot: 11,
}, },
value: { value: {
owner: [ owner: '11111111111111111111111111111111',
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,
],
lamports: minimumAmount, lamports: minimumAmount,
data: [...expectedData], data: bs58.encode(expectedData),
executable: false, executable: false,
}, },
}, },