From 9a8f7db746e2a41708e2fe9d5277561d6abac3d4 Mon Sep 17 00:00:00 2001 From: Takuma TAKEUCHI Date: Wed, 8 Sep 2021 22:12:28 +0900 Subject: [PATCH] fix(validators): add some missing parts Signed-off-by: Takuma TAKEUCHI --- examples/cartrade/script-build-all.sh | 0 examples/cartrade/script-build-cartrade.sh | 0 examples/cartrade/script-build-get-app.sh | 0 examples/cartrade/script-get-app.sh | 0 .../cartrade/script-post-cartrade-sample.sh | 0 examples/cartrade/script-start-cartrade.sh | 0 examples/cartrade/script-start-ledgers.sh | 0 .../script-start-validator-ethereum.sh | 0 .../cartrade/script-start-validator-fabric.sh | 0 examples/cartrade/script-stop-cartrade.sh | 0 .../script-build-packages.sh | 0 .../script-build-validator.sh | 0 .../script-start-validator.sh | 0 .../main/typescript/common/build/Dockerfile | 16 + .../main/typescript/common/core/bin/www.ts | 302 ++++++++++++++++++ .../script-build-validator.sh | 0 .../script-start-validator.sh | 0 .../main/typescript/common/build/Dockerfile | 16 + .../main/typescript/common/core/bin/www.ts | 301 +++++++++++++++++ .../genesis/template/genesis-template.json | 20 ++ .../script-build-validator.sh | 0 .../script-start-validator.sh | 0 .../main/typescript/common/build/Dockerfile | 16 + .../main/typescript/common/core/bin/www.ts | 276 ++++++++++++++++ 24 files changed, 947 insertions(+) mode change 100644 => 100755 examples/cartrade/script-build-all.sh mode change 100644 => 100755 examples/cartrade/script-build-cartrade.sh mode change 100644 => 100755 examples/cartrade/script-build-get-app.sh mode change 100644 => 100755 examples/cartrade/script-get-app.sh mode change 100644 => 100755 examples/cartrade/script-post-cartrade-sample.sh mode change 100644 => 100755 examples/cartrade/script-start-cartrade.sh mode change 100644 => 100755 examples/cartrade/script-start-ledgers.sh mode change 100644 => 100755 examples/cartrade/script-start-validator-ethereum.sh mode change 100644 => 100755 examples/cartrade/script-start-validator-fabric.sh mode change 100644 => 100755 examples/cartrade/script-stop-cartrade.sh mode change 100644 => 100755 packages/cactus-cmd-socketio-server/script-build-packages.sh mode change 100644 => 100755 packages/cactus-plugin-ledger-connector-fabric-socketio/script-build-validator.sh mode change 100644 => 100755 packages/cactus-plugin-ledger-connector-fabric-socketio/script-start-validator.sh create mode 100644 packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/build/Dockerfile create mode 100644 packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/core/bin/www.ts mode change 100644 => 100755 packages/cactus-plugin-ledger-connector-go-ethereum-socketio/script-build-validator.sh mode change 100644 => 100755 packages/cactus-plugin-ledger-connector-go-ethereum-socketio/script-start-validator.sh create mode 100644 packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/build/Dockerfile create mode 100644 packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts create mode 100644 packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/test/typescript/unit-test/geth-docker/genesis/template/genesis-template.json mode change 100644 => 100755 packages/cactus-plugin-ledger-connector-sawtooth-socketio/script-build-validator.sh mode change 100644 => 100755 packages/cactus-plugin-ledger-connector-sawtooth-socketio/script-start-validator.sh create mode 100644 packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/build/Dockerfile create mode 100644 packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/core/bin/www.ts diff --git a/examples/cartrade/script-build-all.sh b/examples/cartrade/script-build-all.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-build-cartrade.sh b/examples/cartrade/script-build-cartrade.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-build-get-app.sh b/examples/cartrade/script-build-get-app.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-get-app.sh b/examples/cartrade/script-get-app.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-post-cartrade-sample.sh b/examples/cartrade/script-post-cartrade-sample.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-start-cartrade.sh b/examples/cartrade/script-start-cartrade.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-start-ledgers.sh b/examples/cartrade/script-start-ledgers.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-start-validator-ethereum.sh b/examples/cartrade/script-start-validator-ethereum.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-start-validator-fabric.sh b/examples/cartrade/script-start-validator-fabric.sh old mode 100644 new mode 100755 diff --git a/examples/cartrade/script-stop-cartrade.sh b/examples/cartrade/script-stop-cartrade.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-cmd-socketio-server/script-build-packages.sh b/packages/cactus-cmd-socketio-server/script-build-packages.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-plugin-ledger-connector-fabric-socketio/script-build-validator.sh b/packages/cactus-plugin-ledger-connector-fabric-socketio/script-build-validator.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-plugin-ledger-connector-fabric-socketio/script-start-validator.sh b/packages/cactus-plugin-ledger-connector-fabric-socketio/script-start-validator.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/build/Dockerfile b/packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/build/Dockerfile new file mode 100644 index 0000000000..fa6a7d59d7 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/build/Dockerfile @@ -0,0 +1,16 @@ +# Copyright 2021 Hyperledger Cactus Contributors +# SPDX-License-Identifier: Apache-2.0 +FROM hyperledger/fabric-ccenv:x86_64-1.0.4 +#ENV http_proxy $HTTP_PROXY +#ENV https_proxy $HTTP_PROXY +#ENV HTTP_PROXY $HTTP_PROXY +#ENV HTTPS_PROXY $HTTP_PROXY +#ENV NO_PROXY "rest-server,ec1-connector,ec2-connector,geth1,geth2" +RUN apt update +RUN apt-get install -y screen +RUN apt-get install -y npm +#RUN npm -g config set proxy $HTTP_PROXY +RUN npm -g install n +RUN n --version +RUN n 8.9.0 +RUN npm -g install express diff --git a/packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/core/bin/www.ts b/packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/core/bin/www.ts new file mode 100644 index 0000000000..a315910908 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-fabric-socketio/src/main/typescript/common/core/bin/www.ts @@ -0,0 +1,302 @@ +#!/usr/bin/env node + +/* + * Copyright 2021 Hyperledger Cactus Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * www.js + */ + +/* Summary: + * Connector: a part independent of end-chains + */ + +/** + * Module dependencies. + */ + +import app from '../app'; +const debug = require('debug')('connector:server'); +import https = require('https'); +import { config } from '../config/default'; +import fs = require('fs'); + +// Log settings +import { getLogger } from "log4js"; +const logger = getLogger('connector_main[' + process.pid + ']'); +logger.level = config.logLevel; + +// implementation class of a part dependent of end-chains (server plugin) +import { ServerPlugin } from '../../../connector/ServerPlugin'; +const Splug = new ServerPlugin(); + +// destination dependency (MONITOR) implementation class +import { ServerMonitorPlugin } from '../../../connector/ServerMonitorPlugin'; +const Smonitor = new ServerMonitorPlugin(); + +/** + * Get port from environment and store in Express. + */ + +const sslport = normalizePort(process.env.PORT || config.sslParam.port); +app.set('port', sslport); + +// Specify private key and certificate +const sslParam = { + key: fs.readFileSync(config.sslParam.key), + cert: fs.readFileSync(config.sslParam.cert) +}; + +/** + * Create HTTPS server. + */ + +const server = https.createServer(sslParam, app); // Start as an https server. +const io = require('socket.io')(server); + +/** + * Listen on provided port, on all network interfaces. + */ + +server.listen(sslport, function(){ + console.log('listening on *:' + sslport); +}); +server.on('error', onError); +server.on('listening', onListening); + +/** + * Normalize a port into a number, string, or false. + */ + +function normalizePort(val) { + var port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + + return false; +} + +/** + * Event listener for HTTPS server "error" event. + */ + +function onError(error) { + 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': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +/** + * Event listener for HTTPS server "listening" event. + */ + +function onListening() { + const addr = server.address(); + const bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + debug('Listening on ' + bind); +} + +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[func](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 { + // 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; + const args = {}; + args["contract"] = data.contract; + args["method"] = data.method; + args["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[func](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 { + // 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() { + // Callback to receive monitoring results + const cb = function(callbackData) { + let emitType = ""; + if(callbackData.status == 200) { + emitType = "eventReceived"; + logger.info('event data callbacked.'); + } else { + emitType = "monitor_error"; + } + client.emit(emitType, callbackData); + } + + Smonitor.startMonitor(client.id, cb); + }); + + /** + * 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); + }); + +}); + diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/script-build-validator.sh b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/script-build-validator.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/script-start-validator.sh b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/script-start-validator.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/build/Dockerfile b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/build/Dockerfile new file mode 100644 index 0000000000..fa6a7d59d7 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/build/Dockerfile @@ -0,0 +1,16 @@ +# Copyright 2021 Hyperledger Cactus Contributors +# SPDX-License-Identifier: Apache-2.0 +FROM hyperledger/fabric-ccenv:x86_64-1.0.4 +#ENV http_proxy $HTTP_PROXY +#ENV https_proxy $HTTP_PROXY +#ENV HTTP_PROXY $HTTP_PROXY +#ENV HTTPS_PROXY $HTTP_PROXY +#ENV NO_PROXY "rest-server,ec1-connector,ec2-connector,geth1,geth2" +RUN apt update +RUN apt-get install -y screen +RUN apt-get install -y npm +#RUN npm -g config set proxy $HTTP_PROXY +RUN npm -g install n +RUN n --version +RUN n 8.9.0 +RUN npm -g install express diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts new file mode 100644 index 0000000000..d99cf05f43 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts @@ -0,0 +1,301 @@ +#!/usr/bin/env node + +/* + * Copyright 2021 Hyperledger Cactus Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * www.js + */ + +/* Summary: + * Connector: a part independent of end-chains + */ + +/** + * Module dependencies. + */ + +import app from '../app'; +const debug = require('debug')('connector:server'); +import https = require('https'); +import { config } from '../config/default'; +import fs = require('fs'); + +// Log settings +import { getLogger } from "log4js"; +const logger = getLogger('connector_main[' + process.pid + ']'); +logger.level = config.logLevel; + +// implementation class of a part dependent of end-chains (server plugin) +import { ServerPlugin } from '../../../connector/ServerPlugin'; +const Splug = new ServerPlugin(); + +// destination dependency (MONITOR) implementation class +import { ServerMonitorPlugin } from '../../../connector/ServerMonitorPlugin'; +const Smonitor = new ServerMonitorPlugin(); + +/** + * Get port from environment and store in Express. + */ + +const sslport = normalizePort(process.env.PORT || config.sslParam.port); +app.set('port', sslport); + +// Specify private key and certificate +const sslParam = { + key: fs.readFileSync(config.sslParam.key), + cert: fs.readFileSync(config.sslParam.cert) +}; + +/** + * Create HTTPS server. + */ + +const server = https.createServer(sslParam, app); // Start as an https server. +const io = require('socket.io')(server); + +/** + * Listen on provided port, on all network interfaces. + */ + +server.listen(sslport, function(){ + console.log('listening on *:' + sslport); +}); +server.on('error', onError); +server.on('listening', onListening); + +/** + * Normalize a port into a number, string, or false. + */ + +function normalizePort(val) { + const port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + + return false; +} + +/** + * Event listener for HTTPS server "error" event. + */ + +function onError(error) { + 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': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +/** + * Event listener for HTTPS server "listening" event. + */ + +function onListening() { + const addr = server.address(); + const bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + debug('Listening on ' + bind); +} + +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; + console.log('##[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[func](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 { + // No such function + const emsg = "Function " + func + " not found!"; + logger.error(emsg); + const retObj = { + "status" : 504, + "errorDetail" : emsg + }; + client.emit("connector_error", retObj); + } + }); + + // 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; + if (data.reqID !== undefined) { + logger.info(`##add reqID: ${data.reqID}`); + args["reqID"] = data.reqID; + } + + + console.log('##[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 (methodType === "web3Eth") { + // Can be called with Server plugin function name. + Splug.web3Eth(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 (methodType === "contract") { + // Can be called with Server plugin function name. + Splug.contract(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 (methodType === "function") { + const func = args["method"].command; + // 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) => { + 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 { + // 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 = "method.type " + methodType + " 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() { + // Callback to receive monitoring results + const cb = function(callbackData) { + let emitType = ""; + if(callbackData.status == 200) { + emitType = "eventReceived"; + logger.info('event data callbacked.'); + } else { + emitType = "monitor_error"; + } + client.emit(emitType, callbackData); + } + + Smonitor.startMonitor(client.id, cb); + }); + + /** + * 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); + }); + +}); + diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/test/typescript/unit-test/geth-docker/genesis/template/genesis-template.json b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/test/typescript/unit-test/geth-docker/genesis/template/genesis-template.json new file mode 100644 index 0000000000..1d3401b722 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/test/typescript/unit-test/geth-docker/genesis/template/genesis-template.json @@ -0,0 +1,20 @@ +{ + "config": { + "chainId": 10, + "homesteadBlock": 0, + "eip155Block": 0, + "eip158Block": 0 + }, + "alloc" : { + "ADDRESS": + {"balance":"100000000000000000000000000"} + }, + "coinbase" : "0x3333333333333333333333333333333333333333", + "difficulty" : "0x4000", + "extraData" : "", + "gasLimit" : "0x8000000", + "nonce" : "0x0000000000000042", + "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", + "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", + "timestamp" : "0x00" +} diff --git a/packages/cactus-plugin-ledger-connector-sawtooth-socketio/script-build-validator.sh b/packages/cactus-plugin-ledger-connector-sawtooth-socketio/script-build-validator.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-plugin-ledger-connector-sawtooth-socketio/script-start-validator.sh b/packages/cactus-plugin-ledger-connector-sawtooth-socketio/script-start-validator.sh old mode 100644 new mode 100755 diff --git a/packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/build/Dockerfile b/packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/build/Dockerfile new file mode 100644 index 0000000000..fa6a7d59d7 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/build/Dockerfile @@ -0,0 +1,16 @@ +# Copyright 2021 Hyperledger Cactus Contributors +# SPDX-License-Identifier: Apache-2.0 +FROM hyperledger/fabric-ccenv:x86_64-1.0.4 +#ENV http_proxy $HTTP_PROXY +#ENV https_proxy $HTTP_PROXY +#ENV HTTP_PROXY $HTTP_PROXY +#ENV HTTPS_PROXY $HTTP_PROXY +#ENV NO_PROXY "rest-server,ec1-connector,ec2-connector,geth1,geth2" +RUN apt update +RUN apt-get install -y screen +RUN apt-get install -y npm +#RUN npm -g config set proxy $HTTP_PROXY +RUN npm -g install n +RUN n --version +RUN n 8.9.0 +RUN npm -g install express diff --git a/packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/core/bin/www.ts b/packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/core/bin/www.ts new file mode 100644 index 0000000000..726de96103 --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-sawtooth-socketio/src/main/typescript/common/core/bin/www.ts @@ -0,0 +1,276 @@ +#!/usr/bin/env node + +/* + * Copyright 2021 Hyperledger Cactus Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * www.js + */ + +/* Summary: + * Connector: a part independent of end-chains + */ + +/** + * Module dependencies. + */ + +import app from '../app'; +const debug = require('debug')('connector:server'); +import https = require('https'); +import { config } from '../config/default'; +import fs = require('fs'); + +// Log settings +import { getLogger } from "log4js"; +const logger = getLogger('connector_main[' + process.pid + ']'); +logger.level = config.logLevel; + +// implementation class of a part dependent of end-chains (server plugin) +import { ServerPlugin } from '../../../connector/ServerPlugin'; +//const Splug = new ServerPlugin(); + +// destination dependency (MONITOR) implementation class +import { ServerMonitorPlugin } from '../../../connector/ServerMonitorPlugin'; +const Smonitor = new ServerMonitorPlugin(); + +/** + * Get port from environment and store in Express. + */ + +const sslport = normalizePort(process.env.PORT || config.sslParam.port); +app.set('port', sslport); + +// Specify private key and certificate +const sslParam = { + key: fs.readFileSync(config.sslParam.key), + cert: fs.readFileSync(config.sslParam.cert) +}; + +/** + * Create HTTPS server. + */ + +const server = https.createServer(sslParam, app); // Start as an https server. +const io = require('socket.io')(server); + +/** + * Listen on provided port, on all network interfaces. + */ + +server.listen(sslport, function(){ + console.log('listening on *:' + sslport); +}); +server.on('error', onError); +server.on('listening', onListening); + +/** + * Normalize a port into a number, string, or false. + */ + +function normalizePort(val) { + const port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + + return false; +} + +/** + * Event listener for HTTPS server "error" event. + */ + +function onError(error) { + 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': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +/** + * Event listener for HTTPS server "listening" event. + */ + +function onListening() { + const addr = server.address(); + const bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + debug('Listening on ' + bind); +} + +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; + console.log('##[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[func](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 { + // No such function + const emsg = "Function " + func + " not found!"; + logger.error(emsg); + const retObj = { + "status" : 504, + "errorDetail" : emsg + }; + client.emit("connector_error", retObj); + } + }); + + // 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; + if (data.reqID !== undefined) { + logger.info(`##add reqID: ${data.reqID}`); + args["reqID"] = data.reqID; + } + + + console.log('##[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 (methodType === "web3Eth") { + // Can be called with Server plugin function name. + Splug.web3Eth(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 (methodType === "contract") { + // Can be called with Server plugin function name. + Splug.contract(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 { + // No such function + const emsg = "method.type " + methodType + " 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(data) { + // Callback to receive monitoring results + const cb = function(callbackData) { + let emitType = ""; + if(callbackData.status == 200) { + emitType = "eventReceived"; + logger.info('event data callbacked.'); + } else { + emitType = "monitor_error"; + } + client.emit(emitType, callbackData); + } + + Smonitor.startMonitor(client.id, data.filterKey, cb); + }); + + /** + * 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); + }); +*/ + +}); +