Fix error handling on monitor workers

This commit is contained in:
Gerardo Nardelli 2019-05-30 15:05:36 -03:00
parent 1ab4a0eca8
commit 5ab299089f
7 changed files with 314 additions and 352 deletions

@ -13,67 +13,57 @@ const foreignProvider = new Web3.providers.HttpProvider(FOREIGN_RPC_URL)
const web3Foreign = new Web3(foreignProvider) const web3Foreign = new Web3(foreignProvider)
async function main() { async function main() {
try { const { foreignDeposits, homeDeposits, homeWithdrawals, foreignWithdrawals } = await eventsInfo()
const {
foreignDeposits,
homeDeposits,
homeWithdrawals,
foreignWithdrawals
} = await eventsInfo()
const xSignatures = foreignDeposits.filter(findDifferences(homeDeposits)) const xSignatures = foreignDeposits.filter(findDifferences(homeDeposits))
const xAffirmations = homeWithdrawals.filter(findDifferences(foreignWithdrawals)) const xAffirmations = homeWithdrawals.filter(findDifferences(foreignWithdrawals))
logger.debug('building misbehavior blocks') logger.debug('building misbehavior blocks')
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)
const baseRange = [false, false, false, false, false] const baseRange = [false, false, false, false, false]
const xSignaturesMisbehavior = buildRangesObject( const xSignaturesMisbehavior = buildRangesObject(
xSignatures.map(findMisbehaviorRange(foreignBlockNumber)).reduce(mergeRanges, baseRange) xSignatures.map(findMisbehaviorRange(foreignBlockNumber)).reduce(mergeRanges, baseRange)
) )
const xAffirmationsMisbehavior = buildRangesObject( const xAffirmationsMisbehavior = buildRangesObject(
xAffirmations.map(findMisbehaviorRange(homeBlockNumber)).reduce(mergeRanges, baseRange) xAffirmations.map(findMisbehaviorRange(homeBlockNumber)).reduce(mergeRanges, baseRange)
) )
logger.debug('extracting most recent transactionHash') logger.debug('extracting most recent transactionHash')
const { transactionHash: xSignaturesMostRecentTxHash = '' } = const { transactionHash: xSignaturesMostRecentTxHash = '' } =
xSignatures.sort(sortEvents).reverse()[0] || {} xSignatures.sort(sortEvents).reverse()[0] || {}
const { transactionHash: xAffirmationsMostRecentTxHash = '' } = const { transactionHash: xAffirmationsMostRecentTxHash = '' } =
xAffirmations.sort(sortEvents).reverse()[0] || {} xAffirmations.sort(sortEvents).reverse()[0] || {}
logger.debug('building transaction objects') logger.debug('building transaction objects')
const foreignValidators = await Promise.all( const foreignValidators = await Promise.all(
xSignatures.map(event => findTxSender(web3Foreign)(event)) xSignatures.map(event => findTxSender(web3Foreign)(event))
) )
const homeValidators = await Promise.all( const homeValidators = await Promise.all(
xAffirmations.map(event => findTxSender(web3Home)(event)) xAffirmations.map(event => findTxSender(web3Home)(event))
) )
const xSignaturesTxs = xSignatures const xSignaturesTxs = xSignatures
.map(normalizeEventInformation) .map(normalizeEventInformation)
.reduce(buildTxList(foreignValidators), {}) .reduce(buildTxList(foreignValidators), {})
const xAffirmationsTxs = xAffirmations const xAffirmationsTxs = xAffirmations
.map(normalizeEventInformation) .map(normalizeEventInformation)
.reduce(buildTxList(homeValidators), {}) .reduce(buildTxList(homeValidators), {})
logger.debug('Done') logger.debug('Done')
return { return {
executeSignatures: { executeSignatures: {
misbehavior: xSignaturesMisbehavior, misbehavior: xSignaturesMisbehavior,
mostRecentTxHash: xSignaturesMostRecentTxHash, mostRecentTxHash: xSignaturesMostRecentTxHash,
transactions: xSignaturesTxs transactions: xSignaturesTxs
}, },
executeAffirmations: { executeAffirmations: {
misbehavior: xAffirmationsMisbehavior, misbehavior: xAffirmationsMisbehavior,
mostRecentTxHash: xAffirmationsMostRecentTxHash, mostRecentTxHash: xAffirmationsMostRecentTxHash,
transactions: xAffirmationsTxs transactions: xAffirmationsTxs
}, },
lastChecked: Math.floor(Date.now() / 1000) lastChecked: Math.floor(Date.now() / 1000)
}
} catch (e) {
logger.error(e)
throw e
} }
} }

@ -40,10 +40,8 @@ async function checkWorker() {
JSON.stringify(vBalances, null, 4) JSON.stringify(vBalances, null, 4)
) )
logger.debug('Done') logger.debug('Done')
return status
} catch (e) { } catch (e) {
logger.error(e) logger.error(e)
throw e
} }
} }
checkWorker() checkWorker()

@ -21,10 +21,8 @@ async function checkWorker2() {
JSON.stringify(_alerts, null, 4) JSON.stringify(_alerts, null, 4)
) )
logger.debug('Done x2') logger.debug('Done x2')
return evStats
} catch (e) { } catch (e) {
logger.error('checkWorker2.js', e) logger.error(e)
throw e
} }
} }
checkWorker2() checkWorker2()

@ -57,38 +57,33 @@ function compareTransferForeign(home) {
} }
async function main() { async function main() {
try { const {
const { foreignDeposits,
foreignDeposits, homeDeposits,
homeDeposits, homeWithdrawals,
homeWithdrawals, foreignWithdrawals,
foreignWithdrawals, isExternalErc20
isExternalErc20 } = await eventsInfo()
} = await eventsInfo()
const onlyInHomeDeposits = homeDeposits.filter(compareDepositsHome(foreignDeposits)) const onlyInHomeDeposits = homeDeposits.filter(compareDepositsHome(foreignDeposits))
const onlyInForeignDeposits = foreignDeposits const onlyInForeignDeposits = foreignDeposits
.concat([]) .concat([])
.filter(compareDepositsForeign(homeDeposits)) .filter(compareDepositsForeign(homeDeposits))
const onlyInHomeWithdrawals = isExternalErc20 const onlyInHomeWithdrawals = isExternalErc20
? homeWithdrawals.filter(compareTransferHome(foreignWithdrawals)) ? homeWithdrawals.filter(compareTransferHome(foreignWithdrawals))
: homeWithdrawals.filter(compareDepositsForeign(foreignWithdrawals)) : homeWithdrawals.filter(compareDepositsForeign(foreignWithdrawals))
const onlyInForeignWithdrawals = isExternalErc20 const onlyInForeignWithdrawals = isExternalErc20
? foreignWithdrawals.filter(compareTransferForeign(homeWithdrawals)) ? foreignWithdrawals.filter(compareTransferForeign(homeWithdrawals))
: foreignWithdrawals.filter(compareDepositsHome(homeWithdrawals)) : foreignWithdrawals.filter(compareDepositsHome(homeWithdrawals))
logger.debug('Done') logger.debug('Done')
return { return {
onlyInHomeDeposits, onlyInHomeDeposits,
onlyInForeignDeposits, onlyInForeignDeposits,
onlyInHomeWithdrawals, onlyInHomeWithdrawals,
onlyInForeignWithdrawals, onlyInForeignWithdrawals,
lastChecked: Math.floor(Date.now() / 1000) lastChecked: Math.floor(Date.now() / 1000)
}
} catch (e) {
logger.error(e)
throw e
} }
} }

@ -26,107 +26,98 @@ const FOREIGN_NATIVE_TO_ERC_ABI = require('../contracts/build/contracts/ForeignB
.abi .abi
async function main(bridgeMode) { async function main(bridgeMode) {
try { if (bridgeMode === BRIDGE_MODES.ERC_TO_ERC) {
if (bridgeMode === BRIDGE_MODES.ERC_TO_ERC) { const foreignBridge = new web3Foreign.eth.Contract(
const foreignBridge = new web3Foreign.eth.Contract( FOREIGN_ERC_TO_ERC_ABI,
FOREIGN_ERC_TO_ERC_ABI, FOREIGN_BRIDGE_ADDRESS
FOREIGN_BRIDGE_ADDRESS )
) const erc20Address = await foreignBridge.methods.erc20token().call()
const erc20Address = await foreignBridge.methods.erc20token().call() const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) logger.debug('calling erc20Contract.methods.balanceOf')
logger.debug('calling erc20Contract.methods.balanceOf') const foreignErc20Balance = await erc20Contract.methods.balanceOf(FOREIGN_BRIDGE_ADDRESS).call()
const foreignErc20Balance = await erc20Contract.methods const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_ERC_ABI, HOME_BRIDGE_ADDRESS)
.balanceOf(FOREIGN_BRIDGE_ADDRESS) logger.debug('calling homeBridge.methods.erc677token')
.call() const tokenAddress = await homeBridge.methods.erc677token().call()
const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_ERC_ABI, HOME_BRIDGE_ADDRESS) const tokenContract = new web3Home.eth.Contract(ERC677_ABI, tokenAddress)
logger.debug('calling homeBridge.methods.erc677token') logger.debug('calling tokenContract.methods.totalSupply()')
const tokenAddress = await homeBridge.methods.erc677token().call() const totalSupply = await tokenContract.methods.totalSupply().call()
const tokenContract = new web3Home.eth.Contract(ERC677_ABI, tokenAddress) const foreignBalanceBN = new BN(foreignErc20Balance)
logger.debug('calling tokenContract.methods.totalSupply()') const foreignTotalSupplyBN = new BN(totalSupply)
const totalSupply = await tokenContract.methods.totalSupply().call() const diff = foreignBalanceBN.minus(foreignTotalSupplyBN).toString(10)
const foreignBalanceBN = new BN(foreignErc20Balance) logger.debug('Done')
const foreignTotalSupplyBN = new BN(totalSupply) return {
const diff = foreignBalanceBN.minus(foreignTotalSupplyBN).toString(10) home: {
logger.debug('Done') totalSupply: Web3Utils.fromWei(totalSupply)
return { },
home: { foreign: {
totalSupply: Web3Utils.fromWei(totalSupply) erc20Balance: Web3Utils.fromWei(foreignErc20Balance)
}, },
foreign: { balanceDiff: Number(Web3Utils.fromWei(diff)),
erc20Balance: Web3Utils.fromWei(foreignErc20Balance) lastChecked: Math.floor(Date.now() / 1000)
},
balanceDiff: Number(Web3Utils.fromWei(diff)),
lastChecked: Math.floor(Date.now() / 1000)
}
} else if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC) {
logger.debug('calling web3Home.eth.getBalance')
const foreignBridge = new web3Foreign.eth.Contract(
FOREIGN_NATIVE_TO_ERC_ABI,
FOREIGN_BRIDGE_ADDRESS
)
const erc20Address = await foreignBridge.methods.erc677token().call()
const homeBalance = await web3Home.eth.getBalance(HOME_BRIDGE_ADDRESS)
const tokenContract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
logger.debug('calling tokenContract.methods.totalSupply()')
const totalSupply = await tokenContract.methods.totalSupply().call()
const homeBalanceBN = new BN(homeBalance)
const foreignTotalSupplyBN = new BN(totalSupply)
const diff = homeBalanceBN.minus(foreignTotalSupplyBN).toString(10)
logger.debug('Done')
return {
home: {
balance: Web3Utils.fromWei(homeBalance)
},
foreign: {
totalSupply: Web3Utils.fromWei(totalSupply)
},
balanceDiff: Number(Web3Utils.fromWei(diff)),
lastChecked: Math.floor(Date.now() / 1000)
}
} else if (bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) {
const foreignBridge = new web3Foreign.eth.Contract(
FOREIGN_ERC_TO_NATIVE_ABI,
FOREIGN_BRIDGE_ADDRESS
)
const erc20Address = await foreignBridge.methods.erc20token().call()
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
logger.debug('calling erc20Contract.methods.balanceOf')
const foreignErc20Balance = await erc20Contract.methods
.balanceOf(FOREIGN_BRIDGE_ADDRESS)
.call()
const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_NATIVE_ABI, HOME_BRIDGE_ADDRESS)
logger.debug('calling homeBridge.methods.blockRewardContract')
const blockRewardAddress = await homeBridge.methods.blockRewardContract().call()
const blockRewardContract = new web3Home.eth.Contract(BLOCK_REWARD_ABI, blockRewardAddress)
logger.debug('calling blockReward.methods.mintedTotally')
const mintedCoins = await blockRewardContract.methods.mintedTotally().call()
logger.debug('calling homeBridge.methods.totalBurntCoins')
const burntCoins = await homeBridge.methods.totalBurntCoins().call()
const mintedCoinsBN = new BN(mintedCoins)
const burntCoinsBN = new BN(burntCoins)
const totalSupplyBN = mintedCoinsBN.minus(burntCoinsBN)
const foreignErc20BalanceBN = new BN(foreignErc20Balance)
const diff = foreignErc20BalanceBN.minus(totalSupplyBN).toFixed()
logger.debug('Done')
return {
home: {
totalSupply: Web3Utils.fromWei(totalSupplyBN.toFixed())
},
foreign: {
erc20Balance: Web3Utils.fromWei(foreignErc20Balance)
},
balanceDiff: Number(Web3Utils.fromWei(diff)),
lastChecked: Math.floor(Date.now() / 1000)
}
} else {
throw new Error(`Unrecognized bridge mode: '${bridgeMode}'`)
} }
} catch (e) { } else if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC) {
logger.error(e) logger.debug('calling web3Home.eth.getBalance')
throw e const foreignBridge = new web3Foreign.eth.Contract(
FOREIGN_NATIVE_TO_ERC_ABI,
FOREIGN_BRIDGE_ADDRESS
)
const erc20Address = await foreignBridge.methods.erc677token().call()
const homeBalance = await web3Home.eth.getBalance(HOME_BRIDGE_ADDRESS)
const tokenContract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
logger.debug('calling tokenContract.methods.totalSupply()')
const totalSupply = await tokenContract.methods.totalSupply().call()
const homeBalanceBN = new BN(homeBalance)
const foreignTotalSupplyBN = new BN(totalSupply)
const diff = homeBalanceBN.minus(foreignTotalSupplyBN).toString(10)
logger.debug('Done')
return {
home: {
balance: Web3Utils.fromWei(homeBalance)
},
foreign: {
totalSupply: Web3Utils.fromWei(totalSupply)
},
balanceDiff: Number(Web3Utils.fromWei(diff)),
lastChecked: Math.floor(Date.now() / 1000)
}
} else if (bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) {
const foreignBridge = new web3Foreign.eth.Contract(
FOREIGN_ERC_TO_NATIVE_ABI,
FOREIGN_BRIDGE_ADDRESS
)
const erc20Address = await foreignBridge.methods.erc20token().call()
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
logger.debug('calling erc20Contract.methods.balanceOf')
const foreignErc20Balance = await erc20Contract.methods.balanceOf(FOREIGN_BRIDGE_ADDRESS).call()
const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_NATIVE_ABI, HOME_BRIDGE_ADDRESS)
logger.debug('calling homeBridge.methods.blockRewardContract')
const blockRewardAddress = await homeBridge.methods.blockRewardContract().call()
const blockRewardContract = new web3Home.eth.Contract(BLOCK_REWARD_ABI, blockRewardAddress)
logger.debug('calling blockReward.methods.mintedTotally')
const mintedCoins = await blockRewardContract.methods.mintedTotally().call()
logger.debug('calling homeBridge.methods.totalBurntCoins')
const burntCoins = await homeBridge.methods.totalBurntCoins().call()
const mintedCoinsBN = new BN(mintedCoins)
const burntCoinsBN = new BN(burntCoins)
const totalSupplyBN = mintedCoinsBN.minus(burntCoinsBN)
const foreignErc20BalanceBN = new BN(foreignErc20Balance)
const diff = foreignErc20BalanceBN.minus(totalSupplyBN).toFixed()
logger.debug('Done')
return {
home: {
totalSupply: Web3Utils.fromWei(totalSupplyBN.toFixed())
},
foreign: {
erc20Balance: Web3Utils.fromWei(foreignErc20Balance)
},
balanceDiff: Number(Web3Utils.fromWei(diff)),
lastChecked: Math.floor(Date.now() / 1000)
}
} else {
throw new Error(`Unrecognized bridge mode: '${bridgeMode}'`)
} }
} }

@ -19,79 +19,74 @@ const { getTokenType } = require('./utils/ercUtils')
const { getPastEvents, getBlockNumber } = require('./utils/contract') const { getPastEvents, getBlockNumber } = require('./utils/contract')
async function main(bridgeMode) { async function main(bridgeMode) {
try { const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode)
const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode) const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS)
const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS)
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS) const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC ? 'erc677token' : 'erc20token'
const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC ? 'erc677token' : 'erc20token' const erc20Address = await foreignBridge.methods[erc20MethodName]().call()
const erc20Address = await foreignBridge.methods[erc20MethodName]().call() const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) const tokenType = await getTokenType(foreignBridge, FOREIGN_BRIDGE_ADDRESS)
const tokenType = await getTokenType(foreignBridge, FOREIGN_BRIDGE_ADDRESS)
logger.debug('getting last block numbers') logger.debug('getting last block numbers')
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)
logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')") logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')")
const homeDeposits = await getPastEvents({ const homeDeposits = await getPastEvents({
contract: homeBridge, contract: homeBridge,
event: 'UserRequestForSignature', event: 'UserRequestForSignature',
fromBlock: HOME_DEPLOYMENT_BLOCK, fromBlock: HOME_DEPLOYMENT_BLOCK,
toBlock: homeBlockNumber, toBlock: homeBlockNumber,
options: {} options: {}
}) })
logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')") logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')")
const foreignDeposits = await getPastEvents({ const foreignDeposits = await getPastEvents({
contract: foreignBridge, contract: foreignBridge,
event: 'RelayedMessage', event: 'RelayedMessage',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK, fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
toBlock: foreignBlockNumber, toBlock: foreignBlockNumber,
options: {} options: {}
}) })
logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')") logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')")
const homeWithdrawals = await getPastEvents({ const homeWithdrawals = await getPastEvents({
contract: homeBridge, contract: homeBridge,
event: 'AffirmationCompleted', event: 'AffirmationCompleted',
fromBlock: HOME_DEPLOYMENT_BLOCK, fromBlock: HOME_DEPLOYMENT_BLOCK,
toBlock: homeBlockNumber, toBlock: homeBlockNumber,
options: {} options: {}
}) })
logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')") logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')")
const foreignWithdrawals = const foreignWithdrawals =
tokenType === ERC_TYPES.ERC20 tokenType === ERC_TYPES.ERC20
? await getPastEvents({ ? await getPastEvents({
contract: erc20Contract, contract: erc20Contract,
event: 'Transfer', event: 'Transfer',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK, fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
toBlock: foreignBlockNumber, toBlock: foreignBlockNumber,
options: { options: {
filter: { to: FOREIGN_BRIDGE_ADDRESS } filter: { to: FOREIGN_BRIDGE_ADDRESS }
} }
}) })
: await getPastEvents({ : await getPastEvents({
contract: foreignBridge, contract: foreignBridge,
event: 'UserRequestForAffirmation', event: 'UserRequestForAffirmation',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK, fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
toBlock: foreignBlockNumber, toBlock: foreignBlockNumber,
options: {} options: {}
}) })
logger.debug('Done') logger.debug('Done')
return { return {
depositsDiff: homeDeposits.length - foreignDeposits.length, depositsDiff: homeDeposits.length - foreignDeposits.length,
withdrawalDiff: homeWithdrawals.length - foreignWithdrawals.length, withdrawalDiff: homeWithdrawals.length - foreignWithdrawals.length,
home: { home: {
deposits: homeDeposits.length, deposits: homeDeposits.length,
withdrawals: homeWithdrawals.length withdrawals: homeWithdrawals.length
}, },
foreign: { foreign: {
deposits: foreignDeposits.length, deposits: foreignDeposits.length,
withdrawals: foreignWithdrawals.length withdrawals: foreignWithdrawals.length
}
} }
} catch (e) {
logger.error(e)
throw e
} }
} }
module.exports = main module.exports = main

@ -47,109 +47,104 @@ async function getGasPrices(type) {
} }
async function main(bridgeMode) { async function main(bridgeMode) {
try { const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode)
const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode) const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS)
const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS)
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS) const homeValidatorsAddress = await homeBridge.methods.validatorContract().call()
const homeValidatorsAddress = await homeBridge.methods.validatorContract().call() const homeBridgeValidators = new web3Home.eth.Contract(
const homeBridgeValidators = new web3Home.eth.Contract( BRIDGE_VALIDATORS_ABI,
BRIDGE_VALIDATORS_ABI, homeValidatorsAddress
homeValidatorsAddress )
)
logger.debug('getting last block numbers') logger.debug('getting last block numbers')
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)
logger.debug('calling foreignBridge.methods.validatorContract().call()') logger.debug('calling foreignBridge.methods.validatorContract().call()')
const foreignValidatorsAddress = await foreignBridge.methods.validatorContract().call() const foreignValidatorsAddress = await foreignBridge.methods.validatorContract().call()
const foreignBridgeValidators = new web3Foreign.eth.Contract( const foreignBridgeValidators = new web3Foreign.eth.Contract(
BRIDGE_VALIDATORS_ABI, BRIDGE_VALIDATORS_ABI,
foreignValidatorsAddress foreignValidatorsAddress
) )
logger.debug('calling foreignBridgeValidators getValidatorList()') logger.debug('calling foreignBridgeValidators getValidatorList()')
const foreignValidators = await getValidatorList( const foreignValidators = await getValidatorList(
foreignValidatorsAddress, foreignValidatorsAddress,
web3Foreign.eth, web3Foreign.eth,
FOREIGN_DEPLOYMENT_BLOCK, FOREIGN_DEPLOYMENT_BLOCK,
foreignBlockNumber foreignBlockNumber
) )
logger.debug('calling homeBridgeValidators getValidatorList()') logger.debug('calling homeBridgeValidators getValidatorList()')
const homeValidators = await getValidatorList( const homeValidators = await getValidatorList(
homeValidatorsAddress, homeValidatorsAddress,
web3Home.eth, web3Home.eth,
HOME_DEPLOYMENT_BLOCK, HOME_DEPLOYMENT_BLOCK,
homeBlockNumber homeBlockNumber
) )
const homeBalances = {} const homeBalances = {}
logger.debug('calling asyncForEach homeValidators homeBalances') logger.debug('calling asyncForEach homeValidators homeBalances')
await asyncForEach(homeValidators, async v => { await asyncForEach(homeValidators, async v => {
homeBalances[v] = Web3Utils.fromWei(await web3Home.eth.getBalance(v)) homeBalances[v] = Web3Utils.fromWei(await web3Home.eth.getBalance(v))
}) })
const foreignVBalances = {} const foreignVBalances = {}
const homeVBalances = {} const homeVBalances = {}
logger.debug('calling getGasPrices') logger.debug('calling getGasPrices')
const gasPriceInGwei = await getGasPrices(GAS_PRICE_SPEED_TYPE) const gasPriceInGwei = await getGasPrices(GAS_PRICE_SPEED_TYPE)
const gasPrice = new Web3Utils.BN(Web3Utils.toWei(gasPriceInGwei.toString(10), 'gwei')) const gasPrice = new Web3Utils.BN(Web3Utils.toWei(gasPriceInGwei.toString(10), 'gwei'))
const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT)) const txCost = gasPrice.mul(new Web3Utils.BN(GAS_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 = new Web3Utils.BN(balance).div(txCost).toString(10) const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10)
foreignVBalances[v] = { foreignVBalances[v] = {
balance: Web3Utils.fromWei(balance), balance: Web3Utils.fromWei(balance),
leftTx: Number(leftTx), leftTx: Number(leftTx),
gasPrice: gasPriceInGwei gasPrice: gasPriceInGwei
}
if (!homeValidators.includes(v)) {
validatorsMatch = false
foreignVBalances[v].onlyOnForeign = true
}
})
logger.debug('calling asyncForEach homeValidators homeVBalances')
await asyncForEach(homeValidators, async v => {
const gasPrice = new Web3Utils.BN(1)
const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT))
const balance = await web3Home.eth.getBalance(v)
const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10)
homeVBalances[v] = {
balance: Web3Utils.fromWei(balance),
leftTx: Number(leftTx),
gasPrice: Number(gasPrice.toString(10))
}
if (!foreignValidators.includes(v)) {
validatorsMatch = false
homeVBalances[v].onlyOnHome = true
}
})
logger.debug('calling homeBridgeValidators.methods.requiredSignatures().call()')
const reqSigHome = await homeBridgeValidators.methods.requiredSignatures().call()
logger.debug('calling foreignBridgeValidators.methods.requiredSignatures().call()')
const reqSigForeign = await foreignBridgeValidators.methods.requiredSignatures().call()
logger.debug('Done')
return {
home: {
validators: {
...homeVBalances
},
requiredSignatures: Number(reqSigHome)
},
foreign: {
validators: {
...foreignVBalances
},
requiredSignatures: Number(reqSigForeign)
},
requiredSignaturesMatch: reqSigHome === reqSigForeign,
validatorsMatch,
lastChecked: Math.floor(Date.now() / 1000)
} }
} catch (e) { if (!homeValidators.includes(v)) {
logger.error(e) validatorsMatch = false
throw e foreignVBalances[v].onlyOnForeign = true
}
})
logger.debug('calling asyncForEach homeValidators homeVBalances')
await asyncForEach(homeValidators, async v => {
const gasPrice = new Web3Utils.BN(1)
const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT))
const balance = await web3Home.eth.getBalance(v)
const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10)
homeVBalances[v] = {
balance: Web3Utils.fromWei(balance),
leftTx: Number(leftTx),
gasPrice: Number(gasPrice.toString(10))
}
if (!foreignValidators.includes(v)) {
validatorsMatch = false
homeVBalances[v].onlyOnHome = true
}
})
logger.debug('calling homeBridgeValidators.methods.requiredSignatures().call()')
const reqSigHome = await homeBridgeValidators.methods.requiredSignatures().call()
logger.debug('calling foreignBridgeValidators.methods.requiredSignatures().call()')
const reqSigForeign = await foreignBridgeValidators.methods.requiredSignatures().call()
logger.debug('Done')
return {
home: {
validators: {
...homeVBalances
},
requiredSignatures: Number(reqSigHome)
},
foreign: {
validators: {
...foreignVBalances
},
requiredSignatures: Number(reqSigForeign)
},
requiredSignaturesMatch: reqSigHome === reqSigForeign,
validatorsMatch,
lastChecked: Math.floor(Date.now() / 1000)
} }
} }