Skip to content

Commit

Permalink
fixup! refactor(experimental): add getBlockProduction API
Browse files Browse the repository at this point in the history
  • Loading branch information
mcintyre94 committed Apr 25, 2023
1 parent ca19b51 commit 5ccf373
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createSolanaRpcApi, SolanaRpcMethods } from '../../index';
import { Commitment } from '../common';
import { Base58EncodedAddress } from '@solana/keys';

describe('getBalance', () => {
describe('getBlockProduction', () => {
let rpc: Rpc<SolanaRpcMethods>;
beforeEach(() => {
fetchMock.resetMocks();
Expand Down Expand Up @@ -35,10 +35,8 @@ describe('getBalance', () => {

it('has the latest context slot as the last slot', async () => {
expect.assertions(1);
const blockProductionPromise = rpc.getBlockProduction({ commitment }).send();
await expect(blockProductionPromise).resolves.toSatisfy(
rpcResponse => rpcResponse.context.slot === rpcResponse.value.range.lastSlot
);
const blockProduction = await rpc.getBlockProduction({ commitment }).send();
expect(blockProduction.value.range.lastSlot).toBe(blockProduction.context.slot);
});
});
});
Expand Down Expand Up @@ -72,7 +70,7 @@ describe('getBalance', () => {
})
.send();
await expect(blockProductionPromise).rejects.toMatchObject({
code: -32602 satisfies (typeof SolanaJsonRpcErrorCode)['JSON_RPC_SERVER_ERROR_LAST_SLOT_TOO_LARGE'],
code: -32602 satisfies (typeof SolanaJsonRpcErrorCode)['JSON_RPC_INVALID_PARAMS'],
message: expect.any(String),
name: 'SolanaJsonRpcError',
});
Expand Down
42 changes: 29 additions & 13 deletions packages/rpc-core/src/rpc-methods/getBlockProduction.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
import { Base58EncodedAddress } from '@solana/keys';
import { Commitment, RpcResponse, U64UnsafeBeyond2Pow53Minus1 } from './common';

type NumberOfLeaderSlots = number;
type NumberOfBlocksProduced = number;
type NumberOfLeaderSlots = U64UnsafeBeyond2Pow53Minus1;
type NumberOfBlocksProduced = U64UnsafeBeyond2Pow53Minus1;

type Range = Readonly<{
type SlotRange = Readonly<{
firstSlot: U64UnsafeBeyond2Pow53Minus1;
lastSlot: U64UnsafeBeyond2Pow53Minus1;
}>;

type GetBlockProductionApiResponse = RpcResponse<{
byIdentity: Readonly<{
[address: string]: [NumberOfLeaderSlots, NumberOfBlocksProduced];
type GetBlockProductionApiConfigBase = Readonly<{
commitment?: Commitment;
range?: SlotRange;
}>;

type GetBlockProductionApiResponseBase = RpcResponse<{
range: SlotRange;
}>;

type GetBlockProductionApiResponseWithAllIdentities = Readonly<{
value: Readonly<{
byIdentity: Record<Base58EncodedAddress, [NumberOfLeaderSlots, NumberOfBlocksProduced]>;
}>;
}>;

type GetBlockProductionApiResponseWithSingleIdentity<TIdentity extends string> = Readonly<{
value: Readonly<{
byIdentity: Readonly<{ [TAddress in TIdentity]?: [NumberOfLeaderSlots, NumberOfBlocksProduced] }>;
}>;
range: Range;
}>;

export interface GetBlockProductionApi {
/**
* Returns recent block production information from the current or previous epoch.
*/
getBlockProduction<TIdentity extends Base58EncodedAddress>(
config: GetBlockProductionApiConfigBase &
Readonly<{
identity: TIdentity;
}>
): GetBlockProductionApiResponseBase & GetBlockProductionApiResponseWithSingleIdentity<TIdentity>;
getBlockProduction(
config?: Readonly<{
commitment?: Commitment;
identity?: Base58EncodedAddress;
range?: Range;
}>
): GetBlockProductionApiResponse;
config?: GetBlockProductionApiConfigBase
): GetBlockProductionApiResponseBase & GetBlockProductionApiResponseWithAllIdentities;
}
2 changes: 1 addition & 1 deletion packages/rpc-transport/src/json-rpc-errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Keep in sync with https://github.com/solana-labs/solana/blob/master/rpc-client-api/src/custom_error.rs
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
export const SolanaJsonRpcErrorCode = {
JSON_RPC_INVALID_PARAMS: -32602,
JSON_RPC_SCAN_ERROR: -32012,
JSON_RPC_SERVER_ERROR_BLOCK_CLEANED_UP: -32001,
JSON_RPC_SERVER_ERROR_BLOCK_NOT_AVAILABLE: -32004,
JSON_RPC_SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET: -32014,
JSON_RPC_SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX: -32010,
JSON_RPC_SERVER_ERROR_LAST_SLOT_TOO_LARGE: -32602,
JSON_RPC_SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED: -32009,
JSON_RPC_SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED: -32016,
JSON_RPC_SERVER_ERROR_NODE_UNHEALTHY: -32005,
Expand Down

0 comments on commit 5ccf373

Please sign in to comment.