From c4b92fc1388504fbf81a6c8d35cc40a5e80cba0f Mon Sep 17 00:00:00 2001 From: phahulin Date: Wed, 20 Jun 2018 23:35:55 +0300 Subject: [PATCH 1/2] optimizations --- .env.example | 3 + .gitignore | 5 +- README.md | 129 ++++++++++++++++++++++++------------- checkWorker.js | 12 +++- checkWorker2.js | 18 ++++++ crontab.example | 2 + eventsStats.js | 27 +++++--- getBalances.js | 7 +- getShortEventStats.js | 29 ++++++--- index.js | 16 +++++ logger.js | 31 +++++++++ responses/.gitkeep | 0 responses/getBalances.json | 16 ----- responses/validators.json | 20 ------ test-srv.js | 9 +++ validators.js | 34 ++++++---- 16 files changed, 243 insertions(+), 115 deletions(-) create mode 100644 checkWorker2.js create mode 100644 crontab.example create mode 100644 logger.js create mode 100644 responses/.gitkeep delete mode 100644 responses/getBalances.json delete mode 100644 responses/validators.json create mode 100644 test-srv.js diff --git a/.env.example b/.env.example index 9aef2c0..12e1556 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,9 @@ FOREIGN_RPC_URL=https://kovan.infura.io/mew HOME_BRIDGE_ADDRESS=0xABb4C1399DcC28FBa3Beb76CAE2b50Be3e087353 FOREIGN_BRIDGE_ADDRESS=0xE405F6872cE38a7a4Ff63DcF946236D458c2ca3a POA20_ADDRESS=0x6F794fb14d01f7551C1fe5614FDDf5895A2e82d3 +HOME_DEPLOYMENT_BLOCK=0 +FOREIGN_DEPLOYMENT_BLOCK=0 GAS_PRICE_SPEED_TYPE=standard GAS_LIMIT=300000 +GAS_PRICE_FALLBACK=21 LEFT_TX_THRESHOLD=100 diff --git a/.gitignore b/.gitignore index 9b1080c..0fad75d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .env node_modules -responses \ No newline at end of file +responses/* +!responses/.gitkeep +*.err +*.out diff --git a/README.md b/README.md index 351849b..eb0ee8f 100644 --- a/README.md +++ b/README.md @@ -7,59 +7,91 @@ Home and Foreign Eth networks to check for balance difference. On Home network it checks for `HOME_BRIDGE_ADDRESS` balance On Foreign network it checks for `POA20_ADDRESS` total supply. -Example of an API `/`: +Example of an API + +* `GET /` - check contract balances & tx numbers ```json { - "home": { - "balance": "145.589637026290448384", - "deposits": 7641, - "withdrawals": 128 - }, - "foreign": { - "totalSupply": "164.0563811", - "deposits": 7642, - "withdrawals": 125 - }, - "balanceDiff": -18.466744073709553, - "lastChecked": 1524872762, - "depositsDiff": -1, - "withdrawalDiff": 3, - "timeDiff": 20 + "home": { + "balance": "3710077.6896438415780044", + "deposits": 481, + "withdrawals": 221, + "requiredSignatures": 2 + }, + "foreign": { + "totalSupply": "3710077.6896438415780044", + "deposits": 481, + "withdrawals": 221, + "requiredSignatures": 2 + }, + "balanceDiff": 0, + "lastChecked": 1529511982, + "depositsDiff": 0, + "withdrawalDiff": 0, + "requiredSignaturesMatch": true } ``` -/validators + +* `GET /validators` - check validators balances ```json { - "home": { - "validators": { - "0xb8988B690910913c97A090c3a6f80FAD8b3A4683": { - "balance": "123.835674629", - "leftTx": 412785582096666, - "gasPrice": 1 - } - } - }, - "foreign": { - "validators": { - "0xb8988B690910913c97A090c3a6f80FAD8b3A4683": { - "balance": "10.523898627992509332", - "leftTx": 17539, - "gasPrice": 2 - } - } - }, - "lastChecked": 1524872829, - "timeDiff": 17, - "homeOk": true, - "foreignOk": true, - "ok": true + "home": { + "validators": { + "0x35DC13c72A9C09C8AEEBD0490C7228C43Ccc38Cd": { + "balance": "19.994900374", + "leftTx": 66649667913333, + "gasPrice": 1 + }, + "0x5D44BC8642947685F45004c936245B969F9709a6": { + "balance": "19.993736069", + "leftTx": 66645786896666, + "gasPrice": 1 + }, + "0x284877074B986A78F01D7Eb1f34B6043b1719002": { + "balance": "19.995139875", + "leftTx": 66650466250000, + "gasPrice": 1 + } + } + }, + "foreign": { + "validators": { + "0x35DC13c72A9C09C8AEEBD0490C7228C43Ccc38Cd": { + "balance": "19.084023268196", + "leftTx": 28915, + "gasPrice": 2.2 + }, + "0x5D44BC8642947685F45004c936245B969F9709a6": { + "balance": "19.086724777075", + "leftTx": 28919, + "gasPrice": 2.2 + }, + "0x284877074B986A78F01D7Eb1f34B6043b1719002": { + "balance": "19.050074813935", + "leftTx": 28863, + "gasPrice": 2.2 + } + } + }, + "lastChecked": 1529512164 +} +``` + +* `GET /eventsStats` - check unprocessed events +```json +{ + "onlyInHomeDeposits": [], + "onlyInForeignDeposits": [], + "onlyInHomeWithdrawals": [], + "onlyInForeignWithdrawals": [], + "lastChecked": 1529512436 } ``` # How to run -Create .env file +Create .env file (see `.env.example` for parameters reference) ```bash HOME_RPC_URL=https://sokol.poa.network FOREIGN_RPC_URL=https://kovan.infura.io/mew @@ -73,12 +105,21 @@ LEFT_TX_THRESHOLD=100 ```bash npm i +# check balances of contracts and validators node checkWorker.js +# check unprocessed events +node checkWorker2.js +# run web interface node index.js ``` -You can create cron job to run worker: +To enabled debug logging, set `DEBUG=1` env variable. + +You can create cron job to run workers (see `crontab.example` for reference): ```bash -crontab -e -* * * * * cd /bridge-monitor; node /bridge-monitor/checkWorker.js +#crontab -e +*/4 * * * * cd $HOME/bridge-monitor; node checkWorker.js >>cronWorker.out 2>>cronWorker.err +*/5 * * * * cd $HOME/bridge-monitor; node checkWorker2.js >>cronWorker2.out 2>>cronWorker2.err ``` + +You can run web interface via [pm2](https://www.npmjs.com/package/pm2) or similar supervisor program. diff --git a/checkWorker.js b/checkWorker.js index 415501b..3d5be7f 100644 --- a/checkWorker.js +++ b/checkWorker.js @@ -1,22 +1,30 @@ const fs = require('fs') +const logger = require('./logger')('checkWorker'); const getBalances = require('./getBalances') const getShortEventStats = require('./getShortEventStats'); const validators = require('./validators'); async function checkWorker() { try { + logger.debug('calling getBalances()'); const balances = await getBalances(); + logger.debug('calling getShortEventStats()'); const events = await getShortEventStats(); const home = Object.assign({}, balances.home, events.home) const foreign = Object.assign({}, balances.foreign, events.foreign) const status = Object.assign({}, balances, events, {home}, {foreign}) + if (!status) throw new Error('status is empty: ' + JSON.stringify(status)); fs.writeFileSync(__dirname + '/responses/getBalances.json', JSON.stringify(status,null,4)); + + logger.debug('calling validators()'); const vBalances = await validators(); + if (!vBalances) throw new Error('vBalances is empty: ' + JSON.stringify(vBalances)); fs.writeFileSync(__dirname + '/responses/validators.json', JSON.stringify(vBalances,null,4)); + logger.debug("Done"); return status; } catch(e) { - console.error(e) + logger.error(e); + throw e; } } checkWorker(); -module.exports = checkWorker; \ No newline at end of file diff --git a/checkWorker2.js b/checkWorker2.js new file mode 100644 index 0000000..2883178 --- /dev/null +++ b/checkWorker2.js @@ -0,0 +1,18 @@ +const fs = require('fs') +const logger = require('./logger')('checkWorker2'); +const eventsStats = require('./eventsStats') + +async function checkWorker2() { + try { + logger.debug('calling eventsStats()'); + const evStats = await eventsStats(); + if (!evStats) throw new Error('evStats is empty: ' + JSON.stringify(evStats)); + fs.writeFileSync(__dirname + '/responses/eventsStats.json', JSON.stringify(evStats,null,4)); + logger.debug("Done"); + return evStats; + } catch(e) { + logger.error('checkWorker2.js', e); + throw e; + } +} +checkWorker2(); diff --git a/crontab.example b/crontab.example new file mode 100644 index 0000000..0fa1787 --- /dev/null +++ b/crontab.example @@ -0,0 +1,2 @@ +*/4 * * * * cd $HOME/bridge-monitor; node checkWorker.js >>cronWorker.out 2>>cronWorker.err +*/5 * * * * cd $HOME/bridge-monitor; node checkWorker2.js >>cronWorker2.out 2>>cronWorker2.err diff --git a/eventsStats.js b/eventsStats.js index 6cdfef1..def9f03 100644 --- a/eventsStats.js +++ b/eventsStats.js @@ -1,9 +1,12 @@ require('dotenv').config(); +const logger = require('./logger')('eventsStats'); const Web3 = require('web3'); const HOME_RPC_URL = process.env.HOME_RPC_URL; const FOREIGN_RPC_URL = process.env.FOREIGN_RPC_URL; const HOME_BRIDGE_ADDRESS = process.env.HOME_BRIDGE_ADDRESS; const FOREIGN_BRIDGE_ADDRESS = process.env.FOREIGN_BRIDGE_ADDRESS; +const HOME_DEPLOYMENT_BLOCK = Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0; +const FOREIGN_DEPLOYMENT_BLOCK = Number(process.env.FOREIGN_DEPLOYMENT_BLOCK) || 0; const homeProvider = new Web3.providers.HttpProvider(HOME_RPC_URL); const web3Home = new Web3(homeProvider); @@ -35,15 +38,16 @@ function compareDepositsForeign(home){ async function main(){ try { - - const FROM_BLOCK = 0; - const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS); const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS); - let homeDeposits = await homeBridge.getPastEvents('Deposit', {fromBlock: FROM_BLOCK}); - let foreignDeposits = await foreignBridge.getPastEvents('Deposit', {fromBlock: FROM_BLOCK}); - let homeWithdrawals = await homeBridge.getPastEvents('Withdraw', {fromBlock: FROM_BLOCK}); - let foreignWithdrawals = await foreignBridge.getPastEvents('Withdraw', {fromBlock: FROM_BLOCK}); + logger.debug("calling homeBridge.getPastEvents('Deposit')"); + let homeDeposits = await homeBridge.getPastEvents('Deposit', {fromBlock: HOME_DEPLOYMENT_BLOCK}); + logger.debug("calling foreignBridge.getPastEvents('Deposit')"); + let foreignDeposits = await foreignBridge.getPastEvents('Deposit', {fromBlock: FOREIGN_DEPLOYMENT_BLOCK}); + logger.debug("calling homeBridge.getPastEvents('Withdraw')"); + let homeWithdrawals = await homeBridge.getPastEvents('Withdraw', {fromBlock: HOME_DEPLOYMENT_BLOCK}); + logger.debug("calling foreignBridge.getPastEvents('Withdraw')"); + let foreignWithdrawals = await foreignBridge.getPastEvents('Withdraw', {fromBlock: FOREIGN_DEPLOYMENT_BLOCK}); const onlyInHomeDeposits = homeDeposits.filter(compareDepositsHome(foreignDeposits)) const onlyInForeignDeposits = foreignDeposits.concat([]).filter(compareDepositsForeign(homeDeposits)) @@ -51,16 +55,19 @@ async function main(){ const onlyInHomeWithdrawals = homeWithdrawals.filter(compareDepositsForeign(foreignWithdrawals)) const onlyInForeignWithdrawals = foreignWithdrawals.filter(compareDepositsHome(homeWithdrawals)) + logger.debug("Done"); return { onlyInHomeDeposits, onlyInForeignDeposits, onlyInHomeWithdrawals, - onlyInForeignWithdrawals + onlyInForeignWithdrawals, + lastChecked: Math.floor(Date.now() / 1000), } } catch(e) { - console.error(e); + logger.error(e); + throw e; } } -main() + module.exports = main; \ No newline at end of file diff --git a/getBalances.js b/getBalances.js index 3f9f4a0..36d8629 100644 --- a/getBalances.js +++ b/getBalances.js @@ -1,4 +1,5 @@ require('dotenv').config(); +const logger = require('./logger')('getBalances'); const fs = require('fs') const Web3 = require('web3'); const Web3Utils = require('web3-utils') @@ -20,12 +21,15 @@ const ERC677_ABI = require('./abis/ERC677.abi'); async function main(){ try { + logger.debug('calling web3Home.eth.getBalance'); const homeBalance = await web3Home.eth.getBalance(HOME_BRIDGE_ADDRESS) const tokenContract = new web3Foreign.eth.Contract(ERC677_ABI, POA20_ADDRESS); + 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) @@ -37,7 +41,8 @@ async function main(){ lastChecked: Math.floor(Date.now() / 1000) } } catch(e) { - console.error(e); + logger.error(e); + throw e; } } diff --git a/getShortEventStats.js b/getShortEventStats.js index e0a23a8..b1d1bad 100644 --- a/getShortEventStats.js +++ b/getShortEventStats.js @@ -1,4 +1,5 @@ require('dotenv').config(); +const logger = require('./logger')('getShortEventStats.js'); const fs = require('fs') const Web3 = require('web3'); const Web3Utils = require('web3-utils') @@ -7,6 +8,8 @@ const FOREIGN_RPC_URL = process.env.FOREIGN_RPC_URL; const HOME_BRIDGE_ADDRESS = process.env.HOME_BRIDGE_ADDRESS; const FOREIGN_BRIDGE_ADDRESS = process.env.FOREIGN_BRIDGE_ADDRESS; const POA20_ADDRESS = process.env.POA20_ADDRESS; +const HOME_DEPLOYMENT_BLOCK = Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0; +const FOREIGN_DEPLOYMENT_BLOCK = Number(process.env.FOREIGN_DEPLOYMENT_BLOCK) || 0; const homeProvider = new Web3.providers.HttpProvider(HOME_RPC_URL); const web3Home = new Web3(homeProvider); @@ -21,20 +24,27 @@ const BRIDGE_VALIDATORS_ABI = require('./abis/BridgeValidators.abi'); async function main(){ try { - const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS); const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS); + logger.debug("calling homeBridge.methods.validatorContract().call()"); const validatorHomeAddress = await homeBridge.methods.validatorContract().call() + logger.debug("calling foreignBridge.methods.validatorContract().call()"); const validatorForeignAddress = await foreignBridge.methods.validatorContract().call() const homeValidators = new web3Home.eth.Contract(BRIDGE_VALIDATORS_ABI, validatorHomeAddress); - const foreignValidators = new web3Foreign.eth.Contract(BRIDGE_VALIDATORS_ABI, FOREIGN_BRIDGE_ADDRESS); + const foreignValidators = new web3Foreign.eth.Contract(BRIDGE_VALIDATORS_ABI, validatorForeignAddress); + logger.debug("calling homeValidators.methods.requiredSignatures().call()"); const reqSigHome = await homeValidators.methods.requiredSignatures().call() - const reqSigForeign = await homeValidators.methods.requiredSignatures().call() - let homeDeposits = await homeBridge.getPastEvents('Deposit', {fromBlock: 0}); - let foreignDeposits = await foreignBridge.getPastEvents('Deposit', {fromBlock: 0}); - let homeWithdrawals = await homeBridge.getPastEvents('Withdraw', {fromBlock: 0}); - let foreignWithdrawals = await foreignBridge.getPastEvents('Withdraw', {fromBlock: 0}); - + logger.debug("calling foreignValidators.methods.requiredSignatures().call()"); + const reqSigForeign = await foreignValidators.methods.requiredSignatures().call() + logger.debug("calling homeBridge.getPastEvents('Deposit')"); + let homeDeposits = await homeBridge.getPastEvents('Deposit', {fromBlock: HOME_DEPLOYMENT_BLOCK}); + logger.debug("calling foreignBridge.getPastEvents('Deposit')"); + let foreignDeposits = await foreignBridge.getPastEvents('Deposit', {fromBlock: FOREIGN_DEPLOYMENT_BLOCK}); + logger.debug("calling homeBridge.getPastEvents('Withdraw')"); + let homeWithdrawals = await homeBridge.getPastEvents('Withdraw', {fromBlock: HOME_DEPLOYMENT_BLOCK}); + logger.debug("calling foreignBridge.getPastEvents('Withdraw')"); + let foreignWithdrawals = await foreignBridge.getPastEvents('Withdraw', {fromBlock: FOREIGN_DEPLOYMENT_BLOCK}); + logger.debug("Done"); return { depositsDiff: homeDeposits.length - foreignDeposits.length, withdrawalDiff: homeWithdrawals.length - foreignWithdrawals.length, @@ -51,7 +61,8 @@ async function main(){ requiredSignaturesMatch: reqSigHome === reqSigForeign } } catch(e) { - console.error(e); + logger.error(e); + throw e; } } diff --git a/index.js b/index.js index fe26529..620e472 100644 --- a/index.js +++ b/index.js @@ -55,6 +55,22 @@ app.get('/validators', async (req, res, next) => { } }) +// responses/eventsStats.json +app.get('/eventsStats', async (req, res, next) => { + try { + const results = await readFile('./responses/eventsStats.json'); + results.ok = ( + results.onlyInHomeDeposits.length == 0 && results.onlyInForeignDeposits.length == 0 + && + results.onlyInHomeWithdrawals.length == 0 && results.onlyInForeignWithdrawals.length == 0 + ); + res.json(results); + } catch (e) { + //this will eventually be handled by your error handling middleware + next(e) + } +}) + const port = process.env.PORT || 3000; app.set('port', port); app.listen(port, () => console.log('Monitoring app listening on port 3000!')) diff --git a/logger.js b/logger.js new file mode 100644 index 0000000..3926b5e --- /dev/null +++ b/logger.js @@ -0,0 +1,31 @@ +'use strict'; +module.exports = function (name) { + var lastlog = 0; + + function log(...args) { + var now = new Date(); + console.log(now.toISOString(), `(+${lastlog?now.getTime()-lastlog : 0}ms)`, `[${name}]`, ...args); + lastlog = now.getTime(); + } + + function error(...args) { + var now = new Date(); + console.error(now.toISOString(), `[${name}]`, ...args); + } + + var dbg; + if (process.env.DEBUG) { + dbg = (...args) => { + log(...args); + } + } + else { + dbg = () => {} + } + + return { + log, + error, + debug: dbg, + }; +}; diff --git a/responses/.gitkeep b/responses/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/responses/getBalances.json b/responses/getBalances.json deleted file mode 100644 index e3f61af..0000000 --- a/responses/getBalances.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "home": { - "balance": "152.0760811", - "deposits": 7423, - "withdrawals": 55 - }, - "foreign": { - "totalSupply": "152.0760811", - "deposits": 7424, - "withdrawals": 55 - }, - "balanceDiff": "0", - "lastChecked": 1524153000, - "depositsDiff": -1, - "withdrawalDiff": 0 -} \ No newline at end of file diff --git a/responses/validators.json b/responses/validators.json deleted file mode 100644 index d939c50..0000000 --- a/responses/validators.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "home": { - "validators": { - "0xb8988B690910913c97A090c3a6f80FAD8b3A4683": { - "balance": "123.912174892", - "leftTx": "413040582973333", - "gasPrice": "1" - } - } - }, - "foreign": { - "validators": { - "0xb8988B690910913c97A090c3a6f80FAD8b3A4683": { - "balance": "10.626173860992509332", - "leftTx": "17710", - "gasPrice": 2 - } - } - } -} \ No newline at end of file diff --git a/test-srv.js b/test-srv.js new file mode 100644 index 0000000..77822c7 --- /dev/null +++ b/test-srv.js @@ -0,0 +1,9 @@ +const express = require('express') +const app = express() + +app.all('/', function (req, res) { + setTimeout(function () { res.status(504); res.end() }, 2000); +}); + +let PORT = process.env.PORT || 4000; +app.listen(PORT, () => console.log('Listening on port ' + PORT)); diff --git a/validators.js b/validators.js index fb6413d..da7f259 100644 --- a/validators.js +++ b/validators.js @@ -2,6 +2,7 @@ // ValidatorAdded // ValidatorRemoved require('dotenv').config(); +const logger = require('./logger')('validators'); const fs = require('fs') const Web3 = require('web3'); const Web3Utils = require('web3-utils') @@ -11,6 +12,8 @@ const FOREIGN_RPC_URL = process.env.FOREIGN_RPC_URL; const HOME_BRIDGE_ADDRESS = process.env.HOME_BRIDGE_ADDRESS; const FOREIGN_BRIDGE_ADDRESS = process.env.FOREIGN_BRIDGE_ADDRESS; const POA20_ADDRESS = process.env.POA20_ADDRESS; +const HOME_DEPLOYMENT_BLOCK = Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0; +const FOREIGN_DEPLOYMENT_BLOCK = Number(process.env.FOREIGN_DEPLOYMENT_BLOCK) || 0; const GAS_PRICE_SPEED_TYPE = process.env.GAS_PRICE_SPEED_TYPE; const GAS_LIMIT = process.env.GAS_LIMIT; @@ -34,26 +37,28 @@ async function getGasPrices(type){ try { const response = await fetch('https://gasprice.poa.network/'); const json = await response.json() + logger.log('Fetched gasprice: ' + json[type]); return json[type] } catch(e) { - console.error("Gas Price API is not available", e) + logger.error("Gas Price API is not available", e) return GAS_PRICE_FALLBACK; } } async function main(){ try { - const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS); const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS); const homeValidatorsAddress = await homeBridge.methods.validatorContract().call() const homeBridgeValidators = new web3Home.eth.Contract(BRIDGE_VALIDATORS_ABI, homeValidatorsAddress); + logger.debug('calling foreignBridge.methods.validatorContract().call()'); const foreignValidatorsAddress = await foreignBridge.methods.validatorContract().call() const foreignBridgeValidators = new web3Foreign.eth.Contract(BRIDGE_VALIDATORS_ABI, foreignValidatorsAddress); - - let ValidatorAddedForeign = await foreignBridgeValidators.getPastEvents('ValidatorAdded', {fromBlock: 0}); - let ValidatorRemovedForeign = await foreignBridgeValidators.getPastEvents('ValidatorRemoved', {fromBlock: 0}); + logger.debug("calling foreignBridgeValidators.getPastEvents('ValidatorAdded')"); + let ValidatorAddedForeign = await foreignBridgeValidators.getPastEvents('ValidatorAdded', {fromBlock: FOREIGN_DEPLOYMENT_BLOCK}); + logger.debug("calling foreignBridgeValidators.getPastEvents('ValidatorRemoved')"); + let ValidatorRemovedForeign = await foreignBridgeValidators.getPastEvents('ValidatorRemoved', {fromBlock: FOREIGN_DEPLOYMENT_BLOCK}); let foreignValidators = ValidatorAddedForeign.map(val => { return val.returnValues.validator }) @@ -61,9 +66,10 @@ async function main(){ return val.returnValues.validator }) foreignValidators = foreignValidators.filter(val => !foreignRemovedValidators.includes(val)); - - let ValidatorAdded = await homeBridgeValidators.getPastEvents('ValidatorAdded', {fromBlock: 0}); - let ValidatorRemoved = await homeBridgeValidators.getPastEvents('ValidatorRemoved', {fromBlock: 0}); + logger.debug("calling homeBridgeValidators.getPastEvents('ValidatorAdded')"); + let ValidatorAdded = await homeBridgeValidators.getPastEvents('ValidatorAdded', {fromBlock: HOME_DEPLOYMENT_BLOCK}); + logger.debug("calling homeBridgeValidators.getPastEvents('ValidatorRemoved')"); + let ValidatorRemoved = await homeBridgeValidators.getPastEvents('ValidatorRemoved', {fromBlock: HOME_DEPLOYMENT_BLOCK}); let homeValidators = ValidatorAdded.map(val => { return val.returnValues.validator }) @@ -72,20 +78,23 @@ async function main(){ }) homeValidators = homeValidators.filter(val => !homeRemovedValidators.includes(val)); let homeBalances = {}; + logger.debug("calling asyncForEach homeValidators homeBalances"); await asyncForEach(homeValidators, async (v) => { homeBalances[v] = Web3Utils.fromWei(await web3Home.eth.getBalance(v)) }) let foreignVBalances = {}; let homeVBalances = {}; + logger.debug("calling getGasPrices"); let gasPriceInGwei = await getGasPrices(GAS_PRICE_SPEED_TYPE) let gasPrice = new Web3Utils.BN(Web3Utils.toWei(gasPriceInGwei.toString(10), 'gwei')) const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT)) + logger.debug("calling asyncForEach foreignValidators foreignVBalances"); await asyncForEach(foreignValidators, async (v) => { const balance = await web3Foreign.eth.getBalance(v) const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10) foreignVBalances[v] = {balance: Web3Utils.fromWei(balance), leftTx: Number(leftTx), gasPrice: gasPriceInGwei} }) - + 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)) @@ -93,7 +102,7 @@ async function main(){ const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10) homeVBalances[v] = {balance: Web3Utils.fromWei(balance), leftTx: Number(leftTx), gasPrice: Number(gasPrice.toString(10))} }) - + logger.debug("Done"); return { home: { validators: { @@ -108,9 +117,10 @@ async function main(){ lastChecked: Math.floor(Date.now() / 1000) } } catch(e) { - console.error(e); + logger.error(e); + throw e; } } -main() + module.exports = main; \ No newline at end of file From 8c2a4a05d282dc46a6cd6b6528627f1166703fd9 Mon Sep 17 00:00:00 2001 From: phahulin Date: Thu, 21 Jun 2018 00:00:48 +0300 Subject: [PATCH 2/2] Add GAS_PRICE_FALLBACK to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eb0ee8f..b24b25b 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ FOREIGN_BRIDGE_ADDRESS=0xE405F6872cE38a7a4Ff63DcF946236D458c2ca3a POA20_ADDRESS=0x6F794fb14d01f7551C1fe5614FDDf5895A2e82d3 GAS_PRICE_SPEED_TYPE=standard GAS_LIMIT=300000 +GAS_PRICE_FALLBACK=21 LEFT_TX_THRESHOLD=100 ```