Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- when verifier proposing, `Chain` checks, if verifier is active instead
of if it is only created.
- split Chain contract into two separate contracts (storage and manager)
- Flexible duration of election phases

## [0.3.0] - 2018-12-13
### Added:
Expand Down
32 changes: 25 additions & 7 deletions contracts/Chain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ contract Chain is IChain, RegistrableWithSingleStorage, ReentrancyGuard, Ownable
bytes32 constant NAME = "Chain";

modifier whenProposePhase() {
require(getCurrentElectionCycleBlock() < blocksPerPhase(), "we are not in propose phase");
require(getCurrentElectionCycleBlock() < blocksPerPropose(), "we are not in propose phase");
_;
}
modifier whenRevealPhase() {
require(getCurrentElectionCycleBlock() >= blocksPerPhase(), "we are not in reveal phase");
require(getCurrentElectionCycleBlock() >= blocksPerPropose(), "we are not in reveal phase");
_;
}

Expand Down Expand Up @@ -113,7 +113,7 @@ contract Chain is IChain, RegistrableWithSingleStorage, ReentrancyGuard, Ownable
}

function getBlockHeight() public view returns (uint256) {
return block.number.div(uint256(blocksPerPhase()) * 2);
return block.number.div(uint256(blocksPerElection()));
}

/// @dev this function needs to be called each time we successfully reveal a proposal
Expand Down Expand Up @@ -214,6 +214,10 @@ contract Chain is IChain, RegistrableWithSingleStorage, ReentrancyGuard, Ownable
_storage().setInitialBlockHeight(_shard, _blockHeight);
}

function updatePhasesDurations(uint8 _blocksPerPropose, uint8 _blocksPerReveal) external onlyOwner {
_storage().updatePhasesDurations(_blocksPerPropose, _blocksPerReveal);
}

function updateMinimumStakingTokenPercentage(uint8 _minimumStakingTokenPercentage)
public
onlyOwner
Expand All @@ -235,18 +239,32 @@ contract Chain is IChain, RegistrableWithSingleStorage, ReentrancyGuard, Ownable
return _storage().minimumStakingTokenPercentage();
}

function blocksPerPhase()
function blocksPerPropose()
public
view
returns (uint8) {
return _storage().blocksPerPropose();
}

function blocksPerReveal()
public
view
returns (uint8) {
return _storage().blocksPerReveal();
}

function blocksPerElection()
public
view
returns (uint8) {
return _storage().blocksPerPhase();
return _storage().blocksPerPropose() + _storage().blocksPerReveal();
}

function getCurrentElectionCycleBlock()
public
view
returns (uint256) {
return block.number % (uint256(blocksPerPhase()) * 2);
return block.number % uint256(blocksPerElection());
}

/// @return first block number (blockchain block) of current cycle
Expand All @@ -261,7 +279,7 @@ contract Chain is IChain, RegistrableWithSingleStorage, ReentrancyGuard, Ownable
public
view
returns (bool) {
return getCurrentElectionCycleBlock() < blocksPerPhase();
return getCurrentElectionCycleBlock() < blocksPerPropose();
}

function initialBlockHeights(uint256 _shard)
Expand Down
46 changes: 39 additions & 7 deletions contracts/ChainStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,41 @@ contract ChainStorage is StorageBase {

bool public updateMinimumStakingTokenPercentageEnabled;

uint8 public blocksPerPhase;
uint8 public blocksPerPropose;
uint8 public blocksPerReveal;

uint8 public minimumStakingTokenPercentage;

event LogChainConfig(uint8 blocksPerPhase, uint8 requirePercentOfTokens, bool updateMinimumStakingTokenPercentageEnabled);
event LogChainConfig(
uint8 blocksPerPropose,
uint8 blocksPerReveal,
uint8 requirePercentOfTokens,
bool updateMinimumStakingTokenPercentageEnabled
);

constructor(uint8 _blocksPerPhase,
constructor(
uint8 _blocksPerPropose,
uint8 _blocksPerReveal,
uint8 _minimumStakingTokenPercentage,
bool _updateMinimumStakingTokenPercentageEnabled) public {
require(_blocksPerPhase > 0, "_blocksPerPhase can't be empty");
blocksPerPhase = _blocksPerPhase;
require(_blocksPerPropose > 0, "_blocksPerPhase can't be empty");
require(_blocksPerReveal > 0, "_blocksPerReveal can't be empty");

blocksPerPropose = _blocksPerPropose;
blocksPerReveal = _blocksPerReveal;

require(_minimumStakingTokenPercentage > 0, "_minimumStakingTokenPercentage can't be empty");
require(_minimumStakingTokenPercentage <= 100, "_minimumStakingTokenPercentage can't be over 100%");
minimumStakingTokenPercentage = _minimumStakingTokenPercentage;

updateMinimumStakingTokenPercentageEnabled = _updateMinimumStakingTokenPercentageEnabled;

emit LogChainConfig(_blocksPerPhase, _minimumStakingTokenPercentage, _updateMinimumStakingTokenPercentageEnabled);
emit LogChainConfig(
_blocksPerPropose,
_blocksPerReveal,
_minimumStakingTokenPercentage,
_updateMinimumStakingTokenPercentageEnabled
);
}

function getInitialBlockHeight(uint256 _shard) public view returns (uint256) {
Expand Down Expand Up @@ -160,6 +176,22 @@ contract ChainStorage is StorageBase {
return true;
}

function updatePhasesDurations(uint8 _blocksPerPropose, uint8 _blocksPerReveal)
external
onlyFromStorageOwner
returns (bool) {
require(_blocksPerPropose > 0, "_blocksPerPropose can't be empty");
require(_blocksPerReveal > 0, "_blocksPerReveal can't be empty");
require(blocksPerPropose + blocksPerReveal == _blocksPerPropose + _blocksPerReveal, "election duration must be fixed");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might add some additional condition like: can't make longer propose if there is already some reveal... but for now, just to see idea - this PR is enough.

blocksPerPropose = _blocksPerPropose;
blocksPerReveal = _blocksPerReveal;

emit LogChainConfig(blocksPerPropose, blocksPerReveal, minimumStakingTokenPercentage, true);

return true;
}

function updateMinimumStakingTokenPercentage(uint8 _minimumStakingTokenPercentage)
external
onlyFromStorageOwner
Expand All @@ -170,7 +202,7 @@ contract ChainStorage is StorageBase {
require(_minimumStakingTokenPercentage <= 100, "_minimumStakingTokenPercentage can't be over 100%");
minimumStakingTokenPercentage = _minimumStakingTokenPercentage;

emit LogChainConfig(blocksPerPhase, _minimumStakingTokenPercentage, true);
emit LogChainConfig(blocksPerPropose, blocksPerReveal, _minimumStakingTokenPercentage, true);

return true;
}
Expand Down