From 302aef394b70e414bfcc0390e368b353d6d5e084 Mon Sep 17 00:00:00 2001 From: Eugene Upston <35315320+eupston@users.noreply.github.com> Date: Wed, 4 May 2022 12:06:48 +1200 Subject: [PATCH 01/11] fixed messageTTL publisher (#84) --- scripts/subscribeEthereumDeposit.js | 4 ++-- test/relayer/subscribeEthereumDeposit.test.ts | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/subscribeEthereumDeposit.js b/scripts/subscribeEthereumDeposit.js index b3a0c05..81936eb 100644 --- a/scripts/subscribeEthereumDeposit.js +++ b/scripts/subscribeEthereumDeposit.js @@ -212,14 +212,14 @@ async function mainPublisher(networkName, pegContractAddress, providerOverride= logger.info(`HEALTH CHECK => OK`); logger.info(`At blocknumber: ${blockNumber}`); }); - + const messageTimeout = 60000 * 5; //5 minutes const peg = new ethers.Contract(pegContractAddress, pegAbi, provider); logger.info(`Connecting to CENNZnet peg contract ${pegContractAddress}...`); const eventConfirmations = (await api.query.ethBridge.eventConfirmations()).toNumber(); let channel; if (channelOverride) channel = channelOverride; else channel = await rabbit.createChannel(); - await channel.assertQueue(TOPIC_CENNZnet_CONFIRM); + await channel.assertQueue(TOPIC_CENNZnet_CONFIRM, {durable: true, messageTtl: messageTimeout}); // On eth side deposit push pub sub queue with the data, if bridge is paused, update tx status as bridge paused peg.on("Deposit", async (sender, tokenAddress, amount, cennznetAddress, eventInfo) => { logger.info(`Got the event...${JSON.stringify(eventInfo)}`); diff --git a/test/relayer/subscribeEthereumDeposit.test.ts b/test/relayer/subscribeEthereumDeposit.test.ts index a104cd4..69ca96e 100644 --- a/test/relayer/subscribeEthereumDeposit.test.ts +++ b/test/relayer/subscribeEthereumDeposit.test.ts @@ -43,12 +43,13 @@ describe('subscribeEthereumDeposit', () => { await BridgeClaim.deleteMany({}); await ClaimEvents.deleteMany({}); rabbit = await amqp.connect(process.env.RABBIT_URL); + const messageTimeout = 60000 * 5; //5 minutes sendClaimChannel = await rabbit.createChannel(); verifyClaimChannel = await rabbit.createChannel(); sendClaimChannel.deleteQueue(TOPIC_CENNZnet_CONFIRM); verifyClaimChannel.deleteQueue(TOPIC_VERIFY_CONFIRM); - await sendClaimChannel.assertQueue(TOPIC_CENNZnet_CONFIRM); - await verifyClaimChannel.assertQueue(TOPIC_VERIFY_CONFIRM); + await sendClaimChannel.assertQueue(TOPIC_CENNZnet_CONFIRM, {durable: true, messageTtl: messageTimeout}); + await verifyClaimChannel.assertQueue(TOPIC_VERIFY_CONFIRM, {durable: true, messageTtl: messageTimeout}); }); afterEach(async () => { From d85f69099bfe2c43f89d3a3951971ae2542f1b26 Mon Sep 17 00:00:00 2001 From: Eug Date: Mon, 9 May 2022 14:48:59 +1200 Subject: [PATCH 02/11] fixed TTL in event poller --- scripts/ethereumEventPoller.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/ethereumEventPoller.js b/scripts/ethereumEventPoller.js index d953726..30b3004 100644 --- a/scripts/ethereumEventPoller.js +++ b/scripts/ethereumEventPoller.js @@ -19,7 +19,8 @@ async function pollDepositEvents( networkName, interval, pegContractAddress, pro api = await Api.create({network: networkName}); let rabbit = await amqp.connect(process.env.RABBIT_URL); let channel = await rabbit.createChannel(); - await channel.assertQueue(TOPIC_CENNZnet_CONFIRM); + const messageTimeout = 60000 * 5; //5 minutes + await channel.assertQueue(TOPIC_CENNZnet_CONFIRM,{durable: true, messageTtl: messageTimeout}); if (networkName === 'azalea') { provider = new ethers.providers.AlchemyProvider(process.env.ETH_NETWORK, process.env.AlCHEMY_API_KEY @@ -74,7 +75,8 @@ async function pollWithdrawEvents( networkName, interval, pegContractAddress, br if(mongoose.connection.readyState !== 1) await mongoose.connect(connectionStr); let rabbit = await amqp.connect(process.env.RABBIT_URL); let channel = await rabbit.createChannel(); - await channel.assertQueue(TOPIC_CENNZnet_CONFIRM); + const messageTimeout = 60000 * 5; //5 minutes + await channel.assertQueue(TOPIC_CENNZnet_CONFIRM,{durable: true, messageTtl: messageTimeout}); if (networkName === 'azalea') { provider = new ethers.providers.AlchemyProvider(process.env.ETH_NETWORK, process.env.AlCHEMY_API_KEY From 6adda7a19d7e602c8a724fef5bd2e69bae477238 Mon Sep 17 00:00:00 2001 From: Eug Date: Mon, 9 May 2022 16:22:59 +1200 Subject: [PATCH 03/11] memory leak fix api --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 5fb2e26..7c2f5b1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "CENNZnet <> Eth Bridge", "main": "index.js", "scripts": { - "api": "nodemon src/api/index.js", + "api": "node src/api/index.js", + "api:dev": "nodemon src/api/index.js", "build": "rm -rf artifacts & hardhat compile", "build:abi": "yarn build && (mkdir abi || true) && cp artifacts/contracts/**/*.json abi/ && rm abi/*.dbg.json", "deploy": "hardhat run --network localhost scripts/deploy.js", From 4e3c603a9b0bd0242819e7cbf55a843b4e92a38c Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Thu, 19 May 2022 16:21:33 +1200 Subject: [PATCH 04/11] Update the relayer to work with new image + old image (#87) (#88) * Update the relayer to work with new image + old image --- .github/workflows/pr-any.yml | 2 +- scripts/e2e.js | 7 ++++--- scripts/ethEndToEndTest.js | 7 ++++--- scripts/subscribeEthereumDeposit.js | 10 +++++----- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/pr-any.yml b/.github/workflows/pr-any.yml index b5a72fb..0ec37b8 100644 --- a/.github/workflows/pr-any.yml +++ b/.github/workflows/pr-any.yml @@ -132,7 +132,7 @@ jobs: run: | docker network create my-bridge-net docker run --pull=always --network=my-bridge-net --name ethNode -p 8545:8545 -p 30303:30303 -d ethereumoptimism/hardhat - docker run --pull=always --network=my-bridge-net --name testnet_node_alice -p 9944:9944 -d cennznet/cennznet:2.1.0 --dev --tmp --unsafe-ws-external --unsafe-rpc-external --eth-http=http://ethNode:8545 --no-mdns + docker run --pull=always --network=my-bridge-net --name testnet_node_alice -p 9944:9944 -d cennznet/cennznet:latest --dev --tmp --unsafe-ws-external --unsafe-rpc-external --eth-http=http://ethNode:8545 --no-mdns docker exec testnet_node_alice curl ethNode:8545 # - name: deposit/withdraw scenarios for erc20 tokens # env: diff --git a/scripts/e2e.js b/scripts/e2e.js index 8999a20..746656b 100644 --- a/scripts/e2e.js +++ b/scripts/e2e.js @@ -1,5 +1,5 @@ const { Api } = require('@cennznet/api'); -const { Keyring } = require('@polkadot/keyring'); +const { encodeAddress, Keyring } = require('@polkadot/keyring'); const { ethers } = require("hardhat"); async function main() { @@ -96,8 +96,9 @@ async function main() { if (status.isInBlock) { for (const {event: {method, section, data}} of events) { console.log('\t', `: ${section}.${method}`, data.toString()); - const [, claimer] = data; - if (section === 'erc20Peg' && method == 'Erc20Claim' && claimer && claimer.toString() === alice.address) { + const [, address] = data; + if (section === 'erc20Peg' && method == 'Erc20Claim' && address && + (address.toString() === alice.address || address.toString() === encodeAddress(claim.beneficiary))) { eventClaimId = data[0]; console.log('*******************************************'); console.log('Deposit claim on CENNZnet side started for claim Id', eventClaimId.toString()); diff --git a/scripts/ethEndToEndTest.js b/scripts/ethEndToEndTest.js index 4229c0d..3bfa2ec 100644 --- a/scripts/ethEndToEndTest.js +++ b/scripts/ethEndToEndTest.js @@ -1,6 +1,6 @@ const { Api } = require('@cennznet/api'); -const { Keyring } = require('@polkadot/keyring'); +const { encodeAddress, Keyring } = require('@polkadot/keyring'); const { ethers } = require("hardhat"); async function main() { @@ -99,8 +99,9 @@ async function main() { if (status.isInBlock) { for (const {event: {method, section, data}} of events) { console.log('\t', `: ${section}.${method}`, data.toString()); - const [, claimer] = data; - if (section === 'erc20Peg' && method == 'Erc20Claim' && claimer && claimer.toString() === alice.address) { + const [, address] = data; + if (section === 'erc20Peg' && method == 'Erc20Claim' && address && + (address.toString() === alice.address || address.toString() === encodeAddress(claim.beneficiary))) { eventClaimId = data[0]; console.log('*******************************************'); console.log('Deposit claim on CENNZnet side started for claim Id', eventClaimId.toString()); diff --git a/scripts/subscribeEthereumDeposit.js b/scripts/subscribeEthereumDeposit.js index 81936eb..f3560ba 100644 --- a/scripts/subscribeEthereumDeposit.js +++ b/scripts/subscribeEthereumDeposit.js @@ -1,5 +1,5 @@ const { Api } = require('@cennznet/api'); -const { Keyring } = require('@polkadot/keyring'); +const { encodeAddress, Keyring } = require('@polkadot/keyring'); const logger = require('./logger'); const mongoose = require('mongoose'); const { BridgeClaim, ClaimEvents } = require('../src/mongo/models'); @@ -9,7 +9,6 @@ const { hexToU8a } = require("@polkadot/util"); const pegAbi = require("../abi/ERC20Peg.json").abi; const BigNumber = require("bignumber.js"); const amqp = require("amqplib"); - require("dotenv").config(); async function airDrop(claimId, signer, api, spendingAssetId) { @@ -76,8 +75,9 @@ async function sendClaim(claim, transactionHash, api, nonce, signer) { const block = await api.rpc.chain.getBlock(blockHash); const blockNumber = block.block.header.number.toNumber(); for (const {event: {method, section, data}} of events) { - const [, claimer] = data; - if (section === 'erc20Peg' && method == 'Erc20Claim' && claimer && claimer.toString() === signer.address) { + const [, address] = data; + if (section === 'erc20Peg' && method == 'Erc20Claim' && address && + (address.toString() === signer.address || address.toString() === encodeAddress(claim.beneficiary))) { const eventClaimId = data[0].toString(); logger.info('CLAIM: *******************************************'); logger.info('CLAIM: at block number: ',blockNumber); @@ -397,4 +397,4 @@ let firstMessage = true; if(state === "publisher") mainPublisher(networkName, pegContractAddress).catch((err) => logger.error(err)); else if (state === "subscriber") mainSubscriber(networkName).catch((err) => logger.error(err)); -module.exports = {wait, handleDepositEvent, mainPublisher, mainSubscriber, TOPIC_VERIFY_CONFIRM, TOPIC_CENNZnet_CONFIRM} \ No newline at end of file +module.exports = {wait, handleDepositEvent, mainPublisher, mainSubscriber, TOPIC_VERIFY_CONFIRM, TOPIC_CENNZnet_CONFIRM} From 8e6eb103f78ef1c433fe5cab98676712d79b353d Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Wed, 25 May 2022 15:36:50 +1200 Subject: [PATCH 05/11] loop through blocks till finalized block (#86) * loop through blocks till finalized block --- .eslintrc.json | 1 + scripts/subscribeEventProof.js | 71 ++++++++++++++++++++++++++-------- src/mongo/models.js | 15 ++++++- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index b10826b..5ae030a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,5 +11,6 @@ "allowImportExportEverywhere": true }, "rules": { + "no-constant-condition": "off" } } diff --git a/scripts/subscribeEventProof.js b/scripts/subscribeEventProof.js index 295e713..b43a7fc 100644 --- a/scripts/subscribeEventProof.js +++ b/scripts/subscribeEventProof.js @@ -4,7 +4,7 @@ require("dotenv").config(); const logger = require('./logger'); const { curly } = require("node-libcurl"); const mongoose = require('mongoose'); -const { EventProcessed } = require('../src/mongo/models'); +const { EventProcessed, LastBlockScan } = require('../src/mongo/models'); const ethers = require('ethers'); const bridgeAbi = require("../abi/CENNZnetBridge.json").abi; @@ -141,6 +141,15 @@ async function main (networkName, bridgeContractAddress) { process.env.INFURA_API_KEY ); + const setProcessedBlock = process.env.BOOTSTRAP; + // for the first start set processed block + if (setProcessedBlock === 'true') { + const currentFinalizedHeadHash = await api.rpc.chain.getFinalizedHead(); + const block = await api.rpc.chain.getBlock(currentFinalizedHeadHash); + const blockNo = block.block.header.number.toString(); + await updateBlockScanned({ processedBlock: blockNo }); + } + let wallet = new ethers.Wallet(process.env.ETH_ACCOUNT_KEY, infuraProvider); const bridge = new ethers.Contract(bridgeContractAddress, bridgeAbi, wallet); @@ -181,22 +190,44 @@ async function main (networkName, bridgeContractAddress) { await api.rpc.chain .subscribeFinalizedHeads(async (head) => { - const blockNumber = head.number.toNumber(); - logger.info(`At blocknumber: ${blockNumber}`); - - const blockHash = head.hash.toString(); - const events = await api.query.system.events.at(blockHash); - events.map(async ({event}) => { - const { section, method, data } = event; - if (section === 'ethBridge' && method === 'AuthoritySetChange') { - const dataFetched = data.toHuman(); - const eventIdFound = dataFetched[0]; - const newValidatorSetId = parseInt(dataFetched[1]); - logger.info(`IMP Event found at block ${blockNumber} hash ${blockHash} event id ${eventIdFound}`); - await getEventPoofAndSubmit(api, eventIdFound, bridge, wallet, newValidatorSetId.toString(), blockHash, infuraProvider); - } - }) + const finalizedBlockAt = head.number.toString(); + logger.info(` finalizedBlockAt::${finalizedBlockAt}`); + const update = { finalizedBlock: finalizedBlockAt }; + await updateBlockScanned(update); }); + + while (true) { + const blockScanned = await LastBlockScan.findOne({}); + if (blockScanned) { + const {processedBlock, finalizedBlock} = blockScanned; + const processBlockNumber = parseInt(processedBlock); + const finalizedBlockNumber = parseInt(finalizedBlock); + if (processBlockNumber < finalizedBlockNumber) { + for (let blockNumber = processBlockNumber; blockNumber < finalizedBlock; blockNumber++) { + logger.info(`At blocknumber: ${blockNumber}`); + + const blockHash = await api.rpc.chain.getBlockHash(blockNumber); + const events = await api.query.system.events.at(blockHash); + events.map(async ({event}) => { + const { section, method, data } = event; + if (section === 'ethBridge' && method === 'AuthoritySetChange') { + const dataFetched = data.toHuman(); + const eventIdFound = dataFetched[0]; + const newValidatorSetId = parseInt(dataFetched[1]); + logger.info(`IMP Event found at block ${blockNumber} hash ${blockHash} event id ${eventIdFound}`); + await getEventPoofAndSubmit(api, eventIdFound, bridge, wallet, newValidatorSetId.toString(), blockHash, infuraProvider); + } + }); + await updateBlockScanned({ processedBlock: blockNumber.toString() }); + } + } + } + await sleep(500); + } +} + +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); } async function withTimeout(promise, timeoutMs) { @@ -213,3 +244,11 @@ async function withTimeout(promise, timeoutMs) { const networkName = process.env.NETWORK; const bridgeContractAddress = process.env.BRIDGE_CONTRACT; main(networkName, bridgeContractAddress).catch((err) => console.log(err)); + + +async function updateBlockScanned(update) { + const filter = {}; + const options = { upsert: true, new: true, setDefaultsOnInsert: true }; // create new if record does not exist, else update + await LastBlockScan.updateOne(filter, update, options); + logger.info(`Updated the block in db..${JSON.stringify(update)}`); +} diff --git a/src/mongo/models.js b/src/mongo/models.js index 510496d..ead9e4c 100644 --- a/src/mongo/models.js +++ b/src/mongo/models.js @@ -60,6 +60,17 @@ const proofsSchema = new Schema( { validators: [] }, { collection: EVENT_PROOF }); +const LastBlockScannedCol = "LastBlockScan"; +const LastBlockScanSchema = new Schema( + { + _id: String, + processedBlock: { type: String, default: "0" }, + finalizedBlock: String, + }, + { collection: LastBlockScannedCol } +); + + module.exports = { BridgeClaim: mongoose.model('BridgeClaim', BridgeClaimSchema), BRIDGE_CLAIM, @@ -70,5 +81,7 @@ module.exports = { WithdrawProof: mongoose.model('WithdrawProof', WithdrawProofSchema), WITHDRAW_PROOF, EventProof: mongoose.model('EventProof', proofsSchema), - EVENT_PROOF + EVENT_PROOF, + LastBlockScan: mongoose.model("LastBlockScan", LastBlockScanSchema), + LastBlockScannedCol, }; From a6949e2901933f1f35896491d64b953f628629b4 Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Wed, 22 Jun 2022 12:20:05 +1200 Subject: [PATCH 06/11] update validator relayer (#93) * update validator relayer --- scripts/subscribeEventProof.js | 11 +++++++---- test/relayer/ethereumEventPoller.test.ts | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/subscribeEventProof.js b/scripts/subscribeEventProof.js index b43a7fc..c7ea683 100644 --- a/scripts/subscribeEventProof.js +++ b/scripts/subscribeEventProof.js @@ -88,8 +88,9 @@ async function getEventPoofAndSubmit(api, eventId, bridge, txExecutor, newValida logger.info('Gas price nw;:', gasPrice.toString()); const gasEstimated = await bridge.estimateGas.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: 5000000, gasPrice: increasedGasPrice}); - - logger.info(JSON.stringify(await bridge.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: gasEstimated.add(BUFFER), gasPrice: increasedGasPrice}))); + const tx = await bridge.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: gasEstimated.add(BUFFER), gasPrice: increasedGasPrice}); + await tx.wait(); // wait till tx is mined + logger.info(JSON.stringify(tx)); await updateLastEventProcessed(eventId, blockHash.toString()); const balance = await provider.getBalance(txExecutor.address); logger.info(`IMP Balance is: ${balance}`); @@ -174,6 +175,7 @@ async function main (networkName, bridgeContractAddress) { for (let i = scanFromEvent; i <= parseInt(lastEventProofIdFromCennznet);i++ ) { console.log('At Event id:',i); const eventProof = await withTimeout(api.derive.ethBridge.eventProof(i), 10000); + if (eventProof && eventProof.tag === 'sys:authority-change') { const checkEventExistsOnEth = await bridge.eventIds(i.toString()); if (!checkEventExistsOnEth) { @@ -203,7 +205,8 @@ async function main (networkName, bridgeContractAddress) { const processBlockNumber = parseInt(processedBlock); const finalizedBlockNumber = parseInt(finalizedBlock); if (processBlockNumber < finalizedBlockNumber) { - for (let blockNumber = processBlockNumber; blockNumber < finalizedBlock; blockNumber++) { + // processes block + 1 will be new block to process + for (let blockNumber = processBlockNumber+1; blockNumber < finalizedBlock; blockNumber++) { logger.info(`At blocknumber: ${blockNumber}`); const blockHash = await api.rpc.chain.getBlockHash(blockNumber); @@ -211,7 +214,7 @@ async function main (networkName, bridgeContractAddress) { events.map(async ({event}) => { const { section, method, data } = event; if (section === 'ethBridge' && method === 'AuthoritySetChange') { - const dataFetched = data.toHuman(); + const dataFetched = data.toJSON(); const eventIdFound = dataFetched[0]; const newValidatorSetId = parseInt(dataFetched[1]); logger.info(`IMP Event found at block ${blockNumber} hash ${blockHash} event id ${eventIdFound}`); diff --git a/test/relayer/ethereumEventPoller.test.ts b/test/relayer/ethereumEventPoller.test.ts index 9e644e0..ef3d766 100644 --- a/test/relayer/ethereumEventPoller.test.ts +++ b/test/relayer/ethereumEventPoller.test.ts @@ -39,6 +39,7 @@ describe('ethereumEventPoller', () => { }); beforeEach(async () => { + const messageTimeout = 60000 * 5; //5 minutes testToken = await deployContract(wallet, TestToken, [1000000], {}); bridge = await deployContract(wallet, CENNZnetBridge, []); erc20Peg = await deployContract(wallet, ERC20Peg, [bridge.address]); @@ -46,7 +47,7 @@ describe('ethereumEventPoller', () => { await BridgeClaim.deleteMany({}); sendClaimChannel = await rabbit.createChannel(); sendClaimChannel.deleteQueue(TOPIC_CENNZnet_CONFIRM); - await sendClaimChannel.assertQueue(TOPIC_CENNZnet_CONFIRM); + await sendClaimChannel.assertQueue(TOPIC_CENNZnet_CONFIRM, {durable: true, messageTtl: messageTimeout}); }); after(async () => { From 7dcce333b39a8b23375bcb67fc7a1aa3c82bc497 Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Mon, 4 Jul 2022 09:06:16 +1200 Subject: [PATCH 07/11] Takes in account any eth wallet extrinsic while bridging (#96) --- package.json | 2 +- scripts/subscribeWithdrawTx.js | 42 +++- yarn.lock | 384 +++++++++++++++++++++++++++++++-- 3 files changed, 407 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 7c2f5b1..5d66447 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "winston": "^3.2.1" }, "devDependencies": { - "@cennznet/api": "2.1.0-rc.4", + "@cennznet/api": "2.1.1-alpha.2", "@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-etherscan": "^2.1.7", "@nomiclabs/hardhat-waffle": "^2.0.1", diff --git a/scripts/subscribeWithdrawTx.js b/scripts/subscribeWithdrawTx.js index 976387c..46a997b 100644 --- a/scripts/subscribeWithdrawTx.js +++ b/scripts/subscribeWithdrawTx.js @@ -8,6 +8,7 @@ const { u8aToString } = require('@polkadot/util'); const ethers = require('ethers'); const bridgeAbi = require("../abi/CENNZnetBridge.json").abi; const pegAbi = require("../abi/ERC20Peg.json").abi; +const {cvmToAddress} = require("@cennznet/types/utils"); let registeredAsset; const timeoutMs = 60000; @@ -69,6 +70,37 @@ function scientificToDecimal(number) { return `${sign}${number}`; } +function isWithdrawErc20Peg(tx) { + const extrinsicCall = tx.method.toHuman(); + return (extrinsicCall.args.call.method === 'withdraw' && extrinsicCall.args.call.section === 'erc20Peg'); +} + +function fetchExtrinsicDetail(block) { + const withdrawTokenExt + = block.block.extrinsics.find(ex => ex.isSigned === true && ex.method.method === 'withdraw' && ex.method.section === 'erc20Peg'); + if (withdrawTokenExt) { + return [withdrawTokenExt.hash.toString(), withdrawTokenExt.signer]; // extrinsicHash, signer + } + // check if blocks has any ethWallet txs + const ethWalletExts + = block.block.extrinsics.filter(ex => ex.method.method === 'call' && ex.method.section === 'ethWallet'); + + if (ethWalletExts.length > 0) { + // find the one with erc20Peg withdraw extrinsic + const pegWithdrawTx = ethWalletExts.find(tx => isWithdrawErc20Peg(tx)); + if (!pegWithdrawTx) return []; + const extrinsicCall = pegWithdrawTx.method.toHuman(); + const evmAddress = extrinsicCall.args.eth_address; + if (evmAddress) { + console.log('cennznet address:',cvmToAddress(evmAddress)); + return [pegWithdrawTx.hash.toString(), cvmToAddress(evmAddress)]; // convert to cennznet address and return + } else { + return []; + } + } + return []; +} + async function getWithdrawProofAndUpdateDB(api, eventDetails, blockHash, bridge) { try { const [eventId, assetId, amountRaw, beneficiary] = eventDetails; @@ -80,14 +112,8 @@ async function getWithdrawProofAndUpdateDB(api, eventDetails, blockHash, bridge) logger.info(`IMP WITHDRAW newValidators:${newValidators}`); logger.info(`IMP WITHDRAW event proof::${JSON.stringify(eventProof)}`); const rawBlock = await api.rpc.chain.getBlock(blockHash); - const block = rawBlock.toHuman(); - const withdrawExtIndex - = block.block.extrinsics.findIndex(ex => ex.isSigned === true && ex.method.method === 'withdraw' && ex.method.section === 'erc20Peg'); - const withdrawExt = block.block.extrinsics[withdrawExtIndex]; - const rawExt = rawBlock.block.extrinsics[withdrawExtIndex]; - const txHash = api.registry.createType('Extrinsic', rawExt).hash.toHex(); - const cennznetAddress = withdrawExt ? withdrawExt.signer : ''; - console.log('withdrawExt::',withdrawExt); + + const [txHash, cennznetAddress] = fetchExtrinsicDetail(rawBlock); const proof = { eventId: eventProof.eventId, diff --git a/yarn.lock b/yarn.lock index 873688b..6455b0a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -30,12 +30,14 @@ dependencies: regenerator-runtime "^0.13.4" -"@cennznet/api@2.1.0-rc.4": - version "2.1.0-rc.4" - resolved "https://registry.yarnpkg.com/@cennznet/api/-/api-2.1.0-rc.4.tgz#1e94e08abed8c2375eca96c8603b882de40469c7" - integrity sha512-qqVlkqsIc1GA9TBz3lhbt4mxyf3yaL4FSmLht2746Du1T3owaAqjBRYC81L2sU1jbFl1PT+8tP7WCsN1aKqMng== - dependencies: - "@cennznet/types" "^2.1.0-rc.4" +"@cennznet/api@2.1.1-alpha.2": + version "2.1.1-alpha.2" + resolved "https://registry.yarnpkg.com/@cennznet/api/-/api-2.1.1-alpha.2.tgz#fb2c97ac75aa3b4459df8b8d777cc7018f068bf9" + integrity sha512-BdgbQa5cH5Rwca8mhP00fmyT8XgAq3EmxUrS46cj1dAxqVlPDQqqEnFfWuEA/5GTLliVBdwdUJICgvmvystFNg== + dependencies: + "@cennznet/types" "^2.1.1-alpha.2" + "@depay/web3-blockchains" "^4.3.0" + "@depay/web3-mock" "^11.7.0" "@ethersproject/keccak256" "^5.5.0" "@ethersproject/signing-key" "^5.5.0" "@polkadot/api" "^7.7.1" @@ -44,12 +46,13 @@ "@polkadot/rpc-provider" "^7.7.1" "@polkadot/types" "^7.7.1" "@polkadot/x-rxjs" "^6.11.2-6" + ethers "^5.6.4" eventemitter3 "^4.0.0" -"@cennznet/types@^2.1.0-rc.4": - version "2.1.0" - resolved "https://registry.yarnpkg.com/@cennznet/types/-/types-2.1.0.tgz#5f2e26ec8ef14c9e0b326fe87e9acf1477c2aa42" - integrity sha512-yIlm9JPh4cd8dicSjtYrwRoYmQV9mzy6RPwyI/Z5P9LiRktjOIlX90dfuQ9VvFHF/ruRKzEopiYD3n3zcHWkBg== +"@cennznet/types@^2.1.1-alpha.2": + version "2.1.1-alpha.4" + resolved "https://registry.yarnpkg.com/@cennznet/types/-/types-2.1.1-alpha.4.tgz#f287c7eac3bc52fed04ed909c1a73fe8727c7b9a" + integrity sha512-2LnOyFW0bRfNQvo0TjOuEDURDAqDOPs48vpmixFP5wxdN6IcL+T/xMf0sUZPq98kuCoviBGzDGJepN9/EW+vNQ== dependencies: "@polkadot/keyring" "^8.3.3" "@polkadot/types" "^7.7.1" @@ -82,6 +85,16 @@ enabled "2.0.x" kuler "^2.0.0" +"@depay/web3-blockchains@^4.3.0": + version "4.5.1" + resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-4.5.1.tgz#72a7259c2f0f31b02fb72a7a8373ad654d8b2936" + integrity sha512-or5+vaOsTZeOUrOf7mTXpGXAGgIizoO1NqDjU6EqTTMv2PTli0HPriIiuFcFwsouK7V1XxdYNMIKnl+W7k2Aaw== + +"@depay/web3-mock@^11.7.0": + version "11.11.0" + resolved "https://registry.yarnpkg.com/@depay/web3-mock/-/web3-mock-11.11.0.tgz#bca1f2600da6cecc7138a0acb6a61d475eb8d171" + integrity sha512-Ngaf2/sRiypqlW4WXzH4hVuFCTb2IhgxAb430/UfcmOFnmAEdGznd6aa9HgSAJ8HfIAW5isLvGyyQ1M+dmqgfA== + "@ensdomains/ens@^0.4.4": version "0.4.5" resolved "https://registry.yarnpkg.com/@ensdomains/ens/-/ens-0.4.5.tgz#e0aebc005afdc066447c6e22feb4eda89a5edbfc" @@ -265,6 +278,21 @@ "@ethersproject/properties" "^5.6.0" "@ethersproject/strings" "^5.6.0" +"@ethersproject/abi@5.6.4": + version "5.6.4" + resolved "https://registry.yarnpkg.com/@ethersproject/abi/-/abi-5.6.4.tgz#f6e01b6ed391a505932698ecc0d9e7a99ee60362" + integrity sha512-TTeZUlCeIHG6527/2goZA6gW5F8Emoc7MrZDC7hhP84aRGvW3TEdTnZR08Ls88YXM1m2SuK42Osw/jSi3uO8gg== + dependencies: + "@ethersproject/address" "^5.6.1" + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/constants" "^5.6.1" + "@ethersproject/hash" "^5.6.1" + "@ethersproject/keccak256" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/strings" "^5.6.1" + "@ethersproject/abstract-provider@5.6.0", "@ethersproject/abstract-provider@^5.4.0", "@ethersproject/abstract-provider@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/abstract-provider/-/abstract-provider-5.6.0.tgz#0c4ac7054650dbd9c476cf5907f588bbb6ef3061" @@ -278,6 +306,19 @@ "@ethersproject/transactions" "^5.6.0" "@ethersproject/web" "^5.6.0" +"@ethersproject/abstract-provider@5.6.1", "@ethersproject/abstract-provider@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/abstract-provider/-/abstract-provider-5.6.1.tgz#02ddce150785caf0c77fe036a0ebfcee61878c59" + integrity sha512-BxlIgogYJtp1FS8Muvj8YfdClk3unZH0vRMVX791Z9INBNT/kuACZ9GzaY1Y4yFq+YSy6/w4gzj3HCRKrK9hsQ== + dependencies: + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/networks" "^5.6.3" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/transactions" "^5.6.2" + "@ethersproject/web" "^5.6.1" + "@ethersproject/abstract-signer@5.6.0", "@ethersproject/abstract-signer@^5.4.0", "@ethersproject/abstract-signer@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/abstract-signer/-/abstract-signer-5.6.0.tgz#9cd7ae9211c2b123a3b29bf47aab17d4d016e3e7" @@ -289,6 +330,17 @@ "@ethersproject/logger" "^5.6.0" "@ethersproject/properties" "^5.6.0" +"@ethersproject/abstract-signer@5.6.2", "@ethersproject/abstract-signer@^5.6.2": + version "5.6.2" + resolved "https://registry.yarnpkg.com/@ethersproject/abstract-signer/-/abstract-signer-5.6.2.tgz#491f07fc2cbd5da258f46ec539664713950b0b33" + integrity sha512-n1r6lttFBG0t2vNiI3HoWaS/KdOt8xyDjzlP2cuevlWLG6EX0OwcKLyG/Kp/cuwNxdy/ous+R/DEMdTUwWQIjQ== + dependencies: + "@ethersproject/abstract-provider" "^5.6.1" + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/address@5.6.0", "@ethersproject/address@>=5.0.0-beta.128", "@ethersproject/address@^5.0.2", "@ethersproject/address@^5.4.0", "@ethersproject/address@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/address/-/address-5.6.0.tgz#13c49836d73e7885fc148ad633afad729da25012" @@ -300,6 +352,17 @@ "@ethersproject/logger" "^5.6.0" "@ethersproject/rlp" "^5.6.0" +"@ethersproject/address@5.6.1", "@ethersproject/address@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/address/-/address-5.6.1.tgz#ab57818d9aefee919c5721d28cd31fd95eff413d" + integrity sha512-uOgF0kS5MJv9ZvCz7x6T2EXJSzotiybApn4XlOgoTX0xdtyVIJ7pF+6cGPxiEq/dpBiTfMiw7Yc81JcwhSYA0Q== + dependencies: + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/keccak256" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/rlp" "^5.6.1" + "@ethersproject/base64@5.6.0", "@ethersproject/base64@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/base64/-/base64-5.6.0.tgz#a12c4da2a6fb86d88563216b0282308fc15907c9" @@ -307,6 +370,13 @@ dependencies: "@ethersproject/bytes" "^5.6.0" +"@ethersproject/base64@5.6.1", "@ethersproject/base64@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/base64/-/base64-5.6.1.tgz#2c40d8a0310c9d1606c2c37ae3092634b41d87cb" + integrity sha512-qB76rjop6a0RIYYMiB4Eh/8n+Hxu2NIZm8S/Q7kNo5pmZfXhHGHmS4MinUainiBC54SCyRnwzL+KZjj8zbsSsw== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/basex@5.6.0", "@ethersproject/basex@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/basex/-/basex-5.6.0.tgz#9ea7209bf0a1c3ddc2a90f180c3a7f0d7d2e8a69" @@ -315,6 +385,14 @@ "@ethersproject/bytes" "^5.6.0" "@ethersproject/properties" "^5.6.0" +"@ethersproject/basex@5.6.1", "@ethersproject/basex@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/basex/-/basex-5.6.1.tgz#badbb2f1d4a6f52ce41c9064f01eab19cc4c5305" + integrity sha512-a52MkVz4vuBXR06nvflPMotld1FJWSj2QT0985v7P/emPZO00PucFAkbcmq2vpVU7Ts7umKiSI6SppiLykVWsA== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/bignumber@5.6.0", "@ethersproject/bignumber@>=5.0.0-beta.130", "@ethersproject/bignumber@^5.4.0", "@ethersproject/bignumber@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/bignumber/-/bignumber-5.6.0.tgz#116c81b075c57fa765a8f3822648cf718a8a0e26" @@ -324,7 +402,16 @@ "@ethersproject/logger" "^5.6.0" bn.js "^4.11.9" -"@ethersproject/bytes@5.6.1", "@ethersproject/bytes@>=5.0.0-beta.129", "@ethersproject/bytes@^5.4.0", "@ethersproject/bytes@^5.6.0": +"@ethersproject/bignumber@5.6.2", "@ethersproject/bignumber@^5.6.2": + version "5.6.2" + resolved "https://registry.yarnpkg.com/@ethersproject/bignumber/-/bignumber-5.6.2.tgz#72a0717d6163fab44c47bcc82e0c550ac0315d66" + integrity sha512-v7+EEUbhGqT3XJ9LMPsKvXYHFc8eHxTowFCG/HgJErmq4XHJ2WR7aeyICg3uTOAQ7Icn0GFHAohXEhxQHq4Ubw== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + bn.js "^5.2.1" + +"@ethersproject/bytes@5.6.1", "@ethersproject/bytes@>=5.0.0-beta.129", "@ethersproject/bytes@^5.4.0", "@ethersproject/bytes@^5.6.0", "@ethersproject/bytes@^5.6.1": version "5.6.1" resolved "https://registry.yarnpkg.com/@ethersproject/bytes/-/bytes-5.6.1.tgz#24f916e411f82a8a60412344bf4a813b917eefe7" integrity sha512-NwQt7cKn5+ZE4uDn+X5RAXLp46E1chXoaMmrxAyA0rblpxz8t58lVkrHXoRIn0lz1joQElQ8410GqhTqMOwc6g== @@ -338,7 +425,14 @@ dependencies: "@ethersproject/bignumber" "^5.6.0" -"@ethersproject/contracts@5.4.0", "@ethersproject/contracts@5.6.0": +"@ethersproject/constants@5.6.1", "@ethersproject/constants@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/constants/-/constants-5.6.1.tgz#e2e974cac160dd101cf79fdf879d7d18e8cb1370" + integrity sha512-QSq9WVnZbxXYFftrjSjZDUshp6/eKp6qrtdBtUCm0QxCV5z1fG/w3kdlcsjMCQuQHUnAclKoK7XpXMezhRDOLg== + dependencies: + "@ethersproject/bignumber" "^5.6.2" + +"@ethersproject/contracts@5.4.0", "@ethersproject/contracts@5.6.0", "@ethersproject/contracts@5.6.2": version "5.4.0" resolved "https://registry.yarnpkg.com/@ethersproject/contracts/-/contracts-5.4.0.tgz#e05fe6bd33acc98741e27d553889ec5920078abb" integrity sha512-hkO3L3IhS1Z3ZtHtaAG/T87nQ7KiPV+/qnvutag35I0IkiQ8G3ZpCQ9NNOpSCzn4pWSW4CfzmtE02FcqnLI+hw== @@ -368,6 +462,20 @@ "@ethersproject/properties" "^5.6.0" "@ethersproject/strings" "^5.6.0" +"@ethersproject/hash@5.6.1", "@ethersproject/hash@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/hash/-/hash-5.6.1.tgz#224572ea4de257f05b4abf8ae58b03a67e99b0f4" + integrity sha512-L1xAHurbaxG8VVul4ankNX5HgQ8PNCTrnVXEiFnE9xoRnaUcgfD12tZINtDinSllxPLCtGwguQxJ5E6keE84pA== + dependencies: + "@ethersproject/abstract-signer" "^5.6.2" + "@ethersproject/address" "^5.6.1" + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/keccak256" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/strings" "^5.6.1" + "@ethersproject/hdnode@5.6.0", "@ethersproject/hdnode@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/hdnode/-/hdnode-5.6.0.tgz#9dcbe8d629bbbcf144f2cae476337fe92d320998" @@ -386,6 +494,24 @@ "@ethersproject/transactions" "^5.6.0" "@ethersproject/wordlists" "^5.6.0" +"@ethersproject/hdnode@5.6.2", "@ethersproject/hdnode@^5.6.2": + version "5.6.2" + resolved "https://registry.yarnpkg.com/@ethersproject/hdnode/-/hdnode-5.6.2.tgz#26f3c83a3e8f1b7985c15d1db50dc2903418b2d2" + integrity sha512-tERxW8Ccf9CxW2db3WsN01Qao3wFeRsfYY9TCuhmG0xNpl2IO8wgXU3HtWIZ49gUWPggRy4Yg5axU0ACaEKf1Q== + dependencies: + "@ethersproject/abstract-signer" "^5.6.2" + "@ethersproject/basex" "^5.6.1" + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/pbkdf2" "^5.6.1" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/sha2" "^5.6.1" + "@ethersproject/signing-key" "^5.6.2" + "@ethersproject/strings" "^5.6.1" + "@ethersproject/transactions" "^5.6.2" + "@ethersproject/wordlists" "^5.6.1" + "@ethersproject/json-wallets@5.6.0", "@ethersproject/json-wallets@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/json-wallets/-/json-wallets-5.6.0.tgz#4c2fc27f17e36c583e7a252fb938bc46f98891e5" @@ -405,6 +531,25 @@ aes-js "3.0.0" scrypt-js "3.0.1" +"@ethersproject/json-wallets@5.6.1", "@ethersproject/json-wallets@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/json-wallets/-/json-wallets-5.6.1.tgz#3f06ba555c9c0d7da46756a12ac53483fe18dd91" + integrity sha512-KfyJ6Zwz3kGeX25nLihPwZYlDqamO6pfGKNnVMWWfEVVp42lTfCZVXXy5Ie8IZTN0HKwAngpIPi7gk4IJzgmqQ== + dependencies: + "@ethersproject/abstract-signer" "^5.6.2" + "@ethersproject/address" "^5.6.1" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/hdnode" "^5.6.2" + "@ethersproject/keccak256" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/pbkdf2" "^5.6.1" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/random" "^5.6.1" + "@ethersproject/strings" "^5.6.1" + "@ethersproject/transactions" "^5.6.2" + aes-js "3.0.0" + scrypt-js "3.0.1" + "@ethersproject/keccak256@5.6.0", "@ethersproject/keccak256@>=5.0.0-beta.127", "@ethersproject/keccak256@^5.5.0", "@ethersproject/keccak256@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/keccak256/-/keccak256-5.6.0.tgz#fea4bb47dbf8f131c2e1774a1cecbfeb9d606459" @@ -413,6 +558,14 @@ "@ethersproject/bytes" "^5.6.0" js-sha3 "0.8.0" +"@ethersproject/keccak256@5.6.1", "@ethersproject/keccak256@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/keccak256/-/keccak256-5.6.1.tgz#b867167c9b50ba1b1a92bccdd4f2d6bd168a91cc" + integrity sha512-bB7DQHCTRDooZZdL3lk9wpL0+XuG3XLGHLh3cePnybsO3V0rdCAOQGpn/0R3aODmnTOOkCATJiD2hnL+5bwthA== + dependencies: + "@ethersproject/bytes" "^5.6.1" + js-sha3 "0.8.0" + "@ethersproject/logger@5.6.0", "@ethersproject/logger@>=5.0.0-beta.129", "@ethersproject/logger@^5.4.0", "@ethersproject/logger@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/logger/-/logger-5.6.0.tgz#d7db1bfcc22fd2e4ab574cba0bb6ad779a9a3e7a" @@ -425,6 +578,13 @@ dependencies: "@ethersproject/logger" "^5.6.0" +"@ethersproject/networks@5.6.4", "@ethersproject/networks@^5.6.3": + version "5.6.4" + resolved "https://registry.yarnpkg.com/@ethersproject/networks/-/networks-5.6.4.tgz#51296d8fec59e9627554f5a8a9c7791248c8dc07" + integrity sha512-KShHeHPahHI2UlWdtDMn2lJETcbtaJge4k7XSjDR9h79QTd6yQJmv6Cp2ZA4JdqWnhszAOLSuJEd9C0PRw7hSQ== + dependencies: + "@ethersproject/logger" "^5.6.0" + "@ethersproject/pbkdf2@5.6.0", "@ethersproject/pbkdf2@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/pbkdf2/-/pbkdf2-5.6.0.tgz#04fcc2d7c6bff88393f5b4237d906a192426685a" @@ -433,6 +593,14 @@ "@ethersproject/bytes" "^5.6.0" "@ethersproject/sha2" "^5.6.0" +"@ethersproject/pbkdf2@5.6.1", "@ethersproject/pbkdf2@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/pbkdf2/-/pbkdf2-5.6.1.tgz#f462fe320b22c0d6b1d72a9920a3963b09eb82d1" + integrity sha512-k4gRQ+D93zDRPNUfmduNKq065uadC2YjMP/CqwwX5qG6R05f47boq6pLZtV/RnC4NZAYOPH1Cyo54q0c9sshRQ== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/sha2" "^5.6.1" + "@ethersproject/properties@5.6.0", "@ethersproject/properties@>=5.0.0-beta.131", "@ethersproject/properties@^5.4.0", "@ethersproject/properties@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/properties/-/properties-5.6.0.tgz#38904651713bc6bdd5bdd1b0a4287ecda920fa04" @@ -465,6 +633,32 @@ bech32 "1.1.4" ws "7.4.6" +"@ethersproject/providers@5.6.8": + version "5.6.8" + resolved "https://registry.yarnpkg.com/@ethersproject/providers/-/providers-5.6.8.tgz#22e6c57be215ba5545d3a46cf759d265bb4e879d" + integrity sha512-Wf+CseT/iOJjrGtAOf3ck9zS7AgPmr2fZ3N97r4+YXN3mBePTG2/bJ8DApl9mVwYL+RpYbNxMEkEp4mPGdwG/w== + dependencies: + "@ethersproject/abstract-provider" "^5.6.1" + "@ethersproject/abstract-signer" "^5.6.2" + "@ethersproject/address" "^5.6.1" + "@ethersproject/base64" "^5.6.1" + "@ethersproject/basex" "^5.6.1" + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/constants" "^5.6.1" + "@ethersproject/hash" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/networks" "^5.6.3" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/random" "^5.6.1" + "@ethersproject/rlp" "^5.6.1" + "@ethersproject/sha2" "^5.6.1" + "@ethersproject/strings" "^5.6.1" + "@ethersproject/transactions" "^5.6.2" + "@ethersproject/web" "^5.6.1" + bech32 "1.1.4" + ws "7.4.6" + "@ethersproject/random@5.6.0", "@ethersproject/random@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/random/-/random-5.6.0.tgz#1505d1ab6a250e0ee92f436850fa3314b2cb5ae6" @@ -473,6 +667,14 @@ "@ethersproject/bytes" "^5.6.0" "@ethersproject/logger" "^5.6.0" +"@ethersproject/random@5.6.1", "@ethersproject/random@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/random/-/random-5.6.1.tgz#66915943981bcd3e11bbd43733f5c3ba5a790255" + integrity sha512-/wtPNHwbmng+5yi3fkipA8YBT59DdkGRoC2vWk09Dci/q5DlgnMkhIycjHlavrvrjJBkFjO/ueLyT+aUDfc4lA== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/rlp@5.6.0", "@ethersproject/rlp@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/rlp/-/rlp-5.6.0.tgz#55a7be01c6f5e64d6e6e7edb6061aa120962a717" @@ -481,6 +683,14 @@ "@ethersproject/bytes" "^5.6.0" "@ethersproject/logger" "^5.6.0" +"@ethersproject/rlp@5.6.1", "@ethersproject/rlp@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/rlp/-/rlp-5.6.1.tgz#df8311e6f9f24dcb03d59a2bac457a28a4fe2bd8" + integrity sha512-uYjmcZx+DKlFUk7a5/W9aQVaoEC7+1MOBgNtvNg13+RnuUwT4F0zTovC0tmay5SmRslb29V1B7Y5KCri46WhuQ== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/sha2@5.6.0", "@ethersproject/sha2@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/sha2/-/sha2-5.6.0.tgz#364c4c11cc753bda36f31f001628706ebadb64d9" @@ -490,6 +700,15 @@ "@ethersproject/logger" "^5.6.0" hash.js "1.1.7" +"@ethersproject/sha2@5.6.1", "@ethersproject/sha2@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/sha2/-/sha2-5.6.1.tgz#211f14d3f5da5301c8972a8827770b6fd3e51656" + integrity sha512-5K2GyqcW7G4Yo3uenHegbXRPDgARpWUiXc6RiF7b6i/HXUoWlb7uCARh7BAHg7/qT/Q5ydofNwiZcim9qpjB6g== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + hash.js "1.1.7" + "@ethersproject/signing-key@5.6.0", "@ethersproject/signing-key@^5.5.0", "@ethersproject/signing-key@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/signing-key/-/signing-key-5.6.0.tgz#4f02e3fb09e22b71e2e1d6dc4bcb5dafa69ce042" @@ -502,6 +721,18 @@ elliptic "6.5.4" hash.js "1.1.7" +"@ethersproject/signing-key@5.6.2", "@ethersproject/signing-key@^5.6.2": + version "5.6.2" + resolved "https://registry.yarnpkg.com/@ethersproject/signing-key/-/signing-key-5.6.2.tgz#8a51b111e4d62e5a62aee1da1e088d12de0614a3" + integrity sha512-jVbu0RuP7EFpw82vHcL+GP35+KaNruVAZM90GxgQnGqB6crhBqW/ozBfFvdeImtmb4qPko0uxXjn8l9jpn0cwQ== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + bn.js "^5.2.1" + elliptic "6.5.4" + hash.js "1.1.7" + "@ethersproject/solidity@5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/solidity/-/solidity-5.6.0.tgz#64657362a596bf7f5630bdc921c07dd78df06dc3" @@ -514,6 +745,18 @@ "@ethersproject/sha2" "^5.6.0" "@ethersproject/strings" "^5.6.0" +"@ethersproject/solidity@5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/solidity/-/solidity-5.6.1.tgz#5845e71182c66d32e6ec5eefd041fca091a473e2" + integrity sha512-KWqVLkUUoLBfL1iwdzUVlkNqAUIFMpbbeH0rgCfKmJp0vFtY4AsaN91gHKo9ZZLkC4UOm3cI3BmMV4N53BOq4g== + dependencies: + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/keccak256" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/sha2" "^5.6.1" + "@ethersproject/strings" "^5.6.1" + "@ethersproject/strings@5.6.0", "@ethersproject/strings@>=5.0.0-beta.130", "@ethersproject/strings@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/strings/-/strings-5.6.0.tgz#9891b26709153d996bf1303d39a7f4bc047878fd" @@ -523,6 +766,15 @@ "@ethersproject/constants" "^5.6.0" "@ethersproject/logger" "^5.6.0" +"@ethersproject/strings@5.6.1", "@ethersproject/strings@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/strings/-/strings-5.6.1.tgz#dbc1b7f901db822b5cafd4ebf01ca93c373f8952" + integrity sha512-2X1Lgk6Jyfg26MUnsHiT456U9ijxKUybz8IM1Vih+NJxYtXhmvKBcHOmvGqpFSVJ0nQ4ZCoIViR8XlRw1v/+Cw== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/constants" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/transactions@5.6.0", "@ethersproject/transactions@^5.0.0-beta.135", "@ethersproject/transactions@^5.4.0", "@ethersproject/transactions@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/transactions/-/transactions-5.6.0.tgz#4b594d73a868ef6e1529a2f8f94a785e6791ae4e" @@ -538,6 +790,21 @@ "@ethersproject/rlp" "^5.6.0" "@ethersproject/signing-key" "^5.6.0" +"@ethersproject/transactions@5.6.2", "@ethersproject/transactions@^5.6.2": + version "5.6.2" + resolved "https://registry.yarnpkg.com/@ethersproject/transactions/-/transactions-5.6.2.tgz#793a774c01ced9fe7073985bb95a4b4e57a6370b" + integrity sha512-BuV63IRPHmJvthNkkt9G70Ullx6AcM+SDc+a8Aw/8Yew6YwT51TcBKEp1P4oOQ/bP25I18JJr7rcFRgFtU9B2Q== + dependencies: + "@ethersproject/address" "^5.6.1" + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/constants" "^5.6.1" + "@ethersproject/keccak256" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/rlp" "^5.6.1" + "@ethersproject/signing-key" "^5.6.2" + "@ethersproject/units@5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/units/-/units-5.6.0.tgz#e5cbb1906988f5740254a21b9ded6bd51e826d9c" @@ -547,6 +814,15 @@ "@ethersproject/constants" "^5.6.0" "@ethersproject/logger" "^5.6.0" +"@ethersproject/units@5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/units/-/units-5.6.1.tgz#ecc590d16d37c8f9ef4e89e2005bda7ddc6a4e6f" + integrity sha512-rEfSEvMQ7obcx3KWD5EWWx77gqv54K6BKiZzKxkQJqtpriVsICrktIQmKl8ReNToPeIYPnFHpXvKpi068YFZXw== + dependencies: + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/constants" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/wallet@5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/wallet/-/wallet-5.6.0.tgz#33d11a806d783864208f348709a5a3badac8e22a" @@ -568,6 +844,27 @@ "@ethersproject/transactions" "^5.6.0" "@ethersproject/wordlists" "^5.6.0" +"@ethersproject/wallet@5.6.2": + version "5.6.2" + resolved "https://registry.yarnpkg.com/@ethersproject/wallet/-/wallet-5.6.2.tgz#cd61429d1e934681e413f4bc847a5f2f87e3a03c" + integrity sha512-lrgh0FDQPuOnHcF80Q3gHYsSUODp6aJLAdDmDV0xKCN/T7D99ta1jGVhulg3PY8wiXEngD0DfM0I2XKXlrqJfg== + dependencies: + "@ethersproject/abstract-provider" "^5.6.1" + "@ethersproject/abstract-signer" "^5.6.2" + "@ethersproject/address" "^5.6.1" + "@ethersproject/bignumber" "^5.6.2" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/hash" "^5.6.1" + "@ethersproject/hdnode" "^5.6.2" + "@ethersproject/json-wallets" "^5.6.1" + "@ethersproject/keccak256" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/random" "^5.6.1" + "@ethersproject/signing-key" "^5.6.2" + "@ethersproject/transactions" "^5.6.2" + "@ethersproject/wordlists" "^5.6.1" + "@ethersproject/web@5.6.0", "@ethersproject/web@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/web/-/web-5.6.0.tgz#4bf8b3cbc17055027e1a5dd3c357e37474eaaeb8" @@ -579,6 +876,17 @@ "@ethersproject/properties" "^5.6.0" "@ethersproject/strings" "^5.6.0" +"@ethersproject/web@5.6.1", "@ethersproject/web@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/web/-/web-5.6.1.tgz#6e2bd3ebadd033e6fe57d072db2b69ad2c9bdf5d" + integrity sha512-/vSyzaQlNXkO1WV+RneYKqCJwualcUdx/Z3gseVovZP0wIlOFcCE1hkRhKBH8ImKbGQbMl9EAAyJFrJu7V0aqA== + dependencies: + "@ethersproject/base64" "^5.6.1" + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/strings" "^5.6.1" + "@ethersproject/wordlists@5.6.0", "@ethersproject/wordlists@^5.6.0": version "5.6.0" resolved "https://registry.yarnpkg.com/@ethersproject/wordlists/-/wordlists-5.6.0.tgz#79e62c5276e091d8575f6930ba01a29218ded032" @@ -590,6 +898,17 @@ "@ethersproject/properties" "^5.6.0" "@ethersproject/strings" "^5.6.0" +"@ethersproject/wordlists@5.6.1", "@ethersproject/wordlists@^5.6.1": + version "5.6.1" + resolved "https://registry.yarnpkg.com/@ethersproject/wordlists/-/wordlists-5.6.1.tgz#1e78e2740a8a21e9e99947e47979d72e130aeda1" + integrity sha512-wiPRgBpNbNwCQFoCr8bcWO8o5I810cqO6mkdtKfLKFlLxeCWcnzDi4Alu8iyNzlhYuS9npCwivMbRWF19dyblw== + dependencies: + "@ethersproject/bytes" "^5.6.1" + "@ethersproject/hash" "^5.6.1" + "@ethersproject/logger" "^5.6.0" + "@ethersproject/properties" "^5.6.0" + "@ethersproject/strings" "^5.6.1" + "@fastify/ajv-compiler@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@fastify/ajv-compiler/-/ajv-compiler-1.1.0.tgz#5ce80b1fc8bebffc8c5ba428d5e392d0f9ed10a1" @@ -2527,6 +2846,11 @@ bn.js@^5.0.0, bn.js@^5.1.1, bn.js@^5.1.2, bn.js@^5.2.0: resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002" integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw== +bn.js@^5.2.1: + version "5.2.1" + resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70" + integrity sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ== + body-parser@1.19.2: version "1.19.2" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.2.tgz#4714ccd9c157d44797b8b5607d72c0b89952f26e" @@ -4358,6 +4682,42 @@ ethers@^5.0.1, ethers@^5.0.2, ethers@^5.4.6, ethers@^5.5.2: "@ethersproject/web" "5.6.0" "@ethersproject/wordlists" "5.6.0" +ethers@^5.6.4: + version "5.6.9" + resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.6.9.tgz#4e12f8dfcb67b88ae7a78a9519b384c23c576a4d" + integrity sha512-lMGC2zv9HC5EC+8r429WaWu3uWJUCgUCt8xxKCFqkrFuBDZXDYIdzDUECxzjf2BMF8IVBByY1EBoGSL3RTm8RA== + dependencies: + "@ethersproject/abi" "5.6.4" + "@ethersproject/abstract-provider" "5.6.1" + "@ethersproject/abstract-signer" "5.6.2" + "@ethersproject/address" "5.6.1" + "@ethersproject/base64" "5.6.1" + "@ethersproject/basex" "5.6.1" + "@ethersproject/bignumber" "5.6.2" + "@ethersproject/bytes" "5.6.1" + "@ethersproject/constants" "5.6.1" + "@ethersproject/contracts" "5.6.2" + "@ethersproject/hash" "5.6.1" + "@ethersproject/hdnode" "5.6.2" + "@ethersproject/json-wallets" "5.6.1" + "@ethersproject/keccak256" "5.6.1" + "@ethersproject/logger" "5.6.0" + "@ethersproject/networks" "5.6.4" + "@ethersproject/pbkdf2" "5.6.1" + "@ethersproject/properties" "5.6.0" + "@ethersproject/providers" "5.6.8" + "@ethersproject/random" "5.6.1" + "@ethersproject/rlp" "5.6.1" + "@ethersproject/sha2" "5.6.1" + "@ethersproject/signing-key" "5.6.2" + "@ethersproject/solidity" "5.6.1" + "@ethersproject/strings" "5.6.1" + "@ethersproject/transactions" "5.6.2" + "@ethersproject/units" "5.6.1" + "@ethersproject/wallet" "5.6.2" + "@ethersproject/web" "5.6.1" + "@ethersproject/wordlists" "5.6.1" + ethjs-unit@0.1.6: version "0.1.6" resolved "https://registry.yarnpkg.com/ethjs-unit/-/ethjs-unit-0.1.6.tgz#c665921e476e87bce2a9d588a6fe0405b2c41699" From f1eea79cd9c8681329735ddfea0128620853d144 Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Thu, 19 May 2022 14:17:15 +1200 Subject: [PATCH 08/11] Update the relayer to work with new image + old image (#87) Restart pod when event proof is not found --- .github/workflows/image-builder.yml | 1 + README.md | 2 +- scripts/subscribeEventProof.js | 181 +++++++++++----------------- 3 files changed, 72 insertions(+), 112 deletions(-) diff --git a/.github/workflows/image-builder.yml b/.github/workflows/image-builder.yml index 7139469..abc6eea 100644 --- a/.github/workflows/image-builder.yml +++ b/.github/workflows/image-builder.yml @@ -6,6 +6,7 @@ on: push: branches: - "main" + - "azalea" paths: - "**.js" - "**.json" diff --git a/README.md b/README.md index c21427d..10c86b9 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ const ROPSTEN_V1 = { const ROPSTEN_V2 = { bridge: "0x452b8dd7b00D51e48cEF6254a48B7426d44658B8", peg: "0x4C411B3Bf36D6DE908C6f4256a72B85E3f2B00bF", - wcennz: "0xcdA767469D434c14D320e1A78D5b612B72B52bf0", + wcennz: "0x673F0244bd0AEbd9918ef089f75C503Df9bfBb38", } ``` diff --git a/scripts/subscribeEventProof.js b/scripts/subscribeEventProof.js index c7ea683..5ce3fac 100644 --- a/scripts/subscribeEventProof.js +++ b/scripts/subscribeEventProof.js @@ -4,11 +4,11 @@ require("dotenv").config(); const logger = require('./logger'); const { curly } = require("node-libcurl"); const mongoose = require('mongoose'); -const { EventProcessed, LastBlockScan } = require('../src/mongo/models'); +const { EventProcessed } = require('../src/mongo/models'); const ethers = require('ethers'); const bridgeAbi = require("../abi/CENNZnetBridge.json").abi; -const timeoutMs = 20000; +const timeoutMs = 40000; const BUFFER = 1000; // Ignore if validator public key is 0x000.. const IGNORE_KEY = '0x000000000000000000000000000000000000000000000000000000000000000000'; @@ -60,62 +60,62 @@ async function sendSlackNotification(message) { // Submit the event proof on Ethereum Bridge contract async function getEventPoofAndSubmit(api, eventId, bridge, txExecutor, newValidatorSetId, blockHash, provider) { const eventExistsOnEth = await bridge.eventIds(eventId.toString()); + if (eventExistsOnEth) return; // return if event proof already exist on Ethereum const eventProof = await withTimeout(api.derive.ethBridge.eventProof(eventId), timeoutMs); - if (eventProof && !eventExistsOnEth) { - const newValidators = await extractNewValidators(api, blockHash); - logger.info(`IMP Sending setValidators tx with the account: ${txExecutor.address}`); - logger.info(`IMP Parameters :::`); - logger.info(`IMP newValidators:${newValidators}`); - logger.info(`IMP newValidatorSetId: ${newValidatorSetId}`); - logger.info(`IMP event proof::${JSON.stringify(eventProof)}`); - const currentValidators = await extractCurrentValidators(api, blockHash); - logger.info(`IMP currentValidators:${currentValidators}`); - const proof = { - eventId: eventProof.eventId, - validatorSetId: eventProof.validatorSetId, - r: eventProof.r, - s: eventProof.s, - v: eventProof.v, - validators: currentValidators - }; - try { - const gasPrice = await provider.getGasPrice(); - logger.info('gas price::', gasPrice.toString()); - // Take 5 percent of current gas price - const percentGasPrice = gasPrice.mul(5).div(100); - logger.info('percentGasPrice:',percentGasPrice.toString()); - const increasedGasPrice = gasPrice.add(percentGasPrice); - logger.info('Gas price nw;:', gasPrice.toString()); - - const gasEstimated = await bridge.estimateGas.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: 5000000, gasPrice: increasedGasPrice}); - const tx = await bridge.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: gasEstimated.add(BUFFER), gasPrice: increasedGasPrice}); - await tx.wait(); // wait till tx is mined - logger.info(JSON.stringify(tx)); - await updateLastEventProcessed(eventId, blockHash.toString()); - const balance = await provider.getBalance(txExecutor.address); - logger.info(`IMP Balance is: ${balance}`); - - logger.info(`IMP Gas price: ${gasPrice.toString()}`); - const gasRequired = gasEstimated.mul(gasPrice); - logger.info(`IMP Gas required: ${gasRequired.toString()}`); - if (balance.lt(gasRequired.mul(2))) { - const message = ` 🚨 To keep the validator relayer running, topup the eth account ${txExecutor.address} on CENNZnets ${process.env.NETWORK} chain`; - await sendSlackNotification(message); - } - } catch (e) { - logger.warn('Something went wrong:'); - logger.error(`IMP Error: ${e.stack}`); - // send slack notification when proof submission fails - const message = ` 🚨 Issue while submitting validator set on ethereum bridge - proof: ${JSON.stringify(proof)} - newValidators: ${newValidators} - newValidatorSetId: ${newValidatorSetId} - on CENNZnets ${process.env.NETWORK} chain`; - await sendSlackNotification(message); - } - } else if (!eventProof){ + if (!eventProof) { logger.info(`IMP Could not retrieve event proof for event id ${eventId} from derived query api.derive.ethBridge.eventProof at ${timeoutMs} timeout`); + process.exit(1); + } + const newValidators = await extractNewValidators(api, blockHash); + logger.info(`IMP Sending setValidators tx with the account: ${txExecutor.address}`); + logger.info(`IMP Parameters :::`); + logger.info(`IMP newValidators:${newValidators}`); + logger.info(`IMP newValidatorSetId: ${newValidatorSetId}`); + logger.info(`IMP event proof::${JSON.stringify(eventProof)}`); + const currentValidators = await extractCurrentValidators(api, blockHash); + logger.info(`IMP currentValidators:${currentValidators}`); + const proof = { + eventId: eventProof.eventId, + validatorSetId: eventProof.validatorSetId, + r: eventProof.r, + s: eventProof.s, + v: eventProof.v, + validators: currentValidators + }; + try { + const gasPrice = await provider.getGasPrice(); + logger.info('gas price::', gasPrice.toString()); + // Take 5 percent of current gas price + const percentGasPrice = gasPrice.mul(5).div(100); + logger.info('percentGasPrice:',percentGasPrice.toString()); + const increasedGasPrice = gasPrice.add(percentGasPrice); + logger.info('Gas price nw;:', gasPrice.toString()); + + const gasEstimated = await bridge.estimateGas.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: 5000000, gasPrice: increasedGasPrice}); + + logger.info(JSON.stringify(await bridge.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: gasEstimated.add(BUFFER), gasPrice: increasedGasPrice}))); + await updateLastEventProcessed(eventId, blockHash.toString()); + const balance = await provider.getBalance(txExecutor.address); + logger.info(`IMP Balance is: ${balance}`); + + logger.info(`IMP Gas price: ${gasPrice.toString()}`); + const gasRequired = gasEstimated.mul(gasPrice); + logger.info(`IMP Gas required: ${gasRequired.toString()}`); + if (balance.lt(gasRequired.mul(2))) { + const message = ` 🚨 To keep the validator relayer running, topup the eth account ${txExecutor.address} on CENNZnets ${process.env.NETWORK} chain`; + await sendSlackNotification(message); + } + } catch (e) { + logger.warn('Something went wrong:'); + logger.error(`IMP Error: ${e.stack}`); + // send slack notification when proof submission fails + const message = ` 🚨 Issue while submitting validator set on ethereum bridge + proof: ${JSON.stringify(proof)} + newValidators: ${newValidators} + newValidatorSetId: ${newValidatorSetId} + on CENNZnets ${process.env.NETWORK} chain`; + await sendSlackNotification(message); } } @@ -142,15 +142,6 @@ async function main (networkName, bridgeContractAddress) { process.env.INFURA_API_KEY ); - const setProcessedBlock = process.env.BOOTSTRAP; - // for the first start set processed block - if (setProcessedBlock === 'true') { - const currentFinalizedHeadHash = await api.rpc.chain.getFinalizedHead(); - const block = await api.rpc.chain.getBlock(currentFinalizedHeadHash); - const blockNo = block.block.header.number.toString(); - await updateBlockScanned({ processedBlock: blockNo }); - } - let wallet = new ethers.Wallet(process.env.ETH_ACCOUNT_KEY, infuraProvider); const bridge = new ethers.Contract(bridgeContractAddress, bridgeAbi, wallet); @@ -175,7 +166,6 @@ async function main (networkName, bridgeContractAddress) { for (let i = scanFromEvent; i <= parseInt(lastEventProofIdFromCennznet);i++ ) { console.log('At Event id:',i); const eventProof = await withTimeout(api.derive.ethBridge.eventProof(i), 10000); - if (eventProof && eventProof.tag === 'sys:authority-change') { const checkEventExistsOnEth = await bridge.eventIds(i.toString()); if (!checkEventExistsOnEth) { @@ -192,45 +182,22 @@ async function main (networkName, bridgeContractAddress) { await api.rpc.chain .subscribeFinalizedHeads(async (head) => { - const finalizedBlockAt = head.number.toString(); - logger.info(` finalizedBlockAt::${finalizedBlockAt}`); - const update = { finalizedBlock: finalizedBlockAt }; - await updateBlockScanned(update); - }); - - while (true) { - const blockScanned = await LastBlockScan.findOne({}); - if (blockScanned) { - const {processedBlock, finalizedBlock} = blockScanned; - const processBlockNumber = parseInt(processedBlock); - const finalizedBlockNumber = parseInt(finalizedBlock); - if (processBlockNumber < finalizedBlockNumber) { - // processes block + 1 will be new block to process - for (let blockNumber = processBlockNumber+1; blockNumber < finalizedBlock; blockNumber++) { - logger.info(`At blocknumber: ${blockNumber}`); - - const blockHash = await api.rpc.chain.getBlockHash(blockNumber); - const events = await api.query.system.events.at(blockHash); - events.map(async ({event}) => { - const { section, method, data } = event; - if (section === 'ethBridge' && method === 'AuthoritySetChange') { - const dataFetched = data.toJSON(); - const eventIdFound = dataFetched[0]; - const newValidatorSetId = parseInt(dataFetched[1]); - logger.info(`IMP Event found at block ${blockNumber} hash ${blockHash} event id ${eventIdFound}`); - await getEventPoofAndSubmit(api, eventIdFound, bridge, wallet, newValidatorSetId.toString(), blockHash, infuraProvider); - } - }); - await updateBlockScanned({ processedBlock: blockNumber.toString() }); + const blockNumber = head.number.toNumber(); + logger.info(`At blocknumber: ${blockNumber}`); + + const blockHash = head.hash.toString(); + const events = await api.query.system.events.at(blockHash); + events.map(async ({event}) => { + const { section, method, data } = event; + if (section === 'ethBridge' && method === 'AuthoritySetChange') { + const dataFetched = data.toHuman(); + const eventIdFound = dataFetched[0]; + const newValidatorSetId = parseInt(dataFetched[1]); + logger.info(`IMP Event found at block ${blockNumber} hash ${blockHash} event id ${eventIdFound}`); + await getEventPoofAndSubmit(api, eventIdFound, bridge, wallet, newValidatorSetId.toString(), blockHash, infuraProvider); } - } - } - await sleep(500); - } -} - -function sleep(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)); + }) + }); } async function withTimeout(promise, timeoutMs) { @@ -247,11 +214,3 @@ async function withTimeout(promise, timeoutMs) { const networkName = process.env.NETWORK; const bridgeContractAddress = process.env.BRIDGE_CONTRACT; main(networkName, bridgeContractAddress).catch((err) => console.log(err)); - - -async function updateBlockScanned(update) { - const filter = {}; - const options = { upsert: true, new: true, setDefaultsOnInsert: true }; // create new if record does not exist, else update - await LastBlockScan.updateOne(filter, update, options); - logger.info(`Updated the block in db..${JSON.stringify(update)}`); -} From 0a0ffb2bb2669650046e4f57c1d4a007879040b5 Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Mon, 8 May 2023 09:29:58 +0400 Subject: [PATCH 09/11] Remove azalea branch from github workflow --- .github/workflows/image-builder.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/image-builder.yml b/.github/workflows/image-builder.yml index abc6eea..7139469 100644 --- a/.github/workflows/image-builder.yml +++ b/.github/workflows/image-builder.yml @@ -6,7 +6,6 @@ on: push: branches: - "main" - - "azalea" paths: - "**.js" - "**.json" From 1d24471d5108125952893c67c82d6c387ad449ae Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Mon, 8 May 2023 14:36:24 +0400 Subject: [PATCH 10/11] On random instance return null for event proof --- scripts/subscribeEventProof.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/subscribeEventProof.js b/scripts/subscribeEventProof.js index 5ce3fac..e5ab135 100644 --- a/scripts/subscribeEventProof.js +++ b/scripts/subscribeEventProof.js @@ -203,7 +203,9 @@ async function main (networkName, bridgeContractAddress) { async function withTimeout(promise, timeoutMs) { return Promise.race ([ promise, - new Promise ((resolve) => { + new Promise ((resolve, reject) => { + const randomValue = Math.random(); + if (networkName.toLowerCase() === 'nikau' && randomValue > 0.5) resolve(null); setTimeout(() => { resolve(null); }, timeoutMs); From e82717e5dac64711fbd6393497b3d901236d7905 Mon Sep 17 00:00:00 2001 From: KarishmaBothara Date: Mon, 8 May 2023 14:41:31 +0400 Subject: [PATCH 11/11] Fix lint err --- scripts/subscribeEventProof.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/subscribeEventProof.js b/scripts/subscribeEventProof.js index e5ab135..8720216 100644 --- a/scripts/subscribeEventProof.js +++ b/scripts/subscribeEventProof.js @@ -203,7 +203,7 @@ async function main (networkName, bridgeContractAddress) { async function withTimeout(promise, timeoutMs) { return Promise.race ([ promise, - new Promise ((resolve, reject) => { + new Promise ((resolve) => { const randomValue = Math.random(); if (networkName.toLowerCase() === 'nikau' && randomValue > 0.5) resolve(null); setTimeout(() => {