fix: update NonceAccount to upstream changes
This commit is contained in:
parent
3b55087a86
commit
890e21c451
|
@ -229,6 +229,13 @@ declare module '@solana/web3.js' {
|
||||||
): Promise<number>;
|
): Promise<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === src/nonce-account.js ===
|
||||||
|
export class NonceAccount {
|
||||||
|
authorizedPubkey: PublicKey;
|
||||||
|
nonce: Blockhash;
|
||||||
|
feeCalculator: FeeCalculator;
|
||||||
|
}
|
||||||
|
|
||||||
// === src/validator-info.js ===
|
// === src/validator-info.js ===
|
||||||
export const VALIDATOR_INFO_KEY: PublicKey;
|
export const VALIDATOR_INFO_KEY: PublicKey;
|
||||||
export type Info = {
|
export type Info = {
|
||||||
|
@ -536,7 +543,6 @@ declare module '@solana/web3.js' {
|
||||||
|
|
||||||
export class SystemProgram {
|
export class SystemProgram {
|
||||||
static programId: PublicKey;
|
static programId: PublicKey;
|
||||||
static nonceSpace: number;
|
|
||||||
|
|
||||||
static createAccount(
|
static createAccount(
|
||||||
from: PublicKey,
|
from: PublicKey,
|
||||||
|
|
|
@ -244,6 +244,13 @@ declare module '@solana/web3.js' {
|
||||||
): Promise<number>;
|
): Promise<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === src/nonce-account.js ===
|
||||||
|
declare export class NonceAccount {
|
||||||
|
authorizedPubkey: PublicKey;
|
||||||
|
nonce: Blockhash;
|
||||||
|
feeCalculator: FeeCalculator;
|
||||||
|
}
|
||||||
|
|
||||||
// === src/stake-program.js ===
|
// === src/stake-program.js ===
|
||||||
declare export type StakeAuthorizationType = {|
|
declare export type StakeAuthorizationType = {|
|
||||||
index: number,
|
index: number,
|
||||||
|
@ -432,7 +439,6 @@ declare module '@solana/web3.js' {
|
||||||
|
|
||||||
declare export class SystemProgram {
|
declare export class SystemProgram {
|
||||||
static programId: PublicKey;
|
static programId: PublicKey;
|
||||||
static nonceSpace: number;
|
|
||||||
|
|
||||||
static createAccount(params: CreateAccountParams): Transaction;
|
static createAccount(params: CreateAccountParams): Transaction;
|
||||||
static transfer(params: TransferParams): Transaction;
|
static transfer(params: TransferParams): Transaction;
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
// @flow
|
// @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
|
* @typedef {Object} FeeCalculator
|
||||||
|
|
|
@ -4,6 +4,8 @@ import * as BufferLayout from 'buffer-layout';
|
||||||
import type {Blockhash} from './blockhash';
|
import type {Blockhash} from './blockhash';
|
||||||
import * as Layout from './layout';
|
import * as Layout from './layout';
|
||||||
import {PublicKey} from './publickey';
|
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
|
* 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
|
* @private
|
||||||
*/
|
*/
|
||||||
const NonceAccountLayout = BufferLayout.struct([
|
const NonceAccountLayout = BufferLayout.struct([
|
||||||
|
BufferLayout.u32('version'),
|
||||||
BufferLayout.u32('state'),
|
BufferLayout.u32('state'),
|
||||||
Layout.publicKey('authorizedPubkey'),
|
Layout.publicKey('authorizedPubkey'),
|
||||||
Layout.publicKey('nonce'),
|
Layout.publicKey('nonce'),
|
||||||
|
BufferLayout.struct([FeeCalculatorLayout], 'feeCalculator'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
export const NONCE_ACCOUNT_LENGTH = NonceAccountLayout.span;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NonceAccount class
|
* NonceAccount class
|
||||||
*/
|
*/
|
||||||
export class NonceAccount {
|
export class NonceAccount {
|
||||||
authorizedPubkey: PublicKey;
|
authorizedPubkey: PublicKey;
|
||||||
nonce: Blockhash;
|
nonce: Blockhash;
|
||||||
|
feeCalculator: FeeCalculator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserialize NonceAccount from the account data.
|
* Deserialize NonceAccount from the account data.
|
||||||
|
|
|
@ -4,6 +4,7 @@ import * as BufferLayout from 'buffer-layout';
|
||||||
|
|
||||||
import {encodeData, decodeData} from './instruction';
|
import {encodeData, decodeData} from './instruction';
|
||||||
import * as Layout from './layout';
|
import * as Layout from './layout';
|
||||||
|
import {NONCE_ACCOUNT_LENGTH} from './nonce-account';
|
||||||
import {PublicKey} from './publickey';
|
import {PublicKey} from './publickey';
|
||||||
import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
|
import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
|
||||||
import {Transaction, TransactionInstruction} from './transaction';
|
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
|
* Generate a Transaction that creates a new account
|
||||||
*/
|
*/
|
||||||
|
@ -530,7 +524,7 @@ export class SystemProgram {
|
||||||
fromPubkey: params.fromPubkey,
|
fromPubkey: params.fromPubkey,
|
||||||
newAccountPubkey: params.noncePubkey,
|
newAccountPubkey: params.noncePubkey,
|
||||||
lamports: params.lamports,
|
lamports: params.lamports,
|
||||||
space: this.nonceSpace,
|
space: NONCE_ACCOUNT_LENGTH,
|
||||||
programId: this.programId,
|
programId: this.programId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import bs58 from 'bs58';
|
import bs58 from 'bs58';
|
||||||
|
|
||||||
import {Account, Connection, SystemProgram} from '../src';
|
import {Account, Connection, SystemProgram} from '../src';
|
||||||
|
import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account';
|
||||||
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch';
|
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch';
|
||||||
import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash';
|
import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash';
|
||||||
import {url} from './url';
|
import {url} from './url';
|
||||||
|
@ -21,7 +22,7 @@ test('create and query nonce account', async () => {
|
||||||
url,
|
url,
|
||||||
{
|
{
|
||||||
method: 'getMinimumBalanceForRentExemption',
|
method: 'getMinimumBalanceForRentExemption',
|
||||||
params: [68, {commitment: 'recent'}],
|
params: [NONCE_ACCOUNT_LENGTH, {commitment: 'recent'}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
error: null,
|
error: null,
|
||||||
|
@ -30,7 +31,7 @@ test('create and query nonce account', async () => {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
||||||
SystemProgram.nonceSpace,
|
NONCE_ACCOUNT_LENGTH,
|
||||||
'recent',
|
'recent',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -94,11 +95,13 @@ test('create and query nonce account', async () => {
|
||||||
});
|
});
|
||||||
await connection.sendTransaction(transaction, from, nonceAccount);
|
await connection.sendTransaction(transaction, from, nonceAccount);
|
||||||
|
|
||||||
const expectedData = Buffer.alloc(68);
|
const expectedData = Buffer.alloc(NONCE_ACCOUNT_LENGTH);
|
||||||
expectedData.writeInt32LE(1, 0);
|
expectedData.writeInt32LE(0, 0); // Version, 4 bytes
|
||||||
from.publicKey.toBuffer().copy(expectedData, 4);
|
expectedData.writeInt32LE(1, 4); // State, 4 bytes
|
||||||
|
from.publicKey.toBuffer().copy(expectedData, 8); // authorizedPubkey, 32 bytes
|
||||||
const mockNonce = new Account();
|
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([
|
mockRpc.push([
|
||||||
url,
|
url,
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
sendAndConfirmRecentTransaction,
|
sendAndConfirmRecentTransaction,
|
||||||
LAMPORTS_PER_SOL,
|
LAMPORTS_PER_SOL,
|
||||||
} from '../src';
|
} from '../src';
|
||||||
|
import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account';
|
||||||
import {mockRpcEnabled} from './__mocks__/node-fetch';
|
import {mockRpcEnabled} from './__mocks__/node-fetch';
|
||||||
import {sleep} from '../src/util/sleep';
|
import {sleep} from '../src/util/sleep';
|
||||||
import {url} from './url';
|
import {url} from './url';
|
||||||
|
@ -95,7 +96,7 @@ test('createNonceAccount', () => {
|
||||||
fromPubkey: params.fromPubkey,
|
fromPubkey: params.fromPubkey,
|
||||||
newAccountPubkey: params.noncePubkey,
|
newAccountPubkey: params.noncePubkey,
|
||||||
lamports: params.lamports,
|
lamports: params.lamports,
|
||||||
space: SystemProgram.nonceSpace,
|
space: NONCE_ACCOUNT_LENGTH,
|
||||||
programId: SystemProgram.programId,
|
programId: SystemProgram.programId,
|
||||||
};
|
};
|
||||||
expect(createParams).toEqual(
|
expect(createParams).toEqual(
|
||||||
|
@ -203,7 +204,7 @@ test('live Nonce actions', async () => {
|
||||||
await connection.requestAirdrop(newAuthority.publicKey, LAMPORTS_PER_SOL);
|
await connection.requestAirdrop(newAuthority.publicKey, LAMPORTS_PER_SOL);
|
||||||
|
|
||||||
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
||||||
SystemProgram.nonceSpace,
|
NONCE_ACCOUNT_LENGTH,
|
||||||
'recent',
|
'recent',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue