This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 922
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
## Summary - Add the `getBlockProduction` API + unit tests - Add jest-extended which provides some extra jest matchers ## Test Plan ``` pnpm turbo test:unit:node test:unit:browser ```
- Loading branch information
1 parent
3290268
commit ab6148f
Showing
8 changed files
with
136 additions
and
3 deletions.
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
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
81 changes: 81 additions & 0 deletions
81
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,81 @@ | ||
import { createJsonRpcTransport } from '@solana/rpc-transport'; | ||
import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-errors'; | ||
import type { Transport } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-transport-types'; | ||
import fetchMock from 'jest-fetch-mock-fork'; | ||
import { createSolanaRpcApi, SolanaRpcMethods } from '../../index'; | ||
import { Commitment } from '../common'; | ||
import { Base58EncodedAddress } from '@solana/keys'; | ||
|
||
describe('getBalance', () => { | ||
let transport: Transport<SolanaRpcMethods>; | ||
beforeEach(() => { | ||
fetchMock.resetMocks(); | ||
fetchMock.dontMock(); | ||
transport = createJsonRpcTransport({ | ||
api: createSolanaRpcApi(), | ||
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 = transport.getBlockProduction({ commitment }).send(); | ||
await expect(blockProductionPromise).resolves.toMatchObject({ | ||
value: expect.objectContaining({ | ||
byIdentity: expect.toBeObject(), | ||
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 blockProductionPromise = transport.getBlockProduction({ commitment }).send(); | ||
await expect(blockProductionPromise).resolves.toSatisfy( | ||
rpcResponse => rpcResponse.context.slot === rpcResponse.value.range.lastSlot | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
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 = transport.getBlockProduction({ identity }).send(); | ||
await expect(blockProductionPromise).resolves.toMatchObject({ | ||
value: expect.objectContaining({ | ||
byIdentity: expect.toBeEmptyObject(), | ||
}), | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called with a `lastSlot` higher than the highest slot available', () => { | ||
it('throws an error', async () => { | ||
expect.assertions(1); | ||
const blockProductionPromise = transport | ||
.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_SERVER_ERROR_LAST_SLOT_TOO_LARGE'], | ||
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,30 @@ | ||
import { Base58EncodedAddress } from '@solana/keys'; | ||
import { Commitment, RpcResponse, U64UnsafeBeyond2Pow53Minus1 } from './common'; | ||
|
||
type NumberOfLeaderSlots = number; | ||
type NumberOfBlocksProduced = number; | ||
|
||
type Range = Readonly<{ | ||
firstSlot: U64UnsafeBeyond2Pow53Minus1; | ||
lastSlot: U64UnsafeBeyond2Pow53Minus1; | ||
}>; | ||
|
||
type GetBlockProductionApiResponse = RpcResponse<{ | ||
byIdentity: Readonly<{ | ||
[address: string]: [NumberOfLeaderSlots, NumberOfBlocksProduced]; | ||
}>; | ||
range: Range; | ||
}>; | ||
|
||
export interface GetBlockProductionApi { | ||
/** | ||
* Returns recent block production information from the current or previous epoch. | ||
*/ | ||
getBlockProduction( | ||
config?: Readonly<{ | ||
commitment?: Commitment; | ||
identity?: Base58EncodedAddress; | ||
range?: Range; | ||
}> | ||
): GetBlockProductionApiResponse; | ||
} |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.