From 0665f411484911edaf16422437c646717bdd9034 Mon Sep 17 00:00:00 2001 From: Konstantin Shuplenkov Date: Tue, 12 May 2020 17:29:53 +0300 Subject: [PATCH] feat: use protocol version interceptor in clients (#63) Co-authored-by: Djavid Gabibiyan --- clients/nodejs/CorePromiseClient.js | 196 ++++++++++-------- clients/nodejs/PlatformPromiseClient.js | 127 ++++++------ .../TransactionsFilterStreamPromiseClient.js | 40 ++-- package-lock.json | 82 ++++++-- package.json | 2 +- 5 files changed, 259 insertions(+), 188 deletions(-) diff --git a/clients/nodejs/CorePromiseClient.js b/clients/nodejs/CorePromiseClient.js index b669849..1c9a826 100644 --- a/clients/nodejs/CorePromiseClient.js +++ b/clients/nodejs/CorePromiseClient.js @@ -9,6 +9,7 @@ const { client: { interceptors: { jsonToProtobufInterceptorFactory, + protocolVersionInterceptorFactory, }, converters: { jsonToProtobufFactory, @@ -55,90 +56,6 @@ const getCoreDefinition = require('../../lib/getCoreDefinition'); const CoreNodeJSClient = getCoreDefinition(); -const getStatusOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocGetStatusResponse, - PBJSGetStatusResponse, - ), - protobufToJsonFactory( - PBJSGetStatusRequest, - ), - ), - ], -}; - -const getBlockOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocGetBlockResponse, - PBJSGetBlockResponse, - ), - protobufToJsonFactory( - PBJSGetBlockRequest, - ), - ), - ], -}; - -const sendTransactionOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocSendTransactionResponse, - PBJSSendTransactionResponse, - ), - protobufToJsonFactory( - PBJSSendTransactionRequest, - ), - ), - ], -}; - -const getTransactionOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocGetTransactionResponse, - PBJSGetTransactionResponse, - ), - protobufToJsonFactory( - PBJSGetTransactionRequest, - ), - ), - ], -}; - -const getEstimatedTransactionFeeOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocGetEstimatedTransactionFeeResponse, - PBJSGetEstimatedTransactionFeeResponse, - ), - protobufToJsonFactory( - PBJSGetEstimatedTransactionFeeRequest, - ), - ), - ], -}; - -const subscribeToBlockHeadersWithChainLocksOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocBlockHeadersWithChainLocksResponse, - PBJSBlockHeadersWithChainLocksResponse, - ), - protobufToJsonFactory( - PBJSBlockHeadersWithChainLocksRequest, - ), - ), - ], -}; - class CorePromiseClient { /** * @param {string} hostname @@ -167,6 +84,8 @@ class CorePromiseClient { this.client.getEstimatedTransactionFee = promisify( this.client.getEstimatedTransactionFee.bind(this.client), ); + + this.protocolVersion = undefined; } /** @@ -182,7 +101,22 @@ class CorePromiseClient { return this.client.getStatus( getStatusRequest, convertObjectToMetadata(metadata), - getStatusOptions, + { + interceptors: [ + protocolVersionInterceptorFactory( + this.protocolVersion, + ), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocGetStatusResponse, + PBJSGetStatusResponse, + ), + protobufToJsonFactory( + PBJSGetStatusRequest, + ), + ), + ], + }, ); } @@ -199,7 +133,22 @@ class CorePromiseClient { return this.client.getBlock( getBlockRequest, convertObjectToMetadata(metadata), - getBlockOptions, + { + interceptors: [ + protocolVersionInterceptorFactory( + this.protocolVersion, + ), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocGetBlockResponse, + PBJSGetBlockResponse, + ), + protobufToJsonFactory( + PBJSGetBlockRequest, + ), + ), + ], + }, ); } @@ -216,7 +165,22 @@ class CorePromiseClient { return this.client.sendTransaction( sendTransactionRequest, convertObjectToMetadata(metadata), - sendTransactionOptions, + { + interceptors: [ + protocolVersionInterceptorFactory( + this.protocolVersion, + ), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocSendTransactionResponse, + PBJSSendTransactionResponse, + ), + protobufToJsonFactory( + PBJSSendTransactionRequest, + ), + ), + ], + }, ); } @@ -233,7 +197,22 @@ class CorePromiseClient { return this.client.getTransaction( getTransactionRequest, convertObjectToMetadata(metadata), - getTransactionOptions, + { + interceptors: [ + protocolVersionInterceptorFactory( + this.protocolVersion, + ), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocGetTransactionResponse, + PBJSGetTransactionResponse, + ), + protobufToJsonFactory( + PBJSGetTransactionRequest, + ), + ), + ], + }, ); } @@ -250,7 +229,22 @@ class CorePromiseClient { return this.client.getEstimatedTransactionFee( getEstimatedTransactionFeeRequest, convertObjectToMetadata(metadata), - getEstimatedTransactionFeeOptions, + { + interceptors: [ + protocolVersionInterceptorFactory( + this.protocolVersion, + ), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocGetEstimatedTransactionFeeResponse, + PBJSGetEstimatedTransactionFeeResponse, + ), + protobufToJsonFactory( + PBJSGetEstimatedTransactionFeeRequest, + ), + ), + ], + }, ); } @@ -268,9 +262,31 @@ class CorePromiseClient { return this.client.subscribeToBlockHeadersWithChainLocks( blockHeadersWithChainLocksRequest, convertObjectToMetadata(metadata), - subscribeToBlockHeadersWithChainLocksOptions, + { + interceptors: [ + protocolVersionInterceptorFactory( + this.protocolVersion, + ), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocBlockHeadersWithChainLocksResponse, + PBJSBlockHeadersWithChainLocksResponse, + ), + protobufToJsonFactory( + PBJSBlockHeadersWithChainLocksRequest, + ), + ), + ], + }, ); } + + /** + * @param {string} protocolVersion + */ + setProtocolVersion(protocolVersion) { + this.setProtocolVersion = protocolVersion; + } } module.exports = CorePromiseClient; diff --git a/clients/nodejs/PlatformPromiseClient.js b/clients/nodejs/PlatformPromiseClient.js index 00ac863..05e580d 100644 --- a/clients/nodejs/PlatformPromiseClient.js +++ b/clients/nodejs/PlatformPromiseClient.js @@ -9,6 +9,7 @@ const { client: { interceptors: { jsonToProtobufInterceptorFactory, + protocolVersionInterceptorFactory, }, converters: { jsonToProtobufFactory, @@ -49,65 +50,10 @@ const getPlatformDefinition = require('../../lib/getPlatformDefinition'); const PlatformNodeJSClient = getPlatformDefinition(); -const applyStateTransitionOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocApplyStateTransitionResponse, - PBJSApplyStateTransitionResponse, - ), - protobufToJsonFactory( - PBJSApplyStateTransitionRequest, - ), - ), - ], -}; - -const getIdentityOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocGetIdentityResponse, - PBJSGetIdentityResponse, - ), - protobufToJsonFactory( - PBJSGetIdentityRequest, - ), - ), - ], -}; - -const getDataContractOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocGetDataContractResponse, - PBJSGetDataContractResponse, - ), - protobufToJsonFactory( - PBJSGetDataContractRequest, - ), - ), - ], -}; - -const getDocumentsOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocGetDocumentsRequest, - PBJSGetDocumentsResponse, - ), - protobufToJsonFactory( - PBJSGetDocumentsRequest, - ), - ), - ], -}; - class PlatformPromiseClient { /** * @param {string} hostname + * @param {string} version * @param {?Object} credentials * @param {?Object} options */ @@ -129,6 +75,8 @@ class PlatformPromiseClient { this.client.getDocuments = promisify( this.client.getDocuments.bind(this.client), ); + + this.protocolVersion = undefined; } /** @@ -144,7 +92,20 @@ class PlatformPromiseClient { return this.client.applyStateTransition( applyStateTransitionRequest, convertObjectToMetadata(metadata), - applyStateTransitionOptions, + { + interceptors: [ + protocolVersionInterceptorFactory(this.protocolVersion), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocApplyStateTransitionResponse, + PBJSApplyStateTransitionResponse, + ), + protobufToJsonFactory( + PBJSApplyStateTransitionRequest, + ), + ), + ], + }, ); } @@ -161,7 +122,20 @@ class PlatformPromiseClient { return this.client.getIdentity( getIdentityRequest, convertObjectToMetadata(metadata), - getIdentityOptions, + { + interceptors: [ + protocolVersionInterceptorFactory(this.protocolVersion), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocGetIdentityResponse, + PBJSGetIdentityResponse, + ), + protobufToJsonFactory( + PBJSGetIdentityRequest, + ), + ), + ], + }, ); } @@ -179,7 +153,20 @@ class PlatformPromiseClient { return this.client.getDataContract( getDataContractRequest, convertObjectToMetadata(metadata), - getDataContractOptions, + { + interceptors: [ + protocolVersionInterceptorFactory(this.protocolVersion), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocGetDataContractResponse, + PBJSGetDataContractResponse, + ), + protobufToJsonFactory( + PBJSGetDataContractRequest, + ), + ), + ], + }, ); } @@ -197,9 +184,29 @@ class PlatformPromiseClient { return this.client.getDocuments( getDocumentsRequest, convertObjectToMetadata(metadata), - getDocumentsOptions, + { + interceptors: [ + protocolVersionInterceptorFactory(this.protocolVersion), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocGetDocumentsRequest, + PBJSGetDocumentsResponse, + ), + protobufToJsonFactory( + PBJSGetDocumentsRequest, + ), + ), + ], + }, ); } + + /** + * @param {string} protocolVersion + */ + setProtocolVersion(protocolVersion) { + this.setProtocolVersion = protocolVersion; + } } module.exports = PlatformPromiseClient; diff --git a/clients/nodejs/TransactionsFilterStreamPromiseClient.js b/clients/nodejs/TransactionsFilterStreamPromiseClient.js index 50c6ac9..c146c34 100644 --- a/clients/nodejs/TransactionsFilterStreamPromiseClient.js +++ b/clients/nodejs/TransactionsFilterStreamPromiseClient.js @@ -8,6 +8,7 @@ const { client: { interceptors: { jsonToProtobufInterceptorFactory, + protocolVersionInterceptorFactory, }, converters: { jsonToProtobufFactory, @@ -41,28 +42,17 @@ const getTransactionsFilterStreamDefinition = require( const TransactionsFilterStreamNodeJSClient = getTransactionsFilterStreamDefinition(); -const subscribeToTransactionsWithProofsOptions = { - interceptors: [ - jsonToProtobufInterceptorFactory( - jsonToProtobufFactory( - ProtocTransactionsWithProofsResponse, - PBJSTransactionsWithProofsResponse, - ), - protobufToJsonFactory( - PBJSTransactionsWithProofsRequest, - ), - ), - ], -}; - class TransactionsFilterStreamPromiseClient { /** * @param {string} hostname + * @param {string} version * @param {?Object} credentials * @param {?Object} options */ constructor(hostname, credentials = grpc.credentials.createInsecure(), options = {}) { this.client = new TransactionsFilterStreamNodeJSClient(hostname, credentials, options); + + this.protocolVersion = undefined; } /** @@ -79,9 +69,29 @@ class TransactionsFilterStreamPromiseClient { return this.client.subscribeToTransactionsWithProofs( transactionsWithProofsRequest, convertObjectToMetadata(metadata), - subscribeToTransactionsWithProofsOptions, + { + interceptors: [ + protocolVersionInterceptorFactory(this.protocolVersion), + jsonToProtobufInterceptorFactory( + jsonToProtobufFactory( + ProtocTransactionsWithProofsResponse, + PBJSTransactionsWithProofsResponse, + ), + protobufToJsonFactory( + PBJSTransactionsWithProofsRequest, + ), + ), + ], + }, ); } + + /** + * @param {string} protocolVersion + */ + setProtocolVersion(protocolVersion) { + this.setProtocolVersion = protocolVersion; + } } module.exports = TransactionsFilterStreamPromiseClient; diff --git a/package-lock.json b/package-lock.json index 0b0dc73..e4185e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,20 +25,28 @@ } }, "@dashevo/grpc-common": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@dashevo/grpc-common/-/grpc-common-0.2.0.tgz", - "integrity": "sha512-SzKl/1KCctkvX+ELVo9EybPLw2FLmMvqbLH7rQwPpmslOU7YyohmmGKm0UoZwn3BipZTj8f6tKBN3B9Eq3amYQ==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@dashevo/grpc-common/-/grpc-common-0.2.2.tgz", + "integrity": "sha512-7r2LWxCKpVbF3xqKPZxOXIvd63TLEX250ud5PG6Wk6IoUNBOFVVNlwIAziG4lz8i061FRVA/Yqd7xSF8o6tZwA==", "requires": { "@grpc/proto-loader": "^0.5.2", "grpc": "^1.24.0", "grpc-health-check": "^1.7.0", - "lodash.get": "^4.4.2" + "lodash.get": "^4.4.2", + "semver": "^7.3.2" + }, + "dependencies": { + "semver": { + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", + "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==" + } } }, "@grpc/proto-loader": { - "version": "0.5.2", - "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.5.2.tgz", - "integrity": "sha512-eBKD/FPxQoY1x6QONW2nBd54QUEyzcFP9FenujmoeDPy1rutVSHki1s/wR68F6O1QfCNDx+ayBH1O2CVNMzyyw==", + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.5.4.tgz", + "integrity": "sha512-HTM4QpI9B2XFkPz7pjwMyMgZchJ93TVkL3kWPW8GDMDKYxsMnmf4w2TNMJK7+KNiYHS5cJrCEAFlF+AwtXWVPA==", "requires": { "lodash.camelcase": "^4.3.0", "protobufjs": "^6.8.6" @@ -162,9 +170,9 @@ "integrity": "sha512-QcAKpaO6nhHLlxWBvpc4WeLrTvPqlHOvaj0s5GriKkA1zq+bsFBPpfYCvQhLqLgYlIko8A9YrPdaMHCo5mBcpg==" }, "acorn": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.2.0.tgz", - "integrity": "sha512-8oe72N3WPMjA+2zVG71Ia0nXZ8DpQH+QyyHO+p06jT8eg8FGG3FbcUIi8KziHlAfheJQZeoqbvq1mQSQHXKYLw==", + "version": "6.4.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.4.1.tgz", + "integrity": "sha512-ZVA9k326Nwrj3Cj9jlh3wGFutC2ZornPNARZwsNYqQYgN0EsV2d53w5RN/co65Ohn4sUAUtb1rSUAOD6XN9idA==", "dev": true }, "acorn-jsx": { @@ -1360,9 +1368,9 @@ } }, "grpc-health-check": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/grpc-health-check/-/grpc-health-check-1.7.0.tgz", - "integrity": "sha512-yrjWW9OLalYfF1BPXn0uMgiVqpma8AoNckeoJTAfCngexkbNH6l4C1+ACC6o0AD5AoH9OrbHyzdzM8IxvMGgJA==", + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/grpc-health-check/-/grpc-health-check-1.8.0.tgz", + "integrity": "sha512-Fqa35Bg4VhliZE3Yfe88mmYm0nRfKVr/QRYw5zP1jtn1R0f7wwJJqlXsjaoexJy+pVTVXZpP6CxmNGuEv/bngw==", "requires": { "google-protobuf": "^3.4.0", "grpc": "^1.6.0", @@ -1732,18 +1740,18 @@ } }, "minimist": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "dev": true }, "mkdirp": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", - "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", "dev": true, "requires": { - "minimist": "0.0.8" + "minimist": "^1.2.5" } }, "mocha": { @@ -1783,6 +1791,12 @@ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", "dev": true }, + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true + }, "cliui": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz", @@ -1842,6 +1856,21 @@ "path-exists": "^3.0.0" } }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, "ms": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", @@ -1935,6 +1964,16 @@ "y18n": "^4.0.0", "yargs-parser": "^13.1.1" } + }, + "yargs-parser": { + "version": "13.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.1.tgz", + "integrity": "sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==", + "dev": true, + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } } } }, @@ -2695,8 +2734,7 @@ }, "yargs-parser": { "version": "13.1.1", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.1.tgz", - "integrity": "sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==", + "resolved": "", "dev": true, "requires": { "camelcase": "^5.0.0", diff --git a/package.json b/package.json index 92948b4..783aadf 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ }, "homepage": "https://github.com/dashevo/dapi-grpc#readme", "dependencies": { - "@dashevo/grpc-common": "^0.2.0", + "@dashevo/grpc-common": "^0.2.2", "google-protobuf": "^3.8.0", "grpc": "^1.24.2", "grpc-web": "^1.0.6",