Skip to content

Commit 9e520d0

Browse files
committed
feat(validator): go-ethereum validator including execSyncFunction
Signed-off-by: Takuma TAKEUCHI <[email protected]>
1 parent 869933b commit 9e520d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2211
-2
lines changed

examples/cartrade/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
## Premise
44
- Launch two Validators (For Ethereum and Fabric)
55
- for Ethereum:
6-
- `/packages/ledger-plugin/go-ethereum/validator`
6+
- `/packages/ledger-plugin/go-ethereum-ts/validator`
77
- "validatorUrl": `https://localhost:5050`,
8+
1. cd `/packages/ledger-plugin/go-ethereum-ts/validator/src`
9+
1. npm install
10+
1. npm run build
11+
1. npm run start
812
- for Fabric:
913
- `/packages/ledger-plugin/fabric/validator`
1014
- "validatorUrl": `https://localhost:5040`,

examples/cartrade/config/usersetting.json

+4
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
"rejectUnauthorized": false,
1515
"reconnection": false,
1616
"timeout": 20000
17+
},
18+
"verifier": {
19+
"maxCounterRequestID":100,
20+
"syncFunctionTimeoutMillisecond":5000
1721
}
1822
}

examples/cartrade/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@hyperledger/cactus",
2+
"name": "@hyperledger-labs/cactus",
33
"private": true,
44
"scripts": {
55
"run-ci": "./tools/ci.sh",

packages/business-logic-plugin/app.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import bodyParser = require('body-parser');
1717
import indexRouter from '../routing-interface/routes/index';
1818
import loginRouter from '../routing-interface/routes/login';
1919
import tradesRouter from '../routing-interface/routes/trades';
20+
import balanceRouter from '../routing-interface/routes/balance';
2021

2122
const app: express.Express = express();
2223

@@ -31,6 +32,7 @@ app.use(bodyParser.json());
3132
app.use('/', indexRouter);
3233
app.use('/api/v1/bl/login/', loginRouter);
3334
app.use('/api/v1/bl/trades/', tradesRouter);
35+
app.use('/api/v1/bl/balance/', balanceRouter);
3436

3537
// catch 404 and forward to error handler
3638
app.use((req: Request, res: Response, next: NextFunction) => {

packages/config/default.json

+4
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
"rejectUnauthorized": false,
1515
"reconnection": false,
1616
"timeout": 20000
17+
},
18+
"verifier": {
19+
"maxCounterRequestID":100,
20+
"syncFunctionTimeoutMillisecond":5000
1721
}
1822
}

packages/ledger-plugin/VerifierBase.ts

+78
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class VerifierBase implements Verifier {
2626
validatorID: string = "";
2727
validatorUrl: string = "";
2828
apiInfo: {} = null;
29+
counterReqID: number = 1;
2930
eventListener: VerifierEventListener | null = null; // Listener for events from Ledger
3031

3132
constructor(ledgerInfo: string) {
@@ -43,6 +44,7 @@ export class VerifierBase implements Verifier {
4344
return makeApiInfoList(this.apiInfo);
4445
};
4546

47+
// NOTE: This function will be deleted due to the updating of API functions
4648
requestLedgerOperation(param: LedgerOperation): void {
4749
logger.debug('call : requestLedgerOperation');
4850
try {
@@ -69,6 +71,75 @@ export class VerifierBase implements Verifier {
6971
throw err;
7072
}
7173
};
74+
75+
execSyncFunction(param: LedgerOperation): Promise<any> {
76+
return new Promise((resolve, reject) => {
77+
logger.debug('call : execSyncFunction');
78+
try {
79+
logger.debug(`##in execSyncFunction, LedgerOperation = ${JSON.stringify(param)}`);
80+
let responseFlag: boolean = false;
81+
82+
const reqID = this.genarateReqID();
83+
logger.debug(`##execSyncFunction, reqID = ${reqID}`);
84+
85+
const socketOptions: {} = {
86+
rejectUnauthorized: config.socketOptions.rejectUnauthorized,
87+
reconnection: config.socketOptions.reconnection,
88+
timeout: config.socketOptions.timeout,
89+
};
90+
logger.debug(`socketOptions = ${JSON.stringify(socketOptions)}`);
91+
const socket: Socket = io(this.validatorUrl, socketOptions);
92+
socket.on("connect_error", (err: object) => {
93+
logger.error("##connect_error:", err);
94+
// end communication
95+
socket.disconnect();
96+
reject(err);
97+
});
98+
socket.on("connect_timeout", (err: object) => {
99+
logger.error("####Error:", err);
100+
// end communication
101+
socket.disconnect();
102+
reject(err);
103+
});
104+
socket.on("error", (err: object) => {
105+
logger.error("####Error:", err);
106+
socket.disconnect();
107+
reject(err);
108+
});
109+
socket.on("response", (result: any) => {
110+
logger.debug("#[recv]response, res: " + json2str(result));
111+
if (reqID === result.id) {
112+
responseFlag = true;
113+
logger.debug(`##execSyncFunction: resObj: ${JSON.stringify(result.resObj)}`);
114+
resolve(result.resObj);
115+
}
116+
});
117+
118+
const apiType: string = param.apiType;
119+
//const progress: string = param.progress;
120+
let data: {} = param.data;
121+
data["reqID"] = reqID;
122+
const requestData: {} = {
123+
func: apiType,
124+
args: data
125+
};
126+
logger.debug('requestData : ' + JSON.stringify(requestData));
127+
socket.emit('request', requestData);
128+
logger.debug('set timeout');
129+
130+
setTimeout(() => {
131+
if (responseFlag === false) {
132+
logger.debug('requestTimeout reqID : ' + reqID);
133+
resolve({"status":504, "amount":0});
134+
}
135+
}, config.verifier.syncFunctionTimeoutMillisecond);
136+
}
137+
catch (err) {
138+
logger.error(`##Error: execSyncFunction, ${err}`);
139+
reject(err);
140+
}
141+
});
142+
}
72143

73144
startMonitor(): Promise<LedgerEvent> {
74145
return new Promise((resolve, reject) => {
@@ -173,6 +244,13 @@ export class VerifierBase implements Verifier {
173244
this.eventListener = eventListener;
174245
return;
175246
};
247+
248+
genarateReqID(): string {
249+
if (this.counterReqID > config.verifier.maxCounterRequestID) {
250+
this.counterReqID = 1;
251+
}
252+
return `${this.validatorID}_${this.counterReqID++}`;
253+
}
176254

177255
// Validator -> Verifier
178256
// NOTE: The following methods are not implemented this time
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
core/node_modules/
2+
core/package-lock.json
3+
dependent/node_modules/
4+
dependent/package-lock.json
5+
core/npm-debug.log
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!--
2+
Copyright 2019-2020 Fujitsu Laboratories Ltd.
3+
SPDX-License-Identifier: Apache-2.0
4+
5+
README.md
6+
-->
7+
# BIF-trial(Validator)
8+
9+
## Assumption
10+
- geth1(geth-docker) is running
11+
- Specify the geth1 URL to connect to with "validatorUrl" in "config/default.js"
12+
13+
## Run
14+
<pre>
15+
cd core
16+
node ./bin/www.js
17+
</pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2019-2020 Fujitsu Laboratories Ltd.
2+
# SPDX-License-Identifier: Apache-2.0
3+
FROM hyperledger/fabric-ccenv:x86_64-1.0.4
4+
#ENV http_proxy $HTTP_PROXY
5+
#ENV https_proxy $HTTP_PROXY
6+
#ENV HTTP_PROXY $HTTP_PROXY
7+
#ENV HTTPS_PROXY $HTTP_PROXY
8+
#ENV NO_PROXY "rest-server,ec1-connector,ec2-connector,geth1,geth2"
9+
RUN apt update
10+
RUN apt-get install -y screen
11+
RUN apt-get install -y npm
12+
#RUN npm -g config set proxy $HTTP_PROXY
13+
RUN npm -g install n
14+
RUN n --version
15+
RUN n 8.9.0
16+
RUN npm -g install express
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import * as shell from 'shelljs';
2+
shell.cp('-R', 'core/CA/', '../dist/core');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBdTCCARoCCQC/F+Mh551QzDAKBggqhkjOPQQDAjBCMQswCQYDVQQGEwJKUDEQ
3+
MA4GA1UECAwHZXNqbXMxMjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkg
4+
THRkMB4XDTE4MDYyNzA3MjIzNVoXDTI4MDYyNDA3MjIzNVowQjELMAkGA1UEBhMC
5+
SlAxEDAOBgNVBAgMB2Vzam1zMTIxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMg
6+
UHR5IEx0ZDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABDPpSD2w0zrqJKraGD1b
7+
5Jq2sDuacThSUqi7fvz8oyrWtuKDjZ15zIaSOtak6XRxFh9V9Gokdg5GNbW/pTZc
8+
TuowCgYIKoZIzj0EAwIDSQAwRgIhAKH6ERsyd5bpEMIkY4clPqguwDWoTLk2VKq6
9+
ONEhUqotAiEA4yJxGmZpFdRScG2gDUIF2VDeX+XfHdJI2J41hyW9/zI=
10+
-----END CERTIFICATE-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN CERTIFICATE REQUEST-----
2+
MIH9MIGkAgEAMEIxCzAJBgNVBAYTAkpQMRAwDgYDVQQIDAdlc2ptczEyMSEwHwYD
3+
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjO
4+
PQMBBwNCAAQz6Ug9sNM66iSq2hg9W+SatrA7mnE4UlKou378/KMq1rbig42decyG
5+
kjrWpOl0cRYfVfRqJHYORjW1v6U2XE7qoAAwCgYIKoZIzj0EAwIDSAAwRQIgCUA1
6+
B5mZK7Hx79J1xBb0MGwuoUkt4bGPXbHqWEMZXQMCIQCRgadPkrNw56+pT5MVxA5K
7+
vV6xTgmxUYrYnpkR4tptqQ==
8+
-----END CERTIFICATE REQUEST-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN EC PARAMETERS-----
2+
BggqhkjOPQMBBw==
3+
-----END EC PARAMETERS-----
4+
-----BEGIN EC PRIVATE KEY-----
5+
MHcCAQEEICIlCfK3zMTFzUgdaj01LAHjJmHlbg6Xql9+i70iPz5EoAoGCCqGSM49
6+
AwEHoUQDQgAEM+lIPbDTOuokqtoYPVvkmrawO5pxOFJSqLt+/PyjKta24oONnXnM
7+
hpI61qTpdHEWH1X0aiR2DkY1tb+lNlxO6g==
8+
-----END EC PRIVATE KEY-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019-2020 Fujitsu Laboratories Ltd.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* app.js
6+
*/
7+
8+
/* Summary:
9+
*
10+
*/
11+
12+
import { NextFunction, Request, Response } from 'express';
13+
import createError = require('http-errors');
14+
import express = require('express');
15+
import cookieParser = require('cookie-parser');
16+
import bodyParser = require('body-parser');
17+
18+
const app: express.Express = express();
19+
20+
app.use(bodyParser.json());
21+
app.use(bodyParser.urlencoded({ extended: false }));
22+
app.use(cookieParser());
23+
24+
// catch 404 and forward to error handler
25+
app.use((req: Request, res: Response, next: NextFunction) => {
26+
next(createError(404));
27+
});
28+
29+
// error handler
30+
app.use((err: { message: string, status?: number }, req: Request, res: Response, next: NextFunction) => {
31+
// set locals, only providing error in development
32+
res.locals.message = err.message;
33+
res.locals.error = req.app.get('env') === 'development' ? err : {};
34+
35+
// set erreor response
36+
const errorResponse: {} = {
37+
"statusCode": err.status || 500,
38+
"message": err.message
39+
};
40+
41+
// render the error page
42+
res.status(err.status || 500);
43+
res.send(errorResponse);
44+
});
45+
46+
export default app;

0 commit comments

Comments
 (0)