瀏覽代碼

add option to not create fresh APIs for every read

master
nitowa 2 年之前
父節點
當前提交
a9d3549d3e
共有 3 個文件被更改,包括 47 次插入33 次删除
  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 查看文件

@@ -14,6 +14,13 @@ export type PublicKey = string
14 14
 export type Amount = number
15 15
 export type TxHash = string
16 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 查看文件

@@ -1,4 +1,4 @@
1
-import { Memo, Options } from '../util/types'
1
+import { defaultOptions, Memo, Options } from '../util/types'
2 2
 import { Client, Payment, TxResponse, Wallet } from 'xrpl'
3 3
 
4 4
 import * as zlib from 'zlib'
@@ -18,11 +18,13 @@ export class xrpIO {
18 18
 
19 19
   constructor(
20 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 28
     this.api = new Client(server, {
27 29
       connectionTimeout: this.options.connectionTimeout
28 30
     })
@@ -34,9 +36,9 @@ export class xrpIO {
34 36
   }
35 37
 
36 38
   public async disconnect(): Promise<void> {
37
-    try{
39
+    try {
38 40
       await this.api.disconnect()
39
-    }catch(e){
41
+    } catch (e) {
40 42
       console.log("DISCONNECT ERROR", e)
41 43
     }
42 44
   }
@@ -46,11 +48,11 @@ export class xrpIO {
46 48
       connectionTimeout: this.options.connectionTimeout
47 49
     })
48 50
 
49
-    while(!_api.isConnected()){
50
-      try{
51
+    while (!_api.isConnected()) {
52
+      try {
51 53
         await _api.connect()
52 54
         return _api
53
-      }catch(e){
55
+      } catch (e) {
54 56
         this.dbg('CLONEAPI ERR', 'Connection failed', String(e['message']))
55 57
         await _api.disconnect()
56 58
         _api = new Client(this.server, {
@@ -101,21 +103,28 @@ export class xrpIO {
101 103
 
102 104
   private async getTransaction(hash: string): Promise<TxResponse> {
103 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,7 +145,7 @@ export class xrpIO {
136 145
     data = await compressB64(data)
137 146
     const chunks = chunkString(data, PAYLOAD_SIZE)
138 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 150
     if (hashes.length === 1) {
142 151
       return hashes[0]
@@ -145,18 +154,18 @@ export class xrpIO {
145 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 158
     const memos = await Promise.all(hashes.map(hash => this.readRaw(hash)))
150 159
     const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
151
-  
160
+
152 161
     if (memos.some(memo => memo.format === 'N')) {
153 162
       return await this.treeRead(JSON.parse(payload))
154 163
     }
155
-  
164
+
156 165
     return payload
157 166
   }
158 167
 
159
-  public async getAccountSequence(address: string): Promise<number>{
168
+  public async getAccountSequence(address: string): Promise<number> {
160 169
     this.dbg("Getting acc info for", address)
161 170
     const accountInfo = await this.api.request({
162 171
       command: 'account_info',

+ 0
- 2
test/primitives.ts 查看文件

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

Loading…
取消
儲存