Skip to content

Commit e143797

Browse files
authored
Repair the StakeHistory sysvar fetcher (#379)
1 parent 2fb1fbc commit e143797

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

.changeset/public-terms-prove.md

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

packages/sysvars/src/__tests__/stake-history-test.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,33 @@ describe('stake history', () => {
1212
it('decode', () => {
1313
// prettier-ignore
1414
const stakeHistoryState = new Uint8Array([
15-
2, 0, 0, 0, // array length
15+
2, 0, 0, 0, 0, 0, 0, 0, // array length
16+
1, 0, 0, 0, 0, 0, 0, 0, // epoch
1617
0, 208, 237, 144, 46, 0, 0, 0, // effective
1718
0, 160, 219, 33, 93, 0, 0, 0, // activating
1819
0, 112, 201, 178, 139, 0, 0, 0, // deactivating
20+
2, 0, 0, 0, 0, 0, 0, 0, // epoch
1921
0, 160, 219, 33, 93, 0, 0, 0, // effective
2022
0, 112, 201, 178, 139, 0, 0, 0, // activating
2123
0, 64, 183, 67, 186, 0, 0, 0, // deactivating
2224
]);
2325
expect(getSysvarStakeHistoryCodec().decode(stakeHistoryState)).toMatchObject(
2426
expect.arrayContaining([
2527
{
26-
activating: 400_000_000_000n,
27-
deactivating: 600_000_000_000n,
28-
effective: 200_000_000_000n,
28+
epoch: 1n,
29+
stakeHistory: {
30+
activating: 400_000_000_000n,
31+
deactivating: 600_000_000_000n,
32+
effective: 200_000_000_000n,
33+
},
2934
},
3035
{
31-
activating: 600_000_000_000n,
32-
deactivating: 800_000_000_000n,
33-
effective: 400_000_000_000n,
36+
epoch: 2n,
37+
stakeHistory: {
38+
activating: 600_000_000_000n,
39+
deactivating: 800_000_000_000n,
40+
effective: 400_000_000_000n,
41+
},
3442
},
3543
]),
3644
);

packages/sysvars/src/stake-history.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@ import {
55
getArrayEncoder,
66
getStructDecoder,
77
getStructEncoder,
8+
getU64Decoder,
9+
getU64Encoder,
810
type VariableSizeCodec,
911
type VariableSizeDecoder,
1012
type VariableSizeEncoder,
1113
} from '@solana/codecs';
1214
import type { GetAccountInfoApi } from '@solana/rpc-api';
1315
import type { Rpc } from '@solana/rpc-spec';
14-
import { getDefaultLamportsDecoder, getDefaultLamportsEncoder, type Lamports } from '@solana/rpc-types';
16+
import { Epoch, getDefaultLamportsDecoder, getDefaultLamportsEncoder, type Lamports } from '@solana/rpc-types';
1517

1618
import { fetchEncodedSysvarAccount, SYSVAR_STAKE_HISTORY_ADDRESS } from './sysvar';
1719

1820
type Entry = Readonly<{
19-
activating: Lamports;
20-
deactivating: Lamports;
21-
effective: Lamports;
21+
epoch: Epoch;
22+
stakeHistory: Readonly<{
23+
activating: Lamports;
24+
deactivating: Lamports;
25+
effective: Lamports;
26+
}>;
2227
}>;
2328

2429
/**
@@ -31,20 +36,34 @@ export type SysvarStakeHistory = Entry[];
3136
export function getSysvarStakeHistoryEncoder(): VariableSizeEncoder<SysvarStakeHistory> {
3237
return getArrayEncoder(
3338
getStructEncoder([
34-
['effective', getDefaultLamportsEncoder()],
35-
['activating', getDefaultLamportsEncoder()],
36-
['deactivating', getDefaultLamportsEncoder()],
39+
['epoch', getU64Encoder()],
40+
[
41+
'stakeHistory',
42+
getStructEncoder([
43+
['effective', getDefaultLamportsEncoder()],
44+
['activating', getDefaultLamportsEncoder()],
45+
['deactivating', getDefaultLamportsEncoder()],
46+
]),
47+
],
3748
]),
49+
{ size: getU64Encoder() },
3850
);
3951
}
4052

4153
export function getSysvarStakeHistoryDecoder(): VariableSizeDecoder<SysvarStakeHistory> {
4254
return getArrayDecoder(
4355
getStructDecoder([
44-
['effective', getDefaultLamportsDecoder()],
45-
['activating', getDefaultLamportsDecoder()],
46-
['deactivating', getDefaultLamportsDecoder()],
56+
['epoch', getU64Decoder()],
57+
[
58+
'stakeHistory',
59+
getStructDecoder([
60+
['effective', getDefaultLamportsDecoder()],
61+
['activating', getDefaultLamportsDecoder()],
62+
['deactivating', getDefaultLamportsDecoder()],
63+
]),
64+
],
4765
]),
66+
{ size: getU64Decoder() },
4867
);
4968
}
5069

0 commit comments

Comments
 (0)