Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/composer-admin/lib/adminconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const fs = require('fs');
const FSConnectionProfileStore = require('composer-common').FSConnectionProfileStore;
const Logger = require('composer-common').Logger;
const Util = require('composer-common').Util;
const uuid = require('uuid');

const LOG = Logger.getLog('AdminConnection');

Expand Down Expand Up @@ -392,7 +393,9 @@ class AdminConnection {
const method = 'activate';
LOG.entry(method);
const json = {
$class: 'org.hyperledger.composer.system.ActivateCurrentIdentity'
$class: 'org.hyperledger.composer.system.ActivateCurrentIdentity',
transactionId: uuid.v4(),
timestamp: new Date().toISOString()
};
return Util.invokeChainCode(this.securityContext, 'submitTransaction', ['default', JSON.stringify(json)])
.then(() => {
Expand Down
17 changes: 12 additions & 5 deletions packages/composer-admin/test/adminconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const ConnectionManager = require('composer-common').ConnectionManager;
const FSConnectionProfileStore = require('composer-common').FSConnectionProfileStore;
const SecurityContext = require('composer-common').SecurityContext;
const Util = require('composer-common').Util;
const uuid = require('uuid');

const version = require('../package.json').version;

Expand All @@ -38,6 +39,7 @@ describe('AdminConnection', () => {
let mockSecurityContext;
let adminConnection;
let sandbox;
let clock;

const config =
{
Expand Down Expand Up @@ -84,11 +86,13 @@ describe('AdminConnection', () => {
sinon.stub(adminConnection.connectionProfileStore, 'delete').withArgs('testprofile').resolves();
delete process.env.COMPOSER_CONFIG;
sandbox = sinon.sandbox.create();
clock = sinon.useFakeTimers();
});

afterEach(() => {
delete process.env.COMPOSER_CONFIG;
sandbox.restore();
clock.restore();
});

describe('#constructor', () => {
Expand Down Expand Up @@ -348,13 +352,14 @@ describe('AdminConnection', () => {
mockConnection.ping.onSecondCall().resolves(Buffer.from(JSON.stringify({
version: version
})));
mockConnection.invokeChainCode.withArgs(mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']).resolves();
sandbox.stub(uuid, 'v4').returns('c89291eb-969f-4b04-b653-82deb5ee0ba1');
mockConnection.invokeChainCode.resolves();
adminConnection.connection = mockConnection;
return adminConnection.ping()
.then(() => {
sinon.assert.calledTwice(mockConnection.ping);
sinon.assert.calledOnce(mockConnection.invokeChainCode);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity","transactionId":"c89291eb-969f-4b04-b653-82deb5ee0ba1","timestamp":"1970-01-01T00:00:00.000Z"}']);
});
});

Expand Down Expand Up @@ -391,7 +396,8 @@ describe('AdminConnection', () => {

it('should perform a security check', () => {
sandbox.stub(Util, 'securityCheck');
mockConnection.invokeChainCode.withArgs(mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']).resolves();
sandbox.stub(uuid, 'v4').returns('c89291eb-969f-4b04-b653-82deb5ee0ba1');
mockConnection.invokeChainCode.resolves();
adminConnection.connection = mockConnection;
return adminConnection.activate()
.then(() => {
Expand All @@ -400,12 +406,13 @@ describe('AdminConnection', () => {
});

it('should submit a request to the chaincode for activation', () => {
mockConnection.invokeChainCode.withArgs(mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']).resolves();
sandbox.stub(uuid, 'v4').returns('c89291eb-969f-4b04-b653-82deb5ee0ba1');
mockConnection.invokeChainCode.resolves();
adminConnection.connection = mockConnection;
return adminConnection.activate()
.then(() => {
sinon.assert.calledOnce(mockConnection.invokeChainCode);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity","transactionId":"c89291eb-969f-4b04-b653-82deb5ee0ba1","timestamp":"1970-01-01T00:00:00.000Z"}']);
});
});

Expand Down
4 changes: 3 additions & 1 deletion packages/composer-client/lib/businessnetworkconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,9 @@ class BusinessNetworkConnection extends EventEmitter {
const method = 'activate';
LOG.entry(method);
const json = {
$class: 'org.hyperledger.composer.system.ActivateCurrentIdentity'
$class: 'org.hyperledger.composer.system.ActivateCurrentIdentity',
transactionId: uuid.v4(),
timestamp: new Date().toISOString()
};
return Util.invokeChainCode(this.securityContext, 'submitTransaction', ['default', JSON.stringify(json)])
.then(() => {
Expand Down
15 changes: 10 additions & 5 deletions packages/composer-client/test/businessnetworkconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const sinon = require('sinon');
describe('BusinessNetworkConnection', () => {

let sandbox;
let clock;
let businessNetworkConnection;
let mockSecurityContext;
let mockConnection;
Expand All @@ -58,6 +59,7 @@ describe('BusinessNetworkConnection', () => {

beforeEach(() => {
sandbox = sinon.sandbox.create();
clock = sinon.useFakeTimers();
mockSecurityContext = sinon.createStubInstance(SecurityContext);
mockConnection = sinon.createStubInstance(Connection);
mockSecurityContext.getConnection.returns(mockConnection);
Expand Down Expand Up @@ -96,6 +98,7 @@ describe('BusinessNetworkConnection', () => {

afterEach(() => {
sandbox.restore();
clock.restore();
delete process.env.COMPOSER_CONFIG;
});

Expand Down Expand Up @@ -857,13 +860,14 @@ describe('BusinessNetworkConnection', () => {
mockConnection.ping.onSecondCall().resolves(Buffer.from(JSON.stringify({
version: version
})));
mockConnection.invokeChainCode.withArgs(mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']).resolves();
sandbox.stub(uuid, 'v4').returns('c89291eb-969f-4b04-b653-82deb5ee0ba1');
mockConnection.invokeChainCode.resolves();
businessNetworkConnection.connection = mockConnection;
return businessNetworkConnection.ping()
.then(() => {
sinon.assert.calledTwice(mockConnection.ping);
sinon.assert.calledOnce(mockConnection.invokeChainCode);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity","transactionId":"c89291eb-969f-4b04-b653-82deb5ee0ba1","timestamp":"1970-01-01T00:00:00.000Z"}']);
});
});

Expand Down Expand Up @@ -900,7 +904,7 @@ describe('BusinessNetworkConnection', () => {

it('should perform a security check', () => {
sandbox.stub(Util, 'securityCheck');
mockConnection.invokeChainCode.withArgs(mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']).resolves();
mockConnection.invokeChainCode.resolves();
businessNetworkConnection.connection = mockConnection;
return businessNetworkConnection.activate()
.then(() => {
Expand All @@ -909,12 +913,13 @@ describe('BusinessNetworkConnection', () => {
});

it('should submit a request to the chaincode for activation', () => {
mockConnection.invokeChainCode.withArgs(mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']).resolves();
sandbox.stub(uuid, 'v4').returns('c89291eb-969f-4b04-b653-82deb5ee0ba1');
mockConnection.invokeChainCode.resolves();
businessNetworkConnection.connection = mockConnection;
return businessNetworkConnection.activate()
.then(() => {
sinon.assert.calledOnce(mockConnection.invokeChainCode);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity"}']);
sinon.assert.calledWith(mockConnection.invokeChainCode, mockSecurityContext, 'submitTransaction', ['default', '{"$class":"org.hyperledger.composer.system.ActivateCurrentIdentity","transactionId":"c89291eb-969f-4b04-b653-82deb5ee0ba1","timestamp":"1970-01-01T00:00:00.000Z"}']);
});
});

Expand Down
42 changes: 27 additions & 15 deletions packages/composer-connector-hlfv1/lib/hlfconnectionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,27 @@ class HLFConnectionManager extends ConnectionManager {
* @param {string} peer The peer URL.
* @param {number} timeout the request timeout
* @param {string} globalCert if provided use this unless cert is provided
* @param {array} peers Array to store any created peers
* @param {array} eventHubDefs Array to store any created event hubs
* @return {Peer} A new peer.
*/
static parsePeer(peer, timeout, globalCert, eventHubDefs) {
static parsePeer(peer, timeout, globalCert, peers, eventHubDefs) {
const method = 'parsePeer';

const opts = HLFConnectionManager._createOpts(timeout, peer.cert, peer.hostnameOverride, globalCert);
if (!peer.requestURL && !peer.eventURL) {
throw new Error('peer incorrectly defined');
}
if (peer.requestURL && !peer.eventURL) {
throw new Error(`The peer at requestURL ${peer.requestURL} has no eventURL defined`);

if (peer.requestURL) {
const hfc_peer = HLFConnectionManager.createPeer(peer.requestURL, opts);
LOG.debug(method, 'Adding peer URL', peer.requestURL);
peers.push(hfc_peer);
}
if (!peer.requestURL && peer.eventURL) {
throw new Error(`The peer at eventURL ${peer.eventURL} has no requestURL defined`);
if (peer.eventURL) {
const eventHub = HLFConnectionManager.createEventHubDefinition(peer.eventURL, opts);
LOG.debug(method, 'Setting event hub URL', peer.eventURL);
eventHubDefs.push(eventHub);
}

const hfc_peer = HLFConnectionManager.createPeer(peer.requestURL, opts);
// extract and save event hub definitions for later.
const eventHub = HLFConnectionManager.createEventHubDefinition(peer.eventURL, opts);
LOG.debug(method, 'Setting event hub URL', peer.eventURL);
eventHubDefs.push(eventHub);
return hfc_peer;
}

/**
Expand Down Expand Up @@ -450,13 +448,27 @@ class HLFConnectionManager extends ConnectionManager {
channel.addOrderer(HLFConnectionManager.parseOrderer(orderer, connectOptions.timeout, connectOptions.globalCert));
});

// Parse all of the peers.
let peers = [];
let eventHubDefs = [];
// Load all of the peers into the client.
connectOptions.peers.forEach((peer) => {
HLFConnectionManager.parsePeer(peer, connectOptions.timeout, connectOptions.globalCert, peers, eventHubDefs);
});

// Check for at least one peer and at least one event hub.
if (peers.length === 0) {
throw new Error('You must specify at least one peer with a valid requestURL for submitting transactions');
} else if (eventHubDefs.length === 0) {
throw new Error('You must specify at least one peer with a valid eventURL for receiving events');
}

// Load all of the peers into the client.
peers.forEach((peer) => {
LOG.debug(method, 'Adding peer URL', peer);
channel.addPeer(HLFConnectionManager.parsePeer(peer, connectOptions.timeout, connectOptions.globalCert, eventHubDefs));
channel.addPeer(peer);
});

// Set up the wallet.
return this._setupWallet(client, wallet, connectOptions.keyValStore)
.then(() => {

Expand Down
Loading