Skip to content

Commit

Permalink
feat(Validator): Validator on Fabric-specific Ledger Plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Takuma TAKEUCHI <takeuchi.takuma@fujitsu.com>
  • Loading branch information
takeutak committed Jul 21, 2020
1 parent f6cf694 commit e6483ca
Show file tree
Hide file tree
Showing 35 changed files with 2,574 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
core/node_modules/
core/package-lock.json
dependent/node_modules/
dependent/package-lock.json
core/npm-debug.log
dependent/wallet/
core/wallet/

17 changes: 17 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
Copyright 2019-2020 Fujitsu Laboratories Ltd.
SPDX-License-Identifier: Apache-2.0
README.md
-->
# Fabric-specific

## Assumption
- A ledger (Hyperledger fabric node) to connect is already running
- Specify URL of the ledger node to connect to in "PluginConfig.js"

## Execution
<pre>
cd core
node ./bin/www.js
</pre>
16 changes: 16 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2019-2020 Fujitsu Laboratories Ltd.
# 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
10 changes: 10 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/core/CA/connector.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN CERTIFICATE-----
MIIBdTCCARoCCQC/F+Mh551QzDAKBggqhkjOPQQDAjBCMQswCQYDVQQGEwJKUDEQ
MA4GA1UECAwHZXNqbXMxMjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkg
THRkMB4XDTE4MDYyNzA3MjIzNVoXDTI4MDYyNDA3MjIzNVowQjELMAkGA1UEBhMC
SlAxEDAOBgNVBAgMB2Vzam1zMTIxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMg
UHR5IEx0ZDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABDPpSD2w0zrqJKraGD1b
5Jq2sDuacThSUqi7fvz8oyrWtuKDjZ15zIaSOtak6XRxFh9V9Gokdg5GNbW/pTZc
TuowCgYIKoZIzj0EAwIDSQAwRgIhAKH6ERsyd5bpEMIkY4clPqguwDWoTLk2VKq6
ONEhUqotAiEA4yJxGmZpFdRScG2gDUIF2VDeX+XfHdJI2J41hyW9/zI=
-----END CERTIFICATE-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN CERTIFICATE REQUEST-----
MIH9MIGkAgEAMEIxCzAJBgNVBAYTAkpQMRAwDgYDVQQIDAdlc2ptczEyMSEwHwYD
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjO
PQMBBwNCAAQz6Ug9sNM66iSq2hg9W+SatrA7mnE4UlKou378/KMq1rbig42decyG
kjrWpOl0cRYfVfRqJHYORjW1v6U2XE7qoAAwCgYIKoZIzj0EAwIDSAAwRQIgCUA1
B5mZK7Hx79J1xBb0MGwuoUkt4bGPXbHqWEMZXQMCIQCRgadPkrNw56+pT5MVxA5K
vV6xTgmxUYrYnpkR4tptqQ==
-----END CERTIFICATE REQUEST-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEICIlCfK3zMTFzUgdaj01LAHjJmHlbg6Xql9+i70iPz5EoAoGCCqGSM49
AwEHoUQDQgAEM+lIPbDTOuokqtoYPVvkmrawO5pxOFJSqLt+/PyjKta24oONnXnM
hpI61qTpdHEWH1X0aiR2DkY1tb+lNlxO6g==
-----END EC PRIVATE KEY-----
41 changes: 41 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/core/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019-2020 Fujitsu Laboratories Ltd.
* SPDX-License-Identifier: Apache-2.0
*
* app.js
*/

/* Summary:
*
*/

var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
console.log(err);
res.send(err);
});

module.exports = app;
208 changes: 208 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/core/bin/www.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#!/usr/bin/env node

/*
* Copyright 2019-2020 Fujitsu Laboratories Ltd.
* SPDX-License-Identifier: Apache-2.0
*
* www.js
*/

/* Summary:
* Connector: a part independent of end-chains
*/

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('connector:server');
var fs = require('fs');
var https = require('https');
var config = require('config');
// Log settings
var log4js = require('log4js');
var logger = log4js.getLogger('connector_main[' + process.pid + ']');
logger.level = config.logLevel;

// implementation class of a part dependent of end-chains (server plugin)
var ServerPlugin = require('../../dependent/ServerPlugin.js');
var Splug = new ServerPlugin();
// destination dependency (MONITOR) implementation class
var ServerMonitorPlugin = require('../../dependent/ServerMonitorPlugin.js');
var Smonitor = new ServerMonitorPlugin();

/**
* Get port from environment and store in Express.
*/

var sslport = normalizePort(process.env.PORT || config.sslParam.port);
app.set('port', sslport);

// Specify private key and certificate
var sslParam = {
key: fs.readFileSync(config.sslParam.key),
cert: fs.readFileSync(config.sslParam.cert)
};

/**
* Create HTTPS server.
*/

var server = https.createServer(sslParam, app); // Start as an https server.
var 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;
}

var 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() {
var addr = server.address();
var 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) {
var func = data.func;
var 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((resp_obj) => {
logger.info('*** RESPONSE ***');
logger.info('Client ID :' + client.id);
logger.info('Response :' + JSON.stringify(resp_obj));
client.emit("response", resp_obj);
})
.catch((err_obj) => {
logger.error('*** ERROR ***');
logger.error('Client ID :' + client.id);
logger.error('Detail :' + JSON.stringify(err_obj));
client.emit("connector_error", err_obj);
});
} else {
// No such function
var emsg = "Function " + func + " not found!";
logger.error(emsg);
var ret_obj = {
"status" : 504,
"errorDetail" : emsg
};
client.emit("connector_error", ret_obj);
}
});

/**
* startMonitor: starting block generation event monitoring
**/
client.on('startMonitor', function() {
// Callback to receive monitoring results
var cb = function(callbackData) {
var emit_type = "";
if(callbackData.status == 200) {
emit_type = "eventReceived";
logger.info('event data callbacked.');
} else {
emit_type = "monitor_error";
}
client.emit(emit_type, 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);
});

});

19 changes: 19 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/core/config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2019-2020 Fujitsu Laboratories Ltd.
* SPDX-License-Identifier: Apache-2.0
*
* default.js
*/

module.exports = {
// Defined value for the destination independent part. I don't think I can use it only at www, so I think I can write directly there.
// Destination dependent definition values should be in lib/PluginConfig.js.
"sslParam" : {
"port" : 5040,
//"port" : 5041,
"key" : './CA/connector.priv',
"cert" : './CA/connector.crt'
},
// Log level (trace/debug/info/warn/error/fatal)
"logLevel" : "debug"
};
19 changes: 19 additions & 0 deletions packages/ledger-plugin/fabric/validator/src/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "connector",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.17.1",
"config": "^1.26.1",
"cookie-parser": "~1.4.3",
"debug": "~4.1.1",
"express": "~4.15.2",
"log4js": "^3.0.6",
"morgan": "~1.8.1",
"serve-favicon": "~2.4.2",
"socket.io": "^2.0.4"
}
}
Loading

0 comments on commit e6483ca

Please sign in to comment.