Add support to disable validator balances check in monitor (#259)

* Add support to disable validator balance check in monitor
* Convert validator address to checksum address
This commit is contained in:
Gerardo Nardelli 2020-01-04 08:44:57 -03:00 committed by Alexander Kolotov
parent b6d96d7f62
commit 9cb1a2041d
2 changed files with 77 additions and 47 deletions

@ -5,6 +5,8 @@ const { isV1Bridge } = require('./utils/serverUtils')
const app = express() const app = express()
const MONITOR_VALIDATOR_HOME_TX_LIMIT = Number(process.env.MONITOR_VALIDATOR_HOME_TX_LIMIT) || 0
const MONITOR_VALIDATOR_FOREIGN_TX_LIMIT = Number(process.env.MONITOR_VALIDATOR_FOREIGN_TX_LIMIT) || 0
const MONITOR_TX_NUMBER_THRESHOLD = Number(process.env.MONITOR_TX_NUMBER_THRESHOLD) || 100 const MONITOR_TX_NUMBER_THRESHOLD = Number(process.env.MONITOR_TX_NUMBER_THRESHOLD) || 100
console.log('MONITOR_TX_NUMBER_THRESHOLD = ' + MONITOR_TX_NUMBER_THRESHOLD) console.log('MONITOR_TX_NUMBER_THRESHOLD = ' + MONITOR_TX_NUMBER_THRESHOLD)
@ -52,18 +54,25 @@ app.get('/validators', async (req, res, next) => {
const results = await readFile('./responses/validators.json') const results = await readFile('./responses/validators.json')
results.homeOk = true results.homeOk = true
results.foreignOk = true results.foreignOk = true
for (const hv in results.home.validators) {
if (results.home.validators[hv].leftTx < MONITOR_TX_NUMBER_THRESHOLD) { if (MONITOR_VALIDATOR_HOME_TX_LIMIT) {
results.homeOk = false for (const hv in results.home.validators) {
break if (results.home.validators[hv].leftTx < MONITOR_TX_NUMBER_THRESHOLD) {
results.homeOk = false
break
}
} }
} }
for (const hv in results.foreign.validators) {
if (results.foreign.validators[hv].leftTx < MONITOR_TX_NUMBER_THRESHOLD) { if (MONITOR_VALIDATOR_FOREIGN_TX_LIMIT) {
results.foreignOk = false for (const hv in results.foreign.validators) {
break if (results.foreign.validators[hv].leftTx < MONITOR_TX_NUMBER_THRESHOLD) {
results.foreignOk = false
break
}
} }
} }
results.ok = results.homeOk && results.foreignOk results.ok = results.homeOk && results.foreignOk
res.json(results) res.json(results)
} catch (e) { } catch (e) {

@ -10,12 +10,10 @@ const {
COMMON_FOREIGN_RPC_URL, COMMON_FOREIGN_RPC_URL,
COMMON_HOME_BRIDGE_ADDRESS, COMMON_HOME_BRIDGE_ADDRESS,
COMMON_FOREIGN_BRIDGE_ADDRESS, COMMON_FOREIGN_BRIDGE_ADDRESS,
MONITOR_VALIDATOR_HOME_TX_LIMIT,
COMMON_HOME_GAS_PRICE_SUPPLIER_URL, COMMON_HOME_GAS_PRICE_SUPPLIER_URL,
COMMON_HOME_GAS_PRICE_SPEED_TYPE, COMMON_HOME_GAS_PRICE_SPEED_TYPE,
COMMON_HOME_GAS_PRICE_FALLBACK, COMMON_HOME_GAS_PRICE_FALLBACK,
COMMON_HOME_GAS_PRICE_FACTOR, COMMON_HOME_GAS_PRICE_FACTOR,
MONITOR_VALIDATOR_FOREIGN_TX_LIMIT,
COMMON_FOREIGN_GAS_PRICE_SUPPLIER_URL, COMMON_FOREIGN_GAS_PRICE_SUPPLIER_URL,
COMMON_FOREIGN_GAS_PRICE_SPEED_TYPE, COMMON_FOREIGN_GAS_PRICE_SPEED_TYPE,
COMMON_FOREIGN_GAS_PRICE_FALLBACK, COMMON_FOREIGN_GAS_PRICE_FALLBACK,
@ -23,6 +21,8 @@ const {
} = process.env } = process.env
const MONITOR_HOME_START_BLOCK = Number(process.env.MONITOR_HOME_START_BLOCK) || 0 const MONITOR_HOME_START_BLOCK = Number(process.env.MONITOR_HOME_START_BLOCK) || 0
const MONITOR_FOREIGN_START_BLOCK = Number(process.env.MONITOR_FOREIGN_START_BLOCK) || 0 const MONITOR_FOREIGN_START_BLOCK = Number(process.env.MONITOR_FOREIGN_START_BLOCK) || 0
const MONITOR_VALIDATOR_HOME_TX_LIMIT = Number(process.env.MONITOR_VALIDATOR_HOME_TX_LIMIT) || 0
const MONITOR_VALIDATOR_FOREIGN_TX_LIMIT = Number(process.env.MONITOR_VALIDATOR_FOREIGN_TX_LIMIT) || 0
const Web3Utils = Web3.utils const Web3Utils = Web3.utils
@ -65,76 +65,97 @@ async function main(bridgeMode) {
const foreignBridgeValidators = new web3Foreign.eth.Contract(BRIDGE_VALIDATORS_ABI, foreignValidatorsAddress) const foreignBridgeValidators = new web3Foreign.eth.Contract(BRIDGE_VALIDATORS_ABI, foreignValidatorsAddress)
logger.debug('calling foreignBridgeValidators getValidatorList()') logger.debug('calling foreignBridgeValidators getValidatorList()')
const foreignValidators = await getValidatorList(foreignValidatorsAddress, web3Foreign.eth, { const foreignValidators = (await getValidatorList(foreignValidatorsAddress, web3Foreign.eth, {
from: MONITOR_FOREIGN_START_BLOCK, from: MONITOR_FOREIGN_START_BLOCK,
to: foreignBlockNumber, to: foreignBlockNumber,
logger logger
}) })).map(web3Foreign.utils.toChecksumAddress)
logger.debug('calling homeBridgeValidators getValidatorList()') logger.debug('calling homeBridgeValidators getValidatorList()')
const homeValidators = await getValidatorList(homeValidatorsAddress, web3Home.eth, { const homeValidators = (await getValidatorList(homeValidatorsAddress, web3Home.eth, {
from: MONITOR_HOME_START_BLOCK, from: MONITOR_HOME_START_BLOCK,
to: homeBlockNumber, to: homeBlockNumber,
logger logger
}) })).map(web3Home.utils.toChecksumAddress)
const homeBalances = {}
logger.debug('calling asyncForEach homeValidators homeBalances')
await asyncForEach(homeValidators, async v => {
homeBalances[v] = Web3Utils.fromWei(await web3Home.eth.getBalance(v))
})
const foreignVBalances = {} const foreignVBalances = {}
const homeVBalances = {} const homeVBalances = {}
logger.debug('calling home getGasPrices') let homeGasPrice
const homeGasPrice = let homeGasPriceGwei
(await gasPriceFromSupplier(() => fetch(COMMON_HOME_GAS_PRICE_SUPPLIER_URL), homeGasPriceSupplierOpts)) || let homeTxCost
Web3Utils.toBN(COMMON_HOME_GAS_PRICE_FALLBACK)
const homeGasPriceGwei = Web3Utils.fromWei(homeGasPrice.toString(), 'gwei')
const homeTxCost = homeGasPrice.mul(Web3Utils.toBN(MONITOR_VALIDATOR_HOME_TX_LIMIT))
logger.debug('calling foreign getGasPrices') if (MONITOR_VALIDATOR_HOME_TX_LIMIT) {
const foreignGasPrice = logger.debug('calling home getGasPrices')
(await gasPriceFromSupplier(() => fetch(COMMON_FOREIGN_GAS_PRICE_SUPPLIER_URL), foreignGasPriceSupplierOpts)) || homeGasPrice =
Web3Utils.toBN(COMMON_FOREIGN_GAS_PRICE_FALLBACK) (await gasPriceFromSupplier(() => fetch(COMMON_HOME_GAS_PRICE_SUPPLIER_URL), homeGasPriceSupplierOpts)) ||
const foreignGasPriceGwei = Web3Utils.fromWei(foreignGasPrice.toString(), 'gwei') Web3Utils.toBN(COMMON_HOME_GAS_PRICE_FALLBACK)
const foreignTxCost = foreignGasPrice.mul(Web3Utils.toBN(MONITOR_VALIDATOR_FOREIGN_TX_LIMIT)) homeGasPriceGwei = Web3Utils.fromWei(homeGasPrice.toString(), 'gwei')
homeTxCost = homeGasPrice.mul(Web3Utils.toBN(MONITOR_VALIDATOR_HOME_TX_LIMIT))
}
let foreignGasPrice
let foreignGasPriceGwei
let foreignTxCost
if (MONITOR_VALIDATOR_FOREIGN_TX_LIMIT) {
logger.debug('calling foreign getGasPrices')
foreignGasPrice =
(await gasPriceFromSupplier(() => fetch(COMMON_FOREIGN_GAS_PRICE_SUPPLIER_URL), foreignGasPriceSupplierOpts)) ||
Web3Utils.toBN(COMMON_FOREIGN_GAS_PRICE_FALLBACK)
foreignGasPriceGwei = Web3Utils.fromWei(foreignGasPrice.toString(), 'gwei')
foreignTxCost = foreignGasPrice.mul(Web3Utils.toBN(MONITOR_VALIDATOR_FOREIGN_TX_LIMIT))
}
let validatorsMatch = true let validatorsMatch = true
logger.debug('calling asyncForEach foreignValidators foreignVBalances') logger.debug('calling asyncForEach foreignValidators foreignVBalances')
await asyncForEach(foreignValidators, async v => { await asyncForEach(foreignValidators, async v => {
const balance = await web3Foreign.eth.getBalance(v) const balance = await web3Foreign.eth.getBalance(v)
const leftTx = Web3Utils.toBN(balance) if (MONITOR_VALIDATOR_FOREIGN_TX_LIMIT) {
.div(foreignTxCost) const leftTx = Web3Utils.toBN(balance)
.toString(10) .div(foreignTxCost)
foreignVBalances[v] = { .toString(10)
balance: Web3Utils.fromWei(balance), foreignVBalances[v] = {
leftTx: Number(leftTx), balance: Web3Utils.fromWei(balance),
gasPrice: Number(foreignGasPriceGwei) leftTx: Number(leftTx),
gasPrice: Number(foreignGasPriceGwei)
}
} else {
foreignVBalances[v] = {
balance: Web3Utils.fromWei(balance)
}
} }
if (!homeValidators.includes(v)) { if (!homeValidators.includes(v)) {
validatorsMatch = false validatorsMatch = false
foreignVBalances[v].onlyOnForeign = true foreignVBalances[v].onlyOnForeign = true
} }
}) })
logger.debug('calling asyncForEach homeValidators homeVBalances') logger.debug('calling asyncForEach homeValidators homeVBalances')
await asyncForEach(homeValidators, async v => { await asyncForEach(homeValidators, async v => {
const balance = await web3Home.eth.getBalance(v) const balance = await web3Home.eth.getBalance(v)
const leftTx = homeTxCost.isZero() if (MONITOR_VALIDATOR_HOME_TX_LIMIT) {
? 999999 const leftTx = Web3Utils.toBN(balance)
: Web3Utils.toBN(balance) .div(homeTxCost)
.div(homeTxCost) .toString(10)
.toString(10) homeVBalances[v] = {
homeVBalances[v] = { balance: Web3Utils.fromWei(balance),
balance: Web3Utils.fromWei(balance), leftTx: Number(leftTx),
leftTx: Number(leftTx), gasPrice: Number(homeGasPriceGwei)
gasPrice: Number(homeGasPriceGwei) }
} else {
homeVBalances[v] = {
balance: Web3Utils.fromWei(balance)
}
} }
if (!foreignValidators.includes(v)) { if (!foreignValidators.includes(v)) {
validatorsMatch = false validatorsMatch = false
homeVBalances[v].onlyOnHome = true homeVBalances[v].onlyOnHome = true
} }
}) })
logger.debug('calling homeBridgeValidators.methods.requiredSignatures().call()') logger.debug('calling homeBridgeValidators.methods.requiredSignatures().call()')
const reqSigHome = await homeBridgeValidators.methods.requiredSignatures().call() const reqSigHome = await homeBridgeValidators.methods.requiredSignatures().call()
logger.debug('calling foreignBridgeValidators.methods.requiredSignatures().call()') logger.debug('calling foreignBridgeValidators.methods.requiredSignatures().call()')