Skip to content

Commit

Permalink
Fabo/make db available from store (#567)
Browse files Browse the repository at this point in the history
* make db available from store

* bad fix

* good fix

* fix delegations, validators, undelegations queries

Co-authored-by: Bitcoinera <[email protected]>
  • Loading branch information
faboweb and Bitcoinera authored Apr 6, 2020
1 parent f9c9fc2 commit d349e4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
13 changes: 5 additions & 8 deletions lib/block-store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BlockStore {
constructor(network) {
constructor(network, database) {
this.network = network
this.latestHeight = 0
this.block = {}
Expand All @@ -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()
}
Expand Down
13 changes: 11 additions & 2 deletions lib/network-container.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -10,6 +17,7 @@ class NetworkContainer {
constructor(network) {
this.network = network
this.id = network.id
this.db = createDBInstance(network.id)
this.requireSourceClass()
this.requireSubscriptionClass()
}
Expand All @@ -20,7 +28,7 @@ class NetworkContainer {
}

createStore() {
this.store = new BlockStore(this.network)
this.store = new BlockStore(this.network, this.db)
}

requireSourceClass() {
Expand Down Expand Up @@ -55,7 +63,8 @@ class NetworkContainer {
this.blockListener = new this.subscriptionClass(
this.network,
this.sourceClass,
this.store
this.store,
this.db
)
}
}
Expand Down
21 changes: 14 additions & 7 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
)
Expand All @@ -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
Expand Down Expand Up @@ -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 => ({
Expand All @@ -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 => ({
Expand Down

0 comments on commit d349e4a

Please sign in to comment.