From ec9c8e10c2e82777bf278dd26452ba36bfc0026d Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Thu, 14 Nov 2019 13:30:23 +0300 Subject: [PATCH] Tests coverage for closing bridge --- tests/run.sh | 5 +++ tests/test/addValidator.js | 58 +++++++++++++++++++++++------ tests/test/changeThreshold.js | 37 +++++++++--------- tests/test/index.js | 13 ++++++- tests/test/removeValidator.js | 27 ++++++++------ tests/test/utils/proxyController.js | 3 ++ 6 files changed, 101 insertions(+), 42 deletions(-) diff --git a/tests/run.sh b/tests/run.sh index 7889847..c0ca1b2 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -22,3 +22,8 @@ docker start -a tests || true echo "Saving test results" docker cp "tests:/tests/results.xml" "./tests/results.xml" > /dev/null 2>&1 || true + +echo "Killing all remaining docker containers" +docker kill $(docker ps | grep validator[1-3]_ | awk '{print $1}') > /dev/null 2>&1 || true +docker kill ganache_home ganache_side > /dev/null 2>&1 || true +docker kill $(docker ps | grep binance-testnet_ | awk '{print $1}') > /dev/null 2>&1 || true diff --git a/tests/test/addValidator.js b/tests/test/addValidator.js index cad94c5..c947d1d 100644 --- a/tests/test/addValidator.js +++ b/tests/test/addValidator.js @@ -17,27 +17,32 @@ module.exports = (newValidator) => { nextValidators = [...initialInfo.validators, newValidator] }) - it('should start voting process', async function () { + it('should start closing epoch 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 controller3.voteStartVoting() - info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.bridgeStatus === 'voting') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.bridgeStatus === 'closing_epoch') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, '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 duplicate vote') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.bridgeStatus, 'closing_epoch', 'Should not do anything after duplicate vote') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, 'Incorrect set of next validators after third vote') }) + it('should finish close epoch process and start voting process', async function () { + this.timeout(120000) + info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.bridgeStatus === 'voting') + }) + it('should add validator', async function () { await controller1.voteAddValidator(newValidator) await delay(5000) @@ -58,12 +63,43 @@ module.exports = (newValidator) => { await delay(5000) info = await controller1.getInfo() assert.strictEqual(info.bridgeStatus, 'voting', 'Should not do anything after duplicate vote') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, nextValidators, 'Incorrect set of next validators after third vote') }) + it('should change close epoch', async function () { + await controller1.voteChangeCloseEpoch(false) + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'voting', 'Should not change state after one vote') + assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, nextValidators, 'Next validators are not set correctly') + assert.strictEqual(info.closeEpoch, true, 'Current close epoch is not set correctly') + assert.strictEqual(info.nextCloseEpoch, true, 'Next close epoch is not set correctly') + + await controller3.voteChangeCloseEpoch(false) + info = await waitPromise( + controller1.getInfo, + (newInfo) => !newInfo.nextCloseEpoch + ) + assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, nextValidators, 'Next validators are not set correctly') + assert.strictEqual(info.closeEpoch, true, 'Current close epoch is not set correctly') + + await controller3.voteChangeCloseEpoch(false) + await delay(5000) + info = await controller1.getInfo() + assert.strictEqual(info.bridgeStatus, 'voting', 'Should not do anything after duplicate vote') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, nextValidators, 'Incorrect set of next validators after third vote') + assert.strictEqual(info.closeEpoch, true, 'Current close epoch is not set correctly') + assert.strictEqual(info.nextCloseEpoch, false, 'Next close epoch is not set correctly') + }) + it('should start keygen process', async function () { await controller1.voteStartKeygen() await delay(5000) @@ -77,8 +113,8 @@ module.exports = (newValidator) => { await delay(5000) info = await controller1.getInfo() assert.strictEqual(info.bridgeStatus, 'keygen', 'Should not do anything after duplicate vote') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, nextValidators, 'Incorrect set of next validators after third vote') }) diff --git a/tests/test/changeThreshold.js b/tests/test/changeThreshold.js index 754e7d5..1f6ee8b 100644 --- a/tests/test/changeThreshold.js +++ b/tests/test/changeThreshold.js @@ -22,17 +22,18 @@ module.exports = (newThreshold) => { assert.strictEqual(info.bridgeStatus, 'ready', 'Should not change state after one vote') await controller2.voteStartVoting() - info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.bridgeStatus === 'voting') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.bridgeStatus === 'closingEpoch' || newInfo.bridgeStatus === 'voting') + assert.strictEqual(info.bridgeStatus, 'voting', 'Should not started closing epoch after previous epoch') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, '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, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, 'Incorrect set of next validators after third vote') }) @@ -43,8 +44,8 @@ module.exports = (newThreshold) => { assert.strictEqual(info.bridgeStatus, 'voting', 'Should not change state after one vote') assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, 'Next validators are not set correctly') - assert.deepStrictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') - assert.deepStrictEqual(info.nextThreshold, initialInfo.threshold, 'Next threshold is not set correctly') + assert.strictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') + assert.strictEqual(info.nextThreshold, initialInfo.threshold, 'Next threshold is not set correctly') await controller2.voteChangeThreshold(newThreshold) info = await waitPromise( @@ -53,18 +54,18 @@ module.exports = (newThreshold) => { ) assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, 'Next validators are not set correctly') - assert.deepStrictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') + assert.strictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') await controller3.voteChangeThreshold(newThreshold) await delay(5000) info = await controller1.getInfo() assert.strictEqual(info.bridgeStatus, 'voting', 'Should not do anything after third vote') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, 'Incorrect set of next validators after third vote') - assert.deepStrictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') - assert.deepStrictEqual(info.nextThreshold, newThreshold, 'Next threshold is not set correctly') + assert.strictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') + assert.strictEqual(info.nextThreshold, newThreshold, 'Next threshold is not set correctly') }) it('should start keygen process', async function () { @@ -80,12 +81,12 @@ module.exports = (newThreshold) => { await delay(5000) info = await controller1.getInfo() assert.strictEqual(info.bridgeStatus, 'keygen', 'Should not do anything after third vote') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, 'Incorrect set of next validators after third vote') - assert.deepStrictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') - assert.deepStrictEqual(info.nextThreshold, newThreshold, 'Next threshold is not set correctly') + assert.strictEqual(info.threshold, initialInfo.threshold, 'Threshold not set correctly') + assert.strictEqual(info.nextThreshold, newThreshold, 'Next threshold is not set correctly') }) it('should finish keygen process and start funds transfer', async function () { @@ -102,8 +103,8 @@ module.exports = (newThreshold) => { assert.deepStrictEqual(info.validators, initialInfo.validators, 'Incorrect set of validators in new epoch') assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Incorrect next epoch') assert.strictEqual(info.bridgeStatus, 'ready', 'Incorrect bridge state in new epoch') - assert.deepStrictEqual(info.threshold, newThreshold, 'Threshold not set correctly') - assert.deepStrictEqual(info.nextThreshold, newThreshold, 'Next threshold is not set correctly') + assert.strictEqual(info.threshold, newThreshold, 'Threshold not set correctly') + assert.strictEqual(info.nextThreshold, newThreshold, 'Next threshold is not set correctly') await delay(5000) const prevBalance = await getBepBalance(initialInfo.foreignBridgeAddress) const newBalance = await getBepBalance(info.foreignBridgeAddress) diff --git a/tests/test/index.js b/tests/test/index.js index 7b7da2e..aa85b05 100644 --- a/tests/test/index.js +++ b/tests/test/index.js @@ -36,7 +36,7 @@ describe('bridge tests', function () { bncPrefundedUser = await createUser(FOREIGN_PRIVATE_KEY, 'bnc') const bnbBalance = await bncPrefundedUser.getBnbBalance() - assert.ok(bnbBalance >= 1, `Insufficient BNB balance on ${bncPrefundedUser.ethAddress} in Binance network, expected 1 BNB, got ${bnbBalance}`) + assert.ok(bnbBalance >= 10, `Insufficient BNB balance on ${bncPrefundedUser.ethAddress} in Binance network, expected 10 BNB, got ${bnbBalance}`) const bepBalance = await bncPrefundedUser.getBepBalance() assert.ok(bepBalance >= 2000, `Insufficient BEP2 balance on ${bncPrefundedUser.ethAddress} in Binance network, expected 2000 ${FOREIGN_ASSET}, got ${bepBalance}`) @@ -63,8 +63,17 @@ describe('bridge tests', function () { info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.epoch === 1) }) + it('should start correct epoch', async function () { + assert.deepStrictEqual(info.validators, validatorsConfig, 'Validators are not set correctly') + assert.deepStrictEqual(info.nextValidators, validatorsConfig, 'Next validators are not set correctly') + assert.strictEqual(info.closeEpoch, true, 'Current close epoch is not set correctly') + assert.strictEqual(info.nextCloseEpoch, true, 'Next close epoch is not set correctly') + assert.strictEqual(info.threshold, 2, 'Threshold not set correctly') + assert.strictEqual(info.nextThreshold, 2, 'Next threshold is not set correctly') + }) + after(async function () { - await bncPrefundedUser.transferBepBnb(info.foreignBridgeAddress, 1000, 0.1) + await bncPrefundedUser.transferBepBnb(info.foreignBridgeAddress, 1000, 5) await ethPrefundedUser.transferErc(HOME_BRIDGE_ADDRESS, 1000) }) }) diff --git a/tests/test/removeValidator.js b/tests/test/removeValidator.js index b50fe83..1918346 100644 --- a/tests/test/removeValidator.js +++ b/tests/test/removeValidator.js @@ -17,27 +17,32 @@ module.exports = (oldValidator) => { nextValidators = initialInfo.validators.filter((validator) => validator !== oldValidator) }) - it('should start voting process', async function () { + it('should start closing epoch 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, (newInfo) => newInfo.bridgeStatus === 'voting') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.bridgeStatus === 'closing_epoch') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, '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, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.bridgeStatus, 'closing_epoch', 'Should not do anything after third vote') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.nextValidators, initialInfo.validators, 'Incorrect set of next validators after third vote') }) + it('should finish close epoch process and start voting process', async function () { + this.timeout(120000) + info = await waitPromise(controller1.getInfo, (newInfo) => newInfo.bridgeStatus === 'voting') + }) + it('should remove validator', async function () { await controller1.voteRemoveValidator(oldValidator) await delay(5000) @@ -58,8 +63,8 @@ module.exports = (oldValidator) => { await delay(5000) info = await controller1.getInfo() assert.strictEqual(info.bridgeStatus, 'voting', 'Should not do anything after third vote') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, nextValidators, 'Incorrect set of next validators after third vote') }) @@ -77,8 +82,8 @@ module.exports = (oldValidator) => { await delay(5000) info = await controller1.getInfo() assert.strictEqual(info.bridgeStatus, 'keygen', 'Should not do anything after third vote') - assert.deepStrictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') - assert.deepStrictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') + assert.strictEqual(info.epoch, initialInfo.epoch, 'Current epoch is not set correctly') + assert.strictEqual(info.nextEpoch, initialInfo.epoch + 1, 'Next epoch is not set correctly') assert.deepStrictEqual(info.validators, initialInfo.validators, 'Validators are not set correctly') assert.deepStrictEqual(info.nextValidators, nextValidators, 'Incorrect set of next validators after third vote') }) diff --git a/tests/test/utils/proxyController.js b/tests/test/utils/proxyController.js index 3dac478..db028c8 100644 --- a/tests/test/utils/proxyController.js +++ b/tests/test/utils/proxyController.js @@ -28,6 +28,9 @@ function createController(validatorId) { }, async voteChangeThreshold(threshold) { return (await retry(() => proxy.get(`/vote/changeThreshold/${threshold}`))).data + }, + async voteChangeCloseEpoch(closeEpoch) { + return (await retry(() => proxy.get(`/vote/changeCloseEpoch/${closeEpoch}`))).data } } }