Support alternative receiver feature in Monitor (#223)

* Update monitor to support changes from alternative receiver
* Add monitor event processing unit tests
* update chrome version used en e2e tests
* update chromedriver version
This commit is contained in:
Gerardo Nardelli 2019-10-29 16:22:02 -03:00 committed by Alexander Kolotov
parent 7a54e584d5
commit 1564ccc580
8 changed files with 284 additions and 130 deletions

@ -8,5 +8,8 @@
"rules": {
"no-use-before-define": "off",
"node/no-unpublished-require": "off"
},
"env": {
"mocha": true
}
}

@ -3,7 +3,7 @@ const Web3 = require('web3')
const logger = require('./logger')('alerts')
const eventsInfo = require('./utils/events')
const { getBlockNumber } = require('./utils/contract')
const { processedMsgNotDelivered } = require('./utils/message')
const { processedMsgNotDelivered, eventWithoutReference } = require('./utils/message')
const { BRIDGE_MODES } = require('../commons')
const { COMMON_HOME_RPC_URL, COMMON_FOREIGN_RPC_URL } = process.env
@ -29,10 +29,9 @@ async function main() {
xSignatures = homeToForeignConfirmations.filter(processedMsgNotDelivered(homeToForeignRequests))
xAffirmations = foreignToHomeConfirmations.filter(processedMsgNotDelivered(foreignToHomeRequests))
} else {
xSignatures = homeToForeignConfirmations.filter(findDifferences(homeToForeignRequests))
xAffirmations = foreignToHomeConfirmations.filter(findDifferences(foreignToHomeRequests))
xSignatures = homeToForeignConfirmations.filter(eventWithoutReference(homeToForeignRequests))
xAffirmations = foreignToHomeConfirmations.filter(eventWithoutReference(foreignToHomeRequests))
}
logger.debug('building misbehavior blocks')
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)
@ -52,8 +51,8 @@ async function main() {
const foreignValidators = await Promise.all(xSignatures.map(event => findTxSender(web3Foreign)(event)))
const homeValidators = await Promise.all(xAffirmations.map(event => findTxSender(web3Home)(event)))
const xSignaturesTxs = xSignatures.map(normalizeEventInformation).reduce(buildTxList(foreignValidators), {})
const xAffirmationsTxs = xAffirmations.map(normalizeEventInformation).reduce(buildTxList(homeValidators), {})
const xSignaturesTxs = xSignatures.reduce(buildTxList(foreignValidators), {})
const xAffirmationsTxs = xAffirmations.reduce(buildTxList(homeValidators), {})
logger.debug('Done')
@ -150,7 +149,7 @@ const findTxSender = web3 => async ({ transactionHash }) => {
* }}}
*/
const buildTxList = validatorsList => (acc, event, index) => {
acc[event.txHash] = {
acc[event.transactionHash] = {
value: event.value,
block: event.blockNumber,
referenceTx: event.referenceTx,
@ -160,38 +159,4 @@ const buildTxList = validatorsList => (acc, event, index) => {
return acc
}
/**
* Finds a missing destDeposit in src list if there's any
* @param {Array} src
* @returns {function(*=): boolean}
*/
const findDifferences = src => dest => {
const b = normalizeEventInformation(dest)
return (
src
.map(normalizeEventInformation)
.filter(a => a.referenceTx === b.referenceTx && a.recipient === b.recipient && a.value === b.value).length === 0
)
}
/**
* Normalizes the different event objects to facilitate data processing
* @param {Object} event
* @returns {{
* txHash: string,
* blockNumber: number,
* referenceTx: string,
* recipient: string | *,
* value: *
* }}
*/
const normalizeEventInformation = event => ({
txHash: event.transactionHash,
blockNumber: event.blockNumber,
referenceTx: event.returnValues.transactionHash || event.transactionHash,
recipient: event.returnValues.recipient || event.returnValues.from,
value: event.returnValues.value
})
module.exports = main

@ -1,69 +1,14 @@
require('dotenv').config()
const eventsInfo = require('./utils/events')
const { processedMsgNotDelivered, deliveredMsgNotProcessed } = require('./utils/message')
const { processedMsgNotDelivered, deliveredMsgNotProcessed, eventWithoutReference } = require('./utils/message')
const { BRIDGE_MODES } = require('../commons')
function compareDepositsHome(foreign) {
return homeDeposit => {
return (
foreign.filter(foreignDeposit => {
return (
foreignDeposit.returnValues.transactionHash === homeDeposit.transactionHash &&
foreignDeposit.returnValues.recipient === homeDeposit.returnValues.recipient &&
foreignDeposit.returnValues.value === homeDeposit.returnValues.value
)
}).length === 0
)
}
}
function compareDepositsForeign(home) {
return foreignDeposit => {
return (
home.filter(homeDeposit => {
return (
homeDeposit.transactionHash === foreignDeposit.returnValues.transactionHash &&
homeDeposit.returnValues.recipient === foreignDeposit.returnValues.recipient &&
homeDeposit.returnValues.value === foreignDeposit.returnValues.value
)
}).length === 0
)
}
}
function compareTransferHome(foreign) {
return homeDeposit => {
return (
foreign.filter(foreignDeposit => {
return (
homeDeposit.returnValues.transactionHash === foreignDeposit.transactionHash &&
homeDeposit.returnValues.recipient === foreignDeposit.returnValues.from &&
homeDeposit.returnValues.value === foreignDeposit.returnValues.value
)
}).length === 0
)
}
}
function compareTransferForeign(home) {
return foreignDeposit => {
return (
home.filter(homeDeposit => {
return (
foreignDeposit.transactionHash === homeDeposit.returnValues.transactionHash &&
foreignDeposit.returnValues.from === homeDeposit.returnValues.recipient &&
foreignDeposit.returnValues.value === homeDeposit.returnValues.value
)
}).length === 0
)
}
}
async function main() {
const {
homeToForeignRequests,
homeToForeignConfirmations,
foreignToHomeConfirmations,
foreignToHomeRequests,
isExternalErc20,
bridgeMode
} = await eventsInfo()
@ -88,17 +33,11 @@ async function main() {
lastChecked: Math.floor(Date.now() / 1000)
}
} else {
const onlyInHomeDeposits = homeToForeignRequests.filter(compareDepositsHome(homeToForeignConfirmations))
const onlyInForeignDeposits = homeToForeignConfirmations
.concat([])
.filter(compareDepositsForeign(homeToForeignRequests))
const onlyInHomeDeposits = homeToForeignRequests.filter(eventWithoutReference(homeToForeignConfirmations))
const onlyInForeignDeposits = homeToForeignConfirmations.filter(eventWithoutReference(homeToForeignRequests))
const onlyInHomeWithdrawals = isExternalErc20
? foreignToHomeConfirmations.filter(compareTransferHome(foreignToHomeRequests))
: foreignToHomeConfirmations.filter(compareDepositsForeign(foreignToHomeRequests))
const onlyInForeignWithdrawals = isExternalErc20
? foreignToHomeRequests.filter(compareTransferForeign(foreignToHomeConfirmations))
: foreignToHomeRequests.filter(compareDepositsHome(foreignToHomeConfirmations))
const onlyInHomeWithdrawals = foreignToHomeConfirmations.filter(eventWithoutReference(foreignToHomeRequests))
const onlyInForeignWithdrawals = foreignToHomeRequests.filter(eventWithoutReference(foreignToHomeConfirmations))
return {
onlyInHomeDeposits,

@ -8,7 +8,8 @@
"start": "node index.js",
"check-and-start": "yarn check-all && yarn start",
"lint": "eslint . --ignore-path ../.eslintignore",
"lint:fix": "eslint . --fix"
"lint:fix": "eslint . --fix",
"test": "NODE_ENV=test mocha"
},
"author": "",
"license": "ISC",
@ -23,5 +24,6 @@
"node": ">=8.9"
},
"devDependencies": {
"chai": "^4.2.0"
}
}

@ -0,0 +1,206 @@
const { expect } = require('chai')
const { normalizeEventInformation, eventWithoutReference } = require('../utils/message')
describe('normalizeEventInformation', () => {
it('should return normalized object for UserRequestForSignature event', () => {
// Given
const event = {
address: '0x7301CFA0e1756B71869E93d4e4Dca5c7d0eb0AA6',
blockNumber: 324231,
transactionHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
returnValues: {
recipient: '0xA84944735B66e957Fe385567dcc85975022Fe68A',
value: '100000000000000000000'
},
event: 'UserRequestForSignature'
}
// When
const result = normalizeEventInformation(event)
// Then
expect(result.transactionHash).to.equal(event.transactionHash)
expect(result.blockNumber).to.equal(event.blockNumber)
expect(result.referenceTx).to.equal(event.transactionHash)
expect(result.recipient).to.equal(event.returnValues.recipient)
expect(result.value).to.equal(event.returnValues.value)
})
it('should return normalized object for UserRequestForAffirmation event', () => {
// Given
const event = {
address: '0x7301CFA0e1756B71869E93d4e4Dca5c7d0eb0AA6',
blockNumber: 324231,
transactionHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
returnValues: {
recipient: '0xA84944735B66e957Fe385567dcc85975022Fe68A',
value: '100000000000000000000'
},
event: 'UserRequestForAffirmation'
}
// When
const result = normalizeEventInformation(event)
// Then
expect(result.transactionHash).to.equal(event.transactionHash)
expect(result.blockNumber).to.equal(event.blockNumber)
expect(result.referenceTx).to.equal(event.transactionHash)
expect(result.recipient).to.equal(event.returnValues.recipient)
expect(result.value).to.equal(event.returnValues.value)
})
it('should return normalized object for transfer event', () => {
// Given
const event = {
address: '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359',
blockNumber: 6593953,
transactionHash: '0x05afb402e27946d3600b100020dc23419ffd10cb61d3b241cee7b4a84909b48a',
returnValues: {
from: '0x13C0a8009A578837fB7A80Aa252F6A3ba4aD6B79',
to: '0x4aa42145Aa6Ebf72e164C9bBC74fbD3788045016',
value: '4000000000000000000'
},
event: 'Transfer'
}
// When
const result = normalizeEventInformation(event)
// Then
expect(result.transactionHash).to.equal(event.transactionHash)
expect(result.blockNumber).to.equal(event.blockNumber)
expect(result.referenceTx).to.equal(event.transactionHash)
expect(result.recipient).to.equal(event.returnValues.from)
expect(result.value).to.equal(event.returnValues.value)
})
it('should return normalized object for RelayedMessage event', () => {
// Given
const event = {
address: '0x4aa42145Aa6Ebf72e164C9bBC74fbD3788045016',
blockNumber: 7025826,
transactionHash: '0x6ee5969973da763d6d9f162d2dd1b1ec34c2dd977dc39e6b25030b4f04471567',
returnValues: {
recipient: '0x38BC00Ea43EbB5ef5150593A0BA6C381803717e2',
value: '4900000000000000000',
transactionHash: '0x5c5c2ab5e333bda4acd035a6a30ea29c7370351891d85373b2d06c7cc6cbb210'
},
event: 'RelayedMessage'
}
// When
const result = normalizeEventInformation(event)
// Then
expect(result.transactionHash).to.equal(event.transactionHash)
expect(result.blockNumber).to.equal(event.blockNumber)
expect(result.referenceTx).to.equal(event.returnValues.transactionHash)
expect(result.recipient).to.equal(event.returnValues.recipient)
expect(result.value).to.equal(event.returnValues.value)
})
it('should return normalized object for AffirmationCompleted event', () => {
// Given
const event = {
address: '0x7301CFA0e1756B71869E93d4e4Dca5c7d0eb0AA6',
blockNumber: 474439,
transactionHash: '0x654004b372ba32754cef34f403153bbdf43f0fbb3191d5e4683bba7f32e0dc4a',
returnValues: {
recipient: '0x9b7b2B4f7a391b6F14A81221AE0920A9735B67Fb',
value: '5000000000000000000',
transactionHash: '0xe96da94bbda2cfc865acd3f98040f5c79a627ee9de839d86885d34acd8ecd10d'
},
event: 'AffirmationCompleted'
}
// When
const result = normalizeEventInformation(event)
// Then
expect(result.transactionHash).to.equal(event.transactionHash)
expect(result.blockNumber).to.equal(event.blockNumber)
expect(result.referenceTx).to.equal(event.returnValues.transactionHash)
expect(result.recipient).to.equal(event.returnValues.recipient)
expect(result.value).to.equal(event.returnValues.value)
})
})
describe('eventWithoutReference', () => {
it('should return false if event is present', () => {
// Given
const event = {
txHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
blockNumber: 474439,
referenceTx: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
recipient: '0x9b7b2B4f7a391b6F14A81221AE0920A9735B67Fb',
value: '5000000000000000000'
}
const otherSideEvents = [
{
txHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
blockNumber: 474439,
referenceTx: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
recipient: '0x9b7b2B4f7a391b6F14A81221AE0920A9735B67Fb',
value: '5000000000000000000'
},
{
txHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
blockNumber: 474439,
referenceTx: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
recipient: '0x38BC00Ea43EbB5ef5150593A0BA6C381803717e2',
value: '6000000000000000000'
},
{
txHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
blockNumber: 474439,
referenceTx: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
recipient: '0x38BC00Ea43EbB5ef5150593A0BA6C381803717e2',
value: '8000000000000000000'
}
]
// When
const result = eventWithoutReference(otherSideEvents)(event)
// Then
expect(result).to.equal(false)
})
it('should return true if event is not present', () => {
// Given
const event = {
txHash: '0xe96da94bbda2cfc865acd3f98040f5c79a627ee9de839d86885d34acd8ecd10d',
blockNumber: 474439,
referenceTx: '0xe96da94bbda2cfc865acd3f98040f5c79a627ee9de839d86885d34acd8ecd10d',
recipient: '0x9b7b2B4f7a391b6F14A81221AE0920A9735B67Fb',
value: '2000000000000000000'
}
const otherSideEvents = [
{
txHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
blockNumber: 474439,
referenceTx: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
recipient: '0x9b7b2B4f7a391b6F14A81221AE0920A9735B67Fb',
value: '5000000000000000000'
},
{
txHash: '0x05afb402e27946d3600b100020dc23419ffd10cb61d3b241cee7b4a84909b48a',
blockNumber: 474439,
referenceTx: '0x05afb402e27946d3600b100020dc23419ffd10cb61d3b241cee7b4a84909b48a',
recipient: '0x38BC00Ea43EbB5ef5150593A0BA6C381803717e2',
value: '6000000000000000000'
},
{
txHash: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
blockNumber: 474439,
referenceTx: '0x17be1e0745136b9e2857124542f8218812db8fe4458236d5ae045c1ceeb79978',
recipient: '0x38BC00Ea43EbB5ef5150593A0BA6C381803717e2',
value: '8000000000000000000'
}
]
// When
const result = eventWithoutReference(otherSideEvents)(event)
// Then
expect(result).to.equal(true)
})
})

@ -13,6 +13,7 @@ const {
getTokenType,
getPastEvents
} = require('../../commons')
const { normalizeEventInformation } = require('./message')
const {
COMMON_HOME_RPC_URL,
@ -40,6 +41,7 @@ async function main(mode) {
const v1Bridge = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC_V1
let isExternalErc20
let erc20Contract
let normalizeEvent = normalizeEventInformation
if (bridgeMode !== BRIDGE_MODES.ARBITRARY_MESSAGE) {
const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC || v1Bridge ? 'erc677token' : 'erc20token'
const erc20Address = await foreignBridge.methods[erc20MethodName]().call()
@ -49,47 +51,59 @@ async function main(mode) {
)
isExternalErc20 = tokenType === ERC_TYPES.ERC20
erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
} else {
normalizeEvent = e => e
}
logger.debug('getting last block numbers')
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)
logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')")
const homeToForeignRequests = await getPastEvents(homeBridge, {
const homeToForeignRequests = (await getPastEvents(homeBridge, {
event: v1Bridge ? 'Deposit' : 'UserRequestForSignature',
fromBlock: MONITOR_HOME_START_BLOCK,
toBlock: homeBlockNumber
})
})).map(normalizeEvent)
logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')")
const homeToForeignConfirmations = await getPastEvents(foreignBridge, {
const homeToForeignConfirmations = (await getPastEvents(foreignBridge, {
event: v1Bridge ? 'Deposit' : 'RelayedMessage',
fromBlock: MONITOR_FOREIGN_START_BLOCK,
toBlock: foreignBlockNumber
})
})).map(normalizeEvent)
logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')")
const foreignToHomeConfirmations = await getPastEvents(homeBridge, {
const foreignToHomeConfirmations = (await getPastEvents(homeBridge, {
event: v1Bridge ? 'Withdraw' : 'AffirmationCompleted',
fromBlock: MONITOR_HOME_START_BLOCK,
toBlock: homeBlockNumber
})
})).map(normalizeEvent)
logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')")
const foreignToHomeRequests = isExternalErc20
? await getPastEvents(erc20Contract, {
event: 'Transfer',
fromBlock: MONITOR_FOREIGN_START_BLOCK,
toBlock: foreignBlockNumber,
options: {
filter: { to: COMMON_FOREIGN_BRIDGE_ADDRESS }
}
})
: await getPastEvents(foreignBridge, {
event: v1Bridge ? 'Withdraw' : 'UserRequestForAffirmation',
fromBlock: MONITOR_FOREIGN_START_BLOCK,
toBlock: foreignBlockNumber
})
let foreignToHomeRequests = (await getPastEvents(foreignBridge, {
event: v1Bridge ? 'Withdraw' : 'UserRequestForAffirmation',
fromBlock: MONITOR_FOREIGN_START_BLOCK,
toBlock: foreignBlockNumber
})).map(normalizeEvent)
if (isExternalErc20) {
logger.debug("calling erc20Contract.getPastEvents('Transfer')")
const transferEvents = (await getPastEvents(erc20Contract, {
event: 'Transfer',
fromBlock: MONITOR_FOREIGN_START_BLOCK,
toBlock: foreignBlockNumber,
options: {
filter: { to: COMMON_FOREIGN_BRIDGE_ADDRESS }
}
})).map(normalizeEvent)
// Get transfer events that didn't have a UserRequestForAffirmation event in the same transaction
const directTransfers = transferEvents.filter(
e => foreignToHomeRequests.findIndex(t => t.referenceTx === e.referenceTx) === -1
)
foreignToHomeRequests = [...foreignToHomeRequests, ...directTransfers]
}
logger.debug('Done')
return {
homeToForeignRequests,

@ -41,7 +41,32 @@ function messageEqualsEvent(parsedMsg, event) {
)
}
/**
* Normalizes the different event objects to facilitate data processing
* @param {Object} event
* @returns {{
* transactionHash: string,
* blockNumber: number,
* referenceTx: string,
* recipient: string | *,
* value: *
* }}
*/
const normalizeEventInformation = event => ({
transactionHash: event.transactionHash,
blockNumber: event.blockNumber,
referenceTx: event.returnValues.transactionHash || event.transactionHash,
recipient: event.returnValues.recipient || event.returnValues.from,
value: event.returnValues.value
})
const eventWithoutReference = otherSideEvents => e =>
otherSideEvents.filter(a => a.referenceTx === e.referenceTx && a.recipient === e.recipient && a.value === e.value)
.length === 0
module.exports = {
deliveredMsgNotProcessed,
processedMsgNotDelivered
processedMsgNotDelivered,
normalizeEventInformation,
eventWithoutReference
}

@ -36,7 +36,7 @@
"initialize": "yarn clean && git submodule update --init && yarn install --unsafe-perm --frozen-lockfile && yarn install:deploy && yarn compile:contracts",
"build": "yarn workspace ui run build",
"lint": "yarn wsrun --exclude token-bridge-contracts lint",
"test": "yarn wsrun --exclude monitor --exclude oracle-e2e --exclude ui-e2e --exclude monitor-e2e test",
"test": "yarn wsrun --exclude oracle-e2e --exclude ui-e2e --exclude monitor-e2e test",
"oracle-e2e": "./oracle-e2e/run-tests.sh",
"ui-e2e": "./ui-e2e/run-tests.sh",
"clean": "rm -rf ./node_modules ./**/node_modules ./**/**/node_modules ./**/build",