Skip to content

Commit

Permalink
Merge pull request #28 from poanetwork/develop
Browse files Browse the repository at this point in the history
Merge the develop branch to the master branch, preparation to v2.2.0
  • Loading branch information
akolotov authored May 8, 2019
2 parents d502716 + 66955b7 commit eaf2a65
Show file tree
Hide file tree
Showing 11 changed files with 1,483 additions and 44 deletions.
597 changes: 597 additions & 0 deletions abis/ERC677BridgeToken.abi.json

Large diffs are not rendered by default.

699 changes: 699 additions & 0 deletions abis/ForeignBridgeErc677ToErc677.abi.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('dotenv').config()
const Web3 = require('web3')
const logger = require('./logger')('alerts')
const eventsInfo = require('./utils/events')
const { getBlockNumber } = require('./utils/contract')

const { HOME_RPC_URL, FOREIGN_RPC_URL } = process.env

Expand All @@ -24,10 +25,7 @@ async function main() {
const xAffirmations = homeWithdrawals.filter(findDifferences(foreignWithdrawals))

logger.debug('building misbehavior blocks')
const getBlockNumber = web3 => web3.eth.getBlockNumber()
const [foreignBlockNumber, homeBlockNumber] = (await Promise.all(
[web3Foreign, web3Home].map(getBlockNumber)
)).map(Web3.utils.toBN)
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)

const baseRange = [false, false, false, false, false]
const xSignaturesMisbehavior = buildRangesObject(
Expand Down
6 changes: 3 additions & 3 deletions eventsStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ async function main() {
homeDeposits,
homeWithdrawals,
foreignWithdrawals,
hasForeignErc
isExternalErc20
} = await eventsInfo()

const onlyInHomeDeposits = homeDeposits.filter(compareDepositsHome(foreignDeposits))
const onlyInForeignDeposits = foreignDeposits
.concat([])
.filter(compareDepositsForeign(homeDeposits))

const onlyInHomeWithdrawals = hasForeignErc
const onlyInHomeWithdrawals = isExternalErc20
? homeWithdrawals.filter(compareTransferHome(foreignWithdrawals))
: homeWithdrawals.filter(compareDepositsForeign(foreignWithdrawals))
const onlyInForeignWithdrawals = hasForeignErc
const onlyInForeignWithdrawals = isExternalErc20
? foreignWithdrawals.filter(compareTransferForeign(homeWithdrawals))
: foreignWithdrawals.filter(compareDepositsHome(homeWithdrawals))

Expand Down
59 changes: 45 additions & 14 deletions getShortEventStats.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require('dotenv').config()
const Web3 = require('web3')
const { toBN } = require('web3').utils
const logger = require('./logger')('getShortEventStats.js')
const { getBridgeABIs, BRIDGE_MODES } = require('./utils/bridgeMode')
const { getBridgeABIs, BRIDGE_MODES, ERC_TYPES } = require('./utils/bridgeMode')

const { HOME_RPC_URL, FOREIGN_RPC_URL, HOME_BRIDGE_ADDRESS, FOREIGN_BRIDGE_ADDRESS } = process.env
const HOME_DEPLOYMENT_BLOCK = Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0
const FOREIGN_DEPLOYMENT_BLOCK = Number(process.env.FOREIGN_DEPLOYMENT_BLOCK) || 0
const HOME_DEPLOYMENT_BLOCK = toBN(Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0)
const FOREIGN_DEPLOYMENT_BLOCK = toBN(Number(process.env.FOREIGN_DEPLOYMENT_BLOCK) || 0)

const homeProvider = new Web3.providers.HttpProvider(HOME_RPC_URL)
const web3Home = new Web3(homeProvider)
Expand All @@ -14,6 +15,8 @@ const foreignProvider = new Web3.providers.HttpProvider(FOREIGN_RPC_URL)
const web3Foreign = new Web3(foreignProvider)

const ERC20_ABI = require('./abis/ERC20.abi')
const { getTokenType } = require('./utils/ercUtils')
const { getPastEvents, getBlockNumber } = require('./utils/contract')

async function main(bridgeMode) {
try {
Expand All @@ -23,27 +26,55 @@ async function main(bridgeMode) {
const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC ? 'erc677token' : 'erc20token'
const erc20Address = await foreignBridge.methods[erc20MethodName]().call()
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
const tokenType = await getTokenType(foreignBridge, FOREIGN_BRIDGE_ADDRESS)

logger.debug('getting last block numbers')
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)
logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')")
const homeDeposits = await homeBridge.getPastEvents('UserRequestForSignature', {
fromBlock: HOME_DEPLOYMENT_BLOCK
const homeDeposits = await getPastEvents({
contract: homeBridge,
event: 'UserRequestForSignature',
fromBlock: HOME_DEPLOYMENT_BLOCK,
toBlock: homeBlockNumber,
options: {}
})

logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')")
const foreignDeposits = await foreignBridge.getPastEvents('RelayedMessage', {
fromBlock: FOREIGN_DEPLOYMENT_BLOCK
const foreignDeposits = await getPastEvents({
contract: foreignBridge,
event: 'RelayedMessage',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
toBlock: foreignBlockNumber,
options: {}
})

logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')")
const homeWithdrawals = await homeBridge.getPastEvents('AffirmationCompleted', {
fromBlock: HOME_DEPLOYMENT_BLOCK
const homeWithdrawals = await getPastEvents({
contract: homeBridge,
event: 'AffirmationCompleted',
fromBlock: HOME_DEPLOYMENT_BLOCK,
toBlock: homeBlockNumber,
options: {}
})

logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')")
const foreignWithdrawals =
bridgeMode === BRIDGE_MODES.ERC_TO_ERC || bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE
? await erc20Contract.getPastEvents('Transfer', {
tokenType === ERC_TYPES.ERC20
? await getPastEvents({
contract: erc20Contract,
event: 'Transfer',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
filter: { to: FOREIGN_BRIDGE_ADDRESS }
toBlock: foreignBlockNumber,
options: {
filter: { to: FOREIGN_BRIDGE_ADDRESS }
}
})
: await foreignBridge.getPastEvents('UserRequestForAffirmation', {
fromBlock: FOREIGN_DEPLOYMENT_BLOCK
: await getPastEvents({
contract: foreignBridge,
event: 'UserRequestForAffirmation',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
toBlock: foreignBlockNumber,
options: {}
})
logger.debug('Done')
return {
Expand Down
10 changes: 8 additions & 2 deletions utils/bridgeMode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const HOME_NATIVE_TO_ERC_ABI = require('../abis/HomeBridgeNativeToErc.abi')
const FOREIGN_NATIVE_TO_ERC_ABI = require('../abis/ForeignBridgeNativeToErc.abi')
const HOME_ERC_TO_ERC_ABI = require('../abis/HomeBridgeErcToErc.abi')
const FOREIGN_ERC_TO_ERC_ABI = require('../abis/ForeignBridgeErcToErc.abi')
const FOREIGN_ERC_TO_ERC_ABI = require('../abis/ForeignBridgeErc677ToErc677.abi')
const HOME_ERC_TO_NATIVE_ABI = require('../abis/HomeBridgeErcToNative.abi')
const FOREIGN_ERC_TO_NATIVE_ABI = require('../abis/ForeignBridgeErcToNative.abi')

Expand All @@ -11,6 +11,11 @@ const BRIDGE_MODES = {
ERC_TO_NATIVE: 'ERC_TO_NATIVE'
}

const ERC_TYPES = {
ERC20: 'ERC20',
ERC677: 'ERC677'
}

function getBridgeABIs(bridgeMode) {
let HOME_ABI = null
let FOREIGN_ABI = null
Expand Down Expand Up @@ -46,5 +51,6 @@ function decodeBridgeMode(bridgeModeHash) {
module.exports = {
decodeBridgeMode,
getBridgeABIs,
BRIDGE_MODES
BRIDGE_MODES,
ERC_TYPES
}
50 changes: 50 additions & 0 deletions utils/contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { toBN } = require('web3').utils

const ONE = toBN(1)
const TWO = toBN(2)

async function getPastEvents({ contract, event, fromBlock, toBlock, options }) {
let events
try {
events = await contract.getPastEvents(event, {
...options,
fromBlock,
toBlock
})
} catch (e) {
if (e.message.includes('query returned more than 1000 results')) {
const middle = fromBlock.add(toBlock).divRound(TWO)
const middlePlusOne = middle.add(ONE)

const firstHalfEvents = await getPastEvents({
contract,
event,
fromBlock,
toBlock: middle,
options
})
const secondHalfEvents = await getPastEvents({
contract,
event,
fromBlock: middlePlusOne,
toBlock,
options
})
events = [...firstHalfEvents, ...secondHalfEvents]
} else {
throw new Error(e)
}
}
return events
}

const getBlockNumberCall = web3 => web3.eth.getBlockNumber()

async function getBlockNumber(web3Home, web3Foreign) {
return (await Promise.all([web3Home, web3Foreign].map(getBlockNumberCall))).map(toBN)
}

module.exports = {
getPastEvents,
getBlockNumber
}
18 changes: 18 additions & 0 deletions utils/ercUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { ERC_TYPES } = require('./bridgeMode')

const getTokenType = async (contract, bridgeAddress) => {
try {
const bridgeContract = await contract.methods.bridgeContract().call()
if (bridgeContract === bridgeAddress) {
return ERC_TYPES.ERC677
} else {
return ERC_TYPES.ERC20
}
} catch (e) {
return ERC_TYPES.ERC20
}
}

module.exports = {
getTokenType
}
61 changes: 44 additions & 17 deletions utils/events.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require('dotenv').config()
const Web3 = require('web3')
const { toBN } = require('web3').utils
const logger = require('../logger')('eventsUtils')
const { BRIDGE_MODES, decodeBridgeMode, getBridgeABIs } = require('./bridgeMode')
const { BRIDGE_MODES, decodeBridgeMode, getBridgeABIs, ERC_TYPES } = require('./bridgeMode')
const { getTokenType } = require('./ercUtils')

const { HOME_RPC_URL, FOREIGN_RPC_URL, HOME_BRIDGE_ADDRESS, FOREIGN_BRIDGE_ADDRESS } = process.env
const HOME_DEPLOYMENT_BLOCK = Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0
const FOREIGN_DEPLOYMENT_BLOCK = Number(process.env.FOREIGN_DEPLOYMENT_BLOCK) || 0
const HOME_DEPLOYMENT_BLOCK = toBN(Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0)
const FOREIGN_DEPLOYMENT_BLOCK = toBN(Number(process.env.FOREIGN_DEPLOYMENT_BLOCK) || 0)

const homeProvider = new Web3.providers.HttpProvider(HOME_RPC_URL)
const web3Home = new Web3(homeProvider)
Expand All @@ -15,52 +17,77 @@ const web3Foreign = new Web3(foreignProvider)

const HOME_ERC_TO_ERC_ABI = require('../abis/HomeBridgeErcToErc.abi')
const ERC20_ABI = require('../abis/ERC20.abi')
const { getPastEvents, getBlockNumber } = require('./contract')

async function main() {
try {
const homeErcBridge = new web3Home.eth.Contract(HOME_ERC_TO_ERC_ABI, HOME_BRIDGE_ADDRESS)
const bridgeModeHash = await homeErcBridge.methods.getBridgeMode().call()
const bridgeMode = decodeBridgeMode(bridgeModeHash)
const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode)
const hasForeignErc =
bridgeMode === BRIDGE_MODES.ERC_TO_ERC || bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE
const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS)
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS)
const tokenType = await getTokenType(foreignBridge, FOREIGN_BRIDGE_ADDRESS)
const isExternalErc20 = tokenType === ERC_TYPES.ERC20
const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC ? 'erc677token' : 'erc20token'
const erc20Address = await foreignBridge.methods[erc20MethodName]().call()
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)

logger.debug('getting last block numbers')
const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign)

logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')")
const homeDeposits = await homeBridge.getPastEvents('UserRequestForSignature', {
fromBlock: HOME_DEPLOYMENT_BLOCK
const homeDeposits = await getPastEvents({
contract: homeBridge,
event: 'UserRequestForSignature',
fromBlock: HOME_DEPLOYMENT_BLOCK,
toBlock: homeBlockNumber,
options: {}
})

logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')")
const foreignDeposits = await foreignBridge.getPastEvents('RelayedMessage', {
fromBlock: FOREIGN_DEPLOYMENT_BLOCK
const foreignDeposits = await getPastEvents({
contract: foreignBridge,
event: 'RelayedMessage',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
toBlock: foreignBlockNumber,
options: {}
})

logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')")
const homeWithdrawals = await homeBridge.getPastEvents('AffirmationCompleted', {
fromBlock: HOME_DEPLOYMENT_BLOCK
const homeWithdrawals = await getPastEvents({
contract: homeBridge,
event: 'AffirmationCompleted',
fromBlock: HOME_DEPLOYMENT_BLOCK,
toBlock: homeBlockNumber,
options: {}
})

logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')")
const foreignWithdrawals = hasForeignErc
? await erc20Contract.getPastEvents('Transfer', {
const foreignWithdrawals = isExternalErc20
? await getPastEvents({
contract: erc20Contract,
event: 'Transfer',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
filter: { to: FOREIGN_BRIDGE_ADDRESS }
toBlock: foreignBlockNumber,
options: {
filter: { to: FOREIGN_BRIDGE_ADDRESS }
}
})
: await foreignBridge.getPastEvents('UserRequestForAffirmation', {
fromBlock: FOREIGN_DEPLOYMENT_BLOCK
: await getPastEvents({
contract: foreignBridge,
event: 'UserRequestForAffirmation',
fromBlock: FOREIGN_DEPLOYMENT_BLOCK,
toBlock: foreignBlockNumber,
options: {}
})
logger.debug('Done')
return {
homeDeposits,
foreignDeposits,
homeWithdrawals,
foreignWithdrawals,
hasForeignErc
isExternalErc20
}
} catch (e) {
logger.error(e)
Expand Down
11 changes: 9 additions & 2 deletions utils/validatorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const bridgeValidatorsAbi = require('../abis/BridgeValidators.abi')
const logger = require('../logger')('validatorsUtils')
const { getPastEvents } = require('./contract')

const parseValidatorEvent = event => {
if (
Expand Down Expand Up @@ -51,7 +52,7 @@ const validatorList = async contract => {
}
}

const getValidatorList = async (address, eth, fromBlock) => {
const getValidatorList = async (address, eth, fromBlock, toBlock) => {
logger.debug('getting validatorList')
const validatorsContract = new eth.Contract(bridgeValidatorsAbi, address)
const validators = await validatorList(validatorsContract)
Expand All @@ -62,7 +63,13 @@ const getValidatorList = async (address, eth, fromBlock) => {

logger.debug('getting validatorsEvents')
const contract = new eth.Contract([], address)
const validatorsEvents = await contract.getPastEvents('allEvents', { fromBlock })
const validatorsEvents = await getPastEvents({
contract,
event: 'allEvents',
fromBlock,
toBlock,
options: {}
})

return processValidatorsEvents(validatorsEvents)
}
Expand Down
Loading

0 comments on commit eaf2a65

Please sign in to comment.