-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): add
getBlockProduction
API (#1263)
- Add the `getBlockProduction` API + unit tests - Add jest-extended which provides some extra jest matchers ``` pnpm turbo test:unit:node test:unit:browser ``` * fixup! refactor(experimental): add `getBlockProduction` API * nit: do without jest-extended for now, formatting nits --------- Co-authored-by: Callum McIntyre <[email protected]> Co-authored-by: steveluscher <[email protected]>
- Loading branch information
1 parent
3244e3e
commit edfcbdc
Showing
4 changed files
with
129 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
79 changes: 79 additions & 0 deletions
79
packages/rpc-core/src/rpc-methods/__tests__/get-block-production-test.ts
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,79 @@ | ||
import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport'; | ||
import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/json-rpc-errors'; | ||
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types'; | ||
import fetchMock from 'jest-fetch-mock-fork'; | ||
import { createSolanaRpcApi, SolanaRpcMethods } from '../../index'; | ||
import { Commitment } from '../common'; | ||
import { Base58EncodedAddress } from '@solana/keys'; | ||
|
||
describe('getBlockProduction', () => { | ||
let rpc: Rpc<SolanaRpcMethods>; | ||
beforeEach(() => { | ||
fetchMock.resetMocks(); | ||
fetchMock.dontMock(); | ||
rpc = createJsonRpc<SolanaRpcMethods>({ | ||
api: createSolanaRpcApi(), | ||
transport: createHttpTransport({ url: 'http://127.0.0.1:8899' }), | ||
}); | ||
}); | ||
|
||
(['confirmed', 'finalized', 'processed'] as Commitment[]).forEach(commitment => { | ||
describe(`when called with \`${commitment}\` commitment`, () => { | ||
it('returns block production data', async () => { | ||
expect.assertions(1); | ||
const blockProductionPromise = rpc.getBlockProduction({ commitment }).send(); | ||
await expect(blockProductionPromise).resolves.toMatchObject({ | ||
value: expect.objectContaining({ | ||
byIdentity: expect.any(Object), | ||
range: expect.objectContaining({ | ||
firstSlot: expect.any(BigInt), | ||
lastSlot: expect.any(BigInt), | ||
}), | ||
}), | ||
}); | ||
}); | ||
|
||
it('has the latest context slot as the last slot', async () => { | ||
expect.assertions(1); | ||
const blockProduction = await rpc.getBlockProduction({ commitment }).send(); | ||
expect(blockProduction.value.range.lastSlot).toBe(blockProduction.context.slot); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called with a single identity', () => { | ||
// Currently this call always returns just one identity in tests, so no way to meaningfully test this | ||
it.todo('returns data for just that identity'); | ||
|
||
it('returns an empty byIdentity if the identity is not a block producer', async () => { | ||
expect.assertions(1); | ||
// Randomly generated address, assumed not to be a block producer | ||
const identity = '9NmqDDZa7mH1DBM4zeq9cm7VcRn2un1i2TwuMvjBoVhU' as Base58EncodedAddress; | ||
const blockProductionPromise = rpc.getBlockProduction({ identity }).send(); | ||
await expect(blockProductionPromise).resolves.toMatchObject({ | ||
value: expect.objectContaining({ | ||
byIdentity: {}, | ||
}), | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called with a `lastSlot` higher than the highest slot available', () => { | ||
it('throws an error', async () => { | ||
expect.assertions(1); | ||
const blockProductionPromise = rpc | ||
.getBlockProduction({ | ||
range: { | ||
firstSlot: 0n, | ||
lastSlot: 2n ** 63n - 1n, // u64:MAX; safe bet it'll be too high. | ||
}, | ||
}) | ||
.send(); | ||
await expect(blockProductionPromise).rejects.toMatchObject({ | ||
code: -32602 satisfies (typeof SolanaJsonRpcErrorCode)['JSON_RPC_INVALID_PARAMS'], | ||
message: expect.any(String), | ||
name: 'SolanaJsonRpcError', | ||
}); | ||
}); | ||
}); | ||
}); |
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,46 @@ | ||
import { Base58EncodedAddress } from '@solana/keys'; | ||
import { Commitment, RpcResponse, U64UnsafeBeyond2Pow53Minus1 } from './common'; | ||
|
||
type NumberOfLeaderSlots = U64UnsafeBeyond2Pow53Minus1; | ||
type NumberOfBlocksProduced = U64UnsafeBeyond2Pow53Minus1; | ||
|
||
type SlotRange = Readonly<{ | ||
firstSlot: U64UnsafeBeyond2Pow53Minus1; | ||
lastSlot: U64UnsafeBeyond2Pow53Minus1; | ||
}>; | ||
|
||
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] }>; | ||
}>; | ||
}>; | ||
|
||
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?: GetBlockProductionApiConfigBase | ||
): GetBlockProductionApiResponseBase & GetBlockProductionApiResponseWithAllIdentities; | ||
} |
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