feat: update transaction confirming apis

This commit is contained in:
Justin Starry
2020-05-20 17:13:21 +08:00
committed by Michael Vines
parent 3b71ec1ff6
commit 839e93480c
13 changed files with 214 additions and 316 deletions

View File

@@ -469,13 +469,6 @@ const GetProgramAccountsRpcResult = jsonRpcResult(
struct.array([ProgramAccountInfoResult]),
);
/**
* Expected JSON RPC response for the "confirmTransaction" message
*/
const ConfirmTransactionAndContextRpcResult = jsonRpcResultAndContext(
'boolean',
);
/**
* Expected JSON RPC response for the "getSlot" message
*/
@@ -1063,35 +1056,44 @@ export class Connection {
});
}
/**
* Confirm the transaction identified by the specified signature, return with context
*/
async confirmTransactionAndContext(
signature: TransactionSignature,
commitment: ?Commitment,
): Promise<RpcResponseAndContext<boolean>> {
const args = this._argsWithCommitment([signature], commitment);
const unsafeRes = await this._rpcRequest('confirmTransaction', args);
const res = ConfirmTransactionAndContextRpcResult(unsafeRes);
if (res.error) {
throw new Error('failed to confirm transaction: ' + res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
/**
* Confirm the transaction identified by the specified signature
*/
async confirmTransaction(
signature: TransactionSignature,
commitment: ?Commitment,
): Promise<boolean> {
return await this.confirmTransactionAndContext(signature, commitment)
.then(x => x.value)
.catch(e => {
throw new Error('failed to confirm transaction: ' + e);
});
confirmations: ?number,
): Promise<RpcResponseAndContext<SignatureStatus | null>> {
const NUM_STATUS_RETRIES = 10;
const MS_PER_SECOND = 1000;
const MS_PER_SLOT =
(DEFAULT_TICKS_PER_SLOT / NUM_TICKS_PER_SECOND) * MS_PER_SECOND;
let statusRetries = NUM_STATUS_RETRIES;
let statusResponse = await this.getSignatureStatus(signature);
for (;;) {
const status = statusResponse.value;
if (status) {
// Received a status, if not an error wait for confirmation
statusRetries = NUM_STATUS_RETRIES;
if (
status.err ||
status.confirmations === null ||
(typeof confirmations === 'number' &&
status.confirmations >= confirmations)
) {
break;
}
} else if (--statusRetries <= 0) {
break;
}
// Sleep for approximately half a slot
await sleep(MS_PER_SLOT / 2);
statusResponse = await this.getSignatureStatus(signature);
}
return statusResponse;
}
/**
@@ -1474,7 +1476,7 @@ export class Connection {
*/
async sendTransaction(
transaction: Transaction,
...signers: Array<Account>
signers: Array<Account>,
): Promise<TransactionSignature> {
if (transaction.nonceInfo) {
transaction.sign(...signers);

View File

@@ -30,10 +30,7 @@ export {
SYSVAR_REWARDS_PUBKEY,
SYSVAR_STAKE_HISTORY_PUBKEY,
} from './sysvar';
export {
sendAndConfirmTransaction,
sendAndConfirmRecentTransaction,
} from './util/send-and-confirm-transaction';
export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
export {sendAndConfirmRawTransaction} from './util/send-and-confirm-raw-transaction';
export {clusterApiUrl} from './util/cluster';

View File

@@ -65,7 +65,12 @@ export class Loader {
space: data.length,
programId,
});
await sendAndConfirmTransaction(connection, transaction, payer, program);
await sendAndConfirmTransaction(
connection,
transaction,
[payer, program],
1,
);
}
const dataLayout = BufferLayout.struct([
@@ -102,7 +107,7 @@ export class Loader {
data,
});
transactions.push(
sendAndConfirmTransaction(connection, transaction, payer, program),
sendAndConfirmTransaction(connection, transaction, [payer, program], 1),
);
// Delay ~1 tick between write transactions in an attempt to reduce AccountInUse errors
@@ -143,7 +148,12 @@ export class Loader {
programId,
data,
});
await sendAndConfirmTransaction(connection, transaction, payer, program);
await sendAndConfirmTransaction(
connection,
transaction,
[payer, program],
1,
);
}
}
}

View File

@@ -1,54 +1,34 @@
// @flow
import {Connection} from '../connection';
import type {Commitment} from '../connection';
import {sleep} from './sleep';
import type {TransactionSignature} from '../transaction';
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../timing';
/**
* Sign, send and confirm a raw transaction
* Send and confirm a raw transaction
*/
export async function sendAndConfirmRawTransaction(
connection: Connection,
rawTransaction: Buffer,
commitment: ?Commitment,
confirmations: ?number,
): Promise<TransactionSignature> {
const start = Date.now();
const statusCommitment = commitment || connection.commitment || 'max';
let signature = await connection.sendRawTransaction(rawTransaction);
const signature = await connection.sendRawTransaction(rawTransaction);
const status = (await connection.confirmTransaction(signature, confirmations))
.value;
// Wait up to a couple slots for a confirmation
let status = null;
let statusRetries = 6;
for (;;) {
status = (await connection.getSignatureStatus(signature)).value;
if (status) {
if (statusCommitment === 'max' && status.confirmations === null) {
break;
} else if (statusCommitment === 'recent') {
break;
}
}
// Sleep for approximately half a slot
await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
if (--statusRetries <= 0) {
const duration = (Date.now() - start) / 1000;
if (status) {
if (status.err) {
throw new Error(
`Raw Transaction '${signature}' was not confirmed in ${duration.toFixed(
2,
)} seconds (${JSON.stringify(status)})`,
`Raw transaction ${signature} failed (${JSON.stringify(status)})`,
);
}
}
if (status && !status.err) {
return signature;
}
const duration = (Date.now() - start) / 1000;
throw new Error(
`Raw transaction ${signature} failed (${JSON.stringify(status)})`,
`Raw transaction '${signature}' was not confirmed in ${duration.toFixed(
2,
)} seconds`,
);
}

View File

@@ -1,111 +1,52 @@
// @flow
import invariant from 'assert';
import {Connection} from '../connection';
import type {Commitment} from '../connection';
import {Transaction} from '../transaction';
import {sleep} from './sleep';
import type {Account} from '../account';
import type {TransactionSignature} from '../transaction';
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../timing';
const MS_PER_SECOND = 1000;
const MS_PER_SLOT =
(DEFAULT_TICKS_PER_SLOT / NUM_TICKS_PER_SECOND) * MS_PER_SECOND;
const NUM_SEND_RETRIES = 10;
const NUM_STATUS_RETRIES = 10;
/**
* Sign, send and confirm a transaction with recent commitment level
*/
export async function sendAndConfirmRecentTransaction(
connection: Connection,
transaction: Transaction,
...signers: Array<Account>
): Promise<TransactionSignature> {
return await _sendAndConfirmTransaction(
connection,
transaction,
signers,
'recent',
);
}
/**
* Sign, send and confirm a transaction
* Sign, send and confirm a transaction.
*
* If `confirmations` count is not specified, wait for transaction to be finalized.
*/
export async function sendAndConfirmTransaction(
connection: Connection,
transaction: Transaction,
...signers: Array<Account>
): Promise<TransactionSignature> {
return await _sendAndConfirmTransaction(connection, transaction, signers);
}
async function _sendAndConfirmTransaction(
connection: Connection,
transaction: Transaction,
signers: Array<Account>,
commitment: ?Commitment,
confirmations: ?number,
): Promise<TransactionSignature> {
const statusCommitment = commitment || connection.commitment || 'max';
const start = Date.now();
let sendRetries = NUM_SEND_RETRIES;
let signature;
for (;;) {
const start = Date.now();
signature = await connection.sendTransaction(transaction, ...signers);
// Wait up to a couple slots for a confirmation
let status = null;
let statusRetries = NUM_STATUS_RETRIES;
for (;;) {
status = (await connection.getSignatureStatus(signature)).value;
if (status) {
// Recieved a status, if not an error wait for confirmation
statusRetries = NUM_STATUS_RETRIES;
if (
status.err ||
status.confirmations === null ||
(statusCommitment === 'recent' && status.confirmations >= 1)
) {
break;
}
}
if (--statusRetries <= 0) {
break;
}
// Sleep for approximately half a slot
await sleep(MS_PER_SLOT / 2);
}
const signature = await connection.sendTransaction(transaction, signers);
const status = (
await connection.confirmTransaction(signature, confirmations)
).value;
if (status) {
if (!status.err) {
break;
} else if (!('AccountInUse' in status.err)) {
if (status.err) {
throw new Error(
`Transaction ${signature} failed (${JSON.stringify(status)})`,
);
}
return signature;
}
if (--sendRetries <= 0) {
const duration = (Date.now() - start) / 1000;
throw new Error(
`Transaction '${signature}' was not confirmed in ${duration.toFixed(
2,
)} seconds (${JSON.stringify(status)})`,
);
}
if (--sendRetries <= 0) break;
// Retry in 0..100ms to try to avoid another AccountInUse collision
await sleep(Math.random() * 100);
}
invariant(signature !== undefined);
return signature;
const duration = (Date.now() - start) / 1000;
throw new Error(
`Transaction was not confirmed in ${duration.toFixed(
2,
)} seconds (${JSON.stringify(status)})`,
);
}