Skip to content

Commit

Permalink
Election fixes (#3033)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asa Oines committed Mar 5, 2020
1 parent c224b1f commit 2155ef0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/protocol/contracts/governance/Election.sol
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ contract Election is
}

// Assign a number of seats to each validator group.
while (totalNumMembersElected < electableValidators.max && electionGroups.length > 0) {
while (totalNumMembersElected < maxElectableValidators && electionGroups.length > 0) {
uint256 groupIndex = keys[0];
// All electable validators have been elected.
if (votesForNextMember[groupIndex].unwrap() == 0) break;
Expand Down
19 changes: 14 additions & 5 deletions packages/protocol/migrations/25_elect_validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,17 @@ module.exports = async (_deployer: any, networkName: string) => {

if (networkName === 'development') {
isGanache = true
config.validators.validatorKeys = [...Array(7)].map((_, i) => ganachePrivateKey(i))
extraKeys = [...Array(3)].map((_, i) => ganachePrivateKey(i + 7))
const addr0 = privateKeyToAddress('0x' + ganachePrivateKey(0))
for (let i = 10; i < 36; i++) {
const key = '0x' + ganachePrivateKey(i)
const addr = privateKeyToAddress(key)
// @ts-ignore
await web3.eth.personal.importRawKey(key, 'passphrase')
await web3.eth.personal.unlockAccount(addr, 'passphrase', 1000000)
await web3.eth.sendTransaction({ from: addr0, to: addr, value: new BigNumber(11000e18) })
}
config.validators.validatorKeys = [...Array(30)].map((_, i) => ganachePrivateKey(i))
extraKeys = [...Array(6)].map((_, i) => ganachePrivateKey(i + 30))
config.validators.attestationKeys = config.validators.validatorKeys
}

Expand All @@ -270,9 +279,9 @@ module.exports = async (_deployer: any, networkName: string) => {
// * Validator 1-n holds funds needed for their own stake
const validator0Key = valKeys[0]

if (valKeys.length < config.validators.minElectableValidators) {
console.warn(
` Warning: Have ${valKeys.length} Validator keys but require a minimum of ${config.validators.minElectableValidators} Validators in order for a new validator set to be elected.`
if (valKeys.length < parseInt(config.election.minElectableValidators, 10)) {
console.info(
` Warning: Have ${valKeys.length} Validator keys but require a minimum of ${config.election.minElectableValidators} Validators in order for a new validator set to be elected.`
)
}

Expand Down
3 changes: 3 additions & 0 deletions packages/protocol/runTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const migrationOverrides = {
downtimeSlasher: {
slashableDowntime: 60, // epoch length is 100 for unit tests
},
election: {
minElectableValidators: '10',
},
epochRewards: {
frozen: false,
},
Expand Down
16 changes: 13 additions & 3 deletions packages/protocol/test/common/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { HeapTestContract, HeapTestInstance } from 'types'

const HeapTest: HeapTestContract = artifacts.require('HeapTest')

function makeRandomList() {
function makeRandomList(n) {
const testLst = []
const len = 5 + Math.floor(Math.random() * 20)
for (let i = 0; i < len; i++) {
testLst[i] = Math.floor(Math.random() * 100000000)
testLst[i] = Math.floor(Math.random() * n)
}
return testLst
}
Expand All @@ -21,7 +21,17 @@ contract('HeapTest', () => {
describe('#sort()', () => {
it('test with random lists', async () => {
for (let i = 0; i < 100; i++) {
const testLst = makeRandomList()
const testLst = makeRandomList(1000000000)
const res: any = await heapTest.sort(testLst)
assert.deepEqual(
testLst.sort((a, b) => a - b),
res.map((a: any) => a.toNumber())
)
}
})
it('test with random lists including repeated items', async () => {
for (let i = 0; i < 100; i++) {
const testLst = makeRandomList(10)
const res: any = await heapTest.sort(testLst)
assert.deepEqual(
testLst.sort((a, b) => a - b),
Expand Down
19 changes: 19 additions & 0 deletions packages/protocol/test/common/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ async function findLessersAndGreaters(
return { ...changes, indices: changed.map((a) => a.index) }
}

contract('Integration: Running elections', (_accounts: string[]) => {
let election: ElectionInstance

before(async () => {
election = await getDeployedProxiedContract('Election', artifacts)
})

describe('When getting the elected validators', () => {
it('should elect all 30 validators', async () => {
const elected = await election.electValidatorSigners()
assert.equal(elected.length, 30)
})
it('should elect specified number validators with electNValidatorSigners', async () => {
const elected = await election.electNValidatorSigners(1, 20)
assert.equal(elected.length, 20)
})
})
})

contract('Integration: Governance slashing', (accounts: string[]) => {
const proposalId = 1
const dequeuedIndex = 0
Expand Down

0 comments on commit 2155ef0

Please sign in to comment.