From 890e21c451a6e4cf3b23c0c6f0475089c569ed04 Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Thu, 5 Mar 2020 09:44:56 -0700 Subject: [PATCH] fix: update NonceAccount to upstream changes --- web3.js/module.d.ts | 8 +++++++- web3.js/module.flow.js | 8 +++++++- web3.js/src/fee-calculator.js | 8 ++++++++ web3.js/src/nonce-account.js | 7 +++++++ web3.js/src/system-program.js | 10 ++-------- web3.js/test/nonce.test.js | 15 +++++++++------ web3.js/test/system-program.test.js | 5 +++-- 7 files changed, 43 insertions(+), 18 deletions(-) diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 40fbe06397..a043601ed0 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -229,6 +229,13 @@ declare module '@solana/web3.js' { ): Promise; } + // === src/nonce-account.js === + export class NonceAccount { + authorizedPubkey: PublicKey; + nonce: Blockhash; + feeCalculator: FeeCalculator; + } + // === src/validator-info.js === export const VALIDATOR_INFO_KEY: PublicKey; export type Info = { @@ -536,7 +543,6 @@ declare module '@solana/web3.js' { export class SystemProgram { static programId: PublicKey; - static nonceSpace: number; static createAccount( from: PublicKey, diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 871279d7a4..c2b7a8de52 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -244,6 +244,13 @@ declare module '@solana/web3.js' { ): Promise; } + // === src/nonce-account.js === + declare export class NonceAccount { + authorizedPubkey: PublicKey; + nonce: Blockhash; + feeCalculator: FeeCalculator; + } + // === src/stake-program.js === declare export type StakeAuthorizationType = {| index: number, @@ -432,7 +439,6 @@ declare module '@solana/web3.js' { declare export class SystemProgram { static programId: PublicKey; - static nonceSpace: number; static createAccount(params: CreateAccountParams): Transaction; static transfer(params: TransferParams): Transaction; diff --git a/web3.js/src/fee-calculator.js b/web3.js/src/fee-calculator.js index d72ab1f297..c8f19491f4 100644 --- a/web3.js/src/fee-calculator.js +++ b/web3.js/src/fee-calculator.js @@ -1,4 +1,12 @@ // @flow +import * as BufferLayout from 'buffer-layout'; + +/** + * https://github.com/solana-labs/solana/blob/90bedd7e067b5b8f3ddbb45da00a4e9cabb22c62/sdk/src/fee_calculator.rs#L7-L11 + * + * @private + */ +export const FeeCalculatorLayout = BufferLayout.nu64('lamportsPerSignature'); /** * @typedef {Object} FeeCalculator diff --git a/web3.js/src/nonce-account.js b/web3.js/src/nonce-account.js index 2f03e91110..1d02cc86ad 100644 --- a/web3.js/src/nonce-account.js +++ b/web3.js/src/nonce-account.js @@ -4,6 +4,8 @@ import * as BufferLayout from 'buffer-layout'; import type {Blockhash} from './blockhash'; import * as Layout from './layout'; import {PublicKey} from './publickey'; +import type {FeeCalculator} from './fee-calculator'; +import {FeeCalculatorLayout} from './fee-calculator'; /** * See https://github.com/solana-labs/solana/blob/0ea2843ec9cdc517572b8e62c959f41b55cf4453/sdk/src/nonce_state.rs#L29-L32 @@ -11,17 +13,22 @@ import {PublicKey} from './publickey'; * @private */ const NonceAccountLayout = BufferLayout.struct([ + BufferLayout.u32('version'), BufferLayout.u32('state'), Layout.publicKey('authorizedPubkey'), Layout.publicKey('nonce'), + BufferLayout.struct([FeeCalculatorLayout], 'feeCalculator'), ]); +export const NONCE_ACCOUNT_LENGTH = NonceAccountLayout.span; + /** * NonceAccount class */ export class NonceAccount { authorizedPubkey: PublicKey; nonce: Blockhash; + feeCalculator: FeeCalculator; /** * Deserialize NonceAccount from the account data. diff --git a/web3.js/src/system-program.js b/web3.js/src/system-program.js index 582f128036..9ede5e9bd6 100644 --- a/web3.js/src/system-program.js +++ b/web3.js/src/system-program.js @@ -4,6 +4,7 @@ import * as BufferLayout from 'buffer-layout'; import {encodeData, decodeData} from './instruction'; import * as Layout from './layout'; +import {NONCE_ACCOUNT_LENGTH} from './nonce-account'; import {PublicKey} from './publickey'; import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar'; import {Transaction, TransactionInstruction} from './transaction'; @@ -437,13 +438,6 @@ export class SystemProgram { ); } - /** - * Max space of a Nonce account - */ - static get nonceSpace(): number { - return 68; - } - /** * Generate a Transaction that creates a new account */ @@ -530,7 +524,7 @@ export class SystemProgram { fromPubkey: params.fromPubkey, newAccountPubkey: params.noncePubkey, lamports: params.lamports, - space: this.nonceSpace, + space: NONCE_ACCOUNT_LENGTH, programId: this.programId, }); diff --git a/web3.js/test/nonce.test.js b/web3.js/test/nonce.test.js index 8de2a7d0db..45b4879bd3 100644 --- a/web3.js/test/nonce.test.js +++ b/web3.js/test/nonce.test.js @@ -3,6 +3,7 @@ import bs58 from 'bs58'; import {Account, Connection, SystemProgram} from '../src'; +import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account'; import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch'; import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash'; import {url} from './url'; @@ -21,7 +22,7 @@ test('create and query nonce account', async () => { url, { method: 'getMinimumBalanceForRentExemption', - params: [68, {commitment: 'recent'}], + params: [NONCE_ACCOUNT_LENGTH, {commitment: 'recent'}], }, { error: null, @@ -30,7 +31,7 @@ test('create and query nonce account', async () => { ]); const minimumAmount = await connection.getMinimumBalanceForRentExemption( - SystemProgram.nonceSpace, + NONCE_ACCOUNT_LENGTH, 'recent', ); @@ -94,11 +95,13 @@ test('create and query nonce account', async () => { }); await connection.sendTransaction(transaction, from, nonceAccount); - const expectedData = Buffer.alloc(68); - expectedData.writeInt32LE(1, 0); - from.publicKey.toBuffer().copy(expectedData, 4); + const expectedData = Buffer.alloc(NONCE_ACCOUNT_LENGTH); + expectedData.writeInt32LE(0, 0); // Version, 4 bytes + expectedData.writeInt32LE(1, 4); // State, 4 bytes + from.publicKey.toBuffer().copy(expectedData, 8); // authorizedPubkey, 32 bytes const mockNonce = new Account(); - mockNonce.publicKey.toBuffer().copy(expectedData, 36); + mockNonce.publicKey.toBuffer().copy(expectedData, 40); // Hash, 32 bytes + expectedData.writeUInt16LE(5000, 72); // feeCalculator, 8 bytes mockRpc.push([ url, diff --git a/web3.js/test/system-program.test.js b/web3.js/test/system-program.test.js index a7021ab5e1..cb90410207 100644 --- a/web3.js/test/system-program.test.js +++ b/web3.js/test/system-program.test.js @@ -11,6 +11,7 @@ import { sendAndConfirmRecentTransaction, LAMPORTS_PER_SOL, } from '../src'; +import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account'; import {mockRpcEnabled} from './__mocks__/node-fetch'; import {sleep} from '../src/util/sleep'; import {url} from './url'; @@ -95,7 +96,7 @@ test('createNonceAccount', () => { fromPubkey: params.fromPubkey, newAccountPubkey: params.noncePubkey, lamports: params.lamports, - space: SystemProgram.nonceSpace, + space: NONCE_ACCOUNT_LENGTH, programId: SystemProgram.programId, }; expect(createParams).toEqual( @@ -203,7 +204,7 @@ test('live Nonce actions', async () => { await connection.requestAirdrop(newAuthority.publicKey, LAMPORTS_PER_SOL); const minimumAmount = await connection.getMinimumBalanceForRentExemption( - SystemProgram.nonceSpace, + NONCE_ACCOUNT_LENGTH, 'recent', );