Skip to content

Commit

Permalink
Add old events processing in AMB monitor (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rill-fedoseev authored Jun 10, 2020
1 parent 8e10a5d commit d512b98
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 8 deletions.
32 changes: 32 additions & 0 deletions commons/abis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions monitor/checkWorker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config()
const Web3 = require('web3')
const logger = require('./logger')('checkWorker')
const { getBridgeMode } = require('../commons')
Expand Down
1 change: 1 addition & 0 deletions monitor/checkWorker2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config()
const logger = require('./logger')('checkWorker2')
const eventsStats = require('./eventsStats')
const alerts = require('./alerts')
Expand Down
1 change: 1 addition & 0 deletions monitor/checkWorker3.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config()
const Web3 = require('web3')
const logger = require('./logger')('checkWorker3')
const stuckTransfers = require('./stuckTransfers')
Expand Down
49 changes: 44 additions & 5 deletions monitor/utils/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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, {
Expand All @@ -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, {
Expand Down
16 changes: 13 additions & 3 deletions monitor/utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)
Expand All @@ -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
)
}

Expand Down

0 comments on commit d512b98

Please sign in to comment.