fix: add TypeScript buffer type to vote-account.ts

This commit is contained in:
steveluscher
2022-03-23 23:26:19 -07:00
committed by Steven Luscher
parent b2f2a68b86
commit 3333f37e88

View File

@ -17,39 +17,69 @@ export type Lockout = {
/** /**
* History of how many credits earned by the end of each epoch * History of how many credits earned by the end of each epoch
*/ */
export type EpochCredits = { export type EpochCredits = Readonly<{
epoch: number; epoch: number;
credits: number; credits: number;
prevCredits: number; prevCredits: number;
}; }>;
export type AuthorizedVoter = { export type AuthorizedVoter = Readonly<{
epoch: number; epoch: number;
authorizedVoter: PublicKey; authorizedVoter: PublicKey;
}; }>;
export type PriorVoter = { type AuthorizedVoterRaw = Readonly<{
authorizedVoter: Uint8Array;
epoch: number;
}>;
type PriorVoters = Readonly<{
buf: PriorVoterRaw[];
idx: number;
isEmpty: number;
}>;
export type PriorVoter = Readonly<{
authorizedPubkey: PublicKey; authorizedPubkey: PublicKey;
epochOfLastAuthorizedSwitch: number; epochOfLastAuthorizedSwitch: number;
targetEpoch: number; targetEpoch: number;
}; }>;
export type BlockTimestamp = { type PriorVoterRaw = Readonly<{
authorizedPubkey: Uint8Array;
epochOfLastAuthorizedSwitch: number;
targetEpoch: number;
}>;
export type BlockTimestamp = Readonly<{
slot: number; slot: number;
timestamp: number; timestamp: number;
}; }>;
type VoteAccountData = Readonly<{
authorizedVoters: AuthorizedVoterRaw[];
authorizedWithdrawer: Uint8Array;
commission: number;
epochCredits: EpochCredits[];
lastTimestamp: BlockTimestamp;
nodePubkey: Uint8Array;
priorVoters: PriorVoters;
rootSlot: number;
rootSlotValid: number;
votes: Lockout[];
}>;
/** /**
* See https://github.com/solana-labs/solana/blob/8a12ed029cfa38d4a45400916c2463fb82bbec8c/programs/vote_api/src/vote_state.rs#L68-L88 * See https://github.com/solana-labs/solana/blob/8a12ed029cfa38d4a45400916c2463fb82bbec8c/programs/vote_api/src/vote_state.rs#L68-L88
* *
* @internal * @internal
*/ */
const VoteAccountLayout = BufferLayout.struct([ const VoteAccountLayout = BufferLayout.struct<VoteAccountData>([
Layout.publicKey('nodePubkey'), Layout.publicKey('nodePubkey'),
Layout.publicKey('authorizedWithdrawer'), Layout.publicKey('authorizedWithdrawer'),
BufferLayout.u8('commission'), BufferLayout.u8('commission'),
BufferLayout.nu64(), // votes.length BufferLayout.nu64(), // votes.length
BufferLayout.seq( BufferLayout.seq<Lockout>(
BufferLayout.struct([ BufferLayout.struct([
BufferLayout.nu64('slot'), BufferLayout.nu64('slot'),
BufferLayout.u32('confirmationCount'), BufferLayout.u32('confirmationCount'),
@ -60,7 +90,7 @@ const VoteAccountLayout = BufferLayout.struct([
BufferLayout.u8('rootSlotValid'), BufferLayout.u8('rootSlotValid'),
BufferLayout.nu64('rootSlot'), BufferLayout.nu64('rootSlot'),
BufferLayout.nu64(), // authorizedVoters.length BufferLayout.nu64(), // authorizedVoters.length
BufferLayout.seq( BufferLayout.seq<AuthorizedVoterRaw>(
BufferLayout.struct([ BufferLayout.struct([
BufferLayout.nu64('epoch'), BufferLayout.nu64('epoch'),
Layout.publicKey('authorizedVoter'), Layout.publicKey('authorizedVoter'),
@ -68,7 +98,7 @@ const VoteAccountLayout = BufferLayout.struct([
BufferLayout.offset(BufferLayout.u32(), -8), BufferLayout.offset(BufferLayout.u32(), -8),
'authorizedVoters', 'authorizedVoters',
), ),
BufferLayout.struct( BufferLayout.struct<PriorVoters>(
[ [
BufferLayout.seq( BufferLayout.seq(
BufferLayout.struct([ BufferLayout.struct([
@ -85,7 +115,7 @@ const VoteAccountLayout = BufferLayout.struct([
'priorVoters', 'priorVoters',
), ),
BufferLayout.nu64(), // epochCredits.length BufferLayout.nu64(), // epochCredits.length
BufferLayout.seq( BufferLayout.seq<EpochCredits>(
BufferLayout.struct([ BufferLayout.struct([
BufferLayout.nu64('epoch'), BufferLayout.nu64('epoch'),
BufferLayout.nu64('credits'), BufferLayout.nu64('credits'),
@ -94,7 +124,7 @@ const VoteAccountLayout = BufferLayout.struct([
BufferLayout.offset(BufferLayout.u32(), -8), BufferLayout.offset(BufferLayout.u32(), -8),
'epochCredits', 'epochCredits',
), ),
BufferLayout.struct( BufferLayout.struct<BlockTimestamp>(
[BufferLayout.nu64('slot'), BufferLayout.nu64('timestamp')], [BufferLayout.nu64('slot'), BufferLayout.nu64('timestamp')],
'lastTimestamp', 'lastTimestamp',
), ),
@ -172,7 +202,10 @@ export class VoteAccount {
} }
} }
function parseAuthorizedVoter({epoch, authorizedVoter}: AuthorizedVoter) { function parseAuthorizedVoter({
authorizedVoter,
epoch,
}: AuthorizedVoterRaw): AuthorizedVoter {
return { return {
epoch, epoch,
authorizedVoter: new PublicKey(authorizedVoter), authorizedVoter: new PublicKey(authorizedVoter),
@ -183,7 +216,7 @@ function parsePriorVoters({
authorizedPubkey, authorizedPubkey,
epochOfLastAuthorizedSwitch, epochOfLastAuthorizedSwitch,
targetEpoch, targetEpoch,
}: PriorVoter) { }: PriorVoterRaw): PriorVoter {
return { return {
authorizedPubkey: new PublicKey(authorizedPubkey), authorizedPubkey: new PublicKey(authorizedPubkey),
epochOfLastAuthorizedSwitch, epochOfLastAuthorizedSwitch,
@ -191,18 +224,13 @@ function parsePriorVoters({
}; };
} }
function getPriorVoters({ function getPriorVoters({buf, idx, isEmpty}: PriorVoters): PriorVoter[] {
buf,
idx,
isEmpty,
}: {
buf: PriorVoter[];
idx: number;
isEmpty: boolean;
}): PriorVoter[] {
if (isEmpty) { if (isEmpty) {
return []; return [];
} }
return [...buf.slice(idx + 1).map(parsePriorVoters), ...buf.slice(0, idx)]; return [
...buf.slice(idx + 1).map(parsePriorVoters),
...buf.slice(0, idx).map(parsePriorVoters),
];
} }