Skip to content

Commit

Permalink
Add validators amount limit
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Jul 16, 2019
1 parent 786be68 commit f62b781
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions contracts/upgradeable_contracts/BaseBridgeValidators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ contract BaseBridgeValidators is EternalStorage, Ownable {
using SafeMath for uint256;

address public constant F_ADDR = 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF;
uint256 internal constant MAX_VALIDATORS = 100;

event ValidatorAdded (address indexed validator);
event ValidatorRemoved (address indexed validator);
Expand Down
1 change: 1 addition & 0 deletions contracts/upgradeable_contracts/BridgeValidators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ contract BridgeValidators is BaseBridgeValidators {
setOwner(_owner);
require(_requiredSignatures != 0);
require(_initialValidators.length >= _requiredSignatures);
require(_initialValidators.length <= MAX_VALIDATORS);

for (uint256 i = 0; i < _initialValidators.length; i++) {
require(_initialValidators[i] != address(0) && _initialValidators[i] != F_ADDR);
Expand Down
1 change: 1 addition & 0 deletions contracts/upgradeable_contracts/RewardableValidators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ contract RewardableValidators is BaseBridgeValidators {
setOwner(_owner);
require(_requiredSignatures != 0);
require(_initialValidators.length >= _requiredSignatures);
require(_initialValidators.length <= MAX_VALIDATORS);
require(_initialValidators.length == _initialRewards.length);

for (uint256 i = 0; i < _initialValidators.length; i++) {
Expand Down
10 changes: 10 additions & 0 deletions test/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,13 @@ function isBN(object) {
}

module.exports.expectEventInLogs = expectEventInLogs

function createAccounts(web3, amount) {
const array = []
for (let i = 0; i < amount; i++) {
array[i] = web3.eth.accounts.create().address
}
return array
}

module.exports.createAccounts = createAccounts
17 changes: 16 additions & 1 deletion test/rewardable_validators_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const EternalStorageProxy = artifacts.require('EternalStorageProxy.sol')

const { expect } = require('chai')
const { ERROR_MSG, ZERO_ADDRESS, F_ADDRESS, BN } = require('./setup')
const { expectEventInLogs } = require('./helpers/helpers')
const { expectEventInLogs, createAccounts } = require('./helpers/helpers')

const ZERO = new BN(0)

Expand Down Expand Up @@ -60,6 +60,21 @@ contract('RewardableValidators', async accounts => {
expect(minor).to.be.bignumber.gte(ZERO)
expect(patch).to.be.bignumber.gte(ZERO)
})
it('should fail if exceed amount of validators', async () => {
// Given
const validators = createAccounts(web3, 101)

// When
await bridgeValidators
.initialize(99, validators, validators, accounts[2], { from: accounts[2] })
.should.be.rejectedWith(ERROR_MSG)
await bridgeValidators.initialize(99, validators.slice(0, 100), validators.slice(0, 100), accounts[2], {
from: accounts[2]
}).should.be.fulfilled

// Then
expect(await bridgeValidators.validatorCount()).to.be.bignumber.equal('100')
})
})

describe('#addValidator', async () => {
Expand Down
16 changes: 15 additions & 1 deletion test/validators_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const EternalStorageProxy = artifacts.require('EternalStorageProxy.sol')

const { expect } = require('chai')
const { ERROR_MSG, ZERO_ADDRESS, F_ADDRESS, BN } = require('./setup')
const { expectEventInLogs } = require('./helpers/helpers')
const { expectEventInLogs, createAccounts } = require('./helpers/helpers')

const ZERO = new BN(0)

Expand Down Expand Up @@ -48,6 +48,20 @@ contract('BridgeValidators', async accounts => {
expect(minor).to.be.bignumber.gte(ZERO)
expect(patch).to.be.bignumber.gte(ZERO)
})
it('should fail if exceed amount of validators', async () => {
// Given
const validators = createAccounts(web3, 101)

// When
await bridgeValidators
.initialize(99, validators, accounts[2], { from: accounts[2] })
.should.be.rejectedWith(ERROR_MSG)
await bridgeValidators.initialize(99, validators.slice(0, 100), accounts[2], { from: accounts[2] }).should.be
.fulfilled

// Then
expect(await bridgeValidators.validatorCount()).to.be.bignumber.equal('100')
})
})

describe('#addValidator', async () => {
Expand Down

0 comments on commit f62b781

Please sign in to comment.