diff --git a/tests/.env b/tests/.env index d6c7a4b..e54b083 100644 --- a/tests/.env +++ b/tests/.env @@ -3,6 +3,8 @@ HOME_BRIDGE_ADDRESS=0x44c158FE850821ae69DaF37AADF5c539e9d0025B HOME_TOKEN_ADDRESS=0xd5fE0D28e058D375b0b038fFbB446Da37E85fFdc HOME_PRIVATE_KEY=e2aeb24eaa63102d0c0821717c3b6384abdabd7af2ad4ec8e650dce300798b27 +SIDE_RPC_URL=http://ganache_side:8545 + FOREIGN_URL=https://testnet-dex.binance.org/ FOREIGN_CHAIN_ID=Binance-Chain-Nile FOREIGN_ASSET=KFT-94F diff --git a/tests/run.sh b/tests/run.sh index a4f7090..a81a6e6 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -11,6 +11,7 @@ docker create --name tests \ tests $@ docker network connect blockchain_home tests +docker network connect blockchain_side tests docker network connect validator1_test_network tests docker network connect validator2_test_network tests docker network connect validator3_test_network tests diff --git a/tests/test/ethToBncWithRestart.js b/tests/test/ethToBncWithRestart.js new file mode 100644 index 0000000..ed6c3f9 --- /dev/null +++ b/tests/test/ethToBncWithRestart.js @@ -0,0 +1,74 @@ +const assert = require('assert') + +const { getBncSequence } = require('./utils/bncController') +const { waitPromise, delay, seqMap } = require('./utils/wait') + +const { controller1 } = require('./utils/proxyController') +const { getNonce } = require('./utils/sideController') +const { signerController1, signerController2 } = require('./utils/signerController') + +const { validators } = require('../config') + +const { HOME_BRIDGE_ADDRESS } = process.env + +module.exports = (getUsers, newAttempt) => { + describe('exchange of tokens in eth => bnc direction with restart', function () { + let info + let users + let ethBalances + let bncBalances + let bncBridgeSequence + let validatorNonces + let newValidatorNonces + + before(async function () { + users = getUsers() + info = await controller1.getInfo() + ethBalances = await Promise.all(users.map((user) => user.getErcBalance())) + bncBalances = await seqMap(users, (user) => user.getBepBalance()) + validatorNonces = await Promise.all(validators.map(getNonce)) + + bncBridgeSequence = await getBncSequence(info.foreignBridgeAddress) + await Promise.all(users.map((user, i) => user.approveErc(HOME_BRIDGE_ADDRESS, 5 + i))) + }) + + it('should accept exchange requests', async function () { + await Promise.all(users.map((user, i) => user.exchangeErc(5 + i))) + const newEthBalances = await Promise.all(users.map((user) => user.getErcBalance())) + for (let i = 0; i < 3; i += 1) { + assert.strictEqual(newEthBalances[i], ethBalances[i] - 5 - i, `Balance of ${users[i].ethAddress} did not updated as expected`) + } + }) + + it('should start signing transaction', async function () { + this.timeout(120000) + newValidatorNonces = await waitPromise( + () => Promise.all(validators.map(getNonce)), + (nonces) => nonces[0] > validatorNonces[0] + 2 + || nonces[1] > validatorNonces[1] + 2 + || nonces[2] > validatorNonces[2] + 2 + ) + }) + + it('should restart signature generation and regenerate signature properly', async function () { + this.timeout(360000) + if (newValidatorNonces[0] > validatorNonces[0] + 2) { + await signerController1.restart(newAttempt) + } else { + await signerController2.restart(newAttempt) + } + await waitPromise( + () => getBncSequence(info.foreignBridgeAddress), + (sequence) => sequence === bncBridgeSequence + 1 + ) + }) + + it('should make correct exchange transaction', async function () { + await delay(10000) + const newBncBalances = await Promise.all(users.map((user) => user.getBepBalance())) + for (let i = 0; i < 3; i += 1) { + assert.strictEqual(newBncBalances[i], bncBalances[i] + 5 + i, `Balance of ${users[i].bncAddress} did not updated as expected`) + } + }) + }) +} diff --git a/tests/test/index.js b/tests/test/index.js index 379689c..cfaa4ea 100644 --- a/tests/test/index.js +++ b/tests/test/index.js @@ -4,6 +4,7 @@ const createUser = require('./utils/user') const { waitPromise, seqMap } = require('./utils/wait') const testEthToBnc = require('./ethToBnc') +const testEthToBncWithRestart = require('./ethToBncWithRestart') const testBncToEth = require('./bncToEth') const testRemoveValidator = require('./removeValidator') const testAddValidator = require('./addValidator') @@ -80,9 +81,11 @@ describe('bridge tests', function () { testEthToBnc(() => users) testBncToEth(() => users) + testEthToBncWithRestart(() => users, 99) testChangeThreshold(3) testEthToBnc(() => users) testBncToEth(() => users) + testEthToBncWithRestart(() => users, 2) }) diff --git a/tests/test/utils/sideController.js b/tests/test/utils/sideController.js new file mode 100644 index 0000000..24fac5e --- /dev/null +++ b/tests/test/utils/sideController.js @@ -0,0 +1,11 @@ +const ethers = require('ethers') + +const { SIDE_RPC_URL } = process.env + +const provider = new ethers.providers.JsonRpcProvider(SIDE_RPC_URL) + +module.exports = { + async getNonce(address) { + return await provider.getTransactionCount(address) + } +} diff --git a/tests/test/utils/signerController.js b/tests/test/utils/signerController.js new file mode 100644 index 0000000..12cfb1c --- /dev/null +++ b/tests/test/utils/signerController.js @@ -0,0 +1,22 @@ +const axios = require('axios') + +function createController(validatorId) { + const url = `http://validator${validatorId}_signer_1:8001/` + + const sideClient = axios.create({ + baseURL: url, + timeout: 10000 + }) + + return { + async restart(attempt) { + return (await sideClient.get(`/restart/${attempt}`)).data + } + } +} + +module.exports = { + signerController1: createController(1), + signerController2: createController(2), + signerController3: createController(3) +}