Skip to content

Commit

Permalink
FABCN-395: Use @grpc/grpc-js for Node.js chaincode (#143) (#144)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone authored May 11, 2020
1 parent 8de8a6e commit a81935a
Show file tree
Hide file tree
Showing 8 changed files with 981 additions and 699 deletions.
1,597 changes: 929 additions & 668 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libraries/fabric-shim/lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
process.env.GRPC_SSL_CIPHER_SUITES = 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384';

const protoLoader = require('@grpc/proto-loader');
const grpc = require('grpc');
const grpc = require('@grpc/grpc-js');
const fabprotos = require('../bundle');
const path = require('path');
const {URL} = require('url');
Expand Down
4 changes: 2 additions & 2 deletions libraries/fabric-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
},
"dependencies": {
"@fidm/x509": "^1.2.1",
"@grpc/proto-loader": "^0.5.1",
"@grpc/grpc-js": "^1.0.3",
"@grpc/proto-loader": "^0.5.4",
"@types/node": "^8.9.4",
"ajv": "^6.5.5",
"fabric-contract-api": "2.1.2-unstable",
"fabric-shim-api": "2.1.2-unstable",
"fs-extra": "8.1.0",
"grpc": "^1.23.3",
"reflect-metadata": "^0.1.12",
"winston": "^3.2.1",
"yargs": "^13.3.0 ",
Expand Down
34 changes: 18 additions & 16 deletions libraries/fabric-shim/test/unit/chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ const expect = chai.expect;
const rewire = require('rewire');
const fabprotos = require('../../bundle');
const path = require('path');
const fs = require('fs');

const Logger = require('../../lib/logger');

const Stub = require('../../lib/stub');
const chaincodePath = '../../lib/chaincode.js';
const StartCommand = require('../../lib/cmds/startCommand.js');

const caPath = path.join(__dirname, 'test-ca.pem');
const certPath = path.join(__dirname, 'test-cert.pem');
const keyPath = path.join(__dirname, 'test-key.pem');

const ca = fs.readFileSync(caPath, 'utf8');
const key = fs.readFileSync(keyPath, 'utf8');
const cert = fs.readFileSync(certPath, 'utf8');

describe('Chaincode', () => {
let Chaincode;
let sandbox;
Expand Down Expand Up @@ -159,15 +168,13 @@ describe('Chaincode', () => {
});

describe ('TLS handling', () => {
const testfile = path.join(__dirname, '../../../../package.json');

const myYargs = {'argv': {'$0': 'fabric-chaincode-node', 'peer.address': 'localhost:7051', 'chaincode-id-name': 'mycc'}};

let getArgsStub;

before(() => {
process.env.CORE_PEER_TLS_ENABLED = true;
process.env.CORE_PEER_TLS_ROOTCERT_FILE = testfile;
process.env.CORE_PEER_TLS_ROOTCERT_FILE = caPath;
});

beforeEach(() => {
Expand Down Expand Up @@ -198,7 +205,7 @@ describe('Chaincode', () => {
});

it ('should throw an error when CORE_TLS_CLIENT_KEY_PATH env var set but CORE_TLS_CLIENT_CERT_PATH env var not set', () => {
process.env.CORE_TLS_CLIENT_KEY_PATH = testfile;
process.env.CORE_TLS_CLIENT_KEY_PATH = keyPath;
expect(() => {
Chaincode.start({Init: function() {}, Invoke: function() {}});
}).to.throw(/The client key and cert are needed when TLS is enabled, but environment variables specifying the paths to these files are missing/);
Expand All @@ -209,8 +216,8 @@ describe('Chaincode', () => {
const handlerClass = Chaincode.__get__('Handler');
const chat = sandbox.stub(handlerClass.prototype, 'chat');

process.env.CORE_TLS_CLIENT_KEY_PATH = testfile;
process.env.CORE_TLS_CLIENT_CERT_PATH = testfile;
process.env.CORE_TLS_CLIENT_KEY_PATH = keyPath;
process.env.CORE_TLS_CLIENT_CERT_PATH = certPath;

Chaincode.start({Init: function() {}, Invoke: function() {}});

Expand Down Expand Up @@ -243,22 +250,17 @@ describe('Chaincode', () => {
const handlerClass = Chaincode.__get__('Handler');
Chaincode.__set__('Handler', MockHandler);

process.env.CORE_TLS_CLIENT_KEY_PATH = testfile;
process.env.CORE_TLS_CLIENT_CERT_PATH = testfile;
process.env.CORE_TLS_CLIENT_KEY_PATH = keyPath;
process.env.CORE_TLS_CLIENT_CERT_PATH = certPath;

Chaincode.start({Init: function() {}, Invoke: function() {}});

sinon.assert.calledOnce(getArgsStub);
sinon.assert.calledWith(getArgsStub, myYargs);

const attributes = ['pem', 'cert', 'key'];

attributes.forEach((attr) => {
expect(typeof testOpts[attr]).to.deep.equal('string');

const json = JSON.parse(testOpts[attr]);
expect(json.name).to.deep.equal('fabric-chaincode-node');
});
testOpts.pem.should.equal(ca);
testOpts.cert.should.equal(cert);
testOpts.key.should.equal(key);

Chaincode.__set__('Handler', handlerClass);
});
Expand Down
30 changes: 18 additions & 12 deletions libraries/fabric-shim/test/unit/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const StateQueryIterator = require('../../../fabric-shim/lib/iterators.js').Stat
const HistoryQueryIterator = require('../../../fabric-shim/lib/iterators.js').HistoryQueryIterator;

const fabprotos = require('../../bundle');
const grpc = require('grpc');
const grpc = require('@grpc/grpc-js');
const fs = require('fs');
const path = require('path');

const sandbox = sinon.createSandbox();

Expand All @@ -30,10 +32,14 @@ const mockChaincodeImpl = {
Invoke: function() {}
};

const ca = fs.readFileSync(path.join(__dirname, 'test-ca.pem'), 'utf8');
const key = fs.readFileSync(path.join(__dirname, 'test-key.pem'), 'utf8');
const cert = fs.readFileSync(path.join(__dirname, 'test-cert.pem'), 'utf8');

const mockOpts = {
pem: 'dummy pem string',
key: 'dummy key',
cert: 'dummy cert'
pem: ca,
key: key,
cert: cert
};

const mockPeerAddress = {
Expand Down Expand Up @@ -401,8 +407,8 @@ describe('Handler', () => {
expect(handler._request_timeout).to.deep.equal(30000);
expect(handler._endpoint.addr).to.deep.equal(mockPeerAddress.base);
expect(credsSpy.calledOnce).to.be.true;
expect(handler._endpoint.creds.constructor.name).to.deep.equal('ChannelCredentials');
expect(handler._client.constructor.name).to.deep.equal('ServiceClient');
expect(handler._endpoint.creds.constructor.name).to.deep.equal('InsecureChannelCredentialsImpl');
expect(handler._client.constructor.name).to.deep.equal('ServiceClientImpl');

credsSpy.restore();
});
Expand Down Expand Up @@ -450,16 +456,16 @@ describe('Handler', () => {
it ('should throw an error if connection secure encoded private key not passed as opt', () => {
expect(() => {
new Handler(mockChaincodeImpl, mockPeerAddress.secure, {
pem: 'dummy pem string'
pem: ca
});
}).to.throw(/encoded Private key is required./);
});

it ('should throw an error if connection secure encoded private key not passed as opt', () => {
expect(() => {
new Handler(mockChaincodeImpl, mockPeerAddress.secure, {
pem: 'dummy pem string',
key: 'dummy key'
pem: ca,
key: key
});
}).to.throw(/encoded client certificate is required./);
});
Expand All @@ -474,8 +480,8 @@ describe('Handler', () => {
expect(handler._endpoint.addr).to.deep.equal(mockPeerAddress.base);
expect(credsSpy.calledOnce).to.be.true;
expect(credsSpy.calledWith(Buffer.from(mockOpts.pem), Buffer.from(mockOpts.key, 'base64'), Buffer.from(mockOpts.cert, 'base64'))).to.be.true;
expect(handler._endpoint.creds.constructor.name).to.deep.equal('ChannelCredentials');
expect(handler._client.constructor.name).to.deep.equal('ServiceClient');
expect(handler._endpoint.creds.constructor.name).to.deep.equal('SecureChannelCredentialsImpl');
expect(handler._client.constructor.name).to.deep.equal('ServiceClientImpl');
});

it ('should set grpc ssl options when ssl-target-name-override passed', () => {
Expand Down Expand Up @@ -1681,7 +1687,7 @@ describe('Handler', () => {
const createStub = Handler.__get__('createStub');
createStub({}, 'channelID', 'txID', 'some input', 'some proposal');

expect(mockStub.calledWithNew).to.be.ok; //believe wrong
expect(mockStub.calledWithNew).to.be.ok; // believe wrong
expect(mockStub.calledWithNew()).to.be.false;
expect(mockStub.firstCall.args[0]).to.deep.equal({});
expect(mockStub.firstCall.args[1]).to.deep.equal('channelID');
Expand Down
11 changes: 11 additions & 0 deletions libraries/fabric-shim/test/unit/test-ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE-----
MIIBkzCB9QIJAJ0Z8jFiPPi9MAoGCCqGSM49BAMCMA4xDDAKBgNVBAMMA3RsczAe
Fw0yMDA1MDcxNjM0MjFaFw0zMDA1MDUxNjM0MjFaMA4xDDAKBgNVBAMMA3RsczCB
mzAQBgcqhkjOPQIBBgUrgQQAIwOBhgAEACoUS3zg9Qj5CgQeNCY+9sPM2YWYHUUQ
SBE/oYpgrWV8E8TqtWkWchwXP4OoZAq7bMJ2bNQE5Sq6IY+bZrYpOKjfASpyS4qR
4xJfCun7BIZAjYHvVqmcuF8aJafh8F93GBjkILHgHTrtLLsAq6sBpzEuVJlsuf1h
LhKnCAqvfEtC1IBVMAoGCCqGSM49BAMCA4GMADCBiAJCANGUp459P3aMhtTZdXFq
mc8QVM7rHR3Zli9kmWsGVdJvbUbuHcX6+0AU1S8R2DhPA8oeBuQT7dbovYfweuVb
QfR3AkIApaKesiN9LNy9arSBGXTFKJquBT63v4bi4fyCNhhdC3wxKetsJ1DCpIdr
Bl8ZncJxUMjCrd7BNlkBNP7jCGdKpPk=
-----END CERTIFICATE-----
1 change: 1 addition & 0 deletions libraries/fabric-shim/test/unit/test-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJrakNCOVFJSkFPZTRqL3NqV3ltRE1Bb0dDQ3FHU000OUJBTUNNQTR4RERBS0JnTlZCQU1NQTNSc2N6QWUKRncweU1EQTFNRGN4TmpNek5UUmFGdzB6TURBMU1EVXhOak16TlRSYU1BNHhEREFLQmdOVkJBTU1BM1JzY3pDQgptekFRQmdjcWhrak9QUUlCQmdVcmdRUUFJd09CaGdBRUFPcllIem5BZmVXZ3pVM3dVZ1Q1Ylk5NkUvT2ZMb3p4CitlUUF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UFJJVkFURSBLRVktLS0tLQ==

0 comments on commit a81935a

Please sign in to comment.