diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index e9e9262dc9..32fe1318af 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -10,7 +10,7 @@ import {Client as RpcWebSocketClient} from 'rpc-websockets'; import {NonceAccount} from './nonce-account'; 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 {Message} from './message'; import {sleep} from './util/sleep'; @@ -1285,19 +1285,14 @@ export class Connection { signature: TransactionSignature, confirmations: ?number, ): Promise> { - 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); 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 || @@ -1306,12 +1301,12 @@ export class Connection { ) { break; } - } else if (--statusRetries <= 0) { + } else if (Date.now() - start >= WAIT_TIMEOUT_MS) { break; } - // Sleep for approximately half a slot - await sleep(MS_PER_SLOT / 2); + // Sleep for approximately one slot + await sleep(MS_PER_SLOT); statusResponse = await this.getSignatureStatus(signature); } @@ -1780,7 +1775,7 @@ export class Connection { } // Sleep for approximately half a slot - await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND); + await sleep(MS_PER_SLOT / 2); ++attempts; } diff --git a/web3.js/src/timing.js b/web3.js/src/timing.js index ca76e74d36..64b6457ffc 100644 --- a/web3.js/src/timing.js +++ b/web3.js/src/timing.js @@ -6,9 +6,20 @@ /** * @ignore */ -export const NUM_TICKS_PER_SECOND = 10; +export const NUM_TICKS_PER_SECOND = 160; /** * @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; diff --git a/web3.js/src/transaction.js b/web3.js/src/transaction.js index 6b4ec95d4d..e18ce65a9b 100644 --- a/web3.js/src/transaction.js +++ b/web3.js/src/transaction.js @@ -210,7 +210,9 @@ export class Transaction { let numReadonlySignedAccounts = 0; let numReadonlyUnsignedAccounts = 0; - const accountKeys = this.signatures.map(({publicKey}) => publicKey.toString()); + const accountKeys = this.signatures.map(({publicKey}) => + publicKey.toString(), + ); const programIds: string[] = []; const accountMetas: AccountMeta[] = []; this.instructions.forEach(instruction => { diff --git a/web3.js/src/util/send-and-confirm-raw-transaction.js b/web3.js/src/util/send-and-confirm-raw-transaction.js index d19273486f..53ef551450 100644 --- a/web3.js/src/util/send-and-confirm-raw-transaction.js +++ b/web3.js/src/util/send-and-confirm-raw-transaction.js @@ -6,6 +6,13 @@ import type {ConfirmOptions} from '../connection'; /** * 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} */ export async function sendAndConfirmRawTransaction( connection: Connection, diff --git a/web3.js/src/util/send-and-confirm-transaction.js b/web3.js/src/util/send-and-confirm-transaction.js index 62cd77600c..7a827b0dab 100644 --- a/web3.js/src/util/send-and-confirm-transaction.js +++ b/web3.js/src/util/send-and-confirm-transaction.js @@ -2,17 +2,20 @@ import {Connection} from '../connection'; import {Transaction} from '../transaction'; -import {sleep} from './sleep'; import type {Account} from '../account'; import type {ConfirmOptions} from '../connection'; import type {TransactionSignature} from '../transaction'; -const NUM_SEND_RETRIES = 10; - /** * Sign, send and confirm a transaction. * * If `confirmations` count is not specified, wait for transaction to be finalized. + * + * @param {Connection} connection + * @param {Transaction} transaction + * @param {Array} signers + * @param {ConfirmOptions} [options] + * @returns {Promise} */ export async function sendAndConfirmTransaction( connection: Connection, @@ -21,34 +24,25 @@ export async function sendAndConfirmTransaction( options?: ConfirmOptions, ): Promise { 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 (;;) { - const signature = await connection.sendTransaction( - transaction, - signers, - 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; + if (status) { + if (status.err) { + throw new Error( + `Transaction ${signature} failed (${JSON.stringify(status)})`, + ); } - - if (--sendRetries <= 0) break; - - // Retry in 0..100ms to try to avoid another AccountInUse collision - await sleep(Math.random() * 100); + return signature; } const duration = (Date.now() - start) / 1000;