Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 0322ad5

Browse files
committed
fixup! refactor(experimental): add getBlockProduction API
1 parent 18f642a commit 0322ad5

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

packages/rpc-core/src/rpc-methods/__tests__/get-block-production-test.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { createJsonRpcTransport } from '@solana/rpc-transport';
2-
import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-errors';
3-
import type { Transport } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-transport-types';
1+
import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
2+
import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/json-rpc-errors';
3+
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
44
import fetchMock from 'jest-fetch-mock-fork';
55
import { createSolanaRpcApi, SolanaRpcMethods } from '../../index';
66
import { Commitment } from '../common';
77
import { Base58EncodedAddress } from '@solana/keys';
88

9-
describe('getBalance', () => {
10-
let transport: Transport<SolanaRpcMethods>;
9+
describe('getBlockProduction', () => {
10+
let rpc: Rpc<SolanaRpcMethods>;
1111
beforeEach(() => {
1212
fetchMock.resetMocks();
1313
fetchMock.dontMock();
14-
transport = createJsonRpcTransport({
14+
rpc = createJsonRpc<SolanaRpcMethods>({
1515
api: createSolanaRpcApi(),
16-
url: 'http://127.0.0.1:8899',
16+
transport: createHttpTransport({ url: 'http://127.0.0.1:8899' }),
1717
});
1818
});
1919

2020
(['confirmed', 'finalized', 'processed'] as Commitment[]).forEach(commitment => {
2121
describe(`when called with \`${commitment}\` commitment`, () => {
2222
it('returns block production data', async () => {
2323
expect.assertions(1);
24-
const blockProductionPromise = transport.getBlockProduction({ commitment }).send();
24+
const blockProductionPromise = rpc.getBlockProduction({ commitment }).send();
2525
await expect(blockProductionPromise).resolves.toMatchObject({
2626
value: expect.objectContaining({
2727
byIdentity: expect.toBeObject(),
@@ -35,10 +35,8 @@ describe('getBalance', () => {
3535

3636
it('has the latest context slot as the last slot', async () => {
3737
expect.assertions(1);
38-
const blockProductionPromise = transport.getBlockProduction({ commitment }).send();
39-
await expect(blockProductionPromise).resolves.toSatisfy(
40-
rpcResponse => rpcResponse.context.slot === rpcResponse.value.range.lastSlot
41-
);
38+
const blockProduction = await rpc.getBlockProduction({ commitment }).send();
39+
expect(blockProduction.value.range.lastSlot).toBe(blockProduction.context.slot);
4240
});
4341
});
4442
});
@@ -51,7 +49,7 @@ describe('getBalance', () => {
5149
expect.assertions(1);
5250
// Randomly generated address, assumed not to be a block producer
5351
const identity = '9NmqDDZa7mH1DBM4zeq9cm7VcRn2un1i2TwuMvjBoVhU' as Base58EncodedAddress;
54-
const blockProductionPromise = transport.getBlockProduction({ identity }).send();
52+
const blockProductionPromise = rpc.getBlockProduction({ identity }).send();
5553
await expect(blockProductionPromise).resolves.toMatchObject({
5654
value: expect.objectContaining({
5755
byIdentity: expect.toBeEmptyObject(),
@@ -63,7 +61,7 @@ describe('getBalance', () => {
6361
describe('when called with a `lastSlot` higher than the highest slot available', () => {
6462
it('throws an error', async () => {
6563
expect.assertions(1);
66-
const blockProductionPromise = transport
64+
const blockProductionPromise = rpc
6765
.getBlockProduction({
6866
range: {
6967
firstSlot: 0n,
@@ -72,7 +70,7 @@ describe('getBalance', () => {
7270
})
7371
.send();
7472
await expect(blockProductionPromise).rejects.toMatchObject({
75-
code: -32602 satisfies (typeof SolanaJsonRpcErrorCode)['JSON_RPC_SERVER_ERROR_LAST_SLOT_TOO_LARGE'],
73+
code: -32602 satisfies (typeof SolanaJsonRpcErrorCode)['JSON_RPC_INVALID_PARAMS'],
7674
message: expect.any(String),
7775
name: 'SolanaJsonRpcError',
7876
});
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
import { Base58EncodedAddress } from '@solana/keys';
22
import { Commitment, RpcResponse, U64UnsafeBeyond2Pow53Minus1 } from './common';
33

4-
type NumberOfLeaderSlots = number;
5-
type NumberOfBlocksProduced = number;
4+
type NumberOfLeaderSlots = U64UnsafeBeyond2Pow53Minus1;
5+
type NumberOfBlocksProduced = U64UnsafeBeyond2Pow53Minus1;
66

7-
type Range = Readonly<{
7+
type SlotRange = Readonly<{
88
firstSlot: U64UnsafeBeyond2Pow53Minus1;
99
lastSlot: U64UnsafeBeyond2Pow53Minus1;
1010
}>;
1111

12-
type GetBlockProductionApiResponse = RpcResponse<{
13-
byIdentity: Readonly<{
14-
[address: string]: [NumberOfLeaderSlots, NumberOfBlocksProduced];
12+
type GetBlockProductionApiResponseBase = RpcResponse<{
13+
range: SlotRange;
14+
}>;
15+
16+
type GetBlockProductionApiResponseWithAllIdentities = Readonly<{
17+
value: Readonly<{
18+
byIdentity: Record<Base58EncodedAddress, [NumberOfLeaderSlots, NumberOfBlocksProduced]>;
19+
}>;
20+
}>;
21+
22+
type GetBlockProductionApiResponseWithSingleIdentity<TIdentity extends string> = Readonly<{
23+
value: Readonly<{
24+
byIdentity: { [TAddress in TIdentity]?: [NumberOfLeaderSlots, NumberOfBlocksProduced] };
1525
}>;
16-
range: Range;
1726
}>;
1827

1928
export interface GetBlockProductionApi {
2029
/**
2130
* Returns recent block production information from the current or previous epoch.
2231
*/
32+
getBlockProduction(
33+
config: Readonly<{
34+
commitment?: Commitment;
35+
identity: Base58EncodedAddress;
36+
range?: SlotRange;
37+
}>
38+
): GetBlockProductionApiResponseBase & GetBlockProductionApiResponseWithSingleIdentity<typeof config.identity>;
39+
2340
getBlockProduction(
2441
config?: Readonly<{
2542
commitment?: Commitment;
26-
identity?: Base58EncodedAddress;
27-
range?: Range;
43+
identity: undefined;
44+
range?: SlotRange;
2845
}>
29-
): GetBlockProductionApiResponse;
46+
): GetBlockProductionApiResponseBase & GetBlockProductionApiResponseWithAllIdentities;
3047
}

packages/rpc-transport/src/json-rpc-errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Keep in sync with https://github.com/solana-labs/solana/blob/master/rpc-client-api/src/custom_error.rs
22
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
33
export const SolanaJsonRpcErrorCode = {
4+
JSON_RPC_INVALID_PARAMS: -32602,
45
JSON_RPC_SCAN_ERROR: -32012,
56
JSON_RPC_SERVER_ERROR_BLOCK_CLEANED_UP: -32001,
67
JSON_RPC_SERVER_ERROR_BLOCK_NOT_AVAILABLE: -32004,
78
JSON_RPC_SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET: -32014,
89
JSON_RPC_SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX: -32010,
9-
JSON_RPC_SERVER_ERROR_LAST_SLOT_TOO_LARGE: -32602,
1010
JSON_RPC_SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED: -32009,
1111
JSON_RPC_SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED: -32016,
1212
JSON_RPC_SERVER_ERROR_NODE_UNHEALTHY: -32005,

pnpm-lock.yaml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)