Wrote tests for removing validator

This commit is contained in:
Kirill Fedoseev 2019-10-29 21:07:11 +03:00
parent 592663aeac
commit 003a59c6ae
6 changed files with 113 additions and 16 deletions

View File

@ -1,4 +1,6 @@
exit: true exit: true
timeout: 60000
bail: true
reporter: mocha-multi-reporters reporter: mocha-multi-reporters
reporter-option: reporter-option:
- configFile=./reportersConfig.json - configFile=./reportersConfig.json

View File

@ -15,5 +15,10 @@
"ethAddress": "0xad6c8127143032d843a260c5d379d8d9b3d51f15", "ethAddress": "0xad6c8127143032d843a260c5d379d8d9b3d51f15",
"bncAddress": "tbnb12epcy4p7ktas0nlyrfuektcyh0e83dwzuq73f4" "bncAddress": "tbnb12epcy4p7ktas0nlyrfuektcyh0e83dwzuq73f4"
} }
],
"validators": [
"0x99Eb3D86663c6Db090eFFdBC20510Ca9f836DCE3",
"0xAa006899B0EC407De930bA8A166DEfe59bBfd3DC",
"0x6352e3e6038e05b9da00C84AE851308f9774F883"
] ]
} }

View File

@ -7,7 +7,6 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => {
let ethBalances let ethBalances
before(async function () { before(async function () {
this.timeout(60000)
users = usersFunc() users = usersFunc()
foreignBridgeAddress = foreignBridgeAddressFunc() foreignBridgeAddress = foreignBridgeAddressFunc()
ethBalances = await Promise.all(users.map(user => user.getEthBalance())) 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 () { it('should make coorect exchange transactions on eth side', async function () {
this.timeout(60000)
for (let i = 0; i < 3; i++) { for (let i = 0; i < 3; i++) {
do { do {
const user = users[i] const user = users[i]

View File

@ -2,8 +2,6 @@ const assert = require('assert')
const { getSequence } = require('./utils/bncController') const { getSequence } = require('./utils/bncController')
const { waitPromise } = require('./utils/wait') const { waitPromise } = require('./utils/wait')
const usersConfig = require('../config').users
const { HOME_BRIDGE_ADDRESS } = process.env const { HOME_BRIDGE_ADDRESS } = process.env
module.exports = (usersFunc, foreignBridgeAddressFunc) => { module.exports = (usersFunc, foreignBridgeAddressFunc) => {
@ -15,7 +13,6 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => {
let bncBridgeSequence let bncBridgeSequence
before(async function () { before(async function () {
this.timeout(60000)
users = usersFunc() users = usersFunc()
foreignBridgeAddress = foreignBridgeAddressFunc() foreignBridgeAddress = foreignBridgeAddressFunc()
ethBalances = await Promise.all(users.map(user => user.getEthBalance())) ethBalances = await Promise.all(users.map(user => user.getEthBalance()))
@ -26,11 +23,10 @@ module.exports = (usersFunc, foreignBridgeAddressFunc) => {
}) })
it('should accept exchange requests', async function () { it('should accept exchange requests', async function () {
this.timeout(60000)
await Promise.all(users.map((user, i) => user.exchangeEth(5 + i))) await Promise.all(users.map((user, i) => user.exchangeEth(5 + i)))
const newEthBalances = await Promise.all(users.map(user => user.getEthBalance())) const newEthBalances = await Promise.all(users.map(user => user.getEthBalance()))
for (let i = 0; i < 3; i++) { 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 () { it('should make correct exchange transaction', async function () {
this.timeout(60000)
const newBncBalances = await Promise.all(users.map(user => user.getBncBalance())) const newBncBalances = await Promise.all(users.map(user => user.getBncBalance()))
for (let i = 0; i < 3; i++) { 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`)
} }
}) })
}) })

View File

@ -1,43 +1,138 @@
const assert = require('assert')
const createController = require('./utils/proxyController') const createController = require('./utils/proxyController')
const createUser = require('./utils/user') 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 testEthToBnc = require('./ethToBnc')
const testBncToEth = require('./bncToEth') const testBncToEth = require('./bncToEth')
const usersConfig = require('../config').users const usersConfig = require('../config').users
const validatorsConfig = require('../config').validators
const { FOREIGN_PRIVATE_KEY } = process.env 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 () { describe('bridge tests', function () {
let users let users
let foreignPrefundedUser let foreignPrefundedUser
let info let info
let prevForeignBridgeBalance
let prevForeignBridgeAddress
before(async function() { before(async function () {
this.timeout(60000)
users = await usersConfig.seqMap(user => createUser(user.privateKey)) users = await usersConfig.seqMap(user => createUser(user.privateKey))
}) })
describe('generation of initial epoch keys', function () { describe('generation of initial epoch keys', function () {
before(async function () { before(async function () {
this.timeout(60000)
foreignPrefundedUser = await createUser(FOREIGN_PRIVATE_KEY) foreignPrefundedUser = await createUser(FOREIGN_PRIVATE_KEY)
}) })
it('should generate keys', async function () { it('should generate keys', async function () {
this.timeout(120000) this.timeout(120000)
info = await waitPromise(getInfo, info => info.epoch === 1) info = await waitPromise(controller1.getInfo, info => info.epoch === 1)
}) })
after(async function () { after(async function () {
this.timeout(60000)
await foreignPrefundedUser.transferBnc(info.foreignBridgeAddress, 50, 0.1) await foreignPrefundedUser.transferBnc(info.foreignBridgeAddress, 50, 0.1)
}) })
}) })
testEthToBnc(() => users, () => info.foreignBridgeAddress) testEthToBnc(() => users, () => info.foreignBridgeAddress)
testBncToEth(() => 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)
}) })

View File

@ -21,6 +21,8 @@ module.exports = async function (privateKey) {
const bncClient = await createBncClient(privateKey) const bncClient = await createBncClient(privateKey)
return { return {
ethAddress,
bncAddress,
getEthBalance: async function () { getEthBalance: async function () {
const balance = await token.balanceOf(ethAddress) const balance = await token.balanceOf(ethAddress)
return parseFloat(new BN(balance).dividedBy(10 ** 18).toFixed(8, 3)) return parseFloat(new BN(balance).dividedBy(10 ** 18).toFixed(8, 3))