Skip to content

Some Minor fixes #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 4, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ contract SpendingConditionMock is ISpendingCondition {
returns (bool)
{
if (shouldRevert) {
// TODO: solhint disabled for now due to bug, https://github.com/protofire/solhint/issues/157
// solhint-disable-next-line reason-string
revert(REVERT_MESSAGE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ contract StateTransitionVerifierMock is IStateTransitionVerifier {
returns (bool)
{
if (shouldRevert) {
// TODO: solhint disabled for now due to bug, https://github.com/protofire/solhint/issues/157
// solhint-disable-next-line reason-string
revert("Failing on purpose");
}

Expand Down
4 changes: 2 additions & 2 deletions plasma_framework/contracts/mocks/utils/BondSizeMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ contract BondSizeMock {

BondSize.Params public bond;

constructor (uint128 _initialBondSize, uint16 _lowerBoundDivisor, uint16 _upperBoundMultiplier) public {
bond = BondSize.buildParams(_initialBondSize, _lowerBoundDivisor, _upperBoundMultiplier);
constructor (uint128 initialBondSize, uint16 lowerBoundDivisor, uint16 upperBoundMultiplier) public {
bond = BondSize.buildParams(initialBondSize, lowerBoundDivisor, upperBoundMultiplier);
}

function bondSize() public view returns (uint128) {
Expand Down
40 changes: 20 additions & 20 deletions plasma_framework/contracts/src/exits/utils/BondSize.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,52 @@ library BondSize {
uint16 upperBoundMultiplier;
}

function buildParams(uint128 _initialBondSize, uint16 _lowerBoundDivisor, uint16 _upperBoundMultiplier)
function buildParams(uint128 initialBondSize, uint16 lowerBoundDivisor, uint16 upperBoundMultiplier)
internal
pure
returns (Params memory)
{
// Set the initial value to far in the future
uint128 initialEffectiveUpdateTime = 2 ** 63;
return Params({
previousBondSize: _initialBondSize,
previousBondSize: initialBondSize,
updatedBondSize: 0,
effectiveUpdateTime: initialEffectiveUpdateTime,
lowerBoundDivisor: _lowerBoundDivisor,
upperBoundMultiplier: _upperBoundMultiplier
lowerBoundDivisor: lowerBoundDivisor,
upperBoundMultiplier: upperBoundMultiplier
});
}

/**
* @notice Updates the bond size.
* @notice The new value is bounded by 0.5 and 2x of current bond size.
* @notice There is a waiting period of 2 days before the new value goes into effect.
* @dev There is a waiting period of 2 days before the new value goes into effect.
* @param newBondSize the new bond size.
*/
function updateBondSize(Params storage _self, uint128 newBondSize) internal {
validateBondSize(_self, newBondSize);
function updateBondSize(Params storage self, uint128 newBondSize) internal {
validateBondSize(self, newBondSize);

if (_self.updatedBondSize != 0 && now > _self.effectiveUpdateTime) {
_self.previousBondSize = _self.updatedBondSize;
if (self.updatedBondSize != 0 && now >= self.effectiveUpdateTime) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This now >= self.effectiveUpdateTime part was a bug! It was now > self.effectiveUpdateTime but in bondSize(self) it is using now < self.effectiveUpdateTime. As a result, when now == self.effectiveUpdateTime it would fail to fall into this if block.

self.previousBondSize = self.updatedBondSize;
}
_self.updatedBondSize = newBondSize;
_self.effectiveUpdateTime = uint64(now) + WAITING_PERIOD;
self.updatedBondSize = newBondSize;
self.effectiveUpdateTime = uint64(now) + WAITING_PERIOD;
}

/**
* @notice Returns the current bond size.
*/
function bondSize(Params memory _self) internal view returns (uint128) {
if (now < _self.effectiveUpdateTime) {
return _self.previousBondSize;
function bondSize(Params memory self) internal view returns (uint128) {
if (now < self.effectiveUpdateTime) {
return self.previousBondSize;
} else {
return _self.updatedBondSize;
return self.updatedBondSize;
}
}

function validateBondSize(Params memory _self, uint128 newBondSize) private view {
uint128 currentBondSize = bondSize(_self);
require(newBondSize >= currentBondSize / _self.lowerBoundDivisor, "Bond size is too low");
require(uint256(newBondSize) <= uint256(currentBondSize) * _self.upperBoundMultiplier, "Bond size is too high");
function validateBondSize(Params memory self, uint128 newBondSize) private view {
uint128 currentBondSize = bondSize(self);
require(newBondSize > 0, "Bond size cannot be zero");
require(newBondSize >= currentBondSize / self.lowerBoundDivisor, "Bond size is too low");
require(uint256(newBondSize) <= uint256(currentBondSize) * self.upperBoundMultiplier, "Bond size is too high");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ contract TxFinalizationVerifier is ITxFinalizationVerifier {
if (data.protocol == Protocol.MORE_VP()) {
return checkInclusionProof(data);
} else if (data.protocol == Protocol.MVP()) {
// TODO: solhint disabled for now due to bug, https://github.com/protofire/solhint/issues/157
// solhint-disable-next-line reason-string
revert("not supporting MVP yet");
} else {
// TODO: solhint disabled for now due to bug, https://github.com/protofire/solhint/issues/157
// solhint-disable-next-line reason-string
revert("invalid protocol value");
}
}
Expand All @@ -44,12 +40,8 @@ contract TxFinalizationVerifier is ITxFinalizationVerifier {
if (data.protocol == Protocol.MORE_VP()) {
return data.txBytes.length > 0;
} else if (data.protocol == Protocol.MVP()) {
// TODO: solhint disabled for now due to bug, https://github.com/protofire/solhint/issues/157
// solhint-disable-next-line reason-string
revert("not supporting MVP yet");
} else {
// TODO: solhint disabled for now due to bug, https://github.com/protofire/solhint/issues/157
// solhint-disable-next-line reason-string
revert("invalid protocol value");
}
}
Expand Down
6 changes: 3 additions & 3 deletions plasma_framework/contracts/src/vaults/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ contract Vault is Operated {
* @return contract address of deposit verifier.
*/
function getEffectiveDepositVerifier() public view returns (address) {
if (now > newDepositVerifierMaturityTimestamp) {
return depositVerifiers[1];
} else {
if (now < newDepositVerifierMaturityTimestamp) {
return depositVerifiers[0];
} else {
return depositVerifiers[1];
}
}

Expand Down
170 changes: 170 additions & 0 deletions plasma_framework/test/src/exits/utils/BondSize.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const BondSizeMock = artifacts.require('BondSizeMock');
const { BN, expectRevert, time } = require('openzeppelin-test-helpers');
const { expect } = require('chai');

contract('BondSize', () => {
const WAITING_PERIOD = time.duration.days(2);
const HALF_WAITING_PERIOD = WAITING_PERIOD.divn(2);

describe('with normal cases', () => {
beforeEach(async () => {
this.initialBondSize = new BN(20000000000);
this.lowerBoundDivisor = 2;
this.upperBoundMultiplier = 3;
this.contract = await BondSizeMock.new(
this.initialBondSize,
this.lowerBoundDivisor,
this.upperBoundMultiplier,
);
});

it('should return the initial bond size', async () => {
const bondSize = await this.contract.bondSize();
expect(bondSize).to.be.bignumber.equal(this.initialBondSize);
});

it('should be able to update the bond to upperBoundMultiplier times its current value', async () => {
const newBondSize = new BN(this.initialBondSize).muln(this.upperBoundMultiplier);
await this.contract.updateBondSize(newBondSize);
});

it('should fail to update bond to more than upperBoundMultiplier times its current value', async () => {
const newBondSize = new BN(this.initialBondSize).muln(this.upperBoundMultiplier).addn(1);
await expectRevert(
this.contract.updateBondSize(newBondSize),
'Bond size is too high',
);
});

it('should be able to update the bond to lowerBoundDivisor of its current value', async () => {
const newBondSize = new BN(this.initialBondSize).divn(this.lowerBoundDivisor);
await this.contract.updateBondSize(newBondSize);
});

it('should fail to update bond to less than lowerBoundDivisor of its current value', async () => {
const newBondSize = new BN(this.initialBondSize).divn(this.lowerBoundDivisor).subn(1);
await expectRevert(
this.contract.updateBondSize(newBondSize),
'Bond size is too low',
);
});

it('should not update the actual bond value until after the waiting period', async () => {
const newBondSize = new BN(this.initialBondSize).muln(2);
await this.contract.updateBondSize(newBondSize);

await time.increase(WAITING_PERIOD.sub(time.duration.seconds(5)));
const bondSize = await this.contract.bondSize();
expect(bondSize).to.be.bignumber.equal(this.initialBondSize);
});

it('should update the actual bond value after the waiting period', async () => {
const newBondSize = new BN(this.initialBondSize).muln(2);
await this.contract.updateBondSize(newBondSize);

await time.increase(WAITING_PERIOD);
const bondSize = await this.contract.bondSize();
expect(bondSize).to.be.bignumber.equal(newBondSize);
});

it('should update the actual bond value after the waiting period', async () => {
const newBondSize = new BN(this.initialBondSize).muln(2);
await this.contract.updateBondSize(newBondSize);

// Wait half the waiting period
await time.increase(HALF_WAITING_PERIOD);

// Update again while the first update is in progress.
const secondNewBondSize = new BN(this.initialBondSize).muln(1.5);
await this.contract.updateBondSize(secondNewBondSize);

// Wait half the waiting period again.
await time.increase(HALF_WAITING_PERIOD);
// Even though the first update's waiting period is over, the second
// update is in progress so the bond size should not have changed yet.
let bondSize = await this.contract.bondSize();
expect(bondSize).to.be.bignumber.equal(this.initialBondSize);

// Wait the remaining waiting period
await time.increase(HALF_WAITING_PERIOD);
bondSize = await this.contract.bondSize();
expect(bondSize).to.be.bignumber.equal(secondNewBondSize);
});

it('should be able to update continuosly', async () => {
const newBondSize1 = new BN(this.initialBondSize).muln(2);
await this.contract.updateBondSize(newBondSize1);
await time.increase(WAITING_PERIOD);

let bondSize = await this.contract.bondSize();
expect(bondSize).to.be.bignumber.equal(newBondSize1);

const newBondSize2 = newBondSize1.muln(2);
await this.contract.updateBondSize(newBondSize2);

// Before a full waiting period is finished, it would still not update to latest
await time.increase(HALF_WAITING_PERIOD);
bondSize = await this.contract.bondSize();
expect(bondSize, 'should not update to latest yet').to.be.bignumber.equal(newBondSize1);

// update to latest after full waiting period is done
await time.increase(HALF_WAITING_PERIOD);
bondSize = await this.contract.bondSize();
expect(bondSize, 'should update to latest').to.be.bignumber.equal(newBondSize2);
});
});

describe('with boundary size of numbers', () => {
it('should able to update to max number of uint128 without having overflow issue', async () => {
const maxSizeOfUint128 = (new BN(2)).pow(new BN(128)).sub(new BN(1)); // 2^128 - 1

const initialBondSize = maxSizeOfUint128.sub(new BN(10000));
const lowerBoundDivisor = 2;
const upperBoundMultiplier = 3;
const contract = await BondSizeMock.new(
initialBondSize,
lowerBoundDivisor,
upperBoundMultiplier,
);

await contract.updateBondSize(maxSizeOfUint128);
await time.increase(WAITING_PERIOD);
const bondSize = await contract.bondSize();
expect(bondSize).to.be.bignumber.equal(maxSizeOfUint128);
});

it('should be able to update to 1', async () => {
const initialBondSize = new BN(2);
const lowerBoundDivisor = 2;
const upperBoundMultiplier = 3;
const contract = await BondSizeMock.new(
initialBondSize,
lowerBoundDivisor,
upperBoundMultiplier,
);
const bondSizeOne = new BN(1);

await contract.updateBondSize(bondSizeOne);
await time.increase(WAITING_PERIOD);
const bondSize = await contract.bondSize();
expect(bondSize).to.be.bignumber.equal(bondSizeOne);
});

it('should NOT be able to update to 0', async () => {
const initialBondSize = new BN(2);
const lowerBoundDivisor = 2;
const upperBoundMultiplier = 3;
const contract = await BondSizeMock.new(
initialBondSize,
lowerBoundDivisor,
upperBoundMultiplier,
);
const bondSizeZero = new BN(0);

await expectRevert(
contract.updateBondSize(bondSizeZero),
'Bond size cannot be zero',
);
});
});
});
Loading