From f8d85b14de04b07da247d2e5c17d0baed3b2613a Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Thu, 29 Oct 2020 11:25:43 +0300 Subject: [PATCH] Add allowance/block lists support to monitor (#477) --- .dockerignore | 5 +++++ .gitignore | 10 ++++------ CONFIGURATION.md | 3 +++ monitor/.env.example | 3 +++ monitor/alerts.js | 19 ++++++------------- monitor/cache/.gitkeep | 0 monitor/checkWorker.js | 13 ++++++++----- monitor/checkWorker2.js | 2 ++ monitor/checkWorker3.js | 7 ++----- monitor/docker-compose.yml | 5 +++-- monitor/eventsStats.js | 29 +++++++++++++++++++++++++++-- monitor/getBalances.js | 18 +++--------------- monitor/getShortEventStats.js | 31 +++++++++++++++++++++++++++++-- monitor/package.json | 2 +- monitor/scripts/getBridgeStats.sh | 13 ++++++++++++- monitor/stuckTransfers.js | 7 ++----- monitor/utils/events.js | 16 ++-------------- monitor/utils/file.js | 12 +++++++++++- monitor/utils/message.js | 18 +++++++++++++++++- monitor/utils/web3.js | 15 +++++++++++++++ monitor/utils/web3Cache.js | 29 +++++++++++++++++++++++++++++ monitor/validators.js | 13 ++----------- oracle/src/utils/utils.js | 5 ++++- yarn.lock | 5 +++++ 24 files changed, 195 insertions(+), 85 deletions(-) create mode 100644 monitor/cache/.gitkeep create mode 100644 monitor/utils/web3.js create mode 100644 monitor/utils/web3Cache.js diff --git a/.dockerignore b/.dockerignore index b4795279..f31efb0c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,11 +8,16 @@ **/docs **/*.md +monitor/**/*.env* +oracle/**/*.env* +!**/.env.example + contracts/test contracts/build oracle/test monitor/test monitor/responses +monitor/cache commons/test oracle/**/*.png oracle/**/*.jpg diff --git a/.gitignore b/.gitignore index 94b2a7df..7cea0539 100644 --- a/.gitignore +++ b/.gitignore @@ -10,11 +10,8 @@ dist # misc .DS_Store -.env -.env.local -.env.development.local -.env.test.local -.env.production.local +*.env* +!.env.example .idea .nyc_output logs/ @@ -49,5 +46,6 @@ __pycache__ #monitor monitor/responses/* -monitor/configs/*.env +monitor/cache/* +!monitor/cache/.gitkeep !monitor/.gitkeep diff --git a/CONFIGURATION.md b/CONFIGURATION.md index adb3c435..b93db0d1 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -79,3 +79,6 @@ MONITOR_TX_NUMBER_THRESHOLD | If estimated number of transaction is equal to or MONITOR_PORT | The port for the Monitor. | integer MONITOR_BRIDGE_NAME | The name to be used in the url path for the bridge | string MONITOR_CACHE_EVENTS | If set to true, monitor will cache obtained events for other workers runs | `true` / `false` +MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST | File with a list of addresses, separated by newlines. If set, determines the privileged set of accounts whose requests should be automatically processed by the CollectedSignatures watcher. | string +MONITOR_HOME_TO_FOREIGN_BLOCK_LIST | File with a list of addresses, separated by newlines. If set, determines the set of accounts whose requests should be marked as unclaimed. Has a lower priority than the `MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST`. | string +MONITOR_HOME_TO_FOREIGN_CHECK_SENDER | If set to `true`, instructs the oracle to do an extra check for transaction origin in the block/allowance list. `false` by default. | `true` / `false` diff --git a/monitor/.env.example b/monitor/.env.example index 66d96f77..31647b03 100644 --- a/monitor/.env.example +++ b/monitor/.env.example @@ -22,3 +22,6 @@ COMMON_FOREIGN_GAS_PRICE_FACTOR=1 MONITOR_TX_NUMBER_THRESHOLD=100 MONITOR_PORT=3003 MONITOR_CACHE_EVENTS=true + +MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST= +MONITOR_HOME_TO_FOREIGN_BLOCK_LIST= diff --git a/monitor/alerts.js b/monitor/alerts.js index b5215dfa..4d118d0f 100644 --- a/monitor/alerts.js +++ b/monitor/alerts.js @@ -1,18 +1,11 @@ require('dotenv').config() -const Web3 = require('web3') +const Web3Utils = require('web3').utils const logger = require('./logger')('alerts') const eventsInfo = require('./utils/events') const { getBlockNumber } = require('./utils/contract') const { processedMsgNotDelivered, eventWithoutReference } = require('./utils/message') const { BRIDGE_MODES } = require('../commons') - -const { COMMON_HOME_RPC_URL, COMMON_FOREIGN_RPC_URL } = process.env - -const homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL) -const web3Home = new Web3(homeProvider) - -const foreignProvider = new Web3.providers.HttpProvider(COMMON_FOREIGN_RPC_URL) -const web3Foreign = new Web3(foreignProvider) +const { web3Home, web3Foreign } = require('./utils/web3') async function main() { const { @@ -77,10 +70,10 @@ async function main() { * @returns {function({blockNumber?: *}): boolean[]} */ const findMisbehaviorRange = currentBlockNumber => ({ blockNumber }) => { - const minus60 = currentBlockNumber.sub(Web3.utils.toBN(60)) - const minus180 = currentBlockNumber.sub(Web3.utils.toBN(180)) - const minus720 = currentBlockNumber.sub(Web3.utils.toBN(720)) - const minus17280 = currentBlockNumber.sub(Web3.utils.toBN(17280)) + const minus60 = currentBlockNumber.sub(Web3Utils.toBN(60)) + const minus180 = currentBlockNumber.sub(Web3Utils.toBN(180)) + const minus720 = currentBlockNumber.sub(Web3Utils.toBN(720)) + const minus17280 = currentBlockNumber.sub(Web3Utils.toBN(17280)) return [ minus60.lte(blockNumber), diff --git a/monitor/cache/.gitkeep b/monitor/cache/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/monitor/checkWorker.js b/monitor/checkWorker.js index 649accd8..2cc1eaa3 100644 --- a/monitor/checkWorker.js +++ b/monitor/checkWorker.js @@ -1,21 +1,20 @@ require('dotenv').config() -const Web3 = require('web3') +const BN = require('bignumber.js') const logger = require('./logger')('checkWorker') const { getBridgeMode } = require('../commons') const getBalances = require('./getBalances') const getShortEventStats = require('./getShortEventStats') const validators = require('./validators') const { writeFile, createDir } = require('./utils/file') +const { saveCache } = require('./utils/web3Cache') +const { web3Home } = require('./utils/web3') -const { COMMON_HOME_BRIDGE_ADDRESS, COMMON_HOME_RPC_URL, MONITOR_BRIDGE_NAME } = process.env +const { COMMON_HOME_BRIDGE_ADDRESS, MONITOR_BRIDGE_NAME } = process.env 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 homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL) -const web3Home = new Web3(homeProvider) - const { HOME_ERC_TO_ERC_ABI } = require('../commons') async function checkWorker() { @@ -31,9 +30,13 @@ async function checkWorker() { 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.balanceDiff && status.unclaimedBalance) { + status.balanceDiff = new BN(status.balanceDiff).minus(status.unclaimedBalance).toFixed() + } if (!status) throw new Error('status is empty: ' + JSON.stringify(status)) status.health = true writeFile(`/responses/${MONITOR_BRIDGE_NAME}/getBalances.json`, status) + saveCache() logger.debug('calling validators()') const vBalances = await validators(bridgeMode) diff --git a/monitor/checkWorker2.js b/monitor/checkWorker2.js index 0cb09aa4..06778c55 100644 --- a/monitor/checkWorker2.js +++ b/monitor/checkWorker2.js @@ -3,6 +3,7 @@ const logger = require('./logger')('checkWorker2') const eventsStats = require('./eventsStats') const alerts = require('./alerts') const { writeFile, createDir } = require('./utils/file') +const { saveCache } = require('./utils/web3Cache') const { MONITOR_BRIDGE_NAME } = process.env @@ -26,6 +27,7 @@ async function checkWorker2() { _alerts.ok = !_alerts.executeAffirmations.mostRecentTxHash && !_alerts.executeSignatures.mostRecentTxHash _alerts.health = true writeFile(`/responses/${MONITOR_BRIDGE_NAME}/alerts.json`, _alerts) + saveCache() logger.debug('Done x2') } catch (e) { logger.error(e) diff --git a/monitor/checkWorker3.js b/monitor/checkWorker3.js index 565a20e8..fdab50cb 100644 --- a/monitor/checkWorker3.js +++ b/monitor/checkWorker3.js @@ -1,15 +1,12 @@ require('dotenv').config() -const Web3 = require('web3') const logger = require('./logger')('checkWorker3') const stuckTransfers = require('./stuckTransfers') const { writeFile, createDir } = require('./utils/file') +const { web3Home } = require('./utils/web3') -const { MONITOR_BRIDGE_NAME, COMMON_HOME_BRIDGE_ADDRESS, COMMON_HOME_RPC_URL } = process.env +const { MONITOR_BRIDGE_NAME, COMMON_HOME_BRIDGE_ADDRESS } = process.env const { getBridgeMode, HOME_NATIVE_TO_ERC_ABI, BRIDGE_MODES } = require('../commons') -const homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL) -const web3Home = new Web3(homeProvider) - async function checkWorker3() { try { const homeBridge = new web3Home.eth.Contract(HOME_NATIVE_TO_ERC_ABI, COMMON_HOME_BRIDGE_ADDRESS) diff --git a/monitor/docker-compose.yml b/monitor/docker-compose.yml index 8d0661e9..9164d9ef 100644 --- a/monitor/docker-compose.yml +++ b/monitor/docker-compose.yml @@ -4,11 +4,12 @@ services: monitor: image: poanetwork/tokenbridge-monitor:latest ports: - - "${MONITOR_PORT}:${MONITOR_PORT}" + - "${MONITOR_PORT}:${MONITOR_PORT}" env_file: ./.env - environment: + environment: - NODE_ENV=production volumes: - ./responses:/mono/monitor/responses + - ./cache:/mono/monitor/cache restart: unless-stopped entrypoint: "yarn start" diff --git a/monitor/eventsStats.js b/monitor/eventsStats.js index c0e49f30..2d5ac542 100644 --- a/monitor/eventsStats.js +++ b/monitor/eventsStats.js @@ -1,8 +1,20 @@ require('dotenv').config() const eventsInfo = require('./utils/events') -const { processedMsgNotDelivered, deliveredMsgNotProcessed, eventWithoutReference } = require('./utils/message') +const { + processedMsgNotDelivered, + deliveredMsgNotProcessed, + eventWithoutReference, + unclaimedHomeToForeignRequests +} = require('./utils/message') +const { getHomeTxSender } = require('./utils/web3Cache') const { BRIDGE_MODES } = require('../commons') +const { + MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST, + MONITOR_HOME_TO_FOREIGN_BLOCK_LIST, + MONITOR_HOME_TO_FOREIGN_CHECK_SENDER +} = process.env + async function main() { const { homeToForeignRequests, @@ -33,17 +45,30 @@ async function main() { lastChecked: Math.floor(Date.now() / 1000) } } else { - const onlyInHomeDeposits = homeToForeignRequests.filter(eventWithoutReference(homeToForeignConfirmations)) + let onlyInHomeDeposits = homeToForeignRequests.filter(eventWithoutReference(homeToForeignConfirmations)) const onlyInForeignDeposits = homeToForeignConfirmations.filter(eventWithoutReference(homeToForeignRequests)) const onlyInHomeWithdrawals = foreignToHomeConfirmations.filter(eventWithoutReference(foreignToHomeRequests)) const onlyInForeignWithdrawals = foreignToHomeRequests.filter(eventWithoutReference(foreignToHomeConfirmations)) + const unclaimedStats = {} + if (MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST || MONITOR_HOME_TO_FOREIGN_BLOCK_LIST) { + const unclaimedFilter = unclaimedHomeToForeignRequests() + if (MONITOR_HOME_TO_FOREIGN_CHECK_SENDER === 'true') { + for (let i = 0; i < onlyInHomeDeposits.length; i++) { + onlyInHomeDeposits[i].sender = await getHomeTxSender(onlyInHomeDeposits[i].transactionHash) + } + } + unclaimedStats.unclaimedHomeDeposits = onlyInHomeDeposits.filter(unclaimedFilter) + onlyInHomeDeposits = onlyInHomeDeposits.filter(e => !unclaimedFilter(e)) + } + return { onlyInHomeDeposits, onlyInForeignDeposits, onlyInHomeWithdrawals, onlyInForeignWithdrawals, + ...unclaimedStats, lastChecked: Math.floor(Date.now() / 1000) } } diff --git a/monitor/getBalances.js b/monitor/getBalances.js index d60a7b90..180587df 100644 --- a/monitor/getBalances.js +++ b/monitor/getBalances.js @@ -1,23 +1,11 @@ require('dotenv').config() const BN = require('bignumber.js') -const Web3 = require('web3') +const Web3Utils = require('web3').utils const logger = require('./logger')('getBalances') const { BRIDGE_MODES } = require('../commons') +const { web3Home, web3Foreign } = require('./utils/web3') -const Web3Utils = Web3.utils - -const { - COMMON_HOME_RPC_URL, - COMMON_FOREIGN_RPC_URL, - COMMON_HOME_BRIDGE_ADDRESS, - COMMON_FOREIGN_BRIDGE_ADDRESS -} = process.env - -const homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL) -const web3Home = new Web3(homeProvider) - -const foreignProvider = new Web3.providers.HttpProvider(COMMON_FOREIGN_RPC_URL) -const web3Foreign = new Web3(foreignProvider) +const { COMMON_HOME_BRIDGE_ADDRESS, COMMON_FOREIGN_BRIDGE_ADDRESS } = process.env const { ERC20_ABI, diff --git a/monitor/getShortEventStats.js b/monitor/getShortEventStats.js index f314abd0..edaee46d 100644 --- a/monitor/getShortEventStats.js +++ b/monitor/getShortEventStats.js @@ -1,6 +1,16 @@ require('dotenv').config() +const BN = require('bignumber.js') +const Web3Utils = require('web3').utils const eventsInfo = require('./utils/events') +const { eventWithoutReference, unclaimedHomeToForeignRequests } = require('./utils/message') const { BRIDGE_MODES } = require('../commons') +const { getHomeTxSender } = require('./utils/web3Cache') + +const { + MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST, + MONITOR_HOME_TO_FOREIGN_BLOCK_LIST, + MONITOR_HOME_TO_FOREIGN_CHECK_SENDER +} = process.env async function main(bridgeMode) { const { @@ -24,9 +34,26 @@ async function main(bridgeMode) { } } } else { - return { + const stats = { depositsDiff: homeToForeignRequests.length - homeToForeignConfirmations.length, - withdrawalDiff: foreignToHomeConfirmations.length - foreignToHomeRequests.length, + withdrawalDiff: foreignToHomeConfirmations.length - foreignToHomeRequests.length + } + if (MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST || MONITOR_HOME_TO_FOREIGN_BLOCK_LIST) { + const onlyInHomeDeposits = homeToForeignRequests.filter(eventWithoutReference(homeToForeignConfirmations)) + if (MONITOR_HOME_TO_FOREIGN_CHECK_SENDER === 'true') { + for (let i = 0; i < onlyInHomeDeposits.length; i++) { + onlyInHomeDeposits[i].sender = await getHomeTxSender(onlyInHomeDeposits[i].transactionHash) + } + } + + const unclaimedPool = onlyInHomeDeposits.filter(unclaimedHomeToForeignRequests()) + + stats.depositsDiff -= unclaimedPool.length + stats.unclaimedDiff = unclaimedPool.length + stats.unclaimedBalance = Web3Utils.fromWei(BN.sum(...unclaimedPool.map(e => e.value)).toFixed()) + } + return { + ...stats, home: { deposits: homeToForeignRequests.length, withdrawals: foreignToHomeConfirmations.length diff --git a/monitor/package.json b/monitor/package.json index c91d7297..ea9ff358 100644 --- a/monitor/package.json +++ b/monitor/package.json @@ -14,7 +14,7 @@ "author": "", "license": "ISC", "dependencies": { - "bignumber.js": "^6.0.0", + "bignumber.js": "^9.0.1", "cors": "^2.8.5", "dotenv": "^5.0.1", "express": "^4.16.3", diff --git a/monitor/scripts/getBridgeStats.sh b/monitor/scripts/getBridgeStats.sh index ccf80dbe..f8df6624 100755 --- a/monitor/scripts/getBridgeStats.sh +++ b/monitor/scripts/getBridgeStats.sh @@ -2,6 +2,10 @@ CONFIGDIR="configs" RESPONSESDIR="responses" +ACLDIR="access-lists" +ALLOWANCEFILE="allowance_list.txt" +BLOCKFILE="block_list.txt" +CACHEDIR="cache" IMAGETAG="latest" cd $(dirname $0)/.. @@ -26,10 +30,17 @@ if /usr/local/bin/docker-compose ps | grep -q -i 'monitor'; then fi done + alist=`source ${file} && echo ${MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST}` + blist=`source ${file} && echo ${MONITOR_HOME_TO_FOREIGN_BLOCK_LIST}` + al_param="$(pwd)/${ACLDIR}/${bridgename}/${ALLOWANCEFILE}:/mono/monitor/access-lists/allowance_list.txt" + bl_param="$(pwd)/${ACLDIR}/${bridgename}/${BLOCKFILE}:/mono/monitor/access-lists/block_list.txt" + containername=${bridgename}"-checker" docker container stats --no-stream ${containername} 2>/dev/null 1>&2 if [ ! "$?" == "0" ]; then docker run --rm --env-file $file -v $(pwd)/${RESPONSESDIR}:/mono/monitor/responses \ + ${alist:+"-v"} ${alist:+"$al_param"} ${blist:+"-v"} ${blist:+"$bl_param"} \ + -v $(pwd)/${CACHEDIR}/${bridgename}:/mono/monitor/cache \ --name ${containername} poanetwork/tokenbridge-monitor:${IMAGETAG} \ /bin/bash -c 'yarn check-all' shasum -a 256 -s -c ${checksumfile} @@ -57,4 +68,4 @@ if /usr/local/bin/docker-compose ps | grep -q -i 'monitor'; then else echo "Monitor is not running, skipping checks." -fi \ No newline at end of file +fi diff --git a/monitor/stuckTransfers.js b/monitor/stuckTransfers.js index 3fdbe32a..c69b27c5 100644 --- a/monitor/stuckTransfers.js +++ b/monitor/stuckTransfers.js @@ -1,14 +1,11 @@ require('dotenv').config() -const Web3 = require('web3') const logger = require('./logger')('stuckTransfers.js') const { FOREIGN_V1_ABI } = require('../commons/abis') +const { web3Foreign } = require('./utils/web3') -const { COMMON_FOREIGN_RPC_URL, COMMON_FOREIGN_BRIDGE_ADDRESS } = process.env +const { COMMON_FOREIGN_BRIDGE_ADDRESS } = process.env const MONITOR_FOREIGN_START_BLOCK = Number(process.env.MONITOR_FOREIGN_START_BLOCK) || 0 -const foreignProvider = new Web3.providers.HttpProvider(COMMON_FOREIGN_RPC_URL) -const web3Foreign = new Web3(foreignProvider) - const ABITransferWithoutData = [ { anonymous: false, diff --git a/monitor/utils/events.js b/monitor/utils/events.js index 054cca15..a037bd2c 100644 --- a/monitor/utils/events.js +++ b/monitor/utils/events.js @@ -1,5 +1,4 @@ require('dotenv').config() -const Web3 = require('web3') const { toBN } = require('web3').utils const logger = require('../logger')('eventsUtils') const { @@ -19,23 +18,12 @@ const { const { normalizeEventInformation } = require('./message') const { filterTransferBeforeES } = require('./tokenUtils') const { writeFile, readCacheFile } = require('./file') +const { web3Home, web3Foreign } = require('./web3') -const { - COMMON_HOME_RPC_URL, - COMMON_FOREIGN_RPC_URL, - COMMON_HOME_BRIDGE_ADDRESS, - COMMON_FOREIGN_BRIDGE_ADDRESS, - MONITOR_CACHE_EVENTS -} = process.env +const { COMMON_HOME_BRIDGE_ADDRESS, COMMON_FOREIGN_BRIDGE_ADDRESS, MONITOR_CACHE_EVENTS } = process.env const MONITOR_HOME_START_BLOCK = toBN(Number(process.env.MONITOR_HOME_START_BLOCK) || 0) const MONITOR_FOREIGN_START_BLOCK = toBN(Number(process.env.MONITOR_FOREIGN_START_BLOCK) || 0) -const homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL) -const web3Home = new Web3(homeProvider) - -const foreignProvider = new Web3.providers.HttpProvider(COMMON_FOREIGN_RPC_URL) -const web3Foreign = new Web3(foreignProvider) - const { getBlockNumber } = require('./contract') const cacheFilePath = '/tmp/cachedEvents.json' diff --git a/monitor/utils/file.js b/monitor/utils/file.js index 2484e0a1..ddf05ea4 100644 --- a/monitor/utils/file.js +++ b/monitor/utils/file.js @@ -38,9 +38,19 @@ function readCacheFile(filePath) { } } +function readAccessListFile(filePath) { + const data = fs.readFileSync(filePath) + return data + .toString() + .split('\n') + .map(addr => addr.trim().toLowerCase()) + .filter(addr => addr.length === 42) +} + module.exports = { readFile, writeFile, createDir, - readCacheFile + readCacheFile, + readAccessListFile } diff --git a/monitor/utils/message.js b/monitor/utils/message.js index 03c2fac1..46282b1f 100644 --- a/monitor/utils/message.js +++ b/monitor/utils/message.js @@ -1,4 +1,7 @@ const { parseAMBMessage } = require('../../commons') +const { readAccessListFile } = require('./file') + +const { MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST, MONITOR_HOME_TO_FOREIGN_BLOCK_LIST } = process.env const keyAMB = e => [e.messageId, e.sender, e.executor].join(',').toLowerCase() @@ -51,9 +54,22 @@ const eventWithoutReference = otherSideEvents => { return e => !keys.has(key(e)) } +const unclaimedHomeToForeignRequests = () => { + if (MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST) { + const allowanceList = readAccessListFile(MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST) + return e => !allowanceList.includes(e.recipient.toLowerCase()) && !(e.sender && allowanceList.includes(e.sender)) + } else if (MONITOR_HOME_TO_FOREIGN_BLOCK_LIST) { + const blockList = readAccessListFile(MONITOR_HOME_TO_FOREIGN_BLOCK_LIST) + return e => blockList.includes(e.recipient.toLowerCase()) || (e.sender && blockList.includes(e.sender)) + } else { + return () => false + } +} + module.exports = { deliveredMsgNotProcessed, processedMsgNotDelivered, normalizeEventInformation, - eventWithoutReference + eventWithoutReference, + unclaimedHomeToForeignRequests } diff --git a/monitor/utils/web3.js b/monitor/utils/web3.js new file mode 100644 index 00000000..863da23d --- /dev/null +++ b/monitor/utils/web3.js @@ -0,0 +1,15 @@ +require('dotenv').config() +const Web3 = require('web3') + +const { COMMON_HOME_RPC_URL, COMMON_FOREIGN_RPC_URL } = process.env + +const homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL) +const web3Home = new Web3(homeProvider) + +const foreignProvider = new Web3.providers.HttpProvider(COMMON_FOREIGN_RPC_URL) +const web3Foreign = new Web3(foreignProvider) + +module.exports = { + web3Home, + web3Foreign +} diff --git a/monitor/utils/web3Cache.js b/monitor/utils/web3Cache.js new file mode 100644 index 00000000..3dc13277 --- /dev/null +++ b/monitor/utils/web3Cache.js @@ -0,0 +1,29 @@ +const logger = require('../logger')('web3Cache') +const { readCacheFile, writeFile } = require('./file') +const { web3Home } = require('./web3') + +let isDirty = false + +const homeTxSendersCacheFile = './cache/homeTxSenders.json' +const cachedHomeTxSenders = readCacheFile(homeTxSendersCacheFile) || {} + +async function getHomeTxSender(txHash) { + if (!cachedHomeTxSenders[txHash]) { + logger.debug(`Fetching sender for tx ${txHash}`) + cachedHomeTxSenders[txHash] = (await web3Home.eth.getTransaction(txHash)).from.toLowerCase() + isDirty = true + } + return cachedHomeTxSenders[txHash] +} + +function saveCache() { + if (isDirty) { + logger.debug('Saving cache on disk') + writeFile(homeTxSendersCacheFile, cachedHomeTxSenders) + } +} + +module.exports = { + getHomeTxSender, + saveCache +} diff --git a/monitor/validators.js b/monitor/validators.js index e8f71dfc..c6b4ea7b 100644 --- a/monitor/validators.js +++ b/monitor/validators.js @@ -1,13 +1,12 @@ require('dotenv').config() -const Web3 = require('web3') +const Web3Utils = require('web3').utils const fetch = require('node-fetch') const logger = require('./logger')('validators') const { getBridgeABIs, BRIDGE_VALIDATORS_ABI, getValidatorList, gasPriceFromSupplier } = require('../commons') const { getBlockNumber } = require('./utils/contract') +const { web3Home, web3Foreign } = require('./utils/web3') const { - COMMON_HOME_RPC_URL, - COMMON_FOREIGN_RPC_URL, COMMON_HOME_BRIDGE_ADDRESS, COMMON_FOREIGN_BRIDGE_ADDRESS, COMMON_HOME_GAS_PRICE_SUPPLIER_URL, @@ -24,14 +23,6 @@ const MONITOR_FOREIGN_START_BLOCK = Number(process.env.MONITOR_FOREIGN_START_BLO 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 homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL) -const web3Home = new Web3(homeProvider) - -const foreignProvider = new Web3.providers.HttpProvider(COMMON_FOREIGN_RPC_URL) -const web3Foreign = new Web3(foreignProvider) - const homeGasPriceSupplierOpts = { speedType: COMMON_HOME_GAS_PRICE_SPEED_TYPE, factor: COMMON_HOME_GAS_PRICE_FACTOR, diff --git a/oracle/src/utils/utils.js b/oracle/src/utils/utils.js index 7ce89b2a..a7052d46 100644 --- a/oracle/src/utils/utils.js +++ b/oracle/src/utils/utils.js @@ -118,7 +118,10 @@ async function readAccessListFile(fileName, logger) { .split('\n') .map(addr => addr.trim().toLowerCase()) .filter(addr => addr.length === 42) - logger.info({ fileName }, `Access list was read successfully, ${data.length} addresses found`) + logger.info( + { fileName }, + `Access list was read successfully, ${readAccessLists[fileName].length} addresses found` + ) logger.debug({ addresses: readAccessLists[fileName] }, `Read addresses from the file`) } catch (e) { readAccessLists[fileName] = [] diff --git a/yarn.lock b/yarn.lock index fc046235..cf385a04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5073,6 +5073,11 @@ bignumber.js@^6.0.0: resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-6.0.0.tgz#bbfa047644609a5af093e9cbd83b0461fa3f6002" integrity sha512-x247jIuy60/+FtMRvscqfxtVHQf8AGx2hm9c6btkgC0x/hp9yt+teISNhvF8WlwRkCc5yF2fDECH8SIMe8j+GA== +bignumber.js@^9.0.1: + version "9.0.1" + resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.0.1.tgz#8d7ba124c882bfd8e43260c67475518d0689e4e5" + integrity sha512-IdZR9mh6ahOBv/hYGiXyVuyCetmGJhtYkqLBpTStdhEGjegpPlUawydyaF3pbIOFynJTpllEs+NP+CS9jKFLjA== + bignumber.js@~8.0.2: version "8.0.2" resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-8.0.2.tgz#d8c4e1874359573b1ef03011a2d861214aeef137"