Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 2f33b79

Browse files
steveluschermergify[bot]
authored andcommittedJul 2, 2022
fix: getPerformanceSamples no longer breaks when the connection has a default commitment
1 parent ea9dffa commit 2f33b79

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed
 

‎src/connection.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ type RpcRequest = (methodName: string, args: Array<any>) => Promise<any>;
195195

196196
type RpcBatchRequest = (requests: RpcParams[]) => Promise<any[]>;
197197

198+
const NO_COMMITMENT =
199+
typeof Symbol === 'function' ? Symbol('NO_COMMITMENT') : ({} as const);
200+
198201
/**
199202
* @internal
200203
*/
@@ -3431,7 +3434,7 @@ export class Connection {
34313434
async getRecentPerformanceSamples(
34323435
limit?: number,
34333436
): Promise<Array<PerfSample>> {
3434-
const args = this._buildArgs(limit ? [limit] : []);
3437+
const args = this._buildArgs(limit ? [limit] : [], NO_COMMITMENT);
34353438
const unsafeRes = await this._rpcRequest(
34363439
'getRecentPerformanceSamples',
34373440
args,
@@ -5062,11 +5065,12 @@ export class Connection {
50625065

50635066
_buildArgs(
50645067
args: Array<any>,
5065-
override?: Commitment,
5068+
override?: Commitment | typeof NO_COMMITMENT,
50665069
encoding?: 'jsonParsed' | 'base64',
50675070
extra?: any,
50685071
): Array<any> {
5069-
const commitment = override || this._commitment;
5072+
const commitment =
5073+
override === NO_COMMITMENT ? undefined : override || this._commitment;
50705074
if (commitment || encoding || extra) {
50715075
let options: any = {};
50725076
if (encoding) {

‎test/connection.test.ts

+33-21
Original file line numberDiff line numberDiff line change
@@ -3154,29 +3154,41 @@ describe('Connection', function () {
31543154
expect(supply.nonCirculatingAccounts.length).to.eq(0);
31553155
});
31563156

3157-
it('get performance samples', async () => {
3158-
await mockRpcResponse({
3159-
method: 'getRecentPerformanceSamples',
3160-
params: [],
3161-
value: [
3162-
{
3163-
slot: 1234,
3164-
numTransactions: 1000,
3165-
numSlots: 60,
3166-
samplePeriodSecs: 60,
3167-
},
3168-
],
3169-
});
3157+
[undefined, 'confirmed' as Commitment].forEach(function (commitment) {
3158+
describe.only(
3159+
"when the connection's default commitment is `" + commitment + '`',
3160+
() => {
3161+
let connectionWithCommitment: Connection;
3162+
beforeEach(() => {
3163+
connectionWithCommitment = new Connection(url, commitment);
3164+
});
3165+
it('get performance samples', async () => {
3166+
await mockRpcResponse({
3167+
method: 'getRecentPerformanceSamples',
3168+
params: [],
3169+
value: [
3170+
{
3171+
slot: 1234,
3172+
numTransactions: 1000,
3173+
numSlots: 60,
3174+
samplePeriodSecs: 60,
3175+
},
3176+
],
3177+
});
31703178

3171-
const perfSamples = await connection.getRecentPerformanceSamples();
3172-
expect(Array.isArray(perfSamples)).to.be.true;
3179+
const perfSamples =
3180+
await connectionWithCommitment.getRecentPerformanceSamples();
3181+
expect(Array.isArray(perfSamples)).to.be.true;
31733182

3174-
if (perfSamples.length > 0) {
3175-
expect(perfSamples[0].slot).to.be.greaterThan(0);
3176-
expect(perfSamples[0].numTransactions).to.be.greaterThan(0);
3177-
expect(perfSamples[0].numSlots).to.be.greaterThan(0);
3178-
expect(perfSamples[0].samplePeriodSecs).to.be.greaterThan(0);
3179-
}
3183+
if (perfSamples.length > 0) {
3184+
expect(perfSamples[0].slot).to.be.greaterThan(0);
3185+
expect(perfSamples[0].numTransactions).to.be.greaterThan(0);
3186+
expect(perfSamples[0].numSlots).to.be.greaterThan(0);
3187+
expect(perfSamples[0].samplePeriodSecs).to.be.greaterThan(0);
3188+
}
3189+
});
3190+
},
3191+
);
31803192
});
31813193

31823194
it('get performance samples limit too high', async () => {

0 commit comments

Comments
 (0)
This repository has been archived.