From 2395e57f45125c4418d6fa3406b4d4934b83c410 Mon Sep 17 00:00:00 2001 From: Jack May Date: Tue, 25 Aug 2020 09:05:33 -0700 Subject: [PATCH] fix: use bpf-loader-deprecated explicitly (#11818) --- web3.js/module.d.ts | 7 +++++-- web3.js/module.flow.js | 9 ++++++--- web3.js/src/bpf-loader-deprecated.js | 7 +++++++ web3.js/src/bpf-loader.js | 27 +++++++-------------------- web3.js/src/connection.js | 12 ++---------- web3.js/test/bpf-loader.test.js | 11 +++++++++-- 6 files changed, 36 insertions(+), 37 deletions(-) create mode 100644 web3.js/src/bpf-loader-deprecated.js diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 765709e66d..0ff13aa4c6 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -938,18 +938,21 @@ declare module '@solana/web3.js' { } // === src/bpf-loader.js === + export const BPF_LOADER_PROGRAM_ID: PublicKey; export class BpfLoader { - static programId(version?: number): PublicKey; static getMinNumSignatures(dataLength: number): number; static load( connection: Connection, payer: Account, program: Account, elfBytes: Buffer | Uint8Array | Array, - version?: number, + loaderProgramId: PublicKey, ): Promise; } + // === src/bpf-loader-deprecated.js === + export const BPF_LOADER_DEPRECATED_PROGRAM_ID: PublicKey; + // === src/util/send-and-confirm-transaction.js === export function sendAndConfirmTransaction( connection: Connection, diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index cc2f6a7b66..3b72de2973 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -708,7 +708,7 @@ declare module '@solana/web3.js' { stakePubkey: PublicKey, authorityBase: PublicKey, authoritySeed: string, - authorityOwner: PublicKey; + authorityOwner: PublicKey, newAuthorizedPubkey: PublicKey, stakeAuthorizationType: StakeAuthorizationType, |}; @@ -953,18 +953,21 @@ declare module '@solana/web3.js' { } // === src/bpf-loader.js === + declare export var BPF_LOADER_PROGRAM_ID; declare export class BpfLoader { - static programId(version: ?number): PublicKey; static getMinNumSignatures(dataLength: number): number; static load( connection: Connection, payer: Account, program: Account, elfBytes: Buffer | Uint8Array | Array, - version: ?number, + loaderProgramId: PublicKey, ): Promise; } + // === src/bpf-loader-deprecated.js === + declare export var BPF_LOADER_DEPRECATED_PROGRAM_ID; + // === src/util/send-and-confirm-transaction.js === declare export function sendAndConfirmTransaction( connection: Connection, diff --git a/web3.js/src/bpf-loader-deprecated.js b/web3.js/src/bpf-loader-deprecated.js new file mode 100644 index 0000000000..907552de82 --- /dev/null +++ b/web3.js/src/bpf-loader-deprecated.js @@ -0,0 +1,7 @@ +// @flow + +import {PublicKey} from './publickey'; + +export const BPF_LOADER_DEPRECATED_PROGRAM_ID = new PublicKey( + 'BPFLoader1111111111111111111111111111111111', +); diff --git a/web3.js/src/bpf-loader.js b/web3.js/src/bpf-loader.js index d4e72d525b..2cefc1b2ac 100644 --- a/web3.js/src/bpf-loader.js +++ b/web3.js/src/bpf-loader.js @@ -5,21 +5,14 @@ import {PublicKey} from './publickey'; import {Loader} from './loader'; import type {Connection} from './connection'; +export const BPF_LOADER_PROGRAM_ID = new PublicKey( + 'BPFLoader2111111111111111111111111111111111', +); + /** * Factory class for transactions to interact with a program loader */ export class BpfLoader { - /** - * Public key that identifies the BpfLoader - */ - static programId(version: number = 2): PublicKey { - if (version === 1) { - return new PublicKey('BPFLoader1111111111111111111111111111111111'); - } else { - return new PublicKey('BPFLoader2111111111111111111111111111111111'); - } - } - /** * Minimum number of signatures required to load a program not including * retries @@ -37,21 +30,15 @@ export class BpfLoader { * @param payer Account that will pay program loading fees * @param program Account to load the program into * @param elf The entire ELF containing the BPF program - * @param version The version of the BPF loader to use + * @param loaderProgramId The program id of the BPF loader to use */ static load( connection: Connection, payer: Account, program: Account, elf: Buffer | Uint8Array | Array, - version: number = 2, + loaderProgramId: PublicKey, ): Promise { - return Loader.load( - connection, - payer, - program, - BpfLoader.programId(version), - elf, - ); + return Loader.load(connection, payer, program, loaderProgramId, elf); } } diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 2cb01fb0dc..16b758ac45 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -1754,11 +1754,7 @@ export class Connection { publicKey: PublicKey, commitment: ?Commitment, ): Promise | null>> { - const args = this._buildArgs( - [publicKey.toBase58()], - commitment, - 'base64', - ); + const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64'); const unsafeRes = await this._rpcRequest('getAccountInfo', args); const res = GetAccountInfoAndContextRpcResult(unsafeRes); if (res.error) { @@ -1868,11 +1864,7 @@ export class Connection { programId: PublicKey, commitment: ?Commitment, ): Promise}>> { - const args = this._buildArgs( - [programId.toBase58()], - commitment, - 'base64', - ); + const args = this._buildArgs([programId.toBase58()], commitment, 'base64'); const unsafeRes = await this._rpcRequest('getProgramAccounts', args); const res = GetProgramAccountsRpcResult(unsafeRes); if (res.error) { diff --git a/web3.js/test/bpf-loader.test.js b/web3.js/test/bpf-loader.test.js index 54e023ab7d..9473f5a090 100644 --- a/web3.js/test/bpf-loader.test.js +++ b/web3.js/test/bpf-loader.test.js @@ -13,6 +13,7 @@ import { import {mockRpcEnabled} from './__mocks__/node-fetch'; import {url} from './url'; import {newAccountWithLamports} from './new-account-with-lamports'; +import {BPF_LOADER_PROGRAM_ID} from '../src/bpf-loader'; if (!mockRpcEnabled) { // The default of 5 seconds is too slow for live testing sometimes @@ -40,7 +41,7 @@ test('load BPF C program', async () => { const from = await newAccountWithLamports(connection, fees + balanceNeeded); const program = new Account(); - await BpfLoader.load(connection, from, program, data); + await BpfLoader.load(connection, from, program, data, BPF_LOADER_PROGRAM_ID); const transaction = new Transaction().add({ keys: [{pubkey: from.publicKey, isSigner: true, isWritable: true}], programId: program.publicKey, @@ -82,7 +83,13 @@ describe('load BPF Rust program', () => { ); program = new Account(); - await BpfLoader.load(connection, payerAccount, program, data); + await BpfLoader.load( + connection, + payerAccount, + program, + data, + BPF_LOADER_PROGRAM_ID, + ); const transaction = new Transaction().add({ keys: [ {pubkey: payerAccount.publicKey, isSigner: true, isWritable: true},