From a9d3549d3ebeec0b20f02a9f8d34ad5d3ebb2bee Mon Sep 17 00:00:00 2001 From: nitowa Date: Mon, 28 Mar 2022 05:30:40 +0200 Subject: [PATCH] add option to not create fresh APIs for every read --- src/util/types.ts | 13 ++++++-- src/xrpIO/xrpl-binding.ts | 65 ++++++++++++++++++++++----------------- test/primitives.ts | 2 -- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/util/types.ts b/src/util/types.ts index a447ad6..487ef7a 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -14,6 +14,13 @@ export type PublicKey = string export type Amount = number export type TxHash = string export type Options = { - debug: boolean, - connectionTimeout: number -} \ No newline at end of file + debug?: boolean + connectionTimeout?: number + readFreshApi?: boolean +} + +export const defaultOptions = { + debug: false, + connectionTimeout: 100000, + readFreshApi: true + } \ No newline at end of file diff --git a/src/xrpIO/xrpl-binding.ts b/src/xrpIO/xrpl-binding.ts index f275d5a..1d20842 100644 --- a/src/xrpIO/xrpl-binding.ts +++ b/src/xrpIO/xrpl-binding.ts @@ -1,4 +1,4 @@ -import { Memo, Options } from '../util/types' +import { defaultOptions, Memo, Options } from '../util/types' import { Client, Payment, TxResponse, Wallet } from 'xrpl' import * as zlib from 'zlib' @@ -18,11 +18,13 @@ export class xrpIO { constructor( private server: string, - private options: Options = { - debug: false, - connectionTimeout: 100000 - } + private options: Options = defaultOptions ) { + + this.options.debug = this.options.debug ? Boolean(this.options.debug) : defaultOptions.debug + this.options.connectionTimeout = this.options.connectionTimeout ? Number(this.options.connectionTimeout) : defaultOptions.connectionTimeout + this.options.readFreshApi = this.options.readFreshApi ? Boolean(this.options.readFreshApi) : defaultOptions.readFreshApi + this.api = new Client(server, { connectionTimeout: this.options.connectionTimeout }) @@ -34,9 +36,9 @@ export class xrpIO { } public async disconnect(): Promise { - try{ + try { await this.api.disconnect() - }catch(e){ + } catch (e) { console.log("DISCONNECT ERROR", e) } } @@ -46,11 +48,11 @@ export class xrpIO { connectionTimeout: this.options.connectionTimeout }) - while(!_api.isConnected()){ - try{ + while (!_api.isConnected()) { + try { await _api.connect() return _api - }catch(e){ + } catch (e) { this.dbg('CLONEAPI ERR', 'Connection failed', String(e['message'])) await _api.disconnect() _api = new Client(this.server, { @@ -101,21 +103,28 @@ export class xrpIO { private async getTransaction(hash: string): Promise { this.dbg("Getting Tx", hash) - let _api = await this.cloneApi() - while(true){ - try{ - const response = await _api.request({ - command: 'tx', - transaction: hash, - }) - await _api.disconnect() - return response - }catch(e){ - this.dbg("Retrying to get", hash) - await _api.disconnect() - _api = await this.cloneApi() + if (this.options.readFreshApi) { + let _api = await this.cloneApi() + while (true) { + try { + const response = await _api.request({ + command: 'tx', + transaction: hash, + }) + await _api.disconnect() + return response + } catch (e) { + this.dbg("Retrying to get", hash) + await _api.disconnect() + _api = await this.cloneApi() + } } + }else{ + return await this.api.request({ + command: 'tx', + transaction: hash, + }) } } @@ -136,7 +145,7 @@ export class xrpIO { data = await compressB64(data) const chunks = chunkString(data, PAYLOAD_SIZE) const latestSequence = await this.getAccountSequence(wallet.address) - const hashes = await Promise.all(Object.entries(chunks).map(([i, chunk]) => this.writeRaw({ data: chunk, format: format }, to, secret, latestSequence+Number(i)))) + const hashes = await Promise.all(Object.entries(chunks).map(([i, chunk]) => this.writeRaw({ data: chunk, format: format }, to, secret, latestSequence + Number(i)))) if (hashes.length === 1) { return hashes[0] @@ -145,18 +154,18 @@ export class xrpIO { return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N') } - public async treeRead(hashes: string[]): Promise{ + public async treeRead(hashes: string[]): Promise { const memos = await Promise.all(hashes.map(hash => this.readRaw(hash))) const payload: string = await decompressB64(memos.map(memo => memo.data).join('')) - + if (memos.some(memo => memo.format === 'N')) { return await this.treeRead(JSON.parse(payload)) } - + return payload } - public async getAccountSequence(address: string): Promise{ + public async getAccountSequence(address: string): Promise { this.dbg("Getting acc info for", address) const accountInfo = await this.api.request({ command: 'account_info', diff --git a/test/primitives.ts b/test/primitives.ts index 07c45cd..372837a 100644 --- a/test/primitives.ts +++ b/test/primitives.ts @@ -9,8 +9,6 @@ let sendWallet: Wallet let receiveWallet: Wallet let api: xrpIO -const ONLY_LARGE_TESTS = true - describe('XRPIO', () => { before(async function(){ this.timeout(15000)