-
Notifications
You must be signed in to change notification settings - Fork 369
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
Changes from all commits
dde7862
adeaa52
c46acb6
abe697a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' | ||
|
@@ -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 | ||
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the right message? If There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 addweb3
as a dependency