From d512b9850f17972dcde372f58520f1db466972a1 Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Wed, 10 Jun 2020 16:22:01 +0300 Subject: [PATCH] Add old events processing in AMB monitor (#354) --- commons/abis.js | 32 ++++++++++++++++++++++++++ monitor/checkWorker.js | 1 + monitor/checkWorker2.js | 1 + monitor/checkWorker3.js | 1 + monitor/utils/events.js | 49 ++++++++++++++++++++++++++++++++++++---- monitor/utils/message.js | 16 ++++++++++--- 6 files changed, 92 insertions(+), 8 deletions(-) diff --git a/commons/abis.js b/commons/abis.js index 0d67902e..62289d2d 100644 --- a/commons/abis.js +++ b/commons/abis.js @@ -53,6 +53,36 @@ const ERC20_BYTES32_ABI = [ } ] +const OLD_AMB_USER_REQUEST_FOR_SIGNATURE_ABI = [ + { + anonymous: false, + inputs: [ + { + indexed: false, + name: 'encodedData', + type: 'bytes' + } + ], + name: 'UserRequestForSignature', + type: 'event' + } +] + +const OLD_AMB_USER_REQUEST_FOR_AFFIRMATION_ABI = [ + { + anonymous: false, + inputs: [ + { + indexed: false, + name: 'encodedData', + type: 'bytes' + } + ], + name: 'UserRequestForAffirmation', + type: 'event' + } +] + function getBridgeABIs(bridgeMode) { let HOME_ABI = null let FOREIGN_ABI = null @@ -103,6 +133,8 @@ module.exports = { ERC20_BYTES32_ABI, HOME_AMB_ABI, FOREIGN_AMB_ABI, + OLD_AMB_USER_REQUEST_FOR_AFFIRMATION_ABI, + OLD_AMB_USER_REQUEST_FOR_SIGNATURE_ABI, BOX_ABI, SAI_TOP, HOME_STAKE_ERC_TO_ERC_ABI, diff --git a/monitor/checkWorker.js b/monitor/checkWorker.js index c6ffdc3e..649accd8 100644 --- a/monitor/checkWorker.js +++ b/monitor/checkWorker.js @@ -1,3 +1,4 @@ +require('dotenv').config() const Web3 = require('web3') const logger = require('./logger')('checkWorker') const { getBridgeMode } = require('../commons') diff --git a/monitor/checkWorker2.js b/monitor/checkWorker2.js index 0d6811a1..0cb09aa4 100644 --- a/monitor/checkWorker2.js +++ b/monitor/checkWorker2.js @@ -1,3 +1,4 @@ +require('dotenv').config() const logger = require('./logger')('checkWorker2') const eventsStats = require('./eventsStats') const alerts = require('./alerts') diff --git a/monitor/checkWorker3.js b/monitor/checkWorker3.js index f61bb42e..565a20e8 100644 --- a/monitor/checkWorker3.js +++ b/monitor/checkWorker3.js @@ -1,3 +1,4 @@ +require('dotenv').config() const Web3 = require('web3') const logger = require('./logger')('checkWorker3') const stuckTransfers = require('./stuckTransfers') diff --git a/monitor/utils/events.js b/monitor/utils/events.js index 6d25f9a7..ff272bf6 100644 --- a/monitor/utils/events.js +++ b/monitor/utils/events.js @@ -12,7 +12,9 @@ const { ERC677_BRIDGE_TOKEN_ABI, getTokenType, getPastEvents, - ZERO_ADDRESS + ZERO_ADDRESS, + OLD_AMB_USER_REQUEST_FOR_SIGNATURE_ABI, + OLD_AMB_USER_REQUEST_FOR_AFFIRMATION_ABI } = require('../../commons') const { normalizeEventInformation } = require('./message') const { filterTransferBeforeES } = require('./tokenUtils') @@ -72,13 +74,48 @@ async function main(mode) { logger.debug('getting last block numbers') const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) + let homeToForeignRequests = [] + let foreignToHomeRequests = [] + let homeMigrationBlock = MONITOR_HOME_START_BLOCK + let foreignMigrationBlock = MONITOR_FOREIGN_START_BLOCK + + if (bridgeMode === BRIDGE_MODES.ARBITRARY_MESSAGE) { + const oldHomeBridge = new web3Home.eth.Contract(OLD_AMB_USER_REQUEST_FOR_SIGNATURE_ABI, COMMON_HOME_BRIDGE_ADDRESS) + const oldForeignBridge = new web3Foreign.eth.Contract( + OLD_AMB_USER_REQUEST_FOR_AFFIRMATION_ABI, + COMMON_FOREIGN_BRIDGE_ADDRESS + ) + + logger.debug("calling oldHomeBridge.getPastEvents('UserRequestForSignature(bytes)')") + homeToForeignRequests = (await getPastEvents(oldHomeBridge, { + event: 'UserRequestForSignature', + fromBlock: MONITOR_HOME_START_BLOCK, + toBlock: homeBlockNumber + })).map(normalizeEvent) + logger.debug(`found ${homeToForeignRequests.length} events`) + if (homeToForeignRequests.length > 0) { + homeMigrationBlock = toBN(Math.max(...homeToForeignRequests.map(x => x.blockNumber))) + } + + logger.debug("calling oldForeignBridge.getPastEvents('UserRequestForAffirmation(bytes)')") + foreignToHomeRequests = (await getPastEvents(oldForeignBridge, { + event: 'UserRequestForAffirmation', + fromBlock: MONITOR_FOREIGN_START_BLOCK, + toBlock: foreignBlockNumber + })).map(normalizeEvent) + logger.debug(`found ${foreignToHomeRequests.length} events`) + if (foreignToHomeRequests.length > 0) { + foreignMigrationBlock = toBN(Math.max(...foreignToHomeRequests.map(x => x.blockNumber))) + } + } logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')") - const homeToForeignRequests = (await getPastEvents(homeBridge, { + const homeToForeignRequestsNew = (await getPastEvents(homeBridge, { event: v1Bridge ? 'Deposit' : 'UserRequestForSignature', - fromBlock: MONITOR_HOME_START_BLOCK, + fromBlock: homeMigrationBlock, toBlock: homeBlockNumber })).map(normalizeEvent) + homeToForeignRequests = [...homeToForeignRequests, ...homeToForeignRequestsNew] logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')") const homeToForeignConfirmations = (await getPastEvents(foreignBridge, { @@ -95,11 +132,13 @@ async function main(mode) { })).map(normalizeEvent) logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')") - let foreignToHomeRequests = (await getPastEvents(foreignBridge, { + const foreignToHomeRequestsNew = (await getPastEvents(foreignBridge, { event: v1Bridge ? 'Withdraw' : 'UserRequestForAffirmation', - fromBlock: MONITOR_FOREIGN_START_BLOCK, + fromBlock: foreignMigrationBlock, toBlock: foreignBlockNumber })).map(normalizeEvent) + foreignToHomeRequests = [...foreignToHomeRequests, ...foreignToHomeRequestsNew] + if (isExternalErc20) { logger.debug("calling erc20Contract.getPastEvents('Transfer')") let transferEvents = (await getPastEvents(erc20Contract, { diff --git a/monitor/utils/message.js b/monitor/utils/message.js index 012d0e72..ea4a9941 100644 --- a/monitor/utils/message.js +++ b/monitor/utils/message.js @@ -3,7 +3,12 @@ const { parseAMBMessage } = require('../../commons') function deliveredMsgNotProcessed(processedList) { return deliveredMsg => { - const msg = parseAMBMessage(deliveredMsg.returnValues.encodedData) + let msgData = deliveredMsg.returnValues.encodedData + if (!deliveredMsg.returnValues.messageId) { + // append tx hash to an old message, where message id was not used + msgData = deliveredMsg.transactionHash + msgData.slice(2) + } + const msg = parseAMBMessage(msgData) return ( processedList.filter(processedMsg => { return messageEqualsEvent(msg, processedMsg.returnValues) @@ -16,7 +21,12 @@ function processedMsgNotDelivered(deliveredList) { return processedMsg => { return ( deliveredList.filter(deliveredMsg => { - const msg = parseAMBMessage(deliveredMsg.returnValues.encodedData) + let msgData = deliveredMsg.returnValues.encodedData + if (!deliveredMsg.returnValues.messageId) { + // append tx hash to an old message, where message id was not used + msgData = deliveredMsg.transactionHash + msgData.slice(2) + } + const msg = parseAMBMessage(msgData) return messageEqualsEvent(msg, processedMsg.returnValues) }).length === 0 ) @@ -27,7 +37,7 @@ function messageEqualsEvent(parsedMsg, event) { return ( web3Utils.toChecksumAddress(parsedMsg.sender) === event.sender && web3Utils.toChecksumAddress(parsedMsg.executor) === event.executor && - parsedMsg.messageId === event.messageId + parsedMsg.messageId === event.messageId // for an old messages, event.messageId is actually a transactionHash ) }