From 2e4ce31aeb67c477eef020ab063fa7c3e57fcd77 Mon Sep 17 00:00:00 2001 From: Dheeraj Manjunath Date: Fri, 16 Aug 2019 15:36:38 -0700 Subject: [PATCH] Cleanup discovery provider client code from libs --- .../discoveryProviderFactoryClient.js | 49 ------------- libs/src/services/dataContracts/index.js | 57 --------------- libs/tests/discoveryProviderClientTest.js | 72 ------------------- libs/tests/index.js | 1 - 4 files changed, 179 deletions(-) delete mode 100644 libs/src/services/dataContracts/discoveryProviderFactoryClient.js delete mode 100644 libs/tests/discoveryProviderClientTest.js diff --git a/libs/src/services/dataContracts/discoveryProviderFactoryClient.js b/libs/src/services/dataContracts/discoveryProviderFactoryClient.js deleted file mode 100644 index bb8f1c58937..00000000000 --- a/libs/src/services/dataContracts/discoveryProviderFactoryClient.js +++ /dev/null @@ -1,49 +0,0 @@ -class DiscoveryProviderFactoryClient { - constructor (web3Manager, contractABI, contractRegistryKey, getRegistryAddress) { - this.web3Manager = web3Manager - this.contractABI = contractABI - this.contractRegistryKey = contractRegistryKey - this.getRegistryAddress = getRegistryAddress - - this.web3 = this.web3Manager.getWeb3() - } - - async init () { - this.contractAddress = await this.getRegistryAddress(this.contractRegistryKey) - this.DiscoveryProviderFactory = new this.web3.eth.Contract(this.contractABI, this.contractAddress) - } - - /* ------- GETTERS ------- */ - - async getDiscoveryProvider (id) { - return this.DiscoveryProviderFactory.methods.getDiscoveryProvider(id).call() - } - - async getTotalNumberOfProviders () { - return this.DiscoveryProviderFactory.methods.getTotalNumberOfProviders().call() - } - - async getDiscoveryProviderList () { - const totalNumberOfProviders = await this.getTotalNumberOfProviders() - let providers = [] - for (let i = 0; i < totalNumberOfProviders; i++) { - providers[i] = this.getDiscoveryProvider(i + 1) - } - let providerList = await Promise.all(providers) - return providerList.map((provider) => provider[1]) - } - - /* ------- SETTERS ------- */ - - async register (endpoint) { - const contractMethod = this.DiscoveryProviderFactory.methods.register(endpoint) - const tx = await this.web3Manager.sendTransaction( - contractMethod, - this.contractRegistryKey, - this.contractAddress - ) - return tx - } -} - -module.exports = DiscoveryProviderFactoryClient diff --git a/libs/src/services/dataContracts/index.js b/libs/src/services/dataContracts/index.js index 0a01379242d..bc0a7692c50 100644 --- a/libs/src/services/dataContracts/index.js +++ b/libs/src/services/dataContracts/index.js @@ -4,7 +4,6 @@ const Utils = require('../../utils') const RegistryClient = require('./registryClient') const UserFactoryClient = require('./userFactoryClient') const TrackFactoryClient = require('./trackFactoryClient') -const DiscoveryProviderFactoryClient = require('./discoveryProviderFactoryClient') const SocialFeatureFactoryClient = require('./socialFeatureFactoryClient') const PlaylistFactoryClient = require('./playlistFactoryClient') const UserLibraryFactoryClient = require('./userLibraryFactoryClient') @@ -15,7 +14,6 @@ const IPLDBlacklistFactoryClient = require('./IPLDBlacklistFactoryClient') const RegistryABI = Utils.importDataContractABI('Registry.json').abi const UserFactoryABI = Utils.importDataContractABI('UserFactory.json').abi const TrackFactoryABI = Utils.importDataContractABI('TrackFactory.json').abi -const DiscoveryProviderFactoryABI = Utils.importDataContractABI('DiscoveryProviderFactory.json').abi const SocialFeatureFactoryABI = Utils.importDataContractABI('SocialFeatureFactory.json').abi const PlaylistFactoryABI = Utils.importDataContractABI('PlaylistFactory.json').abi const UserLibraryFactoryABI = Utils.importDataContractABI('UserLibraryFactory.json').abi @@ -24,7 +22,6 @@ const IPLDBlacklistFactoryABI = Utils.importDataContractABI('IPLDBlacklistFactor // define contract registry keys const UserFactoryRegistryKey = 'UserFactory' const TrackFactoryRegistryKey = 'TrackFactory' -const DiscoveryProviderFactoryRegistryKey = 'DiscoveryProviderFactory' const SocialFeatureFactoryRegistryKey = 'SocialFeatureFactory' const PlaylistFactoryRegistryKey = 'PlaylistFactory' const UserLibraryFactoryRegistryKey = 'UserLibraryFactory' @@ -64,14 +61,6 @@ class AudiusContracts { ) this.clients.push(this.TrackFactoryClient) - this.DiscoveryProviderFactoryClient = new DiscoveryProviderFactoryClient( - this.web3Manager, - DiscoveryProviderFactoryABI, - DiscoveryProviderFactoryRegistryKey, - this.getRegistryAddressForContract - ) - this.clients.push(this.DiscoveryProviderFactoryClient) - this.SocialFeatureFactoryClient = new SocialFeatureFactoryClient( this.web3Manager, SocialFeatureFactoryABI, @@ -131,52 +120,6 @@ class AudiusContracts { } return contractRegistryKey } - - /** - * Registers a discovery service endpoint if it's not currently in the blockchain - * If it is already registered, returns id of the endpoint from the contract - * @param {string} endpoint fully qualified domain name of discprov endpoint - * @param {Boolean} validateEndpointHealth - */ - async registerDiscoveryProviderOnChain (endpoint, validateEndpointHealth = true) { - let listOfProviders = await this.DiscoveryProviderFactoryClient.getDiscoveryProviderList() - for (var i = 0; i < listOfProviders.length; i++) { - let parsedName = listOfProviders[i] - if (parsedName === endpoint) { - // Return discovery provider ID if already registered - return i + 1 - } - } - if (!Utils.isFQDN(endpoint)) { - throw new Error('Not a fully qualified domain name!') - } else if (validateEndpointHealth && !(await Utils.isHealthy(endpoint))) { - throw new Error('Discovery provider failed health check. Provider could not be registered.') - } else { - return this.DiscoveryProviderFactoryClient.register(endpoint) - } - } - - /** TODO: REMOVE AS THIS IS DEPRECATED - * Need to select a discovery service for libs to make queries against */ - async selectDiscoveryProviderToUse (idx) { - let discoveryProviders = await this.DiscoveryProviderFactoryClient.getDiscoveryProviderList() - - // on chain the discprov count starts at 1, not 0, but the list of discovery providers - // starts at index 0, so the index refers to the on chain - // discovery provider id, not the index from the list of discovery providers - if (discoveryProviders && discoveryProviders.length >= 1) { - let offsetIdx = idx - 1 - let discoveryProviderEndpoint - if (idx && idx >= 1 && discoveryProviders[offsetIdx]) { - discoveryProviderEndpoint = discoveryProviders[offsetIdx] - } else { - discoveryProviderEndpoint = discoveryProviders[1] - } - return discoveryProviderEndpoint - } else { - throw new Error('Cannot register discovery provider, not enough providers available') - } - } } module.exports = AudiusContracts diff --git a/libs/tests/discoveryProviderClientTest.js b/libs/tests/discoveryProviderClientTest.js deleted file mode 100644 index fd0ab696a67..00000000000 --- a/libs/tests/discoveryProviderClientTest.js +++ /dev/null @@ -1,72 +0,0 @@ -const assert = require('assert') -let helpers = require('./helpers') - -let audiusInstance = helpers.audiusInstance - -before(async function () { - await audiusInstance.init() -}) - -it('should call getDiscoveryProvider on invalid value and verify blockchain is empty', async function () { - let discprov = await audiusInstance.contracts.DiscoveryProviderFactoryClient.getDiscoveryProvider(-1) - assert.strictEqual(discprov.endpoint, '') -}) - -it('should call register and verify new discovery provider endpoint', async function () { - let endpoint = helpers.constants.creatorNodeURL1 - await audiusInstance.contracts.DiscoveryProviderFactoryClient.register(endpoint) - - let discprov = await audiusInstance.contracts.DiscoveryProviderFactoryClient.getDiscoveryProvider(1) - assert.strictEqual(endpoint, discprov.endpoint) -}) - -it('should call register again and verify new discovery provider endpoint', async function () { - let endpoint = helpers.constants.creatorNodeURL2 - await audiusInstance.contracts.DiscoveryProviderFactoryClient.register(endpoint) - - let discprov = await audiusInstance.contracts.DiscoveryProviderFactoryClient.getDiscoveryProvider(2) - assert.strictEqual(endpoint, discprov.endpoint) -}) - -it('should get back a list of providers and verify the number and contents', async function () { - let providers = await audiusInstance.contracts.DiscoveryProviderFactoryClient.getDiscoveryProviderList() - assert.strictEqual(providers.length, 2) - assert.strictEqual(providers[0], helpers.constants.creatorNodeURL1) - assert.strictEqual(providers[1], helpers.constants.creatorNodeURL2) -}) - -it('should not register a bad url as a discovery provider', async function () { - let endpoint = 'the cow jumped over the moon' - let hitsTryBlock = false - // calls register with an invalid endpoint to trigger the error - try { - await audiusInstance.contracts.registerDiscoveryProviderOnChain(endpoint) - hitsTryBlock = true // if no error, then hitsTryBlock is set to true so that the test would not pass - } catch (err) { - // checks if the error is that specific error - assert.strictEqual(err.message, 'Not a fully qualified domain name!') - } - assert.strictEqual(hitsTryBlock, false) -}) - -it('should register an endpoint that passes validation', async function () { - let endpoint1 = 'http://discoveryprovider.audius.co' - // do not depend on discovery provider being up to pass test; pass false in to skip - // validating health check endpoint - await audiusInstance.contracts.registerDiscoveryProviderOnChain(endpoint1, false) - let discprov = await audiusInstance.contracts.DiscoveryProviderFactoryClient.getDiscoveryProvider(3) - assert.strictEqual(discprov.endpoint, endpoint1) -}) - -it('should not register an endpoint with no health check.', async function () { - let hitsTryBlock = false - let endpoint2 = 'http://www.google.com' - try { - await audiusInstance.contracts.registerDiscoveryProviderOnChain(endpoint2) - hitsTryBlock = true // if no error, then hitsTryBlock is set to true so that the test would not pass - } catch (err) { - // checks if the error is that specific error - assert.strictEqual(err.message, 'Discovery provider failed health check. Provider could not be registered.') - } - assert.strictEqual(hitsTryBlock, false) -}) diff --git a/libs/tests/index.js b/libs/tests/index.js index f2db7817f05..0ee2236b274 100644 --- a/libs/tests/index.js +++ b/libs/tests/index.js @@ -1,5 +1,4 @@ require('./helpers') -require('./discoveryProviderClientTest') require('./registryClientTest') require('./userClientTest') require('./creatorNodeTest')