From 003a59c6aecce142c2245de13b6ecffd7b4fe0d9 Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Tue, 29 Oct 2019 21:07:11 +0300 Subject: [PATCH] Wrote tests for removing validator --- tests/.mocharc.yml | 2 + tests/config.json | 5 ++ tests/test/bncToEth.js | 2 - tests/test/ethToBnc.js | 9 +--- tests/test/index.js | 109 ++++++++++++++++++++++++++++++++++++--- tests/test/utils/user.js | 2 + 6 files changed, 113 insertions(+), 16 deletions(-) diff --git a/tests/.mocharc.yml b/tests/.mocharc.yml index f085534..c7ff00a 100644 --- a/tests/.mocharc.yml +++ b/tests/.mocharc.yml @@ -1,4 +1,6 @@ exit: true +timeout: 60000 +bail: true reporter: mocha-multi-reporters reporter-option: - configFile=./reportersConfig.json diff --git a/tests/config.json b/tests/config.json index fa49f75..8307d15 100644 --- a/tests/config.json +++ b/tests/config.json @@ -15,5 +15,10 @@ "ethAddress": "0xad6c8127143032d843a260c5d379d8d9b3d51f15", "bncAddress": "tbnb12epcy4p7ktas0nlyrfuektcyh0e83dwzuq73f4" } + ], + "validators": [ + "0x99Eb3D86663c6Db090eFFdBC20510Ca9f836DCE3", + "0xAa006899B0EC407De930bA8A166DEfe59bBfd3DC", + "0x6352e3e6038e05b9da00C84AE851308f9774F883" ] } diff --git a/tests/test/bncToEth.js b/tests/test/bncToEth.js index 303ce4e..5d9d1fc 100644 --- a/tests/test/bncToEth.js +++ b/tests/test/bncToEth.js @@ -7,7 +7,6 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => { let ethBalances before(async function () { - this.timeout(60000) users = usersFunc() foreignBridgeAddress = foreignBridgeAddressFunc() ethBalances = await Promise.all(users.map(user => user.getEthBalance())) @@ -16,7 +15,6 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => { }) it('should make coorect exchange transactions on eth side', async function () { - this.timeout(60000) for (let i = 0; i < 3; i++) { do { const user = users[i] diff --git a/tests/test/ethToBnc.js b/tests/test/ethToBnc.js index 25a7fd3..f442263 100644 --- a/tests/test/ethToBnc.js +++ b/tests/test/ethToBnc.js @@ -2,8 +2,6 @@ const assert = require('assert') const { getSequence } = require('./utils/bncController') const { waitPromise } = require('./utils/wait') -const usersConfig = require('../config').users - const { HOME_BRIDGE_ADDRESS } = process.env module.exports = (usersFunc, foreignBridgeAddressFunc) => { @@ -15,7 +13,6 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => { let bncBridgeSequence before(async function () { - this.timeout(60000) users = usersFunc() foreignBridgeAddress = foreignBridgeAddressFunc() ethBalances = await Promise.all(users.map(user => user.getEthBalance())) @@ -26,11 +23,10 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => { }) it('should accept exchange requests', async function () { - this.timeout(60000) await Promise.all(users.map((user, i) => user.exchangeEth(5 + i))) const newEthBalances = await Promise.all(users.map(user => user.getEthBalance())) for (let i = 0; i < 3; i++) { - assert(newEthBalances[i] === ethBalances[i] - 5 - i, `Balance of ${usersConfig[i].ethAddress} did not updated as expected`) + assert.strictEqual(newEthBalances[i], ethBalances[i] - 5 - i, `Balance of ${users[i].ethAddress} did not updated as expected`) } }) @@ -40,10 +36,9 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => { }) it('should make correct exchange transaction', async function () { - this.timeout(60000) const newBncBalances = await Promise.all(users.map(user => user.getBncBalance())) for (let i = 0; i < 3; i++) { - assert(newBncBalances[i] === bncBalances[i] + 5 + i, `Balance of ${usersConfig[i].bncAddress} did not updated as expected`) + 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 a6dafec..0b3628b 100644 --- a/tests/test/index.js +++ b/tests/test/index.js @@ -1,43 +1,138 @@ +const assert = require('assert') + const createController = require('./utils/proxyController') const createUser = require('./utils/user') -const { waitPromise } = require('./utils/wait') +const { waitPromise, delay } = require('./utils/wait') +const { getBalance } = require('./utils/bncController') const testEthToBnc = require('./ethToBnc') const testBncToEth = require('./bncToEth') const usersConfig = require('../config').users +const validatorsConfig = require('../config').validators const { FOREIGN_PRIVATE_KEY } = process.env -let { getInfo } = createController(1) +const controller1 = createController(1) +const controller2 = createController(2) +const controller3 = createController(3) describe('bridge tests', function () { let users let foreignPrefundedUser let info + let prevForeignBridgeBalance + let prevForeignBridgeAddress - before(async function() { - this.timeout(60000) + before(async function () { users = await usersConfig.seqMap(user => createUser(user.privateKey)) }) describe('generation of initial epoch keys', function () { before(async function () { - this.timeout(60000) foreignPrefundedUser = await createUser(FOREIGN_PRIVATE_KEY) }) it('should generate keys', async function () { this.timeout(120000) - info = await waitPromise(getInfo, info => info.epoch === 1) + info = await waitPromise(controller1.getInfo, info => info.epoch === 1) }) after(async function () { - this.timeout(60000) await foreignPrefundedUser.transferBnc(info.foreignBridgeAddress, 50, 0.1) }) }) testEthToBnc(() => users, () => info.foreignBridgeAddress) testBncToEth(() => users, () => info.foreignBridgeAddress) + + describe('remove validator', function () { + before(function () { + prevForeignBridgeBalance = info.foreignBalanceTokens + prevForeignBridgeAddress = info.foreignBridgeAddress + }) + + it('should start voting process', async function () { + await controller1.voteStartVoting() + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'ready', 'Should not change state after one vote') + + await controller2.voteStartVoting() + info = await waitPromise(controller1.getInfo, info => info.bridgeStatus === 'voting') + assert.deepStrictEqual(info.epoch, 1, 'Current epoch is not set correctly') + assert.deepStrictEqual(info.nextEpoch, 2, 'Next epoch is not set correctly') + assert.deepStrictEqual(info.nextValidators, validatorsConfig, 'Next validators are not set correctly') + + await controller3.voteStartVoting() + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'voting', 'Should not do anything after third vote') + assert.deepStrictEqual(info.epoch, 1, 'Current epoch is not set correctly') + assert.deepStrictEqual(info.nextEpoch, 2, 'Next epoch is not set correctly') + assert.deepStrictEqual(info.nextValidators, validatorsConfig, 'Incorrect set of next validators after third vote') + }) + + it('should remove validator', async function () { + await controller1.voteRemoveValidator(validatorsConfig[1]) + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'voting', 'Should not change state after one vote') + assert.deepStrictEqual(info.validators, validatorsConfig, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, validatorsConfig, 'Next validators are not set correctly') + + await controller2.voteRemoveValidator(validatorsConfig[1]) + info = await waitPromise(controller1.getInfo, info => info.nextValidators.length === 2) + assert.deepStrictEqual(info.validators, validatorsConfig, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, [ validatorsConfig[0], validatorsConfig[2] ], 'Next validators are not set correctly') + + await controller3.voteRemoveValidator(validatorsConfig[1]) + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'voting', 'Should not do anything after third vote') + assert.deepStrictEqual(info.epoch, 1, 'Current epoch is not set correctly') + assert.deepStrictEqual(info.nextEpoch, 2, 'Next epoch is not set correctly') + assert.deepStrictEqual(info.validators, validatorsConfig, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, [ validatorsConfig[0], validatorsConfig[2] ], 'Incorrect set of next validators after third vote') + }) + + it('should start keygen process', async function () { + await controller1.voteStartKeygen() + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'voting', 'Should not change state after one vote') + + await controller2.voteStartKeygen() + info = await waitPromise(controller1.getInfo, info => info.bridgeStatus === 'keygen') + + await controller3.voteStartKeygen() + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'keygen', 'Should not do anything after third vote') + assert.deepStrictEqual(info.epoch, 1, 'Current epoch is not set correctly') + assert.deepStrictEqual(info.nextEpoch, 2, 'Next epoch is not set correctly') + assert.deepStrictEqual(info.validators, validatorsConfig, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, [ validatorsConfig[0], validatorsConfig[2] ], 'Incorrect set of next validators after third vote') + }) + + it('should finish keygen process and start funds transfer', async function () { + this.timeout(120000) + info = await waitPromise(controller1.getInfo, info => info.bridgeStatus === 'funds_transfer') + }) + + it('should transfer all funds to new account and start new epoch', async function () { + this.timeout(300000) + info = await waitPromise(controller1.getInfo, info => info.epoch === 2) + assert.deepStrictEqual(info.validators, [ validatorsConfig[0], validatorsConfig[2] ], 'Incorrect set of validators in epoch 2') + assert.strictEqual(info.nextEpoch, 2, 'Incorrect next epoch') + assert.strictEqual(info.bridgeStatus, 'ready', 'Incorrect bridge state in new epoch') + const prevBalance = await getBalance(prevForeignBridgeAddress) + const newBalance = await getBalance(info.foreignBridgeAddress) + assert.strictEqual(prevBalance, 0, "Did not transfer all funds") + assert.strictEqual(newBalance, prevForeignBridgeBalance, "Funds are lost somewhere") + }) + }) + + testEthToBnc(() => users, () => info.foreignBridgeAddress) + testBncToEth(() => users, () => info.foreignBridgeAddress) }) diff --git a/tests/test/utils/user.js b/tests/test/utils/user.js index c2889a3..8294581 100644 --- a/tests/test/utils/user.js +++ b/tests/test/utils/user.js @@ -21,6 +21,8 @@ module.exports = async function (privateKey) { const bncClient = await createBncClient(privateKey) return { + ethAddress, + bncAddress, getEthBalance: async function () { const balance = await token.balanceOf(ethAddress) return parseFloat(new BN(balance).dividedBy(10 ** 18).toFixed(8, 3))