Browse Source

add option to not create fresh APIs for every read

master
nitowa 2 years ago
parent
commit
a9d3549d3e
3 changed files with 47 additions and 33 deletions
  1. 10
    3
      src/util/types.ts
  2. 37
    28
      src/xrpIO/xrpl-binding.ts
  3. 0
    2
      test/primitives.ts

+ 10
- 3
src/util/types.ts View File

14
 export type Amount = number
14
 export type Amount = number
15
 export type TxHash = string
15
 export type TxHash = string
16
 export type Options = {
16
 export type Options = {
17
-    debug: boolean,
18
-    connectionTimeout: number
19
-}
17
+    debug?: boolean
18
+    connectionTimeout?: number
19
+    readFreshApi?: boolean
20
+}
21
+
22
+export const defaultOptions = {
23
+    debug: false,
24
+    connectionTimeout: 100000,
25
+    readFreshApi: true
26
+  }

+ 37
- 28
src/xrpIO/xrpl-binding.ts View File

1
-import { Memo, Options } from '../util/types'
1
+import { defaultOptions, Memo, Options } from '../util/types'
2
 import { Client, Payment, TxResponse, Wallet } from 'xrpl'
2
 import { Client, Payment, TxResponse, Wallet } from 'xrpl'
3
 
3
 
4
 import * as zlib from 'zlib'
4
 import * as zlib from 'zlib'
18
 
18
 
19
   constructor(
19
   constructor(
20
     private server: string,
20
     private server: string,
21
-    private options: Options = {
22
-      debug: false,
23
-      connectionTimeout: 100000
24
-    }
21
+    private options: Options = defaultOptions
25
   ) {
22
   ) {
23
+
24
+    this.options.debug = this.options.debug ? Boolean(this.options.debug) : defaultOptions.debug
25
+    this.options.connectionTimeout = this.options.connectionTimeout ? Number(this.options.connectionTimeout) : defaultOptions.connectionTimeout
26
+    this.options.readFreshApi = this.options.readFreshApi ? Boolean(this.options.readFreshApi) : defaultOptions.readFreshApi
27
+
26
     this.api = new Client(server, {
28
     this.api = new Client(server, {
27
       connectionTimeout: this.options.connectionTimeout
29
       connectionTimeout: this.options.connectionTimeout
28
     })
30
     })
34
   }
36
   }
35
 
37
 
36
   public async disconnect(): Promise<void> {
38
   public async disconnect(): Promise<void> {
37
-    try{
39
+    try {
38
       await this.api.disconnect()
40
       await this.api.disconnect()
39
-    }catch(e){
41
+    } catch (e) {
40
       console.log("DISCONNECT ERROR", e)
42
       console.log("DISCONNECT ERROR", e)
41
     }
43
     }
42
   }
44
   }
46
       connectionTimeout: this.options.connectionTimeout
48
       connectionTimeout: this.options.connectionTimeout
47
     })
49
     })
48
 
50
 
49
-    while(!_api.isConnected()){
50
-      try{
51
+    while (!_api.isConnected()) {
52
+      try {
51
         await _api.connect()
53
         await _api.connect()
52
         return _api
54
         return _api
53
-      }catch(e){
55
+      } catch (e) {
54
         this.dbg('CLONEAPI ERR', 'Connection failed', String(e['message']))
56
         this.dbg('CLONEAPI ERR', 'Connection failed', String(e['message']))
55
         await _api.disconnect()
57
         await _api.disconnect()
56
         _api = new Client(this.server, {
58
         _api = new Client(this.server, {
101
 
103
 
102
   private async getTransaction(hash: string): Promise<TxResponse> {
104
   private async getTransaction(hash: string): Promise<TxResponse> {
103
     this.dbg("Getting Tx", hash)
105
     this.dbg("Getting Tx", hash)
104
-    let _api = await this.cloneApi()
105
 
106
 
106
-    while(true){
107
-      try{
108
-        const response = await _api.request({
109
-          command: 'tx',
110
-          transaction: hash,
111
-        })
112
-        await _api.disconnect()
113
-        return response
114
-      }catch(e){
115
-        this.dbg("Retrying to get", hash)
116
-        await _api.disconnect()
117
-        _api = await this.cloneApi()
107
+    if (this.options.readFreshApi) {
108
+      let _api = await this.cloneApi()
109
+      while (true) {
110
+        try {
111
+          const response = await _api.request({
112
+            command: 'tx',
113
+            transaction: hash,
114
+          })
115
+          await _api.disconnect()
116
+          return response
117
+        } catch (e) {
118
+          this.dbg("Retrying to get", hash)
119
+          await _api.disconnect()
120
+          _api = await this.cloneApi()
121
+        }
118
       }
122
       }
123
+    }else{
124
+      return await this.api.request({
125
+        command: 'tx',
126
+        transaction: hash,
127
+      })
119
     }
128
     }
120
   }
129
   }
121
 
130
 
136
     data = await compressB64(data)
145
     data = await compressB64(data)
137
     const chunks = chunkString(data, PAYLOAD_SIZE)
146
     const chunks = chunkString(data, PAYLOAD_SIZE)
138
     const latestSequence = await this.getAccountSequence(wallet.address)
147
     const latestSequence = await this.getAccountSequence(wallet.address)
139
-    const hashes = await Promise.all(Object.entries(chunks).map(([i, chunk]) => this.writeRaw({ data: chunk, format: format }, to, secret, latestSequence+Number(i))))
148
+    const hashes = await Promise.all(Object.entries(chunks).map(([i, chunk]) => this.writeRaw({ data: chunk, format: format }, to, secret, latestSequence + Number(i))))
140
 
149
 
141
     if (hashes.length === 1) {
150
     if (hashes.length === 1) {
142
       return hashes[0]
151
       return hashes[0]
145
     return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N')
154
     return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N')
146
   }
155
   }
147
 
156
 
148
-  public async treeRead(hashes: string[]): Promise<string>{
157
+  public async treeRead(hashes: string[]): Promise<string> {
149
     const memos = await Promise.all(hashes.map(hash => this.readRaw(hash)))
158
     const memos = await Promise.all(hashes.map(hash => this.readRaw(hash)))
150
     const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
159
     const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
151
-  
160
+
152
     if (memos.some(memo => memo.format === 'N')) {
161
     if (memos.some(memo => memo.format === 'N')) {
153
       return await this.treeRead(JSON.parse(payload))
162
       return await this.treeRead(JSON.parse(payload))
154
     }
163
     }
155
-  
164
+
156
     return payload
165
     return payload
157
   }
166
   }
158
 
167
 
159
-  public async getAccountSequence(address: string): Promise<number>{
168
+  public async getAccountSequence(address: string): Promise<number> {
160
     this.dbg("Getting acc info for", address)
169
     this.dbg("Getting acc info for", address)
161
     const accountInfo = await this.api.request({
170
     const accountInfo = await this.api.request({
162
       command: 'account_info',
171
       command: 'account_info',

+ 0
- 2
test/primitives.ts View File

9
 let receiveWallet: Wallet
9
 let receiveWallet: Wallet
10
 let api: xrpIO
10
 let api: xrpIO
11
 
11
 
12
-const ONLY_LARGE_TESTS = true
13
-
14
 describe('XRPIO', () => {
12
 describe('XRPIO', () => {
15
     before(async function(){
13
     before(async function(){
16
         this.timeout(15000)
14
         this.timeout(15000)

Loading…
Cancel
Save