-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): sysvars package: epoch schedule
This commit introduces the `EpochSchedule` sysvar to the `@solana/sysvars` package.
- Loading branch information
1 parent
cc3a5ac
commit ce829e2
Showing
6 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { GetAccountInfoApi } from '@solana/rpc-api'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
|
||
import { fetchSysvarEpochSchedule, getSysvarEpochScheduleCodec } from '../epoch-schedule'; | ||
import { createLocalhostSolanaRpc } from './__setup__'; | ||
|
||
describe('epoch rewards', () => { | ||
let rpc: Rpc<GetAccountInfoApi>; | ||
beforeEach(() => { | ||
rpc = createLocalhostSolanaRpc(); | ||
}); | ||
it('decode', () => { | ||
// prettier-ignore | ||
const epochScheduleState = new Uint8Array([ | ||
16, 39, 0, 0, 0, 0, 0, 0, // slotsPerEpoch | ||
134, 74, 2, 0, 0, 0, 0, 0, // leaderScheduleSlotOffset | ||
1, // warmup | ||
38, 2, 0, 0, 0, 0, 0, 0, // firstNormalEpoch | ||
128, 147, 220, 20, 0, 0, 0, 0, // firstNormalSlot | ||
]); | ||
expect(getSysvarEpochScheduleCodec().decode(epochScheduleState)).toMatchObject({ | ||
firstNormalEpoch: 550n, | ||
firstNormalSlot: 350_000_000n, | ||
leaderScheduleSlotOffset: 150_150n, | ||
slotsPerEpoch: 10_000n, | ||
warmup: true, | ||
}); | ||
}); | ||
it('fetch', async () => { | ||
expect.assertions(1); | ||
const epochSchedule = await fetchSysvarEpochSchedule(rpc); | ||
expect(epochSchedule).toMatchObject({ | ||
firstNormalEpoch: expect.any(BigInt), | ||
firstNormalSlot: expect.any(BigInt), | ||
leaderScheduleSlotOffset: expect.any(BigInt), | ||
slotsPerEpoch: expect.any(BigInt), | ||
warmup: expect.any(Boolean), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { assertAccountExists, decodeAccount, type FetchAccountConfig } from '@solana/accounts'; | ||
import { | ||
combineCodec, | ||
type FixedSizeCodec, | ||
type FixedSizeDecoder, | ||
type FixedSizeEncoder, | ||
getBooleanDecoder, | ||
getBooleanEncoder, | ||
getStructDecoder, | ||
getStructEncoder, | ||
getU64Decoder, | ||
getU64Encoder, | ||
} from '@solana/codecs'; | ||
import type { GetAccountInfoApi } from '@solana/rpc-api'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
import type { Epoch, Slot } from '@solana/rpc-types'; | ||
|
||
import { fetchEncodedSysvarAccount, SYSVAR_EPOCH_SCHEDULE_ADDRESS } from './sysvar'; | ||
|
||
type SysvarEpochScheduleSize = 33; | ||
|
||
/** | ||
* The `EpochSchedule` sysvar. | ||
* | ||
* Information about epoch duration. | ||
*/ | ||
export type SysvarEpochSchedule = Readonly<{ | ||
firstNormalEpoch: Epoch; | ||
firstNormalSlot: Slot; | ||
leaderScheduleSlotOffset: bigint; | ||
slotsPerEpoch: bigint; | ||
warmup: boolean; | ||
}>; | ||
|
||
export function getSysvarEpochScheduleEncoder(): FixedSizeEncoder<SysvarEpochSchedule, SysvarEpochScheduleSize> { | ||
return getStructEncoder([ | ||
['slotsPerEpoch', getU64Encoder()], | ||
['leaderScheduleSlotOffset', getU64Encoder()], | ||
['warmup', getBooleanEncoder()], | ||
['firstNormalEpoch', getU64Encoder()], | ||
['firstNormalSlot', getU64Encoder()], | ||
]) as FixedSizeEncoder<SysvarEpochSchedule, SysvarEpochScheduleSize>; | ||
} | ||
|
||
export function getSysvarEpochScheduleDecoder(): FixedSizeDecoder<SysvarEpochSchedule, SysvarEpochScheduleSize> { | ||
return getStructDecoder([ | ||
['slotsPerEpoch', getU64Decoder()], | ||
['leaderScheduleSlotOffset', getU64Decoder()], | ||
['warmup', getBooleanDecoder()], | ||
['firstNormalEpoch', getU64Decoder()], | ||
['firstNormalSlot', getU64Decoder()], | ||
]) as FixedSizeDecoder<SysvarEpochSchedule, SysvarEpochScheduleSize>; | ||
} | ||
|
||
export function getSysvarEpochScheduleCodec(): FixedSizeCodec< | ||
SysvarEpochSchedule, | ||
SysvarEpochSchedule, | ||
SysvarEpochScheduleSize | ||
> { | ||
return combineCodec(getSysvarEpochScheduleEncoder(), getSysvarEpochScheduleDecoder()); | ||
} | ||
|
||
/** | ||
* Fetch the `EpochSchedule` sysvar. | ||
* | ||
* Information about epoch duration. | ||
*/ | ||
export async function fetchSysvarEpochSchedule( | ||
rpc: Rpc<GetAccountInfoApi>, | ||
config?: FetchAccountConfig, | ||
): Promise<SysvarEpochSchedule> { | ||
const account = await fetchEncodedSysvarAccount(rpc, SYSVAR_EPOCH_SCHEDULE_ADDRESS, config); | ||
assertAccountExists(account); | ||
const decoded = decodeAccount(account, getSysvarEpochScheduleDecoder()); | ||
return decoded.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './clock'; | ||
export * from './epoch-rewards'; | ||
export * from './epoch-schedule'; | ||
export * from './sysvar'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters