Files
solana/web3.js/src/vote-account.ts

209 lines
4.9 KiB
TypeScript
Raw Normal View History

import * as BufferLayout from '@solana/buffer-layout';
import type {Buffer} from 'buffer';
2019-07-23 18:06:55 -07:00
import * as Layout from './layout';
import {PublicKey} from './publickey';
import {toBuffer} from './util/to-buffer';
2019-07-23 18:06:55 -07:00
export const VOTE_PROGRAM_ID = new PublicKey(
2019-07-24 16:01:07 -07:00
'Vote111111111111111111111111111111111111111',
);
2021-03-15 11:01:35 +08:00
export type Lockout = {
slot: number;
confirmationCount: number;
};
2019-07-23 18:06:55 -07:00
/**
* History of how many credits earned by the end of each epoch
*/
2021-03-15 11:01:35 +08:00
export type EpochCredits = {
epoch: number;
credits: number;
prevCredits: number;
};
2019-07-23 18:06:55 -07:00
export type AuthorizedVoter = {
epoch: number;
authorizedVoter: PublicKey;
};
export type PriorVoter = {
authorizedPubkey: PublicKey;
epochOfLastAuthorizedSwitch: number;
targetEpoch: number;
};
export type BlockTimestamp = {
slot: number;
timetamp: number;
};
2019-07-23 18:06:55 -07:00
/**
* See https://github.com/solana-labs/solana/blob/8a12ed029cfa38d4a45400916c2463fb82bbec8c/programs/vote_api/src/vote_state.rs#L68-L88
*
2021-03-15 11:01:35 +08:00
* @internal
2019-07-23 18:06:55 -07:00
*/
const VoteAccountLayout = BufferLayout.struct([
2019-09-26 16:00:32 -06:00
Layout.publicKey('nodePubkey'),
Layout.publicKey('authorizedWithdrawer'),
2019-09-26 16:00:32 -06:00
BufferLayout.u8('commission'),
2019-07-23 18:06:55 -07:00
BufferLayout.nu64(), // votes.length
BufferLayout.seq(
BufferLayout.struct([
BufferLayout.nu64('slot'),
BufferLayout.u32('confirmationCount'),
]),
BufferLayout.offset(BufferLayout.u32(), -8),
'votes',
),
BufferLayout.u8('rootSlotValid'),
BufferLayout.nu64('rootSlot'),
BufferLayout.nu64(), // authorizedVoters.length
BufferLayout.seq(
BufferLayout.struct([
BufferLayout.nu64('epoch'),
Layout.publicKey('authorizedVoter'),
]),
BufferLayout.offset(BufferLayout.u32(), -8),
'authorizedVoters',
),
BufferLayout.struct(
[
BufferLayout.seq(
BufferLayout.struct([
Layout.publicKey('authorizedPubkey'),
BufferLayout.nu64('epochOfLastAuthorizedSwitch'),
BufferLayout.nu64('targetEpoch'),
]),
32,
'buf',
),
BufferLayout.nu64('idx'),
BufferLayout.u8('isEmpty'),
],
'priorVoters',
),
2019-07-23 18:06:55 -07:00
BufferLayout.nu64(), // epochCredits.length
BufferLayout.seq(
BufferLayout.struct([
BufferLayout.nu64('epoch'),
BufferLayout.nu64('credits'),
BufferLayout.nu64('prevCredits'),
]),
BufferLayout.offset(BufferLayout.u32(), -8),
'epochCredits',
),
BufferLayout.struct(
[BufferLayout.nu64('slot'), BufferLayout.nu64('timestamp')],
'lastTimestamp',
),
2019-07-23 18:06:55 -07:00
]);
2021-03-15 11:01:35 +08:00
type VoteAccountArgs = {
nodePubkey: PublicKey;
authorizedWithdrawer: PublicKey;
2021-03-15 11:01:35 +08:00
commission: number;
rootSlot: number | null;
votes: Lockout[];
authorizedVoters: AuthorizedVoter[];
priorVoters: PriorVoter[];
epochCredits: EpochCredits[];
lastTimestamp: BlockTimestamp;
2021-03-15 11:01:35 +08:00
};
2019-07-23 18:06:55 -07:00
/**
* VoteAccount class
*/
export class VoteAccount {
nodePubkey: PublicKey;
authorizedWithdrawer: PublicKey;
2019-07-23 18:06:55 -07:00
commission: number;
rootSlot: number | null;
votes: Lockout[];
authorizedVoters: AuthorizedVoter[];
priorVoters: PriorVoter[];
epochCredits: EpochCredits[];
lastTimestamp: BlockTimestamp;
2019-07-23 18:06:55 -07:00
2021-03-15 11:01:35 +08:00
/**
* @internal
*/
constructor(args: VoteAccountArgs) {
this.nodePubkey = args.nodePubkey;
this.authorizedWithdrawer = args.authorizedWithdrawer;
2021-03-15 11:01:35 +08:00
this.commission = args.commission;
this.rootSlot = args.rootSlot;
this.votes = args.votes;
this.authorizedVoters = args.authorizedVoters;
this.priorVoters = args.priorVoters;
2021-03-15 11:01:35 +08:00
this.epochCredits = args.epochCredits;
this.lastTimestamp = args.lastTimestamp;
2021-03-15 11:01:35 +08:00
}
2019-07-23 18:06:55 -07:00
/**
* Deserialize VoteAccount from the account data.
*
* @param buffer account data
* @return VoteAccount
*/
2020-02-14 22:33:11 +08:00
static fromAccountData(
buffer: Buffer | Uint8Array | Array<number>,
): VoteAccount {
const versionOffset = 4;
const va = VoteAccountLayout.decode(toBuffer(buffer), versionOffset);
2021-03-15 11:01:35 +08:00
let rootSlot: number | null = va.rootSlot;
2019-07-23 18:06:55 -07:00
if (!va.rootSlotValid) {
2021-03-15 11:01:35 +08:00
rootSlot = null;
2019-07-23 18:06:55 -07:00
}
2021-03-15 11:01:35 +08:00
return new VoteAccount({
nodePubkey: new PublicKey(va.nodePubkey),
authorizedWithdrawer: new PublicKey(va.authorizedWithdrawer),
2021-03-15 11:01:35 +08:00
commission: va.commission,
votes: va.votes,
rootSlot,
authorizedVoters: va.authorizedVoters.map(parseAuthorizedVoter),
priorVoters: getPriorVoters(va.priorVoters),
2021-03-15 11:01:35 +08:00
epochCredits: va.epochCredits,
lastTimestamp: va.lastTimestamp,
2021-03-15 11:01:35 +08:00
});
2019-07-23 18:06:55 -07:00
}
}
function parseAuthorizedVoter({epoch, authorizedVoter}: AuthorizedVoter) {
return {
epoch,
authorizedVoter: new PublicKey(authorizedVoter),
};
}
function parsePriorVoters({
authorizedPubkey,
epochOfLastAuthorizedSwitch,
targetEpoch,
}: PriorVoter) {
return {
authorizedPubkey: new PublicKey(authorizedPubkey),
epochOfLastAuthorizedSwitch,
targetEpoch,
};
}
function getPriorVoters({
buf,
idx,
isEmpty,
}: {
buf: PriorVoter[];
idx: number;
isEmpty: boolean;
}): PriorVoter[] {
if (isEmpty) {
return [];
}
return [...buf.slice(idx + 1).map(parsePriorVoters), ...buf.slice(0, idx)];
}