From d349e4ab52c2a764e062290db9d6267a748c54b0 Mon Sep 17 00:00:00 2001 From: Fabian Date: Mon, 6 Apr 2020 14:07:31 -0500 Subject: [PATCH] Fabo/make db available from store (#567) * make db available from store * bad fix * good fix * fix delegations, validators, undelegations queries Co-authored-by: Bitcoinera --- lib/block-store.js | 13 +++++-------- lib/network-container.js | 13 +++++++++++-- lib/resolvers.js | 21 ++++++++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/block-store.js b/lib/block-store.js index c8e2d90aef..a6b48241d3 100644 --- a/lib/block-store.js +++ b/lib/block-store.js @@ -1,5 +1,5 @@ class BlockStore { - constructor(network) { + constructor(network, database) { this.network = network this.latestHeight = 0 this.block = {} @@ -13,22 +13,19 @@ class BlockStore { this.dataReady = new Promise(resolve => { this.resolveReady = resolve }) + this.db = database } update({ height, block = this.block, - stakingDenom = this.stakingDenom, - annualProvision = this.annualProvision, - signedBlocksWindow = this.signedBlocksWindow, - validators = this.validators + validators = this.validators, + data = this.data // multi purpose block to be used for any chain specific data }) { this.latestHeight = Number(height) this.block = block - this.stakingDenom = stakingDenom - this.annualProvision = Number(annualProvision) - this.signedBlocksWindow = Number(signedBlocksWindow) this.validators = validators + this.data = data this.resolveReady() } diff --git a/lib/network-container.js b/lib/network-container.js index e2abe56f2a..021cb90209 100644 --- a/lib/network-container.js +++ b/lib/network-container.js @@ -1,4 +1,11 @@ const BlockStore = require('./block-store') +const database = require('./database') +const config = require('../config') + +function createDBInstance(networkId) { + const networkSchemaName = networkId ? networkId.replace(/-/g, '_') : false + return new database(config)(networkSchemaName) +} /* This class handles creation and management of each network. @@ -10,6 +17,7 @@ class NetworkContainer { constructor(network) { this.network = network this.id = network.id + this.db = createDBInstance(network.id) this.requireSourceClass() this.requireSubscriptionClass() } @@ -20,7 +28,7 @@ class NetworkContainer { } createStore() { - this.store = new BlockStore(this.network) + this.store = new BlockStore(this.network, this.db) } requireSourceClass() { @@ -55,7 +63,8 @@ class NetworkContainer { this.blockListener = new this.subscriptionClass( this.network, this.sourceClass, - this.store + this.store, + this.db ) } } diff --git a/lib/resolvers.js b/lib/resolvers.js index da9acf3ab6..84a490f6b4 100644 --- a/lib/resolvers.js +++ b/lib/resolvers.js @@ -41,8 +41,8 @@ function localStore(dataSources, networkId) { } // TODO updating the validators with the profiles should happen already in the store so we don't query the db all the time -async function getValidatorInfosMap(networkId) { - const validatorInfo = await createDBInstance(networkId).getValidatorsInfo() +async function getValidatorInfosMap(store) { + const validatorInfo = await store.db.getValidatorsInfo() const validatorInfoMap = keyBy(validatorInfo, 'operator_address') return validatorInfoMap } @@ -71,7 +71,9 @@ async function validators( if (activeOnly) { validators = validators.filter(({ status }) => status === 'ACTIVE') } - const validatorInfoMap = await getValidatorInfosMap(networkId) + const validatorInfoMap = await getValidatorInfosMap( + localStore(dataSources, networkId) + ) validators = validators.map(validator => enrichValidator(validatorInfoMap[validator.operatorAddress], validator) ) @@ -89,9 +91,10 @@ async function validators( async function validator(_, { networkId, operatorAddress }, { dataSources }) { await localStore(dataSources, networkId).dataReady - const validatorInfo = await createDBInstance( + const validatorInfo = await localStore( + dataSources, networkId - ).getValidatorInfoByAddress(operatorAddress) + ).db.getValidatorInfoByAddress(operatorAddress) // here first we check if validators in localStore are an empty object // this is because sometimes it takes time to fetch them on startup @@ -140,7 +143,9 @@ async function delegations( dataSources, networkId ).getDelegationsForDelegatorAddress(delegatorAddress, validatorsDictionary) - const validatorInfoMap = await getValidatorInfosMap(networkId) + const validatorInfoMap = await getValidatorInfosMap( + localStore(dataSources, networkId) + ) return Promise.all( delegations.map(async delegation => ({ @@ -164,7 +169,9 @@ async function undelegations( dataSources, networkId ).getUndelegationsForDelegatorAddress(delegatorAddress, validatorsDictionary) - const validatorInfoMap = await getValidatorInfosMap(networkId) + const validatorInfoMap = await getValidatorInfosMap( + localStore(dataSources, networkId) + ) return Promise.all( undelegations.map(async undelegation => ({