Skip to content

Commit

Permalink
Aleksei/prevent constant db calls (#304)
Browse files Browse the repository at this point in the history
* add validator addresses to db

* linted

* ignore in local dev

* revert

* fixed fetch

* comment

* refactored db into constructor

* cleanup

* prevent constant db interaction

* stored object structure changed

* remove nesting in validators

* filter validators list

Co-authored-by: Fabian <[email protected]>
  • Loading branch information
iambeone and faboweb authored Feb 25, 2020
1 parent f2df617 commit 20f7d60
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/block-listeners/cosmos-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CosmosNodeSubscription {
this.network = network
this.cosmosAPI = new CosmosApiClass(network)
this.store = store
this.validators = []
const networkSchemaName = this.network.id.replace(/-/g, '_')
this.db = new database(config)(networkSchemaName)
this.chainHangup = undefined
Expand Down Expand Up @@ -120,7 +121,30 @@ class CosmosNodeSubscription {

// this adds all the validator addresses to the database so we can easily check in the database which ones have an image and which ones don't
async updateDBValidatorProfiles(validators) {
const validatorRows = validators.map(({ operatorAddress, name }) => ({
// filter only new validators
let newValidators = validators.filter(
validator =>
!this.validators.find(
v =>
v.address == validator.operatorAddress && v.name == validator.name // in case if validator name was changed
)
)
// save all new validators to an array
this.validators = [
...this.validators.filter(
({ operatorAddress }) =>
!newValidators.find(
({ operatorAddress: newValidatorOperatorAddress }) =>
newValidatorOperatorAddress === operatorAddress
)
),
...newValidators.map(v => ({
address: v.operatorAddress,
name: v.name
}))
]
// update only new onces
const validatorRows = newValidators.map(({ operatorAddress, name }) => ({
operator_address: operatorAddress,
name
}))
Expand Down

0 comments on commit 20f7d60

Please sign in to comment.