fix: better handling if confirmed block not found on node

This commit is contained in:
Tyera Eulberg 2020-01-22 13:05:19 -07:00 committed by Michael Vines
parent 43e90a1967
commit 3482953757
2 changed files with 58 additions and 37 deletions

View File

@ -448,46 +448,49 @@ const GetMinimumBalanceForRentExemptionRpcResult = jsonRpcResult('number');
* Expected JSON RPC response for the "getConfirmedBlock" message * Expected JSON RPC response for the "getConfirmedBlock" message
*/ */
export const GetConfirmedBlockRpcResult = jsonRpcResult( export const GetConfirmedBlockRpcResult = jsonRpcResult(
struct({ struct.union([
blockhash: 'string', 'null',
previousBlockhash: 'string', struct({
parentSlot: 'number', blockhash: 'string',
transactions: struct.array([ previousBlockhash: 'string',
struct({ parentSlot: 'number',
transaction: struct({ transactions: struct.array([
signatures: struct.array(['string']), struct({
message: struct({ transaction: struct({
accountKeys: struct.array(['string']), signatures: struct.array(['string']),
header: struct({ message: struct({
numRequiredSignatures: 'number', accountKeys: struct.array(['string']),
numReadonlySignedAccounts: 'number', header: struct({
numReadonlyUnsignedAccounts: 'number', numRequiredSignatures: 'number',
}), numReadonlySignedAccounts: 'number',
instructions: struct.array([ numReadonlyUnsignedAccounts: 'number',
struct.union([ }),
struct.array(['number']), instructions: struct.array([
struct({ struct.union([
accounts: struct.array(['number']), struct.array(['number']),
data: 'string', struct({
programIdIndex: 'number', accounts: struct.array(['number']),
}), data: 'string',
programIdIndex: 'number',
}),
]),
]), ]),
]), recentBlockhash: 'string',
recentBlockhash: 'string', }),
}), }),
meta: struct.union([
'null',
struct({
status: SignatureStatusResult,
fee: 'number',
preBalances: struct.array(['number']),
postBalances: struct.array(['number']),
}),
]),
}), }),
meta: struct.union([ ]),
'null', }),
struct({ ]),
status: SignatureStatusResult,
fee: 'number',
preBalances: struct.array(['number']),
postBalances: struct.array(['number']),
}),
]),
}),
]),
}),
); );
/** /**
@ -1052,6 +1055,9 @@ export class Connection {
throw new Error(result.error.message); throw new Error(result.error.message);
} }
assert(typeof result.result !== 'undefined'); assert(typeof result.result !== 'undefined');
if (!result.result) {
throw new Error('Confirmed block '+slot+' not found');
}
return { return {
blockhash: new PublicKey(result.result.blockhash).toString(), blockhash: new PublicKey(result.result.blockhash).toString(),
previousBlockhash: new PublicKey( previousBlockhash: new PublicKey(

View File

@ -646,6 +646,21 @@ test('get confirmed block', async () => {
} }
x++; x++;
} }
mockRpc.push([
url,
{
method: 'getConfirmedBlock',
params: [10000],
},
{
error: null,
result: null,
},
]);
await expect(
connection.getConfirmedBlock(10000),
).rejects.toThrow();
}); });
test('get recent blockhash', async () => { test('get recent blockhash', async () => {