Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
74 changes: 52 additions & 22 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 = CYCLE_DURATION_BLOCKS;

/**
* @dev This event will be emitted after a change to the validator set has been finalized
Expand Down Expand Up @@ -110,36 +111,49 @@ contract ConsensusUtils is EternalStorage, ValidatorSet {
}
}

function _unBoundStake(address _staker) internal {
require(block.number > unboundingBlock(_staker));
_staker.transfer(unBoundingAmount(_staker));
_setUnBoundingAmount(_staker, 0);
}

function _withdraw(address _staker, uint256 _amount, address _validator) internal {
require(_validator != address(0));
require(_amount > 0);
require(_amount <= stakeAmount(_validator));
require(_amount <= delegatedAmount(_staker, _validator));

bool _isValidator = isValidator(_validator);

// if new stake amount is lesser than minStake and the validator is one of the current validators
if (stakeAmount(_validator).sub(_amount) < getMinStake() && _isValidator) {
// do not withdaw the amount until the validator is in current set
_pendingValidatorsRemove(_validator);
return;
}
if (unBoundingAmount(_staker) != 0)

@sirpy sirpy Sep 30, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Andrew-Pohl @leonprou
this logic prevents multiple withdraw requests, and still breaks wrapping fuse staking for DEFI.

  • user A requests to withdraw from the defi contract
  • defi contract calls withdraw -> now unbounding amount of contract > 0
  • user B requests to withdraw BEFORE unbounding period is over
  • defi contract calls withdraw, this IF resolves to true and _unboundStake is called and reverted because unbounding block has not reached yet

User B needs to wait for unbounding period before he can request a withdraw

{
//If we have requested a withdraw then try and pull it
_unBoundStake(_staker);
} else {
require(_amount <= stakeAmount(_validator));
require(_amount <= delegatedAmount(_staker, _validator));
_setUnboundingPeriod(_staker);
_setUnBoundingAmount(_staker, _amount);

bool _isValidator = isValidator(_validator);

// if new stake amount is lesser than minStake and the validator is one of the current validators
if (stakeAmount(_validator).sub(_amount) < getMinStake() && _isValidator) {
// do not withdaw the amount until the validator is in current set
_pendingValidatorsRemove(_validator);
return;
}


_delegatedAmountSub(_staker, _validator, _amount);
_stakeAmountSub(_validator, _amount);
_delegatedAmountSub(_staker, _validator, _amount);
_stakeAmountSub(_validator, _amount);

// if _validator is one of the current validators
if (_isValidator) {
// the total stake needs to be adjusted for the block reward formula
_totalStakeAmountSub(_amount);
}
// if _validator is one of the current validators
if (_isValidator) {
// the total stake needs to be adjusted for the block reward formula
_totalStakeAmountSub(_amount);
}

// if validator is needed to be removed from pending, but not current
if (stakeAmount(_validator) < getMinStake()) {
_pendingValidatorsRemove(_validator);
// if validator is needed to be removed from pending, but not current
if (stakeAmount(_validator) < getMinStake()) {
_pendingValidatorsRemove(_validator);
}
}
_staker.transfer(_amount);
}

function _setSystemAddress(address _newAddress) internal {
Expand Down Expand Up @@ -366,6 +380,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,10 +392,22 @@ 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))];
}

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

function _setUnBoundingAmount(address _address, uint256 _amount) internal {
uintStorage[keccak256(abi.encodePacked("unBoundingAmount", _address))] = _amount;
}

function _delegatedAmountAdd(address _address, address _validator, uint256 _amount) internal {
uintStorage[keccak256(abi.encodePacked("delegatedAmount", _address, _validator))] = uintStorage[keccak256(abi.encodePacked("delegatedAmount", _address, _validator))].add(_amount);
if (_address != _validator && !isDelegator(_validator, _address)) {
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