fix: avoid double spend in sendAndConfirmTransaction

This commit is contained in:
Justin Starry
2020-06-15 18:32:57 +08:00
committed by Justin Starry
parent d77818c18b
commit f31f66a7c3
5 changed files with 53 additions and 44 deletions

View File

@ -10,7 +10,7 @@ import {Client as RpcWebSocketClient} from 'rpc-websockets';
import {NonceAccount} from './nonce-account'; import {NonceAccount} from './nonce-account';
import {PublicKey} from './publickey'; import {PublicKey} from './publickey';
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from './timing'; import {MS_PER_SLOT} from './timing';
import {Transaction} from './transaction'; import {Transaction} from './transaction';
import {Message} from './message'; import {Message} from './message';
import {sleep} from './util/sleep'; import {sleep} from './util/sleep';
@ -1285,19 +1285,14 @@ export class Connection {
signature: TransactionSignature, signature: TransactionSignature,
confirmations: ?number, confirmations: ?number,
): Promise<RpcResponseAndContext<SignatureStatus | null>> { ): Promise<RpcResponseAndContext<SignatureStatus | null>> {
const NUM_STATUS_RETRIES = 10; const start = Date.now();
const WAIT_TIMEOUT_MS = 60 * 1000;
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); let statusResponse = await this.getSignatureStatus(signature);
for (;;) { for (;;) {
const status = statusResponse.value; const status = statusResponse.value;
if (status) { if (status) {
// Received a status, if not an error wait for confirmation // Received a status, if not an error wait for confirmation
statusRetries = NUM_STATUS_RETRIES;
if ( if (
status.err || status.err ||
status.confirmations === null || status.confirmations === null ||
@ -1306,12 +1301,12 @@ export class Connection {
) { ) {
break; break;
} }
} else if (--statusRetries <= 0) { } else if (Date.now() - start >= WAIT_TIMEOUT_MS) {
break; break;
} }
// Sleep for approximately half a slot // Sleep for approximately one slot
await sleep(MS_PER_SLOT / 2); await sleep(MS_PER_SLOT);
statusResponse = await this.getSignatureStatus(signature); statusResponse = await this.getSignatureStatus(signature);
} }
@ -1780,7 +1775,7 @@ export class Connection {
} }
// Sleep for approximately half a slot // Sleep for approximately half a slot
await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND); await sleep(MS_PER_SLOT / 2);
++attempts; ++attempts;
} }

View File

@ -6,9 +6,20 @@
/** /**
* @ignore * @ignore
*/ */
export const NUM_TICKS_PER_SECOND = 10; export const NUM_TICKS_PER_SECOND = 160;
/** /**
* @ignore * @ignore
*/ */
export const DEFAULT_TICKS_PER_SLOT = 8; export const DEFAULT_TICKS_PER_SLOT = 64;
/**
* @ignore
*/
export const NUM_SLOTS_PER_SECOND =
NUM_TICKS_PER_SECOND / DEFAULT_TICKS_PER_SLOT;
/**
* @ignore
*/
export const MS_PER_SLOT = 1000 / NUM_SLOTS_PER_SECOND;

View File

@ -210,7 +210,9 @@ export class Transaction {
let numReadonlySignedAccounts = 0; let numReadonlySignedAccounts = 0;
let numReadonlyUnsignedAccounts = 0; let numReadonlyUnsignedAccounts = 0;
const accountKeys = this.signatures.map(({publicKey}) => publicKey.toString()); const accountKeys = this.signatures.map(({publicKey}) =>
publicKey.toString(),
);
const programIds: string[] = []; const programIds: string[] = [];
const accountMetas: AccountMeta[] = []; const accountMetas: AccountMeta[] = [];
this.instructions.forEach(instruction => { this.instructions.forEach(instruction => {

View File

@ -6,6 +6,13 @@ import type {ConfirmOptions} from '../connection';
/** /**
* Send and confirm a raw transaction * Send and confirm a raw transaction
*
* If `confirmations` count is not specified, wait for transaction to be finalized.
*
* @param {Connection} connection
* @param {Buffer} rawTransaction
* @param {ConfirmOptions} [options]
* @returns {Promise<TransactionSignature>}
*/ */
export async function sendAndConfirmRawTransaction( export async function sendAndConfirmRawTransaction(
connection: Connection, connection: Connection,

View File

@ -2,17 +2,20 @@
import {Connection} from '../connection'; import {Connection} from '../connection';
import {Transaction} from '../transaction'; import {Transaction} from '../transaction';
import {sleep} from './sleep';
import type {Account} from '../account'; import type {Account} from '../account';
import type {ConfirmOptions} from '../connection'; import type {ConfirmOptions} from '../connection';
import type {TransactionSignature} from '../transaction'; import type {TransactionSignature} from '../transaction';
const NUM_SEND_RETRIES = 10;
/** /**
* Sign, send and confirm a transaction. * Sign, send and confirm a transaction.
* *
* If `confirmations` count is not specified, wait for transaction to be finalized. * If `confirmations` count is not specified, wait for transaction to be finalized.
*
* @param {Connection} connection
* @param {Transaction} transaction
* @param {Array<Account>} signers
* @param {ConfirmOptions} [options]
* @returns {Promise<TransactionSignature>}
*/ */
export async function sendAndConfirmTransaction( export async function sendAndConfirmTransaction(
connection: Connection, connection: Connection,
@ -21,34 +24,25 @@ export async function sendAndConfirmTransaction(
options?: ConfirmOptions, options?: ConfirmOptions,
): Promise<TransactionSignature> { ): Promise<TransactionSignature> {
const start = Date.now(); const start = Date.now();
let sendRetries = NUM_SEND_RETRIES; const signature = await connection.sendTransaction(
transaction,
signers,
options,
);
const status = (
await connection.confirmTransaction(
signature,
options && options.confirmations,
)
).value;
for (;;) { if (status) {
const signature = await connection.sendTransaction( if (status.err) {
transaction, throw new Error(
signers, `Transaction ${signature} failed (${JSON.stringify(status)})`,
options, );
);
const status = (
await connection.confirmTransaction(
signature,
options && options.confirmations,
)
).value;
if (status) {
if (status.err) {
throw new Error(
`Transaction ${signature} failed (${JSON.stringify(status)})`,
);
}
return signature;
} }
return signature;
if (--sendRetries <= 0) break;
// Retry in 0..100ms to try to avoid another AccountInUse collision
await sleep(Math.random() * 100);
} }
const duration = (Date.now() - start) / 1000; const duration = (Date.now() - start) / 1000;