-
Notifications
You must be signed in to change notification settings - Fork 911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(experimental): Add getBlockProduction
API
#1263
Merged
steveluscher
merged 3 commits into
solana-labs:master
from
mcintyre94:get-block-production
Apr 25, 2023
+129
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason that this doesn't work is actually because the validator hasn't advanced to that slot yet. Apparently we code this as ‘invalid params (32602).’
Kind of sounds like we need an actual error code for this in the RPC, similar to
JSON_RPC_SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed the 32602 error to
JSON_RPC_INVALID_PARAMS
for now, since that best matches the current behaviour. Would a github issue on the solana monorepo be the best place to track adding a custom error code?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! Totally. That RPC method could do with throwing a more precise error.