Skip to content

Commit

Permalink
catch inside block polling function (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
faboweb authored Feb 15, 2020
1 parent a92cfb9 commit 73d4677
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions lib/block-listeners/cosmos-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ class CosmosNodeSubscription {

async pollForNewBlock() {
this.pollingTimeout = setTimeout(async () => {
const block = await this.cosmosAPI.getBlockByHeight()
if (this.height !== block.height) {
let block
try {
block = await this.cosmosAPI.getBlockByHeight()
} catch (error) {
console.error('Failed to fetch block for subscription', 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)
Expand Down Expand Up @@ -61,41 +67,49 @@ class CosmosNodeSubscription {
// A GraphQL resolver is listening for these messages and sends the block to
// each subscribed user.
async newBlockHandler(block) {
Sentry.configureScope(function(scope) {
scope.setExtra('height', block.height)
})
try {
Sentry.configureScope(function(scope) {
scope.setExtra('height', block.height)
})

const validators = await this.cosmosAPI.getAllValidators(block.height)
const validatorMap = await this.getValidatorMap(validators)
this.updateDBValidatorProfiles(validators)
this.store.update({ height: block.height, block, validators: validatorMap })
publishBlockAdded(this.network.id, block)
// TODO remove, only for demo purposes
// publishEvent(this.network.id, 'block', '', block)
const validators = await this.cosmosAPI.getAllValidators(block.height)
const validatorMap = await this.getValidatorMap(validators)
this.updateDBValidatorProfiles(validators)
this.store.update({
height: block.height,
block,
validators: validatorMap
})
publishBlockAdded(this.network.id, block)
// TODO remove, only for demo purposes
// publishEvent(this.network.id, 'block', '', block)

// For each transaction listed in a block we extract the relevant addresses. This is published to the network.
// A GraphQL resolver is listening for these messages and sends the
// transaction to each subscribed user.
// TODO doesn't handle failing txs as it doesn't extract addresses from those txs (they are not tagged)
block.transactions.forEach(tx => {
let addresses = []
try {
this.cosmosAPI.extractInvolvedAddresses(tx.raw).forEach(address => {
addresses.push(address)
})
} catch (err) {
Sentry.withScope(function(scope) {
scope.setExtra('transaction', tx.raw)
Sentry.captureException(err)
// For each transaction listed in a block we extract the relevant addresses. This is published to the network.
// A GraphQL resolver is listening for these messages and sends the
// transaction to each subscribed user.
// TODO doesn't handle failing txs as it doesn't extract addresses from those txs (they are not tagged)
block.transactions.forEach(tx => {
let addresses = []
try {
this.cosmosAPI.extractInvolvedAddresses(tx.raw).forEach(address => {
addresses.push(address)
})
} catch (err) {
Sentry.withScope(function(scope) {
scope.setExtra('transaction', tx.raw)
Sentry.captureException(err)
})
}
addresses = _.uniq(addresses)
addresses.forEach(address => {
publishUserTransactionAdded(this.network.id, address, tx)
publishEvent(this.network.id, 'transaction', address, tx)
})
}
addresses = _.uniq(addresses)
addresses.forEach(address => {
publishUserTransactionAdded(this.network.id, address, tx)
publishEvent(this.network.id, 'transaction', address, tx)
})
})

} catch (error) {
console.error('newBlockHandler failed')
Sentry.captureException(error)
}
this.cosmosAPI.memoizedResults.clear()
}

Expand Down

0 comments on commit 73d4677

Please sign in to comment.