Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions contracts/ConsensusUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract ConsensusUtils is EternalStorage, ValidatorSet {
uint256 public constant CYCLE_DURATION_BLOCKS = 34560; // 48 hours [48*60*60/5]
uint256 public constant SNAPSHOTS_PER_CYCLE = 0; // snapshot each 288 minutes [34560/10/60*5]
uint256 public constant DEFAULT_VALIDATOR_FEE = 15e16; // 15%
uint256 public constant UNBOUNDING_PERIOD = 86400; // 120 hours [120*60*60/5]

/**
* @dev This event will be emitted after a change to the validator set has been finalized
Expand Down Expand Up @@ -94,6 +95,9 @@ contract ConsensusUtils is EternalStorage, ValidatorSet {

_delegatedAmountAdd(_staker, _validator, _amount);
_stakeAmountAdd(_validator, _amount);
if (_staker != _validator) {
_setUnboundingPeriod(_staker);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what about the case where staker = validator
It means that the validator stakes himself, right? I think it also need an unbounding period.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct, I will change this actually should really lock everyone in. Sorry for the massive delay in this comment only just seen it!

}

// stake amount of the validator isn't greater than the max stake
require(stakeAmount(_validator) <= getMaxStake());
Expand All @@ -117,6 +121,7 @@ contract ConsensusUtils is EternalStorage, ValidatorSet {
require(_amount <= delegatedAmount(_staker, _validator));

bool _isValidator = isValidator(_validator);
require(block.number > unboundingBlock(_staker));

// if new stake amount is lesser than minStake and the validator is one of the current validators
if (stakeAmount(_validator).sub(_amount) < getMinStake() && _isValidator) {
Expand Down Expand Up @@ -366,6 +371,10 @@ contract ConsensusUtils is EternalStorage, ValidatorSet {
return uintStorage[TOTAL_STAKE_AMOUNT];
}

function unboundingBlock(address _address) public view returns(uint256) {
return uintStorage[keccak256(abi.encodePacked("unboundingBlock", _address))];
}

function _stakeAmountAdd(address _address, uint256 _amount) internal {
uintStorage[keccak256(abi.encodePacked("stakeAmount", _address))] = uintStorage[keccak256(abi.encodePacked("stakeAmount", _address))].add(_amount);
}
Expand All @@ -374,6 +383,10 @@ contract ConsensusUtils is EternalStorage, ValidatorSet {
uintStorage[keccak256(abi.encodePacked("stakeAmount", _address))] = uintStorage[keccak256(abi.encodePacked("stakeAmount", _address))].sub(_amount);
}

function _setUnboundingPeriod(address _address) internal {
uintStorage[keccak256(abi.encodePacked("unboundingBlock", _address))] = block.number + UNBOUNDING_PERIOD;
}

function delegatedAmount(address _address, address _validator) public view returns(uint256) {
return uintStorage[keccak256(abi.encodePacked("delegatedAmount", _address, _validator))];
}
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IConsensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ interface IConsensus {
function isFinalized() external view returns(bool);
function stakeAmount(address _address) external view returns(uint256);
function totalStakeAmount() external view returns(uint256);
function unboundingBlock(address _address) external view returns(uint256);
}
33 changes: 31 additions & 2 deletions test/consensus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { toBN, toWei, toChecksumAddress } = web3.utils
const MAX_VALIDATORS = 100
const MIN_STAKE_AMOUNT = 10000
const MAX_STAKE_AMOUNT = 50000
const UNBOUNDING_PERIOD = 86400
const MULTIPLY_AMOUNT = 3
const MIN_STAKE = toWei(toBN(MIN_STAKE_AMOUNT), 'ether')
const MAX_STAKE = toWei(toBN(MAX_STAKE_AMOUNT), 'ether')
Expand Down Expand Up @@ -804,6 +805,11 @@ contract('Consensus', async (accounts) => {
it('should not allow more the maximum stake', async () => {
await consensus.stake({from: firstCandidate, value: MORE_THAN_MAX_STAKE}).should.be.rejectedWith(ERROR_MSG)
})

it('unbounding period should remain 0', async () => {
await consensus.stake({from: firstCandidate, value: LESS_THAN_MIN_STAKE}).should.be.fulfilled
ZERO.should.be.bignumber.equal(await consensus.unboundingBlock(firstCandidate))
})
})
describe('advanced', async () => {
it('minimum stake amount, in more than one transaction', async () => {
Expand Down Expand Up @@ -1176,6 +1182,7 @@ contract('Consensus', async (accounts) => {

// await mockEoC()
// withdraw

await consensus.methods['withdraw(uint256)'](MIN_STAKE, {from: firstCandidate})
MIN_STAKE.should.be.bignumber.equal(await web3.eth.getBalance(consensus.address))
ZERO_AMOUNT.should.be.bignumber.equal(await consensus.stakeAmount(firstCandidate))
Expand Down Expand Up @@ -1324,12 +1331,31 @@ contract('Consensus', async (accounts) => {
await consensus.delegate(firstCandidate, {from: firstDelegator, value: MIN_STAKE})
await consensus.methods['withdraw(address,uint256)'](firstCandidate, MORE_THAN_MIN_STAKE, {from: firstDelegator}).should.be.rejectedWith(ERROR_MSG)
})
it('Check unbounding period', async () => {
await consensus.delegate(firstCandidate, {from: firstDelegator, value: MAX_STAKE})
let currentBlockNumber = await web3.eth.getBlockNumber()
//check unbounding has updated
toBN((UNBOUNDING_PERIOD + currentBlockNumber)).should.be.bignumber.equal(await consensus.unboundingBlock(firstDelegator))
await consensus.methods['withdraw(address,uint256)'](firstCandidate, MIN_STAKE, {from: firstDelegator}).should.be.rejectedWith(ERROR_MSG)
await advanceBlocks(UNBOUNDING_PERIOD)
await consensus.methods['withdraw(address,uint256)'](firstCandidate, MIN_STAKE, {from: firstDelegator}).should.be.fulfilled
//check a second withdraw
await consensus.methods['withdraw(address,uint256)'](firstCandidate, MIN_STAKE, {from: firstDelegator}).should.be.fulfilled
let oldunBound = await consensus.unboundingBlock(firstDelegator)
//check for updated
await consensus.delegate(firstCandidate, {from: firstDelegator, value: MIN_STAKE}).should.be.fulfilled
oldunBound.should.be.bignumber.below(await consensus.unboundingBlock(firstDelegator))
await consensus.methods['withdraw(address,uint256)'](firstCandidate, MIN_STAKE, {from: firstDelegator}).should.be.rejectedWith(ERROR_MSG)
await advanceBlocks(UNBOUNDING_PERIOD)
await consensus.methods['withdraw(address,uint256)'](firstCandidate, MIN_STAKE, {from: firstDelegator}).should.be.fulfilled
})
it('can withdraw all staked amount', async () => {
// stake
await consensus.delegate(firstCandidate, {from: firstDelegator, value: MIN_STAKE})
// stake
await consensus.delegate(secondCandidate, {from: firstDelegator, value: MIN_STAKE})
// withdraw
//advance block and withdraw
await advanceBlocks(UNBOUNDING_PERIOD)
await consensus.methods['withdraw(address,uint256)'](firstCandidate, MIN_STAKE, {from: firstDelegator})
MIN_STAKE.should.be.bignumber.equal(await web3.eth.getBalance(consensus.address))
ZERO_AMOUNT.should.be.bignumber.equal(await consensus.stakeAmount(firstCandidate))
Expand All @@ -1353,9 +1379,11 @@ contract('Consensus', async (accounts) => {
firstDelegator.should.be.equal(await consensus.delegatorsAtPosition(secondCandidate, 0))
})
it('can withdraw less than staked amount', async () => {

// stake
await consensus.delegate(firstCandidate, {from: firstDelegator, value: MIN_STAKE})
// withdraw
//advance block and withdraw
await advanceBlocks(UNBOUNDING_PERIOD)
await consensus.methods['withdraw(address,uint256)'](firstCandidate, ONE_ETHER, {from: firstDelegator})
let expectedAmount = toWei(toBN(MIN_STAKE_AMOUNT - 1), 'ether')
let expectedValidators = []
Expand All @@ -1374,6 +1402,7 @@ contract('Consensus', async (accounts) => {
it('can withdraw multiple times', async () => {
// stake
await consensus.delegate(firstCandidate, {from: firstDelegator, value: MIN_STAKE})
await advanceBlocks(UNBOUNDING_PERIOD)
// withdraw 1st time
await consensus.methods['withdraw(address,uint256)'](firstCandidate, ONE_ETHER, {from: firstDelegator})
let expectedAmount = toWei(toBN(MIN_STAKE_AMOUNT - 1), 'ether')
Expand Down