Skip to content

Commit

Permalink
build(deps): fix npm (grpc) build on NodeJS v20.4.0
Browse files Browse the repository at this point in the history
1. Eliminates all uses of the old `grpc` dependency from the codebase.
1.1. Upgraded all outdated fabirc-network, fabric-client and fabric-ca-client
dependencies to the latest stable 2.2.x version which is 2.2.18 at the time.
1.2. Also outright swapped `grpc` declarations with `@grpc/grpc-js`.
The rest of the diff is due to the incompatibility of the two mentioned.

Fixes https://github.com/hyperledger/cacti/issues/2562
Fixes https://github.com/hyperledger/cacti/issues/1507

Signed-off-by: Sandeep Nishad <[email protected]>
Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Oct 5, 2023
1 parent 9e66850 commit 252909d
Show file tree
Hide file tree
Showing 30 changed files with 97 additions and 3,194 deletions.
2 changes: 1 addition & 1 deletion examples/cactus-example-cbdc-bridging-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"axios": "^0.27.2",
"crypto-js": "4.1.1",
"dotenv": "^16.0.1",
"fabric-network": "2.2.10",
"fabric-network": "2.2.18",
"fs-extra": "10.1.0",
"ipfs-http-client": "51.0.1",
"knex": "2.5.1",
Expand Down
4 changes: 1 addition & 3 deletions examples/cactus-example-discounted-asset-trade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
"ethereumjs-common": "1.5.2",
"ethereumjs-tx": "2.1.2",
"express": "4.16.4",
"fabric-ca-client": "1.4.19",
"fabric-client": "1.4.19",
"fabric-network": "1.4.19",
"fabric-network": "2.2.18",
"http-errors": "1.6.3",
"jsonwebtoken": "9.0.0",
"jsrsasign": "10.5.25",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ISocketApiClient } from "@hyperledger/cactus-core-api";
import { Verifier } from "@hyperledger/cactus-verifier-client";
import { signProposal } from "./sign-utils";

import { FileSystemWallet } from "fabric-network";
import { Wallets } from "fabric-network";

const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
Expand Down Expand Up @@ -50,14 +50,13 @@ export function makeSignedProposal<T extends ISocketApiClient<unknown>>(
let certPem = undefined;
let privateKeyPem = undefined;
const submitter = config.assetTradeInfo.fabric.submitter.name;
const wallet = new FileSystemWallet(
const wallet = await Wallets.newFileSystemWallet(
config.assetTradeInfo.fabric.keystore,
);
logger.debug(`Wallet path: ${config.assetTradeInfo.fabric.keystore}`);

const submitterExists = await wallet.exists(submitter);
if (submitterExists) {
const submitterIdentity = await wallet.export(submitter);
const submitterIdentity = await wallet.get(submitter);
if (submitterIdentity) {
certPem = (submitterIdentity as any).certificate;
privateKeyPem = (submitterIdentity as any).privateKey;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cactus-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"webpack:dev:web": "webpack --env=dev --target=web --config ../../webpack.config.js"
},
"dependencies": {
"@grpc/grpc-js": "1.9.0",
"@grpc/grpc-js": "1.9.5",
"@grpc/proto-loader": "0.7.8",
"@hyperledger/cactus-common": "2.0.0-alpha.2",
"@hyperledger/cactus-core": "2.0.0-alpha.2",
Expand Down
3 changes: 1 addition & 2 deletions packages/cactus-cmd-socketio-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
"ethereumjs-common": "1.5.2",
"ethereumjs-tx": "2.1.2",
"express": "4.16.4",
"fabric-ca-client": "1.4.19",
"fabric-network": "1.4.19",
"fabric-network": "2.2.18",
"http-errors": "1.6.3",
"js-yaml": "3.14.1",
"jsonwebtoken": "9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cactus-core-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"axios": "0.21.4"
},
"devDependencies": {
"@grpc/grpc-js": "1.9.0",
"@grpc/grpc-js": "1.9.5",
"@grpc/proto-loader": "0.7.8",
"@types/express": "4.17.13",
"@types/google-protobuf": "3.15.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
"start": "cd ./dist && node common/core/bin/www.js"
},
"dependencies": {
"@grpc/grpc-js": "1.9.5",
"@hyperledger/cactus-cmd-socketio-server": "2.0.0-alpha.2",
"@hyperledger/cactus-common": "2.0.0-alpha.2",
"@types/node": "14.18.54",
"body-parser": "1.17.2",
"cookie-parser": "1.4.6",
"debug": "3.1.0",
"express": "4.17.3",
"fabric-ca-client": "1.4.19",
"fabric-client": "1.4.19",
"fabric-network": "1.4.19",
"fabric-ca-client": "2.2.18",
"fabric-network": "2.2.18",
"fs-extra": "10.1.0",
"grpc": "1.24.11",
"js-yaml": "3.14.1",
"jsonwebtoken": "9.0.0",
"lodash": "4.17.21",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,274 +29,6 @@ import { getLogger } from "log4js";
const logger = getLogger("connector_main[" + process.pid + "]");
logger.level = configRead('logLevel', 'info');

// implementation class of a part dependent of end-chains (server plugin)
import { ServerPlugin } from "../../../connector/ServerPlugin";

// destination dependency (MONITOR) implementation class
import { ServerMonitorPlugin } from "../../../connector/ServerMonitorPlugin";

export async function startFabricSocketIOConnector() {
const Splug = new ServerPlugin();
const Smonitor = new ServerMonitorPlugin();

// Get port from environment and store in Express.
const sslport = normalizePort(process.env.PORT || configRead('sslParam.port'));
app.set("port", sslport);

// Specify private key and certificate
let keyString: string;
let certString: string;
try {
keyString = configRead<string>('sslParam.keyValue');
certString = configRead<string>('sslParam.certValue');
} catch {
keyString = fs.readFileSync(configRead('sslParam.key'), "ascii");
certString = fs.readFileSync(configRead('sslParam.cert'), "ascii");
}

// Create HTTPS server.
const server = https.createServer({
key: keyString,
cert: certString,
}, app); // Start as an https server.
const io = new Server(server);

// Event listener for HTTPS server "error" event.
server.on("error", (error: any) => {
if (error.syscall !== "listen") {
throw error;
}

const bind =
typeof sslport === "string" ? "Pipe " + sslport : "Port " + sslport;

// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
logger.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
logger.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
});

io.on("connection", function (client) {
logger.info("Client " + client.id + " connected.");

/**
* request: The server plugin's request to execute a function
* @param {JSON} data: Request Body (following format)
* JSON: {
* "func": (string) Function name ,// For example : "transferNumericAsset"
* "args": (Object) argument// for example , {"from" : "xxx" , "to" : "yyy" , "value" : "10,000"}
* }
**/
client.on("request", function (data) {
const func = data.func;
const args = data.args;
if (data.reqID !== undefined) {
logger.info(`##add reqID: ${data.reqID}`);
args["reqID"] = data.reqID;
}
logger.info("##[HL-BC] Invoke smart contract to transfer asset(D1)");
logger.info("*** REQUEST ***");
logger.info("Client ID :" + client.id);
logger.info("Data :" + JSON.stringify(data));

// 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 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: unknown) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
client.emit("connector_error", errObj);
});
} else {
// No such function
const emsg = "Function " + func + " not found!";
logger.error(emsg);
const retObj = {
status: 504,
errorDetail: emsg,
};
client.emit("connector_error", retObj);
}
});

client.on("request2", function (data) {
const func = data.method.method;
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;
}
logger.info("##[HL-BC] Invoke smart contract to transfer asset(D1)");
logger.info("*** REQUEST ***");
logger.info("Client ID :" + client.id);
logger.info("Data :" + JSON.stringify(data));

// Check for the presence of a request ID.
if (
data.method.type === "evaluateTransaction" ||
data.method.type === "submitTransaction"
) {
// Call a synchronous method.
Splug.contractTransaction(args)
.then((respObj) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
logger.info("Response :" + JSON.stringify(respObj));
client.emit("response", respObj);
})
.catch((errObj) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
client.emit("connector_error", errObj);
});
} else if (data.method.type === "sendSignedTransaction") {
// Call an asynchronous method.
Splug.sendSignedTransaction(args)
.then((respObj) => {
logger.info("*** RESPONSE ***");
logger.info("Client ID :" + client.id);
logger.info("Response :" + JSON.stringify(respObj));
client.emit("response", respObj);
})
.catch((errObj) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
client.emit("connector_error", errObj);
});
} else if (data.method.type === "function") {
const func = args["method"].command;
logger.info(`##method.type: function, function: ${func}`);
// logger.info(`##args: ${JSON.stringify(args)}`);
if (Splug.isExistFunction(func)) {
// Can be called with Server plugin function name.
(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: unknown) => {
logger.error("*** ERROR ***");
logger.error("Client ID :" + client.id);
logger.error("Detail :" + JSON.stringify(errObj));
client.emit("connector_error", errObj);
});
} else {
// No such function
const emsg = "Function " + func + " not found!";
logger.error(emsg);
const retObj = {
status: 504,
errorDetail: emsg,
};
client.emit("connector_error", retObj);
}
} else {
// No such function
const emsg = "Function " + func + " not found!";
logger.error(emsg);
const retObj = {
status: 504,
errorDetail: emsg,
};
client.emit("connector_error", retObj);
}
});

/**
* startMonitor: starting block generation event monitoring
**/
client.on("startMonitor", function () {
Smonitor.startMonitor(client.id, (event) => {
let emitType = "";

if (event.status == 200) {
emitType = "eventReceived";
logger.info("event data callbacked.");
} else {
emitType = "monitor_error";
}

client.emit(emitType, event);
});
});

/**
* stopMonitor: block generation events monitoring stopping
**/
// I think it is more common to stop from the disconnect described later, but I will prepare for it.
client.on("stopMonitor", function (reason) {
Smonitor.stopMonitor(client.id);
});

client.on("disconnect", function (reason) {
// Unexpected disconnect as well as explicit disconnect request can be received here
logger.info("Client " + client.id + " disconnected.");
logger.info("Reason :" + reason);
// Stop monitoring if disconnected client is for event monitoring
Smonitor.stopMonitor(client.id);
});
});

// Listen on provided port, on all network interfaces.
return new Promise<https.Server>((resolve) => server.listen(sslport, () => resolve(server)));
};

// Normalize a port into a number, string, or false.
function normalizePort(val: string) {
const port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

if (require.main === module) {
// When this file executed as a script, not loaded as module - run the connector
startFabricSocketIOConnector().then((server) => {
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;
logger.debug("Listening on " + bind);
}).catch((err) => {
logger.error("Could not start fabric-socketio connector:", err);
});
}
Loading

0 comments on commit 252909d

Please sign in to comment.