-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmonitor.js
executable file
·343 lines (315 loc) · 12.3 KB
/
monitor.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'use strict'
const pu = require('promisefy-util');
let config;
const be = require('./ccUtil.js').Backend;
//let databaseGroup = require('./wanchaindb/index.js').databaseGroup;
let backendConfig = {};
let logger;
let handlingList = {};
const MonitorRecord = {
async init(cfg, ethSender, wanSender){
config = cfg? cfg:require('./config.js');
logger = config.getLogger("monitorRecord");
backendConfig.ethGroupAddr = config.originalChainHtlc;
backendConfig.wethGroupAddr = config.wanchainHtlcAddr;
this.ethSender = ethSender;
this.wanSender = wanSender;
handlingList = {};
},
getSenderbyChain(chainType){
return chainType == "ETH"? this.ethSender : this.wanSender;
},
getTxReceipt(sender,txhash){
let bs = pu.promisefy(sender.sendMessage, ['getTransactionReceipt',txhash], sender);
return bs;
},
monitorTxConfirm(sender, txhash, waitBlocks) {
let p = pu.promisefy(sender.sendMessage, ['getTransactionConfirm', txhash, waitBlocks], sender);
return p;
},
monitorTask(){
let collection = be.getCrossdbCollection();
let history = collection.find({ 'status' : { '$nin' : ['refundFinished','revokeFinished','sentHashFailed'] } });
//logger.debug(history);
let self = this;
logger.debug('handlingList length is ', Object.keys(handlingList).length);
for(let i=0; i<history.length; i++){
let record = history[i];
let cur = Date.now();
if( handlingList[record.HashX]) {
if(handlingList[record.HashX]+300000 < cur){
delete handlingList[record.HashX];
}else{
continue;
}
}
handlingList[record.HashX] = cur;
try{
self.monitorRecord(record);
}catch(error){
logger.error("monitorRecord error:", error);
}
}
},
async checkOriginLockOnline(record){
try {
let sender;
let receipt;
if(record.chain == "ETH"){
sender = this.getSenderbyChain("ETH");
receipt = await be.getDepositOrigenLockEvent(sender,record.HashX);
}else {
sender = this.getSenderbyChain("WAN");
receipt = await be.getWithdrawOrigenLockEvent(sender,record.HashX);
}
if(receipt && receipt.length>0){
record.status = 'sentHashConfirming';
this.updateRecord(record );
}
}catch(err){
logger.error("checkOriginLockOnline:", err);
}
},
async checkXOnline(record){
try {
let sender;
let receipt;
if(record.chain == "ETH"){
sender = this.getSenderbyChain("WAN");
receipt = await be.getDepositOriginRefundEvent(sender,record.HashX);
} else {
sender = this.getSenderbyChain("ETH");
receipt = await be.getWithdrawOriginRefundEvent(sender,record.HashX);
}
if(receipt && receipt.length>0){
record.status = 'sentXConfirming';
this.updateRecord(record );
}
}catch(err){
console.log("checkTxOnline:", err);
}
},
async checkRevokeOnline(chain, record){
try {
let sender;
let receipt;
if(record.chain == "ETH"){
sender = this.getSenderbyChain("ETH");
receipt = await be.getDepositRevokeEvent(sender,record.HashX);
}else {
sender = this.getSenderbyChain("WAN");
receipt = await be.getWithdrawRevokeEvent(sender,record.HashX);
}
if(receipt && receipt.length>0){
record.status = 'sentRevokeConfirming';
this.updateRecord(record );
}
}catch(err){
console.log("checkRevokeOnline:", err);
}
},
async checkHashConfirm(record, waitBlocks){
try {
let sender = this.getSenderbyChain(record.chain);
let receipt = await this.monitorTxConfirm(sender, record.lockTxHash, waitBlocks);
if(receipt){
record.lockConfirmed += 1;
if(record.lockConfirmed >= config.confirmBlocks){
record.status = 'waitingCross';
}
this.updateRecord(record);
}
}catch(err){
console.log("checkHashConfirm:", err);
}
},
async checkHashReceiptOnline(record){
try {
let sender = this.getSenderbyChain(record.chain);
let receipt = await this.monitorTxConfirm(sender, record.lockTxHash, 0);
if(receipt){
if(receipt.status === '0x1'){
record.status = 'sentHashConfirming';
// update the time to block time.
let block = await be.getBlockByNumber(sender, receipt.blockNumber)
let newtime = Number(block.timestamp)*1000;
record.time = newtime.toString();
record.suspendTime = (1000*Number(global.lockedTime)+newtime).toString();
record.HTLCtime = (100000+2*1000*Number(global.lockedTime)+newtime).toString();
}else{
record.status = 'sentHashFailed';
}
this.updateRecord(record);
}
}catch(err){
//console.log("checkHashReceiptOnline:", err);
}
},
async checkXConfirm(record, waitBlocks){
try {
let chain = record.chain=='ETH'?"WAN":"ETH";
let sender = this.getSenderbyChain(chain);
let receipt = await this.monitorTxConfirm(sender, record.refundTxHash, waitBlocks);
if(receipt){
record.refundConfirmed += 1;
if(record.refundConfirmed >= config.confirmBlocks){
record.status = 'refundFinished';
}
this.updateRecord(record);
}
}catch(err){
console.log("checkXConfirm:", err);
}
},
async checkRevokeConfirm(chain, record, waitBlocks){
try {
let sender = this.getSenderbyChain(chain);
let receipt = await this.monitorTxConfirm(sender, record.revokeTxHash, waitBlocks);
if(receipt){
record.revokeConfirmed += 1;
if(record.revokeConfirmed >= config.confirmBlocks){
record.status = 'revokeFinished';
}
this.updateRecord(record);
}
}catch(err){
console.log("checkRevokeConfirm:", err);
}
},
async checkCrossHashConfirm(record, waitBlocks){
try {
let chain = record.chain=='ETH'?"WAN":"ETH";
let sender = this.getSenderbyChain(chain);
let receipt = await this.monitorTxConfirm(sender, record.crossLockHash, waitBlocks);
if(receipt){
if(!record.crossConfirmed) record.crossConfirmed = 0;
record.crossConfirmed += 1;
if(record.crossConfirmed >= config.confirmBlocks){
record.status = 'waitingX';
this.updateRecord(record);
}
}
}catch(err){
console.log("checkCrossHashConfirm:", err);
}
},
async checkHashTimeout( record){
if(record.status === "sentHashFailed") {
return false;
}
if(record.status == "waitingRevoke,"
|| record.status =="sentRevokePending"
|| record.status =="sentRevokeConfirming"){
return true;
}
try {
let HTLCtime = Number(record.HTLCtime);
let suspendTime = Number(record.suspendTime);
if(HTLCtime <= Date.now()){
record.status = 'waitingRevoke';
this.updateRecord(record);
return true;
}else if(suspendTime <= Date.now()){
record.status = 'suspending';
this.updateRecord(record);
return true;
}
}catch(err){
console.log("checkHashTimeout:", err);
}
return false;
},
async checkCrossHashOnline(record){
try {
let receipt;
let sender
if(record.chain=="ETH"){
sender = this.getSenderbyChain("WAN");
receipt = await be.getDepositCrossLockEvent(sender,record.HashX);
}else {
sender = this.getSenderbyChain("ETH");
receipt = await be.getWithdrawCrossLockEvent(sender,record.HashX);
}
if(receipt && receipt.length>0){
record.crossConfirmed = 1;
record.crossLockHash = receipt[0].transactionHash;
record.status = 'waitingCrossConfirming';
this.updateRecord(record);
// console.log("######waitingCross done ");
}
}catch(err){
console.log("checkCrossHashOnline:", err);
}
},
updateRecord(record){
let collection = be.getCrossdbCollection();
collection.update(record);
},
async monitorRecord(record){
let waitBlock = config.confirmBlocks;
let chain = record.chain;
if(this.checkHashTimeout(record) == true){
console.log("tx timeout: ", record);
}
//logger.debug("record status is ", record.status);
switch(record.status) {
case 'sentHashPending':
//await this.checkOriginLockOnline(record);
await this.checkHashReceiptOnline(record);
break;
case 'sentHashConfirming':
waitBlock = record.lockConfirmed < config.confirmBlocks ? record.lockConfirmed: config.confirmBlocks;
await this.checkHashConfirm(record, waitBlock);
break;
case 'waitingCross':
await this.checkCrossHashOnline(record);
break;
case 'waitingCrossConfirming':
if(record.refundTxHash){
record.status = 'sentXPending';
this.updateRecord(record);
break;
}
await this.checkCrossHashConfirm( record, config.confirmBlocks);
break;
case 'waitingX':
if(record.refundTxHash){
record.status = 'sentXPending';
this.updateRecord(record);
}
break;
case 'suspending':
// do nothing.
break;
case 'waitingRevoke':
if(record.revokeTxHash){
record.status = 'sentRevokePending';
this.updateRecord(record);
}
break;
case 'sentRevokePending':
await this.checkRevokeOnline(record.chain, record);
break;
case 'sentRevokeConfirming':
waitBlock = record.lockConfirmed < config.confirmBlocks ? record.lockConfirmed: config.confirmBlocks;
await this.checkRevokeConfirm(record.chain, record, waitBlock);
break;
case 'sentXPending':
await this.checkXOnline(record);
break;
case 'sentXConfirming':
waitBlock = record.refundConfirmed < config.confirmBlocks ? record.refundConfirmed: config.confirmBlocks;
await this.checkXConfirm(record, waitBlock);
break;
case 'refundFinished':
case 'revokeFinished':
break;
default:
break;
}
if( handlingList[record.HashX]) {
delete handlingList[record.HashX];
}
},
}
exports.MonitorRecord = MonitorRecord;