Cache monitor RPC calls to increase performance (#322)

This commit is contained in:
Kirill Fedoseev 2020-05-07 20:42:27 +03:00 committed by GitHub
parent bcbe34a839
commit b4abbc4910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 47 additions and 5 deletions

@ -70,3 +70,4 @@ MONITOR_VALIDATOR_FOREIGN_TX_LIMIT | Average gas usage of a transaction sent by
MONITOR_TX_NUMBER_THRESHOLD | If estimated number of transaction is equal to or below this value, the monitor will report that the validator has less funds than it is required. | integer
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

@ -21,6 +21,7 @@ In `group_vars/xdai.yml`
---
MONITOR_BRIDGE_NAME: "xdai"
MONITOR_PORT: 3003
MONITOR_CACHE_EVENTS: "true"
COMMON_HOME_RPC_URL: "https://dai.poa.network"
COMMON_HOME_BRIDGE_ADDRESS: "0x7301CFA0e1756B71869E93d4e4Dca5c7d0eb0AA6"
@ -64,6 +65,7 @@ In `group_vars/wetc.yml`
```
---
MONITOR_BRIDGE_NAME: "wetc"
MONITOR_CACHE_EVENTS: "true"
COMMON_HOME_RPC_URL: "https://ethereumclassic.network"
COMMON_HOME_BRIDGE_ADDRESS: "0x073081832B4Ecdce79d4D6753565c85Ba4b3BeA9"

@ -44,6 +44,7 @@ UI_FOREIGN_GAS_PRICE_UPDATE_INTERVAL: 600000
## Monitor
MONITOR_BRIDGE_NAME: "xdai"
MONITOR_PORT: 3003
MONITOR_CACHE_EVENTS: "true"
MONITOR_HOME_START_BLOCK: 759
MONITOR_FOREIGN_START_BLOCK: 6478417
MONITOR_VALIDATOR_HOME_TX_LIMIT: 300000

@ -46,6 +46,7 @@ UI_FOREIGN_GAS_PRICE_UPDATE_INTERVAL: 600000
## Monitor
MONITOR_BRIDGE_NAME: "bridge"
MONITOR_PORT: 3003
MONITOR_CACHE_EVENTS: "false"
MONITOR_HOME_START_BLOCK: 0
MONITOR_FOREIGN_START_BLOCK: 0
MONITOR_VALIDATOR_HOME_TX_LIMIT: 300000

@ -43,6 +43,7 @@ UI_FOREIGN_GAS_PRICE_UPDATE_INTERVAL: 600000
#monitor
MONITOR_BRIDGE_NAME: "bridge"
MONITOR_CACHE_EVENTS: "true"
MONITOR_HOME_START_BLOCK: 0
MONITOR_FOREIGN_START_BLOCK: 0
MONITOR_VALIDATOR_HOME_TX_LIMIT: 300000

@ -45,6 +45,7 @@ UI_FOREIGN_GAS_PRICE_UPDATE_INTERVAL: 600000
## Monitor
MONITOR_BRIDGE_NAME: "wetc"
MONITOR_PORT: 3003
MONITOR_CACHE_EVENTS: "true"
MONITOR_HOME_START_BLOCK: 7703292
MONITOR_FOREIGN_START_BLOCK: 7412459
MONITOR_VALIDATOR_HOME_TX_LIMIT: 300000

@ -32,3 +32,5 @@ MONITOR_FOREIGN_START_BLOCK={{ MONITOR_FOREIGN_START_BLOCK }}
MONITOR_VALIDATOR_HOME_TX_LIMIT={{ MONITOR_VALIDATOR_HOME_TX_LIMIT }}
MONITOR_VALIDATOR_FOREIGN_TX_LIMIT={{ MONITOR_VALIDATOR_FOREIGN_TX_LIMIT }}
MONITOR_TX_NUMBER_THRESHOLD={{ MONITOR_TX_NUMBER_THRESHOLD }}
MONITOR_CACHE_EVENTS={{ MONITOR_CACHE_EVENTS }}

@ -17,3 +17,4 @@ COMMON_FOREIGN_GAS_PRICE_FACTOR=1
MONITOR_TX_NUMBER_THRESHOLD=100
MONITOR_PORT=3013
MONITOR_BRIDGE_NAME=bridge
MONITOR_CACHE_EVENTS=false

@ -17,3 +17,4 @@ COMMON_FOREIGN_GAS_PRICE_FACTOR=1
MONITOR_TX_NUMBER_THRESHOLD=100
MONITOR_PORT=3012
MONITOR_BRIDGE_NAME=bridge
MONITOR_CACHE_EVENTS=false

@ -17,3 +17,4 @@ COMMON_FOREIGN_GAS_PRICE_FACTOR=1
MONITOR_TX_NUMBER_THRESHOLD=100
MONITOR_PORT=3011
MONITOR_BRIDGE_NAME=bridge
MONITOR_CACHE_EVENTS=false

@ -17,3 +17,4 @@ COMMON_FOREIGN_GAS_PRICE_FACTOR=1
MONITOR_TX_NUMBER_THRESHOLD=100
MONITOR_PORT=3010
MONITOR_BRIDGE_NAME=bridge
MONITOR_CACHE_EVENTS=false

@ -21,3 +21,4 @@ COMMON_FOREIGN_GAS_PRICE_FACTOR=1
MONITOR_TX_NUMBER_THRESHOLD=100
MONITOR_PORT=3003
MONITOR_CACHE_EVENTS=true

@ -16,12 +16,14 @@ const {
} = require('../../commons')
const { normalizeEventInformation } = require('./message')
const { filterTransferBeforeES } = require('./tokenUtils')
const { writeFile, readCacheFile } = require('./file')
const {
COMMON_HOME_RPC_URL,
COMMON_FOREIGN_RPC_URL,
COMMON_HOME_BRIDGE_ADDRESS,
COMMON_FOREIGN_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)
@ -34,7 +36,17 @@ const web3Foreign = new Web3(foreignProvider)
const { getBlockNumber } = require('./contract')
const cacheFilePath = '/tmp/cachedEvents.json'
async function main(mode) {
if (MONITOR_CACHE_EVENTS === 'true') {
logger.debug('checking existing events cache')
const cachedEvents = readCacheFile(cacheFilePath)
if (cachedEvents !== false) {
logger.debug('returning events stored in cache')
return cachedEvents
}
}
const homeErcBridge = new web3Home.eth.Contract(HOME_ERC_TO_ERC_ABI, COMMON_HOME_BRIDGE_ADDRESS)
const bridgeMode = mode || (await getBridgeMode(homeErcBridge))
const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode)
@ -170,7 +182,7 @@ async function main(mode) {
}
logger.debug('Done')
return {
const result = {
homeToForeignRequests,
homeToForeignConfirmations,
foreignToHomeConfirmations,
@ -178,6 +190,12 @@ async function main(mode) {
isExternalErc20,
bridgeMode
}
if (MONITOR_CACHE_EVENTS === 'true') {
logger.debug('saving obtained events into cache file')
writeFile(cacheFilePath, result, false)
}
return result
}
module.exports = main

@ -15,8 +15,9 @@ async function readFile(filePath) {
}
}
function writeFile(filePath, object) {
fs.writeFileSync(path.join(process.cwd(), filePath), JSON.stringify(object, null, 4))
function writeFile(filePath, object, useCwd = true) {
const fullPath = useCwd ? path.join(process.cwd(), filePath) : filePath
fs.writeFileSync(fullPath, JSON.stringify(object, null, 4))
}
function createDir(dirPath) {
@ -29,8 +30,17 @@ function createDir(dirPath) {
}
}
function readCacheFile(filePath) {
try {
return JSON.parse(fs.readFileSync(filePath))
} catch (_) {
return false
}
}
module.exports = {
readFile,
writeFile,
createDir
createDir,
readCacheFile
}