Skip to content

Commit

Permalink
[FABN-909] Use new fabric-protos module
Browse files Browse the repository at this point in the history
Delete all of the protocol buffer definitions from the fabric-client module,
since they are now present in the fabric-protos module.

For both fabric-client and fabric-ca-client, remove the ProtoLoader class, add
a dependency on fabric-protos, and remove the copying of protos between the
two modules via gulp script.

Rewrite all code in the fabric-client module to load the protocol buffer
definitions from the fabric-protos module instead; this means a general rewrite
of code like:

const _commonProtos = ProtoLoader.load(...).common;
...
const block = _commonProtos.Block.decode(...);

-to-

const fabprotos = require('fabric-protos');
const block = fabprotos.common.Block.decode(...);

Change-Id: Id632b78bcadb1e4b037b0c153cc66920f3f0e733
Signed-off-by: Simon Stone <[email protected]>
  • Loading branch information
Simon Stone committed Jan 22, 2019
1 parent 97813bc commit 7703030
Show file tree
Hide file tree
Showing 66 changed files with 413 additions and 3,863 deletions.
3 changes: 0 additions & 3 deletions build/tasks/ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const DEPS = [
'fabric-client/lib/hash.js',
'fabric-client/lib/utils.js',
'fabric-client/lib/BaseClient.js',
'fabric-client/lib/ProtoLoader.js',
'fabric-client/lib/Remote.js',
'fabric-client/lib/User.js',
'fabric-client/lib/impl/bccsp_pkcs11.js',
Expand All @@ -24,8 +23,6 @@ const DEPS = [
'fabric-client/lib/impl/FileKeyValueStore.js',
'fabric-client/lib/msp/identity.js',
'fabric-client/lib/msp/msp.js',
'fabric-client/lib/protos/msp/identities.proto',
'fabric-client/lib/protos/msp/msp_config.proto',
'fabric-client/types/tsconfig.json',
'fabric-client/types/base.d.ts'
];
Expand Down
31 changes: 0 additions & 31 deletions fabric-ca-client/lib/ProtoLoader.js

This file was deleted.

1 change: 1 addition & 0 deletions fabric-ca-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"bn.js": "^4.11.3",
"elliptic": "^6.2.3",
"fabric-common": "file:../fabric-common",
"fabric-protos": "file:../fabric-protos",
"fs-extra": "^6.0.1",
"grpc": "1.14.2",
"js-sha3": "^0.7.0",
Expand Down
127 changes: 54 additions & 73 deletions fabric-client/lib/BlockDecoder.js

Large diffs are not rendered by default.

215 changes: 97 additions & 118 deletions fabric-client/lib/Channel.js

Large diffs are not rendered by default.

44 changes: 20 additions & 24 deletions fabric-client/lib/ChannelEventHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@ const util = require('util');

const BlockDecoder = require('./BlockDecoder.js');

const ProtoLoader = require('./ProtoLoader');
const _abProto = ProtoLoader.load(__dirname + '/protos/orderer/ab.proto').orderer;
const _eventsProto = ProtoLoader.load(__dirname + '/protos/peer/events.proto').protos;
const _commonProto = ProtoLoader.load(__dirname + '/protos/common/common.proto').common;
const _transProto = ProtoLoader.load(__dirname + '/protos/peer/transaction.proto').protos;
const fabprotos = require('fabric-protos');

const _validation_codes = {};
let keys = Object.keys(_transProto.TxValidationCode);
let keys = Object.keys(fabprotos.protos.TxValidationCode);
for (const key of keys) {
const new_key = _transProto.TxValidationCode[key];
const new_key = fabprotos.protos.TxValidationCode[key];
_validation_codes[new_key] = key;
}

const _header_types = {};
keys = Object.keys(_commonProto.HeaderType);
keys = Object.keys(fabprotos.common.HeaderType);
for (const key of keys) {
const new_key = _commonProto.HeaderType[key];
const new_key = fabprotos.common.HeaderType[key];
_header_types[new_key] = key;
}

Expand Down Expand Up @@ -382,7 +378,7 @@ class ChannelEventHub {
options = utils.checkAndAddConfigSetting('grpc.http2.min_time_between_pings_ms', five_minutes_ms, options);

logger.debug('_connect - options %j', options);
this._event_client = new _eventsProto.Deliver(this._peer._endpoint.addr, this._peer._endpoint.creds, options);
this._event_client = new fabprotos.protos.Deliver(this._peer._endpoint.addr, this._peer._endpoint.creds, options);
if (this._filtered_stream) {
this._stream = this._event_client.deliverFiltered();
} else {
Expand Down Expand Up @@ -684,38 +680,38 @@ class ChannelEventHub {
}

// The behavior when a missing block is encountered
let behavior = _abProto.SeekInfo.SeekBehavior.BLOCK_UNTIL_READY;
let behavior = fabprotos.orderer.SeekInfo.SeekBehavior.BLOCK_UNTIL_READY;
// build start
const seekStart = new _abProto.SeekPosition();
const seekStart = new fabprotos.orderer.SeekPosition();
if (this._starting_block_number) {
const seekSpecifiedStart = new _abProto.SeekSpecified();
const seekSpecifiedStart = new fabprotos.orderer.SeekSpecified();
seekSpecifiedStart.setNumber(this._starting_block_number);
seekStart.setSpecified(seekSpecifiedStart);
} else {
const seekNewest = new _abProto.SeekNewest();
const seekNewest = new fabprotos.orderer.SeekNewest();
seekStart.setNewest(seekNewest);
}

// build stop
const seekStop = new _abProto.SeekPosition();
const seekStop = new fabprotos.orderer.SeekPosition();
if (this._ending_block_newest) {
const seekNewest = new _abProto.SeekNewest();
const seekNewest = new fabprotos.orderer.SeekNewest();
seekStop.setNewest(seekNewest);
behavior = _abProto.SeekInfo.SeekBehavior.FAIL_IF_NOT_READY;
behavior = fabprotos.orderer.SeekInfo.SeekBehavior.FAIL_IF_NOT_READY;
} else {
const seekSpecifiedStop = new _abProto.SeekSpecified();
const seekSpecifiedStop = new fabprotos.orderer.SeekSpecified();
if (this._ending_block_number) {
seekSpecifiedStop.setNumber(this._ending_block_number);
// user should know the block does not exist
behavior = _abProto.SeekInfo.SeekBehavior.FAIL_IF_NOT_READY;
behavior = fabprotos.orderer.SeekInfo.SeekBehavior.FAIL_IF_NOT_READY;
} else {
seekSpecifiedStop.setNumber(Long.MAX_VALUE);
}
seekStop.setSpecified(seekSpecifiedStop);
}

// seek info with all parts
const seekInfo = new _abProto.SeekInfo();
const seekInfo = new fabprotos.orderer.SeekInfo();
seekInfo.setStart(seekStart);
seekInfo.setStop(seekStop);
// BLOCK_UNTIL_READY will mean hold the stream open and keep sending as
Expand All @@ -726,7 +722,7 @@ class ChannelEventHub {

// build the header for use with the seekInfo payload
const seekInfoHeader = clientUtils.buildChannelHeader(
_commonProto.HeaderType.DELIVER_SEEK_INFO,
fabprotos.common.HeaderType.DELIVER_SEEK_INFO,
this._channel._name,
txId.getTransactionID(),
this._initial_epoch,
Expand All @@ -736,7 +732,7 @@ class ChannelEventHub {
);

const seekHeader = clientUtils.buildHeader(identity, seekInfoHeader, txId.getNonce());
const seekPayload = new _commonProto.Payload();
const seekPayload = new fabprotos.common.Payload();
seekPayload.setHeader(seekHeader);
seekPayload.setData(seekInfo.toBuffer());
const seekPayloadBytes = seekPayload.toBuffer();
Expand Down Expand Up @@ -1291,7 +1287,7 @@ class ChannelEventHub {
}
} else {
logger.debug(`_processTxEvents block num=${block.header.number}`);
const txStatusCodes = block.metadata.metadata[_commonProto.BlockMetadataIndex.TRANSACTIONS_FILTER];
const txStatusCodes = block.metadata.metadata[fabprotos.common.BlockMetadataIndex.TRANSACTIONS_FILTER];
for (let index = 0; index < block.data.data.length; index++) {
const channel_header = block.data.data[index].payload.header.channel_header;
this._checkTransactionId(channel_header.tx_id,
Expand Down Expand Up @@ -1372,7 +1368,7 @@ class ChannelEventHub {
const chaincode_event = payload.action.proposal_response_payload.extension.events;
logger.debug('_processChaincodeEvents - chaincode_event %s', chaincode_event);

const txStatusCodes = block.metadata.metadata[_commonProto.BlockMetadataIndex.TRANSACTIONS_FILTER];
const txStatusCodes = block.metadata.metadata[fabprotos.common.BlockMetadataIndex.TRANSACTIONS_FILTER];
const channelHeader = block.data.data[index].payload.header.channel_header;
const val_code = txStatusCodes[index];

Expand Down
31 changes: 14 additions & 17 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ const path = require('path');
const yaml = require('js-yaml');
const Constants = require('./Constants.js');

const ProtoLoader = require('./ProtoLoader');
const _commonProto = ProtoLoader.load(__dirname + '/protos/common/common.proto').common;
const _configtxProto = ProtoLoader.load(__dirname + '/protos/common/configtx.proto').common;
const _queryProto = ProtoLoader.load(__dirname + '/protos/peer/query.proto').protos;
const fabprotos = require('fabric-protos');

const config = sdkUtils.getConfig();
// setup the location of the default config shipped with code
Expand Down Expand Up @@ -625,9 +622,9 @@ const Client = class extends BaseClient {
*/
extractChannelConfig(config_envelope) {
logger.debug('extractConfigUpdate - start');
const envelope = _commonProto.Envelope.decode(config_envelope);
const payload = _commonProto.Payload.decode(envelope.getPayload().toBuffer());
const configtx = _configtxProto.ConfigUpdateEnvelope.decode(payload.getData().toBuffer());
const envelope = fabprotos.common.Envelope.decode(config_envelope);
const payload = fabprotos.common.Payload.decode(envelope.getPayload().toBuffer());
const configtx = fabprotos.common.ConfigUpdateEnvelope.decode(payload.getData().toBuffer());
return configtx.getConfigUpdate().toBuffer();
}

Expand Down Expand Up @@ -666,7 +663,7 @@ const Client = class extends BaseClient {
const signer = this._getSigningIdentity(true);

// signature is across a signature header and the config update
const proto_signature_header = new _commonProto.SignatureHeader();
const proto_signature_header = new fabprotos.common.SignatureHeader();
proto_signature_header.setCreator(signer.serialize());
proto_signature_header.setNonce(sdkUtils.getNonce());
const signature_header_bytes = proto_signature_header.toBuffer();
Expand All @@ -677,7 +674,7 @@ const Client = class extends BaseClient {
const signature_bytes = Buffer.from(sig);

// build the return object
const proto_config_signature = new _configtxProto.ConfigSignature();
const proto_config_signature = new fabprotos.common.ConfigSignature();
proto_config_signature.setSignatureHeader(signature_header_bytes);
proto_config_signature.setSignature(signature_bytes);

Expand Down Expand Up @@ -763,7 +760,7 @@ const Client = class extends BaseClient {
let payload = null;
if (have_envelope) {
logger.debug('_createOrUpdateChannel - have envelope');
const envelope = _commonProto.Envelope.decode(request.envelope);
const envelope = fabprotos.common.Envelope.decode(request.envelope);
signature = envelope.signature;
payload = envelope.payload;
} else {
Expand All @@ -779,21 +776,21 @@ const Client = class extends BaseClient {
}

logger.debug('_createOrUpdateChannel - have config_update');
const proto_config_Update_envelope = new _configtxProto.ConfigUpdateEnvelope();
const proto_config_Update_envelope = new fabprotos.common.ConfigUpdateEnvelope();
proto_config_Update_envelope.setConfigUpdate(request.config);
const signatures = _stringToSignature(request.signatures);
proto_config_Update_envelope.setSignatures(signatures);

const proto_channel_header = clientUtils.buildChannelHeader(
_commonProto.HeaderType.CONFIG_UPDATE,
fabprotos.common.HeaderType.CONFIG_UPDATE,
request.name,
request.txId.getTransactionID()
);

const signer = this._getSigningIdentity(request.txId.isAdmin());

const proto_header = clientUtils.buildHeader(signer, proto_channel_header, request.txId.getNonce());
const proto_payload = new _commonProto.Payload();
const proto_payload = new fabprotos.common.Payload();
proto_payload.setHeader(proto_header);
proto_payload.setData(proto_config_Update_envelope.toBuffer());
const payload_bytes = proto_payload.toBuffer();
Expand Down Expand Up @@ -940,7 +937,7 @@ const Client = class extends BaseClient {
}
if (response.response) {
logger.debug('queryChannels - response status :: %d', response.response.status);
const queryTrans = _queryProto.ChannelQueryResponse.decode(response.response.payload);
const queryTrans = fabprotos.protos.ChannelQueryResponse.decode(response.response.payload);
logger.debug('queryChannels - ProcessedTransaction.channelInfo.length :: %s', queryTrans.channels.length);
for (const channel of queryTrans.channels) {
logger.debug('>>> channel id %s ', channel.channel_id);
Expand Down Expand Up @@ -1014,7 +1011,7 @@ const Client = class extends BaseClient {
}
if (response.response) {
logger.debug('queryInstalledChaincodes - response status :: %d', response.response.status);
const queryTrans = _queryProto.ChaincodeQueryResponse.decode(response.response.payload);
const queryTrans = fabprotos.protos.ChaincodeQueryResponse.decode(response.response.payload);
logger.debug('queryInstalledChaincodes - ProcessedTransaction.chaincodeInfo.length :: %s', queryTrans.chaincodes.length);
for (const chaincode of queryTrans.chaincodes) {
logger.debug('>>> name %s, version %s, path %s', chaincode.name, chaincode.version, chaincode.path);
Expand Down Expand Up @@ -1163,7 +1160,7 @@ const Client = class extends BaseClient {
}

const channelHeader = clientUtils.buildChannelHeader(
_commonProto.HeaderType.ENDORSER_TRANSACTION,
fabprotos.common.HeaderType.ENDORSER_TRANSACTION,
'', // install does not target a channel
tx_id.getTransactionID(),
null,
Expand Down Expand Up @@ -1842,7 +1839,7 @@ function _stringToSignature(string_signatures) {
} else {
logger.debug('_stringToSignature - signature is string');
const signature_bytes = Buffer.from(signature, 'hex');
signature = _configtxProto.ConfigSignature.decode(signature_bytes);
signature = fabprotos.common.ConfigSignature.decode(signature_bytes);
}
signatures.push(signature);
}
Expand Down
15 changes: 6 additions & 9 deletions fabric-client/lib/Orderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
const utils = require('./utils.js');
const Remote = require('./Remote');

const ProtoLoader = require('./ProtoLoader');
const fabprotos = require('fabric-protos');
const logger = utils.getLogger('Orderer.js');

const _abProto = ProtoLoader.load(__dirname + '/protos/orderer/ab.proto').orderer;
const _common = ProtoLoader.load(__dirname + '/protos/common/common.proto').common;

/**
* @typedef {Error} SYSTEM_TIMEOUT The Error message string that indicates that
* the request operation has timed out due to a system issue. This will
Expand Down Expand Up @@ -80,7 +77,7 @@ class Orderer extends Remote {
super(url, opts);

logger.debug('Orderer.const - url: %s timeout: %s', url, this._request_timeout);
this._ordererClient = new _abProto.AtomicBroadcast(this._endpoint.addr, this._endpoint.creds, this._options);
this._ordererClient = new fabprotos.orderer.AtomicBroadcast(this._endpoint.addr, this._endpoint.creds, this._options);
this._sendDeliverConnect = false;
}

Expand Down Expand Up @@ -237,16 +234,16 @@ class Orderer extends Remote {
logger.debug('sendDeliver - on data'); // response: %j', response);
// check the type of the response
if (response.Type === 'block') {
const blockHeader = new _common.BlockHeader();
const blockHeader = new fabprotos.common.BlockHeader();
blockHeader.setNumber(response.block.header.number);
blockHeader.setPreviousHash(response.block.header.previous_hash);
blockHeader.setDataHash(response.block.header.data_hash);
const blockData = new _common.BlockData();
const blockData = new fabprotos.common.BlockData();
blockData.setData(response.block.data.data);
const blockMetadata = new _common.BlockMetadata();
const blockMetadata = new fabprotos.common.BlockMetadata();
blockMetadata.setMetadata(response.block.metadata.metadata);

const block = new _common.Block();
const block = new fabprotos.common.Block();
block.setHeader(blockHeader);
block.setData(blockData);
block.setMetadata(blockMetadata);
Expand Down
7 changes: 3 additions & 4 deletions fabric-client/lib/Package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
'use strict';

const clientUtils = require('./client-utils.js');
const ProtoLoader = require('./ProtoLoader');
const fabprotos = require('fabric-protos');
const Packager = require('./Packager.js');
const tar = require('tar-stream');
const utils = require('./utils.js');
const zlib = require('zlib');

const _ccProto = ProtoLoader.load(__dirname + '/protos/peer/chaincode.proto').protos;
const logger = utils.getLogger('package');

/**
Expand Down Expand Up @@ -73,7 +72,7 @@ class Package {
* @returns {Package} The smart contract package.
*/
static async fromBuffer(buffer) {
const chaincodeDeploymentSpec = _ccProto.ChaincodeDeploymentSpec.decode(buffer);
const chaincodeDeploymentSpec = fabprotos.protos.ChaincodeDeploymentSpec.decode(buffer);
const fileNames = await Package._findFileNames(chaincodeDeploymentSpec);
return new Package(chaincodeDeploymentSpec, fileNames);
}
Expand Down Expand Up @@ -102,7 +101,7 @@ class Package {
}
};
logger.debug('Package.fromDirectory - built chaincode specification %s', JSON.stringify(chaincodeSpec));
const chaincodeDeploymentSpec = new _ccProto.ChaincodeDeploymentSpec();
const chaincodeDeploymentSpec = new fabprotos.protos.ChaincodeDeploymentSpec();
chaincodeDeploymentSpec.setChaincodeSpec(chaincodeSpec);
chaincodeDeploymentSpec.setCodePackage(codePackage);
const fileNames = await Package._findFileNames(chaincodeDeploymentSpec);
Expand Down
9 changes: 3 additions & 6 deletions fabric-client/lib/Peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

const utils = require('./utils.js');
const Remote = require('./Remote');
const ProtoLoader = require('./ProtoLoader');
const fabprotos = require('fabric-protos');
const util = require('util');

const _serviceProto = ProtoLoader.load(__dirname + '/protos/peer/peer.proto').protos;
const _discoveryProto = ProtoLoader.load(__dirname + '/protos/discovery/protocol.proto').discovery;

const logger = utils.getLogger('Peer.js');

/**
Expand Down Expand Up @@ -59,11 +56,11 @@ class Peer extends Remote {
_createClients() {
if (!this._endorserClient) {
logger.debug('_createClients - create peer endorser connection ' + this._endpoint.addr);
this._endorserClient = new _serviceProto.Endorser(this._endpoint.addr, this._endpoint.creds, this._options);
this._endorserClient = new fabprotos.protos.Endorser(this._endpoint.addr, this._endpoint.creds, this._options);
}
if (!this._discoveryClient) {
logger.debug('_createClients - create peer discovery connection ' + this._endpoint.addr);
this._discoveryClient = new _discoveryProto.Discovery(this._endpoint.addr, this._endpoint.creds, this._options);
this._discoveryClient = new fabprotos.discovery.Discovery(this._endpoint.addr, this._endpoint.creds, this._options);
}
}

Expand Down
Loading

0 comments on commit 7703030

Please sign in to comment.