-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathretry.js
139 lines (127 loc) · 3.74 KB
/
retry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* eslint no-await-in-loop: off */
const publisher = require('./util/gcloudPub');
const config = require('./config/config');
const {
STATUS,
getTransactionStatus: getWeb3TxStatus,
resendTransaction: resendWeb3Tx,
} = require('./util/web3');
const {
getTransactionStatus: getCosmosTxStatus,
resendTransaction: resendCosmosTx,
} = require('./util/cosmos');
const { getTxAmountForLog } = require('./util/misc');
const PUBSUB_TOPIC_MISC = 'misc';
const TX_LOOP_INTERVAL = config.TX_LOOP_INTERVAL || 90 * 1000; // fallback: 90s
const RETRY_NOT_FOUND_INTERVAL = config.RETRY_NOT_FOUND_INTERVAL || 30 * 1000; // fallback: 30s
const NOT_FOUND_COUNT_BEFORE_RETRY = config.NOT_FOUND_COUNT_BEFORE_RETRY || 3;
const TIME_BEFORE_FIRST_ENQUEUE = config.TIME_BEFORE_FIRST_ENQUEUE || 60 * 1000; // fallback: 60s
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
class RetryTxMonitor {
constructor(doc, rateLimiter) {
this.txHash = doc.id;
this.data = doc.data();
this.ts = Number.parseInt(this.data.ts, 10) || Date.now();
this.rateLimiter = rateLimiter;
this.shouldStop = false;
}
logRetry(known) {
const logType = known ? 'eventRetryKnown' : 'eventRetry';
const {
fromId,
from,
toId,
to,
nonce,
type,
} = this.data;
const {
likeAmount,
likeAmountUnitStr,
ETHAmount,
ETHAmountUnitStr,
} = getTxAmountForLog(this.data);
publisher.publish(PUBSUB_TOPIC_MISC, {
logType,
txHash: this.txHash,
txNonce: nonce,
txType: type,
fromUser: fromId,
fromWallet: from,
toUser: toId,
toWallet: to,
likeAmount,
likeAmountUnitStr,
ETHAmount,
ETHAmountUnitStr,
});
}
async getTransactionStatus() {
if (this.data.type.includes('cosmos')) {
return getCosmosTxStatus(this.txHash);
}
return getWeb3TxStatus(this.txHash, { requireReceipt: true });
}
async resendTransaction() {
if (this.data.type.includes('cosmos')) {
return resendCosmosTx(this.data.rawSignedTx, this.txHash);
}
return resendWeb3Tx(this.data.rawSignedTx, this.txHash);
}
async startLoop() {
try {
const startDelay = (this.ts + TIME_BEFORE_FIRST_ENQUEUE) - Date.now();
if (startDelay > 0) {
await sleep(startDelay);
}
let finished = false;
let count = 0;
while (!this.shouldStop) {
const { status } = await this.rateLimiter.schedule(() => this.getTransactionStatus());
this.status = status;
let nextLoopDelay = TX_LOOP_INTERVAL;
switch (this.status) {
case STATUS.CONFIRMED:
finished = true;
break;
case STATUS.MINED:
case STATUS.PENDING:
count = 0;
break;
case STATUS.NOT_FOUND:
nextLoopDelay = RETRY_NOT_FOUND_INTERVAL;
count += 1;
if (count >= NOT_FOUND_COUNT_BEFORE_RETRY) {
try {
const known = await this.resendTransaction();
if (known) {
count = 0;
nextLoopDelay = TX_LOOP_INTERVAL;
}
this.logRetry(known);
} catch (err) {
console.error(this.txHash, 'Error when resending tx:', err); // eslint-disable-line no-console
}
}
break;
default:
}
if (finished) {
break;
}
await sleep(nextLoopDelay);
}
} catch (err) {
console.error(this.txHash, 'Error in RetryTxMonitor loop:', err); // eslint-disable-line no-console
}
if (this.onFinish) {
this.onFinish(this);
}
}
stop() {
this.shouldStop = true;
}
}
module.exports = RetryTxMonitor;