|
| 1 | +import NodeDirect from './Direct'; |
| 2 | +import { getIntervals } from '../utils/autorest'; |
| 3 | +import { pause } from '../utils/other'; |
| 4 | +import { buildTx, unpackTx } from '../tx/builder'; |
| 5 | +import { Tag } from '../tx/builder/constants'; |
| 6 | +import getTransactionSignerAddress from '../tx/transaction-signer'; |
| 7 | +import { Encoded } from '../utils/encoder'; |
| 8 | +import { IllegalArgumentError } from '../utils/errors'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Implements request retry strategies to improve reliability of connection to multiple nodes behind |
| 12 | + * load balancer. |
| 13 | + */ |
| 14 | +export default class NodeGateway extends NodeDirect { |
| 15 | + #nonces: Record<string, number> = {}; |
| 16 | + |
| 17 | + readonly #retryIntervals: number[]; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param url - Url for node API |
| 21 | + * @param options - Options |
| 22 | + */ |
| 23 | + constructor( |
| 24 | + url: string, |
| 25 | + { |
| 26 | + retryCount = 8, retryOverallDelay = 3000, ...options |
| 27 | + }: ConstructorParameters<typeof NodeDirect>[1] = {}, |
| 28 | + ) { |
| 29 | + super(url, { |
| 30 | + ...options, retryCount, retryOverallDelay, _disableGatewayWarning: true, |
| 31 | + }); |
| 32 | + this.#retryIntervals = getIntervals(retryCount, retryOverallDelay); |
| 33 | + } |
| 34 | + |
| 35 | + #saveNonce(tx: Encoded.Transaction): void { |
| 36 | + const { encodedTx } = unpackTx(tx, Tag.SignedTx); |
| 37 | + if (encodedTx.tag === Tag.GaMetaTx) return; |
| 38 | + if (!('nonce' in encodedTx)) { |
| 39 | + throw new IllegalArgumentError('Transaction doesn\'t have nonce field'); |
| 40 | + } |
| 41 | + const address = getTransactionSignerAddress(tx); |
| 42 | + this.#nonces[address] = encodedTx.nonce; |
| 43 | + if (encodedTx.tag === Tag.PayingForTx) { |
| 44 | + this.#saveNonce(buildTx(encodedTx.tx)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // @ts-expect-error use code generation to create node class or integrate bigint to autorest |
| 49 | + override async postTransaction( |
| 50 | + ...args: Parameters<NodeDirect['postTransaction']> |
| 51 | + ): ReturnType<NodeDirect['postTransaction']> { |
| 52 | + const res = super.postTransaction(...args); |
| 53 | + try { |
| 54 | + this.#saveNonce(args[0].tx as Encoded.Transaction); |
| 55 | + } catch (error) { |
| 56 | + console.warn('NodeGateway: failed to save nonce,', error); |
| 57 | + } |
| 58 | + return res; |
| 59 | + } |
| 60 | + |
| 61 | + async #retryNonceRequest<T>( |
| 62 | + address: string, |
| 63 | + doRequest: () => Promise<T>, |
| 64 | + getNonce: (t: T) => number, |
| 65 | + ): Promise<T> { |
| 66 | + for (let attempt = 0; attempt < this.#retryIntervals.length; attempt += 1) { |
| 67 | + const result = await doRequest(); |
| 68 | + const nonce = getNonce(result); |
| 69 | + if (nonce >= (this.#nonces[address] ?? -1)) { |
| 70 | + return result; |
| 71 | + } |
| 72 | + await pause(this.#retryIntervals[attempt]); |
| 73 | + } |
| 74 | + return doRequest(); |
| 75 | + } |
| 76 | + |
| 77 | + // @ts-expect-error use code generation to create node class or integrate bigint to autorest |
| 78 | + override async getAccountByPubkey( |
| 79 | + ...args: Parameters<NodeDirect['getAccountByPubkey']> |
| 80 | + ): ReturnType<NodeDirect['getAccountByPubkey']> { |
| 81 | + return this.#retryNonceRequest( |
| 82 | + args[0], |
| 83 | + async () => super.getAccountByPubkey(...args), |
| 84 | + ({ nonce, kind }) => (kind === 'generalized' ? Number.MAX_SAFE_INTEGER : nonce), |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + // @ts-expect-error use code generation to create node class or integrate bigint to autorest |
| 89 | + override async getAccountNextNonce( |
| 90 | + ...args: Parameters<NodeDirect['getAccountNextNonce']> |
| 91 | + ): ReturnType<NodeDirect['getAccountNextNonce']> { |
| 92 | + return this.#retryNonceRequest( |
| 93 | + args[0], |
| 94 | + async () => super.getAccountNextNonce(...args), |
| 95 | + ({ nextNonce }) => (nextNonce === 0 ? Number.MAX_SAFE_INTEGER : nextNonce - 1), |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments