diff --git a/fabric-client/lib/Client.js b/fabric-client/lib/Client.js index d2c0d6d31c..d5a5c77834 100644 --- a/fabric-client/lib/Client.js +++ b/fabric-client/lib/Client.js @@ -204,7 +204,8 @@ const Client = class extends BaseClient { const method = 'getConnectionOptions'; logger.debug('%s - start', method); let return_options = Object.assign({}, Client.getConfigSetting('connection-options')); - return_options = Object.assign({}, this._connection_options); + return_options = Object.assign(return_options, this._connection_options); + return_options = Object.assign(return_options, this._getLegacyOptions()); // keep for a while legacy options return_options = Object.assign(return_options, options); if (!return_options.clientCert) { @@ -214,6 +215,34 @@ const Client = class extends BaseClient { return return_options; } + // @deprecated + // Deprecated, but keep for a while for backward compatibility. Code is moved here from Remote.js; + _getLegacyOptions() { + const MAX_SEND = 'grpc.max_send_message_length'; + const MAX_RECEIVE = 'grpc.max_receive_message_length'; + const MAX_SEND_V10 = 'grpc-max-send-message-length'; + const MAX_RECEIVE_V10 = 'grpc-max-receive-message-length'; + const LEGACY_WARN_MESSAGE = 'Setting grpc options by utils.setConfigSetting() is deprecated. Use utils.g(s)etConfigSetting("connection-options")'; + const result = {}; + + if (typeof sdkUtils.getConfigSetting(MAX_RECEIVE) !== 'undefined') { + Object.assign(result, {[MAX_RECEIVE]: sdkUtils.getConfigSetting(MAX_RECEIVE)}); + } + if (typeof sdkUtils.getConfigSetting(MAX_RECEIVE_V10) !== 'undefined') { + Object.assign(result, {[MAX_RECEIVE]: sdkUtils.getConfigSetting(MAX_RECEIVE_V10)}); + } + if (typeof sdkUtils.getConfigSetting(MAX_SEND) !== 'undefined') { + Object.assign(result, {[MAX_SEND]: sdkUtils.getConfigSetting(MAX_SEND)}); + } + if (typeof sdkUtils.getConfigSetting(MAX_SEND_V10) !== 'undefined') { + Object.assign(result, {[MAX_SEND]: sdkUtils.getConfigSetting(MAX_SEND_V10)}); + } + if (Object.keys(result).length > 0) { + logger.warn(LEGACY_WARN_MESSAGE); + } + return result; + } + /** * Add a set of connection options to this client. These will be * available to be merged into an application's options when new diff --git a/fabric-client/lib/Remote.js b/fabric-client/lib/Remote.js index eff58d2c75..fb4ce7ffca 100644 --- a/fabric-client/lib/Remote.js +++ b/fabric-client/lib/Remote.js @@ -94,7 +94,9 @@ class Remote { if (typeof grpc_receive_max === 'undefined') { grpc_receive_max = -1; // default is unlimited } - this._options[MAX_RECEIVE] = grpc_receive_max; + // keep this for backward compatibility until remove probably deprecated code (lines 83-115) + this._options[MAX_RECEIVE] = (this._options[MAX_RECEIVE] && (this._options[MAX_RECEIVE] !== -1)) + ? this._options[MAX_RECEIVE] : grpc_receive_max; let grpc_send_max; if (opts[MAX_SEND_V10]) { @@ -110,7 +112,7 @@ class Remote { if (typeof grpc_send_max === 'undefined') { grpc_send_max = -1; // default is unlimited } - this._options[MAX_SEND] = grpc_send_max; + this._options[MAX_SEND] = (this._options[MAX_SEND] && (this._options[MAX_SEND] !== -1)) ? this._options[MAX_SEND] : grpc_send_max; // service connection this._url = url; diff --git a/fabric-client/test/Client.js b/fabric-client/test/Client.js index 8dd937259b..13f5e2b66c 100644 --- a/fabric-client/test/Client.js +++ b/fabric-client/test/Client.js @@ -2974,10 +2974,10 @@ describe('Client', () => { newOpts.hope.should.equal('found'); }); - it('should call _buildConnectionOptions and return opts', () => { + it('should call _buildConnectionOptions and return default opts', () => { const client = new Client(); const opts = client.getConfigSetting('connection-options'); - const newOpts = client._buildConnectionOptions(opts); + const newOpts = client._buildConnectionOptions(); newOpts.should.deep.equal(opts); }); }); diff --git a/test/integration/grpc.js b/test/integration/grpc.js index 5434718c7b..3ad6d47ce8 100644 --- a/test/integration/grpc.js +++ b/test/integration/grpc.js @@ -86,7 +86,7 @@ test('\n\n*** GRPC message size tests ***\n\n', async (t) => { } }; - // setup reusable artificates + // setup reusable certificates const opts = { pem: Buffer.from(data).toString(), clientCert: tlsInfo.certificate, @@ -97,6 +97,11 @@ test('\n\n*** GRPC message size tests ***\n\n', async (t) => { t.pass('Successfully setup grpc testing environment'); + // check default connection settings are loaded from the config/default.json + const defaultConnectionOptions = Client.getConfigSetting('connection-options'); + const clientOptionsSubset = getClientOptionsDefinedInDefault(client, defaultConnectionOptions); + t.deepEqual(defaultConnectionOptions, clientOptionsSubset, 'Test default connection settings are applied'); + // use the connection profile defined peer which includes a GRPC max setting response = await sendToConnectionProfile(client, channel, connection_profile, go_cc, 1); checkResponse(t, response, 'Test golang cc able to use connection profile set with grpc max receive', 'Received|max|1024'); @@ -160,18 +165,11 @@ test('\n\n*** GRPC message size tests ***\n\n', async (t) => { }); async function send(client, channel, url, cc, opts, megs, grpc_send_max, grpc_receive_max, sdk_send_max, sdk_receive_max) { - if (grpc_send_max !== null) { - utils.setConfigSetting('grpc.max_send_message_length', grpc_send_max); - } - if (grpc_receive_max !== null) { - utils.setConfigSetting('grpc.max_receive_message_length', grpc_receive_max); - } - if (sdk_send_max !== null) { - utils.setConfigSetting('grpc-max-send-message-length', sdk_send_max); - } - if (sdk_receive_max !== null) { - utils.setConfigSetting('grpc-max-receive-message-length', sdk_receive_max); - } + + sentConnectionOptionsIfDefined('grpc.max_send_message_length', grpc_send_max); + sentConnectionOptionsIfDefined('grpc.max_receive_message_length', grpc_receive_max); + sentConnectionOptionsIfDefined('grpc-max-send-message-length', sdk_send_max); + sentConnectionOptionsIfDefined('grpc-max-receive-message-length', sdk_receive_max); // replace the default loading client._connection_options = {}; @@ -223,3 +221,18 @@ function checkResponse(t, response, message, error_message) { t.fail('Failed to get an error message for ' + message); } } + +function sentConnectionOptionsIfDefined(optionName, optionValue) { + if (optionValue !== null) { + let connectionOptions = utils.getConfigSetting('connection-options'); + connectionOptions = Object.assign(connectionOptions, {[optionName]:optionValue}); + utils.setConfigSetting('connection-options', connectionOptions); + } +} + +function getClientOptionsDefinedInDefault(client, defaultConnectionOptions) { + const clientConnectionOptions = client._buildConnectionOptions(); + const clientOptionsSubset = {}; + Object.keys(defaultConnectionOptions).forEach(key => clientOptionsSubset[key] = clientConnectionOptions[key]); + return clientOptionsSubset; +}