Skip to content

Commit

Permalink
refactor(experimental): a comparator for commitments (#1698)
Browse files Browse the repository at this point in the history
# Summary

A handy utility that lets you know if one commitment is ‘lower’ than the other.

# Test Plan

```
cd packages/rpc-core
pnpm test:unit:browser
pnpm test:unit:node
```
  • Loading branch information
steveluscher authored Oct 7, 2023
1 parent 7da1908 commit 524cb6e
Show file tree
Hide file tree
Showing 75 changed files with 138 additions and 74 deletions.
11 changes: 11 additions & 0 deletions packages/rpc-core/src/__tests__/commitment-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Commitment, commitmentComparator } from '../commitment';

describe('commitmentComparator', () => {
it('sorts commitments according to their level of finality ascending', () => {
expect((['finalized', 'processed', 'confirmed'] as Commitment[]).sort(commitmentComparator)).toEqual([
'processed',
'confirmed',
'finalized',
]);
});
});
23 changes: 23 additions & 0 deletions packages/rpc-core/src/commitment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export type Commitment = 'confirmed' | 'finalized' | 'processed';

function getCommitmentScore(commitment: Commitment): number {
switch (commitment) {
case 'finalized':
return 2;
case 'confirmed':
return 1;
case 'processed':
return 0;
default:
return ((_: never) => {
throw new Error(`Unrecognized commitment \`${commitment}\`.`);
})(commitment);
}
}

export function commitmentComparator(a: Commitment, b: Commitment): -1 | 0 | 1 {
if (a === b) {
return 0;
}
return getCommitmentScore(a) < getCommitmentScore(b) ? -1 : 1;
}
1 change: 1 addition & 0 deletions packages/rpc-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './commitment';
export * from './lamports';
export * from './rpc-methods';
export * from './rpc-subscriptions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getBalance', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getBlockHeight', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getEpochInfo', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import { Blockhash, SerializedMessageBytesBase64 } from '@solana/transactions';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getInflationGovernor', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getInflationReward', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { open } from 'fs/promises';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const validatorKeypairPath = path.resolve(__dirname, '../../../../../test-ledger/validator-keypair.json');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getMinimumBalanceForRentExemption', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getSlot', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

// See scripts/fixtures/stake-account.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getStakeMinimumDelegation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/js
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getTransactionCount', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import { Blockhash } from '@solana/transactions';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('isBlockhashValid', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../../commitment';
import { LamportsUnsafeBeyond2Pow53Minus1 } from '../../lamports';
import { Commitment } from '../common';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('requestAirdrop', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import { Base64EncodedWireTransaction } from '@solana/transactions';
import fetchMock from 'jest-fetch-mock-fork';

import { Commitment } from '../common';
import { Commitment } from '../../commitment';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

function getMockTransactionMessage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import { Base64EncodedWireTransaction } from '@solana/transactions';
import fetchMock from 'jest-fetch-mock-fork';

import { Base58EncodedBytes, Commitment } from '../common';
import { Commitment } from '../../commitment';
import { Base58EncodedBytes } from '../common';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

const CONTEXT_MATCHER = expect.objectContaining({
Expand Down
2 changes: 0 additions & 2 deletions packages/rpc-core/src/rpc-methods/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { LamportsUnsafeBeyond2Pow53Minus1 } from '../lamports';
import { StringifiedBigInt } from '../stringified-bigint';
import { StringifiedNumber } from '../stringified-number';

export type Commitment = 'confirmed' | 'finalized' | 'processed';

export type DataSlice = Readonly<{
offset: number;
length: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-core/src/rpc-methods/getAccountInfo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Base58EncodedAddress } from '@solana/addresses';

import { Commitment } from '../commitment';
import {
AccountInfoBase,
AccountInfoWithBase58Bytes,
AccountInfoWithBase58EncodedData,
AccountInfoWithBase64EncodedData,
AccountInfoWithBase64EncodedZStdCompressedData,
AccountInfoWithJsonData,
Commitment,
DataSlice,
RpcResponse,
Slot,
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getBalance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Base58EncodedAddress } from '@solana/addresses';

import { Commitment } from '../commitment';
import { LamportsUnsafeBeyond2Pow53Minus1 } from '../lamports';
import { Commitment, RpcResponse, Slot } from './common';
import { RpcResponse, Slot } from './common';

type GetBalanceApiResponse = RpcResponse<LamportsUnsafeBeyond2Pow53Minus1>;

Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getBlock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Blockhash, TransactionVersion } from '@solana/transactions';

import { Commitment } from '../commitment';
import { UnixTimestamp } from '../unix-timestamp';
import { Base58EncodedBytes, Commitment, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';
import { Base58EncodedBytes, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';
import {
Reward,
TransactionForAccounts,
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getBlockHeight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Commitment, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';
import { Commitment } from '../commitment';
import { Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';

type GetBlockHeightApiResponse = U64UnsafeBeyond2Pow53Minus1;

Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getBlockProduction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Base58EncodedAddress } from '@solana/addresses';

import { Commitment, RpcResponse, U64UnsafeBeyond2Pow53Minus1 } from './common';
import { Commitment } from '../commitment';
import { RpcResponse, U64UnsafeBeyond2Pow53Minus1 } from './common';

type NumberOfLeaderSlots = U64UnsafeBeyond2Pow53Minus1;
type NumberOfBlocksProduced = U64UnsafeBeyond2Pow53Minus1;
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Commitment, Slot } from './common';
import { Commitment } from '../commitment';
import { Slot } from './common';

type GetBlocksApiResponse = Slot[];

Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getBlocksWithLimit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Commitment, Slot } from './common';
import { Commitment } from '../commitment';
import { Slot } from './common';

type GetBlocksWithLimitApiResponse = Slot[];

Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getEpochInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Commitment, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';
import { Commitment } from '../commitment';
import { Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';

type GetEpochInfoApiResponse = Readonly<{
/** the current slot */
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getFeeForMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SerializedMessageBytesBase64 } from '@solana/transactions';

import { Commitment, RpcResponse, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';
import { Commitment } from '../commitment';
import { RpcResponse, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';

/** Fee corresponding to the message at the specified blockhash */
type GetFeeForMessageApiResponse = RpcResponse<U64UnsafeBeyond2Pow53Minus1 | null>;
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/src/rpc-methods/getInflationGovernor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Commitment, F64UnsafeSeeDocumentation } from './common';
import { Commitment } from '../commitment';
import { F64UnsafeSeeDocumentation } from './common';

type GetInflationGovernorApiResponse = Readonly<{
/** The initial inflation percentage from time 0 */
Expand Down
Loading

0 comments on commit 524cb6e

Please sign in to comment.