Skip to content

Commit abb57d4

Browse files
committed
tests(Chain): tests for individual phase durations
1 parent a813e76 commit abb57d4

21 files changed

Lines changed: 161 additions & 130 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
###Changed
9+
- Each phase has its own duration
10+
711
## [0.5.1] - 2020-01-27
812
### Changed
913
- when updating contract via `ContractRegistry`, old `Chain` contract is not killed

config/development.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"verifiersPerShard": "10"
1111
},
1212
"Chain": {
13-
"blocksPerPhase": "10",
13+
"blocksPerPropose": "100",
14+
"blocksPerReveal": "10",
1415
"minimumStakingTokenPercentage": "70"
1516
},
1617
"ContractRegistry": {

config/production.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"Chain": {
1313
"verifierRegistryAddress": "0x614cb086657c47739068c12eeff43a4018be1190",
14-
"blocksPerPhase": "10",
14+
"blocksPerPropose": "100",
15+
"blocksPerReveal": "10",
1516
"minimumStakingTokenPercentage": "70"
1617
},
1718
"ContractRegistry": {

config/staging.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"Chain": {
1313
"verifierRegistryAddress": "0xd209f28511f37e3e975d4631b7cfa2f52ffabcef",
14-
"blocksPerPhase": "10",
14+
"blocksPerPropose": "100",
15+
"blocksPerReveal": "10",
1516
"minimumStakingTokenPercentage": "70"
1617
},
1718
"ContractRegistry": {

migrations/2.0_deploy_chain_storage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = (deployer, network, accounts) => {
77

88
return deployer.deploy(
99
ChainStorageArtifact,
10-
config.Chain.blocksPerPhase,
10+
config.Chain.blocksPerPropose,
11+
config.Chain.blocksPerReveal,
1112
config.Chain.minimumStakingTokenPercentage,
1213
false,
1314
options,

migrations/deployers/ChainStorage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module.exports = (
1010

1111
return deployer.deploy(
1212
ChainStorageArtifact,
13-
config.Chain.blocksPerPhase,
13+
config.Chain.blocksPerPropose,
14+
config.Chain.blocksPerReveal,
1415
config.Chain.minimumStakingTokenPercentage,
1516
false,
1617
options,

test/helpers/CycleFunctions.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
function blockOfCycle(currentBlock, phaseDuration) {
2-
return currentBlock % (phaseDuration * 2);
1+
function blockOfCycle(currentBlock, cycleDuration) {
2+
return currentBlock % cycleDuration;
33
}
44

5-
function isProposePhase(currentBlock, phaseDuration) {
6-
return blockOfCycle(currentBlock, phaseDuration) < phaseDuration;
5+
function isProposePhase(currentBlock, proposePhaseDuration, revealPhaseDuration) {
6+
const cycleBlock = blockOfCycle(currentBlock, proposePhaseDuration + revealPhaseDuration);
7+
return cycleBlock < proposePhaseDuration;
78
}
89

9-
function isRevealPhase(currentBlock, phaseDuration) {
10-
return !isProposePhase(currentBlock, phaseDuration);
10+
function isRevealPhase(currentBlock, proposePhaseDuration, revealPhaseDuration) {
11+
return !isProposePhase(currentBlock, proposePhaseDuration, revealPhaseDuration);
1112
}
1213

13-
function blocksToWaitForPropose(currentBlock, phaseDuration) {
14-
if (isProposePhase(currentBlock, phaseDuration)) return 0;
14+
function blocksToWaitForPropose(currentBlock, proposePhaseDuration, revealPhaseDuration) {
15+
if (isProposePhase(currentBlock, proposePhaseDuration, revealPhaseDuration)) return 0;
1516

16-
return (phaseDuration * 2) - blockOfCycle(currentBlock, phaseDuration);
17+
// +1 because we want to be in first block in a phase
18+
return (proposePhaseDuration + revealPhaseDuration + 1) -
19+
blockOfCycle(currentBlock, proposePhaseDuration + revealPhaseDuration);
1720
}
1821

19-
function blocksToWaitForReveal(currentBlock, phaseDuration) {
20-
if (isRevealPhase(currentBlock, phaseDuration)) return 0;
22+
function blocksToWaitForReveal(currentBlock, proposePhaseDuration, revealPhaseDuration) {
23+
if (isRevealPhase(currentBlock, proposePhaseDuration, revealPhaseDuration)) return 0;
2124

22-
return phaseDuration - blockOfCycle(currentBlock, phaseDuration);
25+
return proposePhaseDuration -
26+
blockOfCycle(currentBlock, proposePhaseDuration + revealPhaseDuration);
2327
}
2428

2529
export {

test/helpers/SpecHelper.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const advanceToBlock = async (number) => {
3737
const blockNumber = await web3.eth.getBlockNumber();
3838
if (blockNumber > number) {
3939
throw Error(`block number ${number} is in the past (current is ${blockNumber})`);
40+
} else if (blockNumber === number) {
41+
return Promise.resolve();
4042
}
4143

4244
const awaits = [];
@@ -50,30 +52,32 @@ const advanceToBlock = async (number) => {
5052
return Promise.all(awaits);
5153
};
5254

53-
const mineUntilPropose = async (phaseDuration) => {
55+
const mineUntilPropose = async (proposePhaseDuration, revealPhaseDuration) => {
5456
const blockNumber = await web3.eth.getBlockNumber();
57+
5558
const toMine =
56-
blocksToWaitForPropose(blockNumber, phaseDuration);
59+
blocksToWaitForPropose(blockNumber, proposePhaseDuration, revealPhaseDuration);
5760

5861
const prosalStartBlockNumber = new BigNumber(blockNumber).plus(toMine);
5962

6063
await advanceToBlock(prosalStartBlockNumber.toNumber());
6164
};
6265

63-
const mineUntilReveal = async (phaseDuration) => {
66+
const mineUntilReveal = async (proposePhaseDuration, revealPhaseDuration) => {
6467
const blockNumber = await web3.eth.getBlockNumber();
68+
6569
const toMine =
66-
blocksToWaitForReveal(blockNumber, phaseDuration);
70+
blocksToWaitForReveal(blockNumber, proposePhaseDuration, revealPhaseDuration);
6771

6872
const revealStartBlockNumber = new BigNumber(blockNumber).plus(toMine);
6973
await advanceToBlock(revealStartBlockNumber.toNumber());
7074
};
7175

72-
const getBlockHeight = async (phaseDuration) => {
76+
const getBlockHeight = async (proposePhaseDuration, revealPhaseDuration) => {
7377
const blockNumber = await web3.eth.getBlockNumber();
78+
7479
return BigNumber(blockNumber)
75-
.div(phaseDuration, 10)
76-
.div(2, 10)
80+
.div(parseInt(proposePhaseDuration, 10) + parseInt(revealPhaseDuration, 10), 10)
7781
.toString(10)
7882
.split('.')[0];
7983
};

test/helpers/deployers.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ async function deployVerifierRegistry(owner, contractRegistryAddr) {
2121

2222
async function deployChain(
2323
owner, verifiersAddr,
24-
blocksPerPhase, minimumStakingTokenPercentage, updateMinimumStakingTokenPercentageEnabled,
24+
blocksPerPropose,
25+
blocksPerReveal,
26+
minimumStakingTokenPercentage,
27+
updateMinimumStakingTokenPercentageEnabled,
2528
) {
2629
const contractRegistry = await deployContractRegistry();
2730

2831
const chainStorageInstance = await ChainStorageArtifact.new(
29-
blocksPerPhase,
32+
blocksPerPropose,
33+
blocksPerReveal,
3034
minimumStakingTokenPercentage,
3135
updateMinimumStakingTokenPercentageEnabled,
3236
);

test/integration/TestActiveVerifiers.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const {
1313
} = require('../helpers/deployers');
1414

1515
const verifiersCount = 3;
16-
const phaseDuration = 5 * verifiersCount * 2;
16+
const proposePhaseDuration = 5 * verifiersCount * 2;
17+
const revealPhaseDuration = 5 * verifiersCount * 2;
1718
const requirePercentOfTokens = 70;
1819

1920
contract('Chain: testing active/non active verifiers', (accounts) => {
@@ -27,7 +28,7 @@ contract('Chain: testing active/non active verifiers', (accounts) => {
2728

2829
before(async () => {
2930
ministroChain = await deployChain(
30-
accounts[0], verifiersAddr, phaseDuration,
31+
accounts[0], verifiersAddr, proposePhaseDuration, revealPhaseDuration,
3132
requirePercentOfTokens, true,
3233
);
3334

@@ -49,12 +50,12 @@ contract('Chain: testing active/non active verifiers', (accounts) => {
4950

5051
describe('when we are in propose phase', async () => {
5152
before(async () => {
52-
await mineUntilPropose(phaseDuration);
53+
await mineUntilPropose(proposePhaseDuration, revealPhaseDuration);
5354
});
5455

5556
it('should NOT be possible to propose', async () => {
5657
const awaits = [];
57-
const blockHeight = await getBlockHeight(phaseDuration);
58+
const blockHeight = await getBlockHeight(proposePhaseDuration, revealPhaseDuration);
5859
for (let i = 0; i < verifiersCount; i += 1) {
5960
awaits.push(ministroChain.propose(
6061
blindedProposals[i],
@@ -80,12 +81,12 @@ contract('Chain: testing active/non active verifiers', (accounts) => {
8081

8182
describe('when we are in propose phase', async () => {
8283
before(async () => {
83-
await mineUntilPropose(phaseDuration);
84+
await mineUntilPropose(proposePhaseDuration, revealPhaseDuration);
8485
});
8586

8687
it('should be possible to propose', async () => {
8788
const awaits = [];
88-
const blockHeight = await getBlockHeight(phaseDuration);
89+
const blockHeight = await getBlockHeight(proposePhaseDuration, revealPhaseDuration);
8990

9091
for (let i = 0; i < verifiersCount; i += 1) {
9192
awaits.push(ministroChain.propose(
@@ -110,7 +111,7 @@ contract('Chain: testing active/non active verifiers', (accounts) => {
110111

111112
describe('they should be able to finish election and reveal vote', async () => {
112113
before(async () => {
113-
await mineUntilReveal(phaseDuration);
114+
await mineUntilReveal(proposePhaseDuration, revealPhaseDuration);
114115
});
115116

116117
it('should be possible to reveal', async () => {

0 commit comments

Comments
 (0)