-
Notifications
You must be signed in to change notification settings - Fork 791
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: last restart slot
This commit introduces the `LastRestartSlot` sysvar to the `@solana/sysvars` package.
- Loading branch information
1 parent
c835722
commit d607ab4
Showing
6 changed files
with
126 additions
and
1 deletion.
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,25 @@ | ||
import type { GetAccountInfoApi } from '@solana/rpc-api'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
|
||
import { fetchSysvarLastRestartSlot, getSysvarLastRestartSlotCodec } from '../last-restart-slot'; | ||
import { createLocalhostSolanaRpc } from './__setup__'; | ||
|
||
describe('last restart slot', () => { | ||
let rpc: Rpc<GetAccountInfoApi>; | ||
beforeEach(() => { | ||
rpc = createLocalhostSolanaRpc(); | ||
}); | ||
it('decode', () => { | ||
const lastRestartSlotState = new Uint8Array([119, 233, 246, 16, 0, 0, 0, 0]); | ||
expect(getSysvarLastRestartSlotCodec().decode(lastRestartSlotState)).toMatchObject({ | ||
lastRestartSlot: 284_617_079n, | ||
}); | ||
}); | ||
it('fetch', async () => { | ||
expect.assertions(1); | ||
const lastRestartSlot = await fetchSysvarLastRestartSlot(rpc); | ||
expect(lastRestartSlot).toMatchObject({ | ||
lastRestartSlot: expect.any(BigInt), | ||
}); | ||
}); | ||
}); |
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
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,72 @@ | ||
import { assertAccountExists, decodeAccount, type FetchAccountConfig } from '@solana/accounts'; | ||
import { | ||
combineCodec, | ||
type FixedSizeCodec, | ||
type FixedSizeDecoder, | ||
type FixedSizeEncoder, | ||
getStructDecoder, | ||
getStructEncoder, | ||
getU64Decoder, | ||
getU64Encoder, | ||
} from '@solana/codecs'; | ||
import type { GetAccountInfoApi } from '@solana/rpc-api'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
import type { Slot } from '@solana/rpc-types'; | ||
|
||
import { fetchEncodedSysvarAccount, SYSVAR_LAST_RESTART_SLOT_ADDRESS } from './sysvar'; | ||
|
||
type SysvarLastRestartSlotSize = 8; | ||
|
||
/** | ||
* The `LastRestartSlot` sysvar. | ||
* | ||
* Information about the last restart slot (hard fork). | ||
* | ||
* The `LastRestartSlot` sysvar provides access to the last restart slot kept in the | ||
* bank fork for the slot on the fork that executes the current transaction. | ||
* In case there was no fork it returns `0`. | ||
*/ | ||
export type SysvarLastRestartSlot = Readonly<{ | ||
lastRestartSlot: Slot; | ||
}>; | ||
|
||
export function getSysvarLastRestartSlotEncoder(): FixedSizeEncoder<SysvarLastRestartSlot, SysvarLastRestartSlotSize> { | ||
return getStructEncoder([['lastRestartSlot', getU64Encoder()]]) as FixedSizeEncoder< | ||
SysvarLastRestartSlot, | ||
SysvarLastRestartSlotSize | ||
>; | ||
} | ||
|
||
export function getSysvarLastRestartSlotDecoder(): FixedSizeDecoder<SysvarLastRestartSlot, SysvarLastRestartSlotSize> { | ||
return getStructDecoder([['lastRestartSlot', getU64Decoder()]]) as FixedSizeDecoder< | ||
SysvarLastRestartSlot, | ||
SysvarLastRestartSlotSize | ||
>; | ||
} | ||
|
||
export function getSysvarLastRestartSlotCodec(): FixedSizeCodec< | ||
SysvarLastRestartSlot, | ||
SysvarLastRestartSlot, | ||
SysvarLastRestartSlotSize | ||
> { | ||
return combineCodec(getSysvarLastRestartSlotEncoder(), getSysvarLastRestartSlotDecoder()); | ||
} | ||
|
||
/** | ||
* Fetch the `LastRestartSlot` sysvar. | ||
* | ||
* Information about the last restart slot (hard fork). | ||
* | ||
* The `LastRestartSlot` sysvar provides access to the last restart slot kept in the | ||
* bank fork for the slot on the fork that executes the current transaction. | ||
* In case there was no fork it returns `0`. | ||
*/ | ||
export async function fetchSysvarLastRestartSlot( | ||
rpc: Rpc<GetAccountInfoApi>, | ||
config?: FetchAccountConfig, | ||
): Promise<SysvarLastRestartSlot> { | ||
const account = await fetchEncodedSysvarAccount(rpc, SYSVAR_LAST_RESTART_SLOT_ADDRESS, config); | ||
assertAccountExists(account); | ||
const decoded = decodeAccount(account, getSysvarLastRestartSlotDecoder()); | ||
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