Skip to content

Commit 8e2d8db

Browse files
outSHpetermetz
authored andcommitted
refactor(connector-go-ethereum-socketio): fix strict flag warnings
cactus-plugin-ledger-connector-go-ethereum-socketio will compile with global strict flag. Related issue: hyperledger-cacti#1671 Signed-off-by: Michal Bajer <[email protected]>
1 parent 4e9d9a1 commit 8e2d8db

File tree

5 files changed

+56
-56
lines changed

5 files changed

+56
-56
lines changed

packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts

+24-20
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ server.on("listening", onListening);
6969
* Normalize a port into a number, string, or false.
7070
*/
7171

72-
function normalizePort(val) {
72+
function normalizePort(val: string) {
7373
const port = parseInt(val, 10);
7474

7575
if (isNaN(port)) {
@@ -89,7 +89,7 @@ function normalizePort(val) {
8989
* Event listener for HTTPS server "error" event.
9090
*/
9191

92-
function onError(error) {
92+
function onError(error: any) {
9393
if (error.syscall !== "listen") {
9494
throw error;
9595
}
@@ -118,6 +118,12 @@ function onError(error) {
118118

119119
function onListening() {
120120
const addr = server.address();
121+
122+
if (!addr) {
123+
logger.error("Could not get running server address - exit.");
124+
process.exit(1);
125+
}
126+
121127
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
122128
debug("Listening on " + bind);
123129
}
@@ -144,14 +150,14 @@ io.on("connection", function (client) {
144150
// Check for the existence of the specified function and call it if it exists.
145151
if (Splug.isExistFunction(func)) {
146152
// Can be called with Server plugin function name.
147-
Splug[func](args)
148-
.then((respObj) => {
153+
(Splug as any)[func](args)
154+
.then((respObj: unknown) => {
149155
logger.info("*** RESPONSE ***");
150156
logger.info("Client ID :" + client.id);
151157
logger.info("Response :" + JSON.stringify(respObj));
152158
client.emit("response", respObj);
153159
})
154-
.catch((errObj) => {
160+
.catch((errObj: unknown) => {
155161
logger.error("*** ERROR ***");
156162
logger.error("Client ID :" + client.id);
157163
logger.error("Detail :" + JSON.stringify(errObj));
@@ -172,11 +178,12 @@ io.on("connection", function (client) {
172178
// TODO: "request2" -> "request"
173179
client.on("request2", function (data) {
174180
const methodType = data.method.type;
175-
// const args = data.args;
176-
const args = {};
177-
args["contract"] = data.contract;
178-
args["method"] = data.method;
179-
args["args"] = data.args;
181+
let args: Record<string, any> = {
182+
contract: data.contract,
183+
method: data.method,
184+
args: data.args,
185+
};
186+
180187
if (data.reqID !== undefined) {
181188
logger.info(`##add reqID: ${data.reqID}`);
182189
args["reqID"] = data.reqID;
@@ -223,14 +230,14 @@ io.on("connection", function (client) {
223230
// Check for the existence of the specified function and call it if it exists.
224231
if (Splug.isExistFunction(func)) {
225232
// Can be called with Server plugin function name.
226-
Splug[func](args)
227-
.then((respObj) => {
233+
(Splug as any)[func](args)
234+
.then((respObj: unknown) => {
228235
logger.info("*** RESPONSE ***");
229236
logger.info("Client ID :" + client.id);
230237
logger.info("Response :" + JSON.stringify(respObj));
231238
client.emit("response", respObj);
232239
})
233-
.catch((errObj) => {
240+
.catch((errObj: unknown) => {
234241
logger.error("*** ERROR ***");
235242
logger.error("Client ID :" + client.id);
236243
logger.error("Detail :" + JSON.stringify(errObj));
@@ -262,19 +269,16 @@ io.on("connection", function (client) {
262269
* startMonitor: starting block generation event monitoring
263270
**/
264271
client.on("startMonitor", function () {
265-
// Callback to receive monitoring results
266-
const cb = function (callbackData) {
272+
Smonitor.startMonitor(client.id, (event) => {
267273
let emitType = "";
268-
if (callbackData.status == 200) {
274+
if (event.status == 200) {
269275
emitType = "eventReceived";
270276
logger.info("event data callbacked.");
271277
} else {
272278
emitType = "monitor_error";
273279
}
274-
client.emit(emitType, callbackData);
275-
};
276-
277-
Smonitor.startMonitor(client.id, cb);
280+
client.emit(emitType, event);
281+
});
278282
});
279283

280284
/**

packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/PluginUtil.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* 3.78*10^14
2424
* 3.78e14
2525
*/
26-
export function convNum(value, defaultValue) {
26+
export function convNum(value: number | string, defaultValue: number | string) {
2727
let retValue = 0;
2828
let defValue = 0;
2929

packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerMonitorPlugin.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,30 @@ import { ValidatorAuthentication } from "./ValidatorAuthentication";
2525
const Web3 = require("web3");
2626
import safeStringify from "fast-safe-stringify";
2727

28+
export type MonitorCallback = (callback: {
29+
status: number;
30+
blockData?: string;
31+
errorDetail?: string;
32+
}) => void;
33+
2834
/*
2935
* ServerMonitorPlugin
3036
* Class definitions of server monitoring
3137
*/
3238
export class ServerMonitorPlugin {
33-
_filterTable: object;
34-
35-
/*
36-
* constructors
37-
*/
38-
constructor() {
39-
// Define dependent specific settings
40-
// Initialize monitored filter
41-
this._filterTable = {};
42-
}
39+
_filterTable = new Map<string, any>();
4340

4441
/*
4542
* startMonitor
4643
* Start Monitoring
4744
* @param {string} clientId: Client ID from which monitoring start request was made
4845
* @param {function} cb: A callback function that receives monitoring results at any time.
4946
*/
50-
startMonitor(clientId, cb) {
47+
startMonitor(clientId: string, cb: MonitorCallback) {
5148
logger.info("*** START MONITOR ***");
5249
logger.info("Client ID :" + clientId);
5350
// Implement handling to receive events from an end-chain and return them in a callback function
54-
let filter = this._filterTable[clientId];
51+
let filter = this._filterTable.get(clientId);
5552
if (!filter) {
5653
logger.info("create new web3 filter and start watching.");
5754
try {
@@ -63,8 +60,8 @@ export class ServerMonitorPlugin {
6360
filter = web3.eth.filter("latest");
6461
// filter should be managed by client ID
6562
// (You should never watch multiple urls from the same client)
66-
this._filterTable[clientId] = filter;
67-
filter.watch(function (error, blockHash) {
63+
this._filterTable.set(clientId, filter);
64+
filter.watch(function (error: any, blockHash: string) {
6865
if (!error) {
6966
console.log("##[HL-BC] Notify new block data(D2)");
7067
const blockData = web3.eth.getBlock(blockHash, true);
@@ -92,7 +89,7 @@ export class ServerMonitorPlugin {
9289
} else {
9390
const errObj = {
9491
status: 504,
95-
errorDetail: error,
92+
errorDetail: safeStringify(error),
9693
};
9794
cb(errObj);
9895
}
@@ -115,14 +112,14 @@ export class ServerMonitorPlugin {
115112
* monitoring stop
116113
* @param {string} clientId: Client ID from which monitoring stop request was made
117114
*/
118-
stopMonitor(clientId) {
115+
stopMonitor(clientId: string) {
119116
// Implement a process to end EC monitoring
120-
const filter = this._filterTable[clientId];
117+
let filter = this._filterTable.get(clientId);
121118
if (filter) {
122119
// Stop the filter & Remove it from table
123120
logger.info("stop watching and remove filter.");
124121
filter.stopWatching();
125-
delete this._filterTable[clientId];
122+
this._filterTable.delete(clientId);
126123
}
127124
}
128125
}

packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerPlugin.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export class ServerPlugin {
4747
* Scope of this function is in this class
4848
* Functions that should not be called directly should be implemented outside this class like utilities.
4949
*/
50-
isExistFunction(funcName) {
51-
if (this[funcName] != undefined) {
50+
isExistFunction(funcName: string) {
51+
if ((this as any)[funcName]) {
5252
return true;
5353
} else {
5454
return false;
@@ -67,11 +67,11 @@ export class ServerPlugin {
6767
* }
6868
* @return {Object} JSON object
6969
*/
70-
getNumericBalance(args) {
70+
getNumericBalance(args: any) {
7171
// * The Web3 API can be used synchronously, but each function is always an asynchronous specification because of the use of other APIs such as REST,
7272
return new Promise((resolve, reject) => {
7373
logger.info("getNumericBalance start");
74-
let retObj = {};
74+
let retObj: Record<string, any>;
7575

7676
const referedAddress = args.args.args[0];
7777
const reqID = args["reqID"];
@@ -139,11 +139,11 @@ export class ServerPlugin {
139139
* }
140140
* @return {Object} JSON object
141141
*/
142-
transferNumericAsset(args) {
142+
transferNumericAsset(args: any) {
143143
return new Promise((resolve, reject) => {
144144
logger.info("transferNumericAsset start");
145145

146-
let retObj = {};
146+
let retObj: Record<string, any>;
147147
let sendArgs = {};
148148
const sendFunction = "sendTransaction";
149149
// const funcParam = args;
@@ -231,11 +231,11 @@ export class ServerPlugin {
231231
* }
232232
* @return {Object} JSON object
233233
*/
234-
getNonce(args) {
234+
getNonce(args: any) {
235235
// * The Web3 API can be used synchronously, but each function is always an asynchronous specification because of the use of other APIs such as REST,
236236
return new Promise((resolve, reject) => {
237237
logger.info("getNonce start");
238-
let retObj = {};
238+
let retObj: Record<string, any>;
239239

240240
const targetAddress = args.args.args.args[0];
241241
const reqID = args["reqID"];
@@ -316,11 +316,11 @@ export class ServerPlugin {
316316
* }
317317
* @return {Object} JSON object
318318
*/
319-
toHex(args) {
319+
toHex(args: any) {
320320
// * The Web3 API can be used synchronously, but each function is always an asynchronous specification because of the use of other APIs such as REST,
321321
return new Promise((resolve, reject) => {
322322
logger.info("toHex start");
323-
let retObj = {};
323+
let retObj: Record<string, any>;
324324

325325
const targetValue = args.args.args.args[0];
326326
const reqID = args["reqID"];
@@ -393,11 +393,11 @@ export class ServerPlugin {
393393
* }
394394
* @return {Object} JSON object
395395
*/
396-
sendRawTransaction(args) {
396+
sendRawTransaction(args: any) {
397397
return new Promise((resolve, reject) => {
398398
logger.info("sendRawTransaction(start");
399399

400-
let retObj = {};
400+
let retObj: Record<string, any>;
401401
const sendArgs = {};
402402
const sendFunction = "sendTransaction";
403403
const funcParam = args.args.args[0];
@@ -451,11 +451,11 @@ export class ServerPlugin {
451451
* }
452452
* @return {Object} JSON object
453453
*/
454-
web3Eth(args) {
454+
web3Eth(args: any) {
455455
return new Promise((resolve, reject) => {
456456
logger.info("web3Eth start");
457457

458-
let retObj = {};
458+
let retObj: Record<string, any>;
459459
const sendFunction = args.method.command;
460460
const sendArgs = args.args.args[0];
461461
const reqID = args["reqID"];
@@ -518,11 +518,11 @@ export class ServerPlugin {
518518
* }
519519
* @return {Object} JSON object
520520
*/
521-
contract(args) {
521+
contract(args: any) {
522522
return new Promise((resolve, reject) => {
523523
logger.info("contract start");
524524

525-
let retObj = {};
525+
let retObj: Record<string, any>;
526526
const sendCommand = args.method.command;
527527
const sendFunction = args.method.function;
528528
const sendArgs = args.args.args;

packages/cactus-plugin-ledger-connector-go-ethereum-socketio/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"emitDecoratorMetadata": true,
99
"experimentalDecorators": true,
1010
"tsBuildInfoFile": "../../.build-cache/cactus-plugin-ledger-connector-go-ethereum-socketio.tsbuildinfo",
11-
"strict": false // TODO - True, fix warnings
1211
},
1312
"include": [
1413
"./src/main/typescript/common/core/*.ts",

0 commit comments

Comments
 (0)