forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusinessLogicCartrade.ts
741 lines (661 loc) · 24.5 KB
/
BusinessLogicCartrade.ts
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* BusinessLogicCartrade.ts
*/
import { Request } from "express";
import { RequestInfo } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/RequestInfo";
import { TradeInfo } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/TradeInfo";
import { TransactionInfoManagement } from "./TransactionInfoManagement";
import { TransactionInfo } from "./TransactionInfo";
import { TransactionData } from "./TransactionData";
import { BusinessLogicInquireCartradeStatus } from "./BusinessLogicInquireCartradeStatus";
import { TxInfoData } from "./TxInfoData";
import { transactionManagement } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/routes/index";
import { verifierFactory } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/routes/index";
import { LedgerOperation } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/business-logic-plugin/LedgerOperation";
import { BusinessLogicBase } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/business-logic-plugin/BusinessLogicBase";
import { makeRawTransaction } from "./TransactionEthereum";
import { makeSignedProposal } from "./TransactionFabric";
import {
ApiInfo,
LedgerEvent,
} from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/LedgerPlugin";
import { json2str } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/DriverCommon";
import { CartradeStatus } from "./define";
import { RIFUtil } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/util/RIFUtil";
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
//const config: any = JSON.parse(fs.readFileSync("/etc/cactus/default.json", 'utf8'));
const config: any = yaml.safeLoad(
fs.readFileSync("/etc/cactus/default.yaml", "utf8")
);
import { getLogger } from "log4js";
const moduleName = "BusinessLogicCartrade";
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;
export class BusinessLogicCartrade extends BusinessLogicBase {
transactionInfoManagement: TransactionInfoManagement;
// useValidator: {};
constructor() {
super();
this.transactionInfoManagement = new TransactionInfoManagement();
}
startTransaction(req: Request, businessLogicID: string, tradeID: string) {
logger.debug("called startTransaction");
// set RequestInfo
const requestInfo: RequestInfo = new RequestInfo();
requestInfo.businessLogicID = businessLogicID;
requestInfo.tradeInfo.ethereumAccountFrom = req.body.tradeParams[0];
requestInfo.tradeInfo.ethereumAccountTo = req.body.tradeParams[1];
requestInfo.tradeInfo.fabricAccountFrom = req.body.tradeParams[2];
requestInfo.tradeInfo.fabricAccountTo = req.body.tradeParams[3];
requestInfo.tradeInfo.tradingValue = req.body.tradeParams[4];
requestInfo.tradeInfo.carID = req.body.tradeParams[5];
// requestInfo.authInfo.company = req.body.authParams[0];
// set TradeID
requestInfo.setTradeID(tradeID);
// Register transaction information in transaction information management
const transactionInfo: TransactionInfo = new TransactionInfo();
transactionInfo.setRequestInfo(0, requestInfo);
this.transactionInfoManagement.addTransactionInfo(transactionInfo);
// Create trade information
const tradeInfo: TradeInfo = new TradeInfo(
requestInfo.businessLogicID,
requestInfo.tradeID
);
// trade status update
this.transactionInfoManagement.setStatus(
tradeInfo,
CartradeStatus.UnderEscrow
);
// Get varidator information
// this.useValidator = JSON.parse(transactionManagement.getValidatorToUse(requestInfo.businessLogicID));
// this.dummyTransaction(requestInfo, tradeInfo);
this.firstTransaction(requestInfo, tradeInfo);
}
dummyTransaction(requestInfo: RequestInfo, tradeInfo: TradeInfo) {
logger.debug("called dummyTransaction");
let transactionData: TransactionData = new TransactionData(
"escrow",
"ledger001",
"tid001"
);
this.transactionInfoManagement.setTransactionData(
tradeInfo,
transactionData
);
this.transactionInfoManagement.setStatus(
tradeInfo,
CartradeStatus.UnderTransfer
);
transactionData = new TransactionData("transfer", "ledger002", "tid002");
this.transactionInfoManagement.setTransactionData(
tradeInfo,
transactionData
);
this.transactionInfoManagement.setStatus(
tradeInfo,
CartradeStatus.UnderSettlement
);
transactionData = new TransactionData("settlement", "ledger003", "tid003");
this.transactionInfoManagement.setTransactionData(
tradeInfo,
transactionData
);
this.transactionInfoManagement.setStatus(
tradeInfo,
CartradeStatus.Completed
);
}
firstTransaction(requestInfo: RequestInfo, tradeInfo: TradeInfo) {
logger.debug("called firstTransaction");
///// Eth Escrow
// Get Verifier Instance
logger.debug(
`##firstTransaction(): businessLogicID: ${tradeInfo.businessLogicID}`
);
const useValidator = JSON.parse(
transactionManagement.getValidatorToUse(tradeInfo.businessLogicID)
);
// const verifierEthereum = transactionManagement.getVerifier(useValidator['validatorID'][0]);
const verifierEthereum = verifierFactory.getVerifier(
useValidator["validatorID"][0],
"BusinessLogicCartrade"
);
logger.debug("getVerifierEthereum");
// TODO: get private key from
logger.debug(
`####fromAddress: ${requestInfo.tradeInfo.ethereumAccountFrom}`
);
const fromAddressPkey =
config.cartradeInfo.ethereum[
"fromAddressPkey_" + requestInfo.tradeInfo.ethereumAccountFrom
];
logger.debug(`####fromAddressPkey: ${fromAddressPkey}`);
// TODO: Get address of escrow and set parameter
const escrowAddress = config.cartradeInfo.ethereum.escrowAddress;
// Generate parameters for// sendRawTransaction
const txParam: {
fromAddress: string;
fromAddressPkey: string;
toAddress: string;
amount: number;
gas: number;
} = {
fromAddress: requestInfo.tradeInfo.ethereumAccountFrom,
fromAddressPkey: fromAddressPkey,
toAddress: escrowAddress,
amount: Number(requestInfo.tradeInfo.tradingValue),
gas: config.cartradeInfo.ethereum.gas,
};
logger.debug(`####exec makeRawTransaction!!`);
makeRawTransaction(txParam)
.then((result) => {
logger.info("firstTransaction txId : " + result.txId);
// Register transaction data in DB
const transactionData: TransactionData = new TransactionData(
"escrow",
"ledger001",
result.txId
);
this.transactionInfoManagement.setTransactionData(
tradeInfo,
transactionData
);
// Set Parameter
logger.debug("firstTransaction data : " + JSON.stringify(result.data));
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = { type: "web3Eth", command: "sendRawTransaction" };
const template = "default";
const args = { args: [result.data["serializedTx"]] };
// const method = "default";
// const args = {"method": {type: "web3Eth", command: "sendRawTransaction"},"args": {"args": [result.data["serializedTx"]]}};
// Run Verifier (Ethereum)
verifierEthereum
.sendAsyncRequest(contract, method, args)
.then(() => {
logger.debug(`##firstTransaction sendAsyncRequest finish`);
})
.catch((err) => {
logger.error(err);
});
})
.catch((err) => {
logger.error(err);
});
}
secondTransaction(
carID: string,
fabricAccountTo: string,
tradeInfo: TradeInfo
) {
logger.debug("called secondTransaction");
///// Fab Transfer
// Get Verifier Instance
logger.debug(
`##secondTransaction(): businessLogicID: ${tradeInfo.businessLogicID}`
);
const useValidator = JSON.parse(
transactionManagement.getValidatorToUse(tradeInfo.businessLogicID)
);
// const verifierFabric = transactionManagement.getVerifier(useValidator['validatorID'][1]);
const verifierFabric = verifierFactory.getVerifier(
useValidator["validatorID"][1],
"BusinessLogicCartrade"
);
logger.debug("getVerifierFabric");
// Generate parameters for sendSignedProposal(changeCarOwner)
const ccFncName = "changeCarOwner";
const ccArgs: string[] = [
carID, // CarID
fabricAccountTo, // Owner
];
makeSignedProposal(ccFncName, ccArgs, verifierFabric)
.then((result) => {
// logger.info(`##ret makeSignedProposal: result: ${JSON.stringify(result)}`);
logger.info("secondTransaction txId : " + result.txId);
// Register transaction data in DB
const transactionData: TransactionData = new TransactionData(
"transfer",
"ledger002",
result.txId
);
this.transactionInfoManagement.setTransactionData(
tradeInfo,
transactionData
);
// NOTE: Convert properties to binary.
// If you do not convert the following, you will get an error.
result.data["signedCommitProposal"].signature = Buffer.from(
result.data["signedCommitProposal"].signature
);
result.data["signedCommitProposal"].proposal_bytes = Buffer.from(
result.data["signedCommitProposal"].proposal_bytes
);
// Set Parameter
//logger.debug('secondTransaction data : ' + JSON.stringify(result.data));
const contract = { channelName: "mychannel" };
const method = { type: "sendSignedTransaction" };
const template = "default";
const args = { args: [result.data] };
// Run Verifier (Fabric)
verifierFabric
.sendAsyncRequest(contract, method, args)
.then(() => {
logger.debug(`##secondTransaction sendAsyncRequest finish`);
})
.catch((err) => {
logger.error(err);
});
})
.catch((err) => {
logger.error(err);
});
}
thirdTransaction(
ethereumAccountTo: string,
tradingValue: string,
tradeInfo: TradeInfo
) {
logger.debug("called thirdTransaction");
// Get Verifier Instance
logger.debug(
`##thirdTransaction(): businessLogicID: ${tradeInfo.businessLogicID}`
);
const useValidator = JSON.parse(
transactionManagement.getValidatorToUse(tradeInfo.businessLogicID)
);
// const verifierEthereum = transactionManagement.getVerifier(useValidator['validatorID'][0]);
const verifierEthereum = verifierFactory.getVerifier(
useValidator["validatorID"][0],
"BusinessLogicCartrade"
);
logger.debug("getVerifierEthereum");
// TODO: Get address of escrow and set parameter
const escrowAddress = config.cartradeInfo.ethereum.escrowAddress;
// TODO: get escrow secret key
const escrowAddressPkey = config.cartradeInfo.ethereum.escrowAddressPkey;
// Generate parameters for sendRawTransaction
const txParam: {
fromAddress: string;
fromAddressPkey: string;
toAddress: string;
amount: number;
gas: number;
} = {
fromAddress: escrowAddress,
fromAddressPkey: escrowAddressPkey,
toAddress: ethereumAccountTo,
amount: Number(tradingValue),
gas: config.cartradeInfo.ethereum.gas,
};
makeRawTransaction(txParam)
.then((result) => {
logger.info("thirdTransaction txId : " + result.txId);
// Register transaction data in DB
const transactionData: TransactionData = new TransactionData(
"settlement",
"ledger003",
result.txId
);
this.transactionInfoManagement.setTransactionData(
tradeInfo,
transactionData
);
// Set LedgerOperation
logger.debug("thirdTransaction data : " + JSON.stringify(result.data));
const ledgerOperation: LedgerOperation = new LedgerOperation(
"sendRawTransaction",
"",
result.data
);
// Run Verifier (Ethereum)
// verifierEthereum.requestLedgerOperation(ledgerOperation);
// TODO: Neo!!
// Set Parameter
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = { type: "web3Eth", command: "sendRawTransaction" };
const template = "default";
const args = { args: [result.data["serializedTx"]] };
// const method = "default";
// const args = {"method": {type: "web3Eth", command: "sendRawTransaction"}, "args": {"args": [result.data["serializedTx"]]}};
// Run Verifier (Ethereum)
verifierEthereum
.sendAsyncRequest(contract, method, args)
.then(() => {
logger.debug(`##thirdTransaction sendAsyncRequest finish`);
})
.catch((err) => {
logger.error(err);
});
})
.catch((err) => {
logger.error(err);
});
}
completedTransaction(tradeInfo: TradeInfo) {
logger.debug("called completedTransaction");
logger.debug(
`##completedTransaction(): businessLogicID: ${tradeInfo.businessLogicID}`
);
const useValidator = JSON.parse(
transactionManagement.getValidatorToUse(tradeInfo.businessLogicID)
);
const validatorId = useValidator["validatorID"][0];
}
finish() {
logger.debug("called finish");
}
onEvent(ledgerEvent: LedgerEvent, targetIndex: number): void {
logger.debug(`##in BLP:onEvent()`);
logger.debug(`##onEvent(): ${json2str(ledgerEvent)}`);
switch (ledgerEvent.verifierId) {
case config.cartradeInfo.ethereum.validatorID:
this.onEvenEtherem(ledgerEvent.data, targetIndex);
break;
case config.cartradeInfo.fabric.validatorID:
this.onEvenFabric(ledgerEvent.data, targetIndex);
break;
default:
logger.error(
`##onEvent(), invalid verifierId: ${ledgerEvent.verifierId}`
);
return;
}
}
onEvenEtherem(event: object, targetIndex: number): void {
logger.debug(`##in onEvenEtherem()`);
const tx = this.getTransactionFromEthereumEvent(event, targetIndex);
if (tx == null) {
logger.error(`##onEvenEtherem(): invalid event: ${json2str(event)}`);
return;
}
try {
const txId = tx["hash"];
const status = event["status"];
logger.debug(`##txId = ${txId}`);
logger.debug(`##status =${status}`);
if (status !== 200) {
logger.error(
`##onEvenEtherem(): error event, status: ${status}, txId: ${txId}`
);
return;
}
// Perform the following transaction actions
this.executeNextTransaction(tx, txId);
} catch (err) {
logger.error(`##onEvenEtherem(): err: ${err}, event: ${json2str(event)}`);
}
}
getTransactionFromEthereumEvent(
event: object,
targetIndex: number
): object | null {
try {
const retTransaction = event["blockData"]["transactions"][targetIndex];
logger.debug(
`##getTransactionFromEthereumEvent(), retTransaction: ${retTransaction}`
);
return retTransaction;
} catch (err) {
logger.error(
`##getTransactionFromEthereumEvent(): invalid even, err:${err}, event:${event}`
);
}
}
onEvenFabric(event: object, targetIndex: number): void {
logger.debug(`##in onEvenFabric()`);
const tx = this.getTransactionFromFabricEvent(event, targetIndex);
if (tx == null) {
logger.warn(`##onEvenFabric(): invalid event: ${json2str(event)}`);
return;
}
try {
const txId = tx["txId"];
const status = event["status"];
logger.debug(`##txId = ${txId}`);
logger.debug(`##status =${status}`);
if (status !== 200) {
logger.error(
`##onEvenFabric(): error event, status: ${status}, txId: ${txId}`
);
return;
}
// Perform the following transaction actions
this.executeNextTransaction(tx, txId);
} catch (err) {
logger.error(
`##onEvenFabric(): onEvent, err: ${err}, event: ${json2str(event)}`
);
}
}
getTransactionFromFabricEvent(event: object, targetIndex): object | null {
try {
const retTransaction = event["blockData"][targetIndex];
logger.debug(
`##getTransactionFromFabricEvent(): retTransaction: ${retTransaction}`
);
return retTransaction;
} catch (err) {
logger.error(
`##getTransactionFromFabricEvent(): invalid even, err:${err}, event:${event}`
);
}
}
executeNextTransaction(txInfo: object, txId: string): void {
let transactionInfo: TransactionInfo = null;
try {
// Retrieve DB transaction information
transactionInfo =
this.transactionInfoManagement.getTransactionInfoByTxId(txId);
if (transactionInfo != null) {
logger.debug(
`##onEvent(A), transactionInfo: ${json2str(transactionInfo)}`
);
} else {
logger.warn(`##onEvent(B), not found transactionInfo, txId: ${txId}`);
return;
}
const txStatus = transactionInfo.status;
const tradeInfo =
this.createTradeInfoFromTransactionInfo(transactionInfo);
let txInfoData: TxInfoData;
switch (txStatus) {
case CartradeStatus.UnderEscrow:
// store transaction information in DB
txInfoData = new TxInfoData("escrow", json2str(txInfo));
this.transactionInfoManagement.setTxInfo(tradeInfo, txInfoData);
// underEscrow -> underTransfer
logger.info(
`##INFO: underEscrow -> underTransfer, businessLogicID: ${transactionInfo.businessLogicID}, tradeID: ${transactionInfo.tradeID}`
);
// const tradeInfo = this.createTradeInfoFromTransactionInfo(transactionInfo);
this.transactionInfoManagement.setStatus(
tradeInfo,
CartradeStatus.UnderTransfer
);
this.secondTransaction(
transactionInfo.carID,
transactionInfo.fabricAccountTo,
tradeInfo
);
break;
case CartradeStatus.UnderTransfer:
// store transaction information in DB
txInfoData = new TxInfoData("transfer", json2str(txInfo));
this.transactionInfoManagement.setTxInfo(tradeInfo, txInfoData);
// underTransfer -> underSettlement
logger.info(
`##INFO: underTransfer -> underSettlement, businessLogicID: ${transactionInfo.businessLogicID}, tradeID: ${transactionInfo.tradeID}`
);
// const tradeInfo = this.createTradeInfoFromTransactionInfo(transactionInfo);
this.transactionInfoManagement.setStatus(
tradeInfo,
CartradeStatus.UnderSettlement
);
this.thirdTransaction(
transactionInfo.ethereumAccountTo,
transactionInfo.tradingValue,
tradeInfo
);
break;
case CartradeStatus.UnderSettlement:
// store transaction information in DB
txInfoData = new TxInfoData("settlement", json2str(txInfo));
this.transactionInfoManagement.setTxInfo(tradeInfo, txInfoData);
// underSettlement -> completed
// const tradeInfo = this.createTradeInfoFromTransactionInfo(transactionInfo);
this.transactionInfoManagement.setStatus(
tradeInfo,
CartradeStatus.Completed
);
logger.info(
`##INFO: completed cartrade, businessLogicID: ${transactionInfo.businessLogicID}, tradeID: ${transactionInfo.tradeID}`
);
this.completedTransaction(tradeInfo);
break;
case CartradeStatus.Completed:
logger.warn(
`##WARN: already completed, txinfo: ${json2str(transactionInfo)}`
);
return;
default:
logger.error(`##ERR: bad txStatus: ${txStatus}`);
return;
}
} catch (err) {
logger.error(
`##ERR: executeNextTransaction(), err: ${err}, tx: ${json2str(
transactionInfo
)}`
);
}
}
createTradeInfoFromTransactionInfo(txInfo: TransactionInfo): TradeInfo {
try {
return new TradeInfo(txInfo.businessLogicID, txInfo.tradeID);
} catch (err) {
logger.error(`##ERR: createTradeInfoFromTransactionInfo(), ${err}`);
throw err;
}
}
getOperationStatus(tradeID: string): object {
logger.debug(`##in getOperationStatus()`);
const businessLogicInquireCartradeStatus: BusinessLogicInquireCartradeStatus =
new BusinessLogicInquireCartradeStatus();
const transactionStatusData =
businessLogicInquireCartradeStatus.getCartradeOperationStatus(tradeID);
return transactionStatusData;
}
getTxIDFromEvent(
ledgerEvent: LedgerEvent,
targetIndex: number
): string | null {
logger.debug(`##in getTxIDFromEvent`);
logger.debug(`##event: ${json2str(ledgerEvent)}`);
switch (ledgerEvent.verifierId) {
case config.cartradeInfo.ethereum.validatorID:
return this.getTxIDFromEventEtherem(ledgerEvent.data, targetIndex);
case config.cartradeInfo.fabric.validatorID:
return this.getTxIDFromEventFabric(ledgerEvent.data, targetIndex);
default:
logger.error(
`##getTxIDFromEvent(): invalid verifierId: ${ledgerEvent.verifierId}`
);
}
return null;
}
getTxIDFromEventEtherem(event: object, targetIndex: number): string | null {
logger.debug(`##in getTxIDFromEventEtherem()`);
const tx = this.getTransactionFromEthereumEvent(event, targetIndex);
if (tx == null) {
logger.warn(`#getTxIDFromEventEtherem(): skip(not found tx)`);
return null;
}
try {
const txId = tx["hash"];
if (typeof txId !== "string") {
logger.warn(
`#getTxIDFromEventEtherem(): skip(invalid block, not found txId.), event: ${json2str(
event
)}`
);
return null;
}
logger.debug(`###getTxIDFromEventEtherem(): txId: ${txId}`);
return txId;
} catch (err) {
logger.error(
`##getTxIDFromEventEtherem(): err: ${err}, event: ${json2str(event)}`
);
return null;
}
}
getTxIDFromEventFabric(event: object, targetIndex: number): string | null {
logger.debug(`##in getTxIDFromEventFabric()`);
const tx = this.getTransactionFromFabricEvent(event, targetIndex);
if (tx == null) {
logger.warn(`#getTxIDFromEventFabric(): skip(not found tx)`);
return null;
}
try {
const txId = tx["txId"];
if (typeof txId !== "string") {
logger.warn(
`#getTxIDFromEventFabric(): skip(invalid block, not found txId.), event: ${json2str(
event
)}`
);
return null;
}
logger.debug(`###getTxIDFromEventFabric(): txId: ${txId}`);
return txId;
} catch (err) {
logger.error(
`##getTxIDFromEventFabric(): err: ${err}, event: ${json2str(event)}`
);
return null;
}
}
hasTxIDInTransactions(txID: string): boolean {
logger.debug(`##in hasTxIDInTransactions(), txID: ${txID}`);
const transactionInfo =
this.transactionInfoManagement.getTransactionInfoByTxId(txID);
logger.debug(`##hasTxIDInTransactions(), ret: ${transactionInfo !== null}`);
return transactionInfo !== null;
}
getEventDataNum(ledgerEvent: LedgerEvent): number {
logger.debug(
`##in BLP:getEventDataNum(), ledgerEvent.verifierId: ${ledgerEvent.verifierId}`
);
const event = ledgerEvent.data;
let retEventNum = 0;
try {
switch (ledgerEvent.verifierId) {
case config.cartradeInfo.ethereum.validatorID:
retEventNum = event["blockData"]["transactions"].length;
break;
case config.cartradeInfo.fabric.validatorID:
retEventNum = event["blockData"].length;
break;
default:
logger.error(
`##getEventDataNum(): invalid verifierId: ${ledgerEvent.verifierId}`
);
break;
}
logger.debug(
`##getEventDataNum(): retEventNum: ${retEventNum}, verifierId: ${ledgerEvent.verifierId}`
);
return retEventNum;
} catch (err) {
logger.error(
`##getEventDataNum(): invalid even, err: ${err}, event: ${event}`
);
return 0;
}
}
}