Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check sync status of attestation service #2191

Merged
merged 4 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions packages/attestation-service/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContractKit, newKit } from '@celo/contractkit'
import { FindOptions, Sequelize } from 'sequelize'
import { Block } from 'web3/eth/types'
import { fetchEnv } from './env'
import { rootLogger } from './logger'
import Attestation, { AttestationModel, AttestationStatic } from './models/attestation'
Expand All @@ -22,11 +23,31 @@ export let kit: ContractKit
export async function initializeKit() {
if (kit === undefined) {
kit = newKit(fetchEnv('CELO_PROVIDER'))
const blockNumber = await kit.web3.eth.getBlockNumber()
if (blockNumber === 0) {
throw new Error(
'Could not fetch latest block from web3 provider ' + fetchEnv('CELO_PROVIDER')
// Copied from @celo/cli/src/utils/helpers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better to have a function for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but we only have utils and I did not want to add web3 as a dependency

try {
const syncProgress = await kit.web3.eth.isSyncing()
if (typeof syncProgress === 'boolean' && !syncProgress) {
const latestBlock: Block = await kit.web3.eth.getBlock('latest')
if (latestBlock && latestBlock.number > 0) {
// To catch the case in which syncing has happened in the past,
// has stopped, and hasn't started again, check for an old timestamp
// on the latest block
const ageOfBlock = Date.now() / 1000 - latestBlock.timestamp
if (ageOfBlock > 120) {
throw new Error(
`Latest block is ${ageOfBlock} seconds old, and syncing is not currently in progress`
)
}
}
} else {
throw new Error('Node is not syncing')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right message? If eth_syncing returned a non-false then the node is syncing. Should this message read 'Node is not synced' instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I've opened #2372

}
} catch (error) {
rootLogger.error(
'Initializing Kit failed, are you running your node and specified it with the "CELO_PROVIDER" env var?. It\' currently set as ' +
fetchEnv('CELO_PROVIDER')
)
throw error
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/attestation-service/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { createStream } from 'bunyan-gke-stackdriver'
import { fetchEnvOrDefault } from './env'

const logLevel = fetchEnvOrDefault('LOG_LEVEL', 'info') as LogLevelString
const logFormat = fetchEnvOrDefault('LOG_FORMAT', 'json')
const logFormat = fetchEnvOrDefault('LOG_FORMAT', 'human')

let stream: any
switch (logFormat) {
case 'stackdriver':
stream = createStream(levelFromName[logLevel])
break
case 'human':
stream = { level: logLevel, stream: bunyanDebugStream() }
case 'json':
stream = { stream: process.stdout, level: logLevel }
break
default:
stream = { stream: process.stdout, level: logLevel }
stream = { level: logLevel, stream: bunyanDebugStream() }
break
}

Expand Down