Skip to content

Commit

Permalink
improve blockHandler logic (#498)
Browse files Browse the repository at this point in the history
(cherry picked from commit b67fab47adbf5a19caf99e17a9395e62419e8dad)
  • Loading branch information
faboweb authored Mar 23, 2020
1 parent 7e6260c commit e698a5a
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions lib/block-listeners/cosmos-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const COSMOS_DB_DELAY = 2000
class CosmosNodeSubscription {
constructor(network, CosmosApiClass, store) {
this.network = network
this.cosmosAPI = new CosmosApiClass(network)
this.CosmosApiClass = CosmosApiClass
this.store = store
this.validators = []
const networkSchemaName = this.network.id.replace(/-/g, '_')
Expand All @@ -31,23 +31,11 @@ class CosmosNodeSubscription {
}

async pollForNewBlock() {
const cosmosAPI = new this.CosmosApiClass(this.network, this.store)
await this.checkForNewBlock(cosmosAPI)

this.pollingTimeout = setTimeout(async () => {
let block
try {
block = await this.cosmosAPI.getBlockByHeightV2()
} catch (error) {
console.error('Failed to fetch block', error)
Sentry.captureException(error)
}
if (block && this.height !== block.height) {
// apparently the cosmos db takes a while to serve the content after a block has been updated
// if we don't do this, we run into errors as the data is not yet available
setTimeout(() => this.newBlockHandler(block), COSMOS_DB_DELAY)
this.height = block.height // this needs to be set somewhere

// we are safe, that the chain produced a block so it didn't hang up
if (this.chainHangup) clearTimeout(this.chainHangup)
}
await this.checkForNewBlock(cosmosAPI)

this.pollForNewBlock()
}, POLLING_INTERVAL)
Expand All @@ -64,16 +52,40 @@ class CosmosNodeSubscription {
}, EXPECTED_MAX_BLOCK_WINDOW)
}

async checkForNewBlock(cosmosAPI) {
let block
try {
block = await cosmosAPI.getBlockByHeightV2()
} catch (error) {
console.error('Failed to fetch block', error)
Sentry.captureException(error)
}
if (block && this.height !== block.height) {
// apparently the cosmos db takes a while to serve the content after a block has been updated
// if we don't do this, we run into errors as the data is not yet available
setTimeout(() => this.newBlockHandler(block, cosmosAPI), COSMOS_DB_DELAY)
this.height = block.height // this needs to be set somewhere

// we are safe, that the chain produced a block so it didn't hang up
if (this.chainHangup) clearTimeout(this.chainHangup)
}
}

// For each block event, we fetch the block information and publish a message.
// A GraphQL resolver is listening for these messages and sends the block to
// each subscribed user.
async newBlockHandler(block) {
async newBlockHandler(block, cosmosAPI) {
try {
Sentry.configureScope(function(scope) {
scope.setExtra('height', block.height)
})

const validators = await this.cosmosAPI.getAllValidators(block.height)
// allow for network specific block handlers
if (cosmosAPI.newBlockHandler) {
await cosmosAPI.newBlockHandler(block, this.store)
}

const validators = await cosmosAPI.getAllValidators(block.height)
const validatorMap = await this.getValidatorMap(validators)
this.updateDBValidatorProfiles(validators)
this.store.update({
Expand All @@ -99,7 +111,6 @@ class CosmosNodeSubscription {
console.error('newBlockHandler failed', error)
Sentry.captureException(error)
}
this.cosmosAPI.memoizedResults.clear()
}

async getValidatorMap(validators) {
Expand Down

0 comments on commit e698a5a

Please sign in to comment.