Skip to content

Commit

Permalink
refactor(connector-go-ethereum-socketio): fix strict flag warnings
Browse files Browse the repository at this point in the history
cactus-plugin-ledger-connector-go-ethereum-socketio will compile with global strict flag.

Related issue: hyperledger-cacti#1671

Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com>
  • Loading branch information
outSH authored and takeutak committed Jul 15, 2022
1 parent 70b2f11 commit 599205a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ server.on("listening", onListening);
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
function normalizePort(val: string) {
const port = parseInt(val, 10);

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

function onError(error) {
function onError(error: any) {
if (error.syscall !== "listen") {
throw error;
}
Expand Down Expand Up @@ -118,6 +118,12 @@ function onError(error) {

function onListening() {
const addr = server.address();

if (!addr) {
logger.error("Could not get running server address - exit.");
process.exit(1);
}

const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
Expand All @@ -144,14 +150,14 @@ io.on("connection", function (client) {
// Check for the existence of the specified function and call it if it exists.
if (Splug.isExistFunction(func)) {
// Can be called with Server plugin function name.
Splug[func](args)
.then((respObj) => {
(Splug as any)[func](args)
.then((respObj: unknown) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
logger.info("Response :" + JSON.stringify(respObj));
client.emit("response", respObj);
})
.catch((errObj) => {
.catch((errObj: unknown) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
Expand All @@ -172,11 +178,12 @@ io.on("connection", function (client) {
// TODO: "request2" -> "request"
client.on("request2", function (data) {
const methodType = data.method.type;
// const args = data.args;
const args = {};
args["contract"] = data.contract;
args["method"] = data.method;
args["args"] = data.args;
let args: Record<string, any> = {
contract: data.contract,
method: data.method,
args: data.args,
};

if (data.reqID !== undefined) {
logger.info(`##add reqID: ${data.reqID}`);
args["reqID"] = data.reqID;
Expand Down Expand Up @@ -223,14 +230,14 @@ io.on("connection", function (client) {
// Check for the existence of the specified function and call it if it exists.
if (Splug.isExistFunction(func)) {
// Can be called with Server plugin function name.
Splug[func](args)
.then((respObj) => {
(Splug as any)[func](args)
.then((respObj: unknown) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
logger.info("Response :" + JSON.stringify(respObj));
client.emit("response", respObj);
})
.catch((errObj) => {
.catch((errObj: unknown) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
Expand Down Expand Up @@ -262,19 +269,16 @@ io.on("connection", function (client) {
* startMonitor: starting block generation event monitoring
**/
client.on("startMonitor", function () {
// Callback to receive monitoring results
const cb = function (callbackData) {
Smonitor.startMonitor(client.id, (event) => {
let emitType = "";
if (callbackData.status == 200) {
if (event.status == 200) {
emitType = "eventReceived";
logger.info("event data callbacked.");
} else {
emitType = "monitor_error";
}
client.emit(emitType, callbackData);
};

Smonitor.startMonitor(client.id, cb);
client.emit(emitType, event);
});
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* 3.78*10^14
* 3.78e14
*/
export function convNum(value, defaultValue) {
export function convNum(value: number | string, defaultValue: number | string) {
let retValue = 0;
let defValue = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,30 @@ import { ValidatorAuthentication } from "./ValidatorAuthentication";
const Web3 = require("web3");
import safeStringify from "fast-safe-stringify";

export type MonitorCallback = (callback: {
status: number;
blockData?: string;
errorDetail?: string;
}) => void;

/*
* ServerMonitorPlugin
* Class definitions of server monitoring
*/
export class ServerMonitorPlugin {
_filterTable: object;

/*
* constructors
*/
constructor() {
// Define dependent specific settings
// Initialize monitored filter
this._filterTable = {};
}
_filterTable = new Map<string, any>();

/*
* startMonitor
* Start Monitoring
* @param {string} clientId: Client ID from which monitoring start request was made
* @param {function} cb: A callback function that receives monitoring results at any time.
*/
startMonitor(clientId, cb) {
startMonitor(clientId: string, cb: MonitorCallback) {
logger.info("*** START MONITOR ***");
logger.info("Client ID :" + clientId);
// Implement handling to receive events from an end-chain and return them in a callback function
let filter = this._filterTable[clientId];
let filter = this._filterTable.get(clientId);
if (!filter) {
logger.info("create new web3 filter and start watching.");
try {
Expand All @@ -63,8 +60,8 @@ export class ServerMonitorPlugin {
filter = web3.eth.filter("latest");
// filter should be managed by client ID
// (You should never watch multiple urls from the same client)
this._filterTable[clientId] = filter;
filter.watch(function (error, blockHash) {
this._filterTable.set(clientId, filter);
filter.watch(function (error: any, blockHash: string) {
if (!error) {
console.log("##[HL-BC] Notify new block data(D2)");
const blockData = web3.eth.getBlock(blockHash, true);
Expand Down Expand Up @@ -92,7 +89,7 @@ export class ServerMonitorPlugin {
} else {
const errObj = {
status: 504,
errorDetail: error,
errorDetail: safeStringify(error),
};
cb(errObj);
}
Expand All @@ -115,14 +112,14 @@ export class ServerMonitorPlugin {
* monitoring stop
* @param {string} clientId: Client ID from which monitoring stop request was made
*/
stopMonitor(clientId) {
stopMonitor(clientId: string) {
// Implement a process to end EC monitoring
const filter = this._filterTable[clientId];
let filter = this._filterTable.get(clientId);
if (filter) {
// Stop the filter & Remove it from table
logger.info("stop watching and remove filter.");
filter.stopWatching();
delete this._filterTable[clientId];
this._filterTable.delete(clientId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export class ServerPlugin {
* Scope of this function is in this class
* Functions that should not be called directly should be implemented outside this class like utilities.
*/
isExistFunction(funcName) {
if (this[funcName] != undefined) {
isExistFunction(funcName: string) {
if ((this as any)[funcName]) {
return true;
} else {
return false;
Expand All @@ -67,11 +67,11 @@ export class ServerPlugin {
* }
* @return {Object} JSON object
*/
getNumericBalance(args) {
getNumericBalance(args: any) {
// * 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,
return new Promise((resolve, reject) => {
logger.info("getNumericBalance start");
let retObj = {};
let retObj: Record<string, any>;

const referedAddress = args.args.args[0];
const reqID = args["reqID"];
Expand Down Expand Up @@ -139,11 +139,11 @@ export class ServerPlugin {
* }
* @return {Object} JSON object
*/
transferNumericAsset(args) {
transferNumericAsset(args: any) {
return new Promise((resolve, reject) => {
logger.info("transferNumericAsset start");

let retObj = {};
let retObj: Record<string, any>;
let sendArgs = {};
const sendFunction = "sendTransaction";
// const funcParam = args;
Expand Down Expand Up @@ -231,11 +231,11 @@ export class ServerPlugin {
* }
* @return {Object} JSON object
*/
getNonce(args) {
getNonce(args: any) {
// * 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,
return new Promise((resolve, reject) => {
logger.info("getNonce start");
let retObj = {};
let retObj: Record<string, any>;

const targetAddress = args.args.args.args[0];
const reqID = args["reqID"];
Expand Down Expand Up @@ -316,11 +316,11 @@ export class ServerPlugin {
* }
* @return {Object} JSON object
*/
toHex(args) {
toHex(args: any) {
// * 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,
return new Promise((resolve, reject) => {
logger.info("toHex start");
let retObj = {};
let retObj: Record<string, any>;

const targetValue = args.args.args.args[0];
const reqID = args["reqID"];
Expand Down Expand Up @@ -393,11 +393,11 @@ export class ServerPlugin {
* }
* @return {Object} JSON object
*/
sendRawTransaction(args) {
sendRawTransaction(args: any) {
return new Promise((resolve, reject) => {
logger.info("sendRawTransaction(start");

let retObj = {};
let retObj: Record<string, any>;
const sendArgs = {};
const sendFunction = "sendTransaction";
const funcParam = args.args.args[0];
Expand Down Expand Up @@ -451,11 +451,11 @@ export class ServerPlugin {
* }
* @return {Object} JSON object
*/
web3Eth(args) {
web3Eth(args: any) {
return new Promise((resolve, reject) => {
logger.info("web3Eth start");

let retObj = {};
let retObj: Record<string, any>;
const sendFunction = args.method.command;
const sendArgs = args.args.args[0];
const reqID = args["reqID"];
Expand Down Expand Up @@ -518,11 +518,11 @@ export class ServerPlugin {
* }
* @return {Object} JSON object
*/
contract(args) {
contract(args: any) {
return new Promise((resolve, reject) => {
logger.info("contract start");

let retObj = {};
let retObj: Record<string, any>;
const sendCommand = args.method.command;
const sendFunction = args.method.function;
const sendArgs = args.args.args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"tsBuildInfoFile": "../../.build-cache/cactus-plugin-ledger-connector-go-ethereum-socketio.tsbuildinfo",
"strict": false // TODO - True, fix warnings
},
"include": [
"./src/main/typescript/common/core/*.ts",
Expand Down

0 comments on commit 599205a

Please sign in to comment.