Skip to content

Commit 2fb1fbc

Browse files
authored
Repair the SysvarEpochRewards fetcher/decoder/encoder (#374)
1 parent 7e049d6 commit 2fb1fbc

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

Diff for: .changeset/better-masks-tease.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@solana/sysvars': patch
3+
---
4+
5+
The `SysvarEpochRewards` encoder/decoder no longer produces malformed data

Diff for: packages/sysvars/src/__tests__/epoch-rewards-test.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,29 @@ describe('epoch rewards', () => {
44
it('decode', () => {
55
// prettier-ignore
66
const epochRewardsState = new Uint8Array([
7-
0, 45, 49, 1, 0, 0, 0, 0, // distributionCompleteBlockHeight
8-
134, 74, 2, 0, 0, 0, 0, 0, // distributedRewards
9-
0, 132, 215, 23, 0, 0, 0, 0, // totalRewards
7+
// distributionStartingBlockHeight
8+
0xab, 0xa8, 0x87, 0x12, 0x00, 0x00, 0x00, 0x00,
9+
// numPartitions
10+
0x3a, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11+
// parentBlockhash
12+
0x67, 0x8b, 0xd4, 0xe4, 0xc8, 0x5c, 0x10, 0x87, 0xa8, 0x0a, 0xfb, 0x2f, 0x0d, 0xbb, 0x13, 0x27, 0x16, 0x11, 0x3a, 0xc7, 0xc7, 0xb0, 0xc7, 0xe4, 0x99, 0x51, 0x4d, 0x42, 0xdb, 0x43, 0xd7, 0x1c,
13+
// totalPoints
14+
0x10, 0xbe, 0x90, 0x99, 0x7a, 0x16, 0x9e, 0xa5, 0xc2, 0x2d, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
15+
// totalRewards
16+
0x00, 0xb3, 0x04, 0x4e, 0xd0, 0x20, 0x89, 0x00,
17+
// distributedRewards
18+
0x00, 0xb8, 0xea, 0x37, 0xd0, 0x20, 0x89, 0x00,
19+
// active
20+
0x00,
1021
]);
11-
expect(getSysvarEpochRewardsCodec().decode(epochRewardsState)).toMatchObject({
12-
distributedRewards: 150_150n,
13-
distributionCompleteBlockHeight: 20_000_000n,
14-
totalRewards: 400_000_000n,
22+
expect(getSysvarEpochRewardsCodec().decode(epochRewardsState)).toStrictEqual({
23+
active: false,
24+
distributedRewards: 38598150472775680n,
25+
distributionStartingBlockHeight: 310880427n,
26+
numPartitions: 314n,
27+
parentBlockhash: '7yCfKTaamnrmkAfefSgsonQ6rtwCfVaxQJircWb9K4Qj',
28+
totalPoints: 2633948733309470433656336n,
29+
totalRewards: 38598150843577088n,
1530
});
1631
});
1732
// TODO: This account does not seem to exist on-chain yet.

Diff for: packages/sysvars/src/epoch-rewards.ts

+34-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@ import {
44
type FixedSizeCodec,
55
type FixedSizeDecoder,
66
type FixedSizeEncoder,
7+
getBooleanDecoder,
8+
getBooleanEncoder,
79
getStructDecoder,
810
getStructEncoder,
911
getU64Decoder,
1012
getU64Encoder,
13+
getU128Decoder,
14+
getU128Encoder,
1115
} from '@solana/codecs';
1216
import type { GetAccountInfoApi } from '@solana/rpc-api';
1317
import type { Rpc } from '@solana/rpc-spec';
18+
import {
19+
Blockhash,
20+
getBlockhashDecoder,
21+
getBlockhashEncoder,
22+
getDefaultLamportsDecoder,
23+
getDefaultLamportsEncoder,
24+
Lamports,
25+
} from '@solana/rpc-types';
1426

1527
import { fetchEncodedSysvarAccount, SYSVAR_EPOCH_REWARDS_ADDRESS } from './sysvar';
1628

17-
type SysvarEpochRewardsSize = 24;
29+
type SysvarEpochRewardsSize = 81;
1830

1931
/**
2032
* The `EpochRewards` sysvar.
@@ -30,24 +42,36 @@ type SysvarEpochRewardsSize = 24;
3042
* See https://github.com/anza-xyz/agave/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/docs/src/runtime/sysvars.md?plain=1#L155-L168
3143
*/
3244
export type SysvarEpochRewards = Readonly<{
33-
distributedRewards: bigint;
34-
distributionCompleteBlockHeight: bigint;
35-
totalRewards: bigint;
45+
active: boolean;
46+
distributedRewards: Lamports;
47+
distributionStartingBlockHeight: bigint;
48+
numPartitions: bigint;
49+
parentBlockhash: Blockhash;
50+
totalPoints: bigint;
51+
totalRewards: Lamports;
3652
}>;
3753

3854
export function getSysvarEpochRewardsEncoder(): FixedSizeEncoder<SysvarEpochRewards, SysvarEpochRewardsSize> {
3955
return getStructEncoder([
40-
['distributionCompleteBlockHeight', getU64Encoder()],
41-
['distributedRewards', getU64Encoder()],
42-
['totalRewards', getU64Encoder()],
56+
['distributionStartingBlockHeight', getU64Encoder()],
57+
['numPartitions', getU64Encoder()],
58+
['parentBlockhash', getBlockhashEncoder()],
59+
['totalPoints', getU128Encoder()],
60+
['totalRewards', getDefaultLamportsEncoder()],
61+
['distributedRewards', getDefaultLamportsEncoder()],
62+
['active', getBooleanEncoder()],
4363
]) as FixedSizeEncoder<SysvarEpochRewards, SysvarEpochRewardsSize>;
4464
}
4565

4666
export function getSysvarEpochRewardsDecoder(): FixedSizeDecoder<SysvarEpochRewards, SysvarEpochRewardsSize> {
4767
return getStructDecoder([
48-
['distributionCompleteBlockHeight', getU64Decoder()],
49-
['distributedRewards', getU64Decoder()],
50-
['totalRewards', getU64Decoder()],
68+
['distributionStartingBlockHeight', getU64Decoder()],
69+
['numPartitions', getU64Decoder()],
70+
['parentBlockhash', getBlockhashDecoder()],
71+
['totalPoints', getU128Decoder()],
72+
['totalRewards', getDefaultLamportsDecoder()],
73+
['distributedRewards', getDefaultLamportsDecoder()],
74+
['active', getBooleanDecoder()],
5175
]) as FixedSizeDecoder<SysvarEpochRewards, SysvarEpochRewardsSize>;
5276
}
5377

0 commit comments

Comments
 (0)