Skip to content

Commit

Permalink
Merge pull request JoinColony#256 from filiplazovic/fix/denominator-c…
Browse files Browse the repository at this point in the history
…onstraint

Fix denominator constraint
  • Loading branch information
area authored Jul 9, 2018
2 parents 69ca157 + 64d4394 commit b333ae5
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 127 deletions.
16 changes: 8 additions & 8 deletions contracts/ColonyFunding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,18 @@ contract ColonyFunding is ColonyStorage, DSMath {
// squareRoots[5] - square root of denominator
// squareRoots[6] - square root of payout.amount

require(mul(_squareRoots[0], _squareRoots[0]) <= _userReputation, "colony-reward-payout-invalid-parametar-user-reputation");
require(mul(_squareRoots[1], _squareRoots[1]) <= userTokens, "colony-reward-payout-invalid-parametar-user-token");
require(mul(_squareRoots[2], _squareRoots[2]) <= _totalReputation, "colony-reward-payout-invalid-parametar-total-reputation");
require(mul(_squareRoots[3], _squareRoots[3]) <= payout.totalTokens, "colony-reward-payout-invalid-parametar-total-tokens");
require(mul(_squareRoots[6], _squareRoots[6]) <= payout.amount, "colony-reward-payout-invalid-parametar-amount");
require(mul(_squareRoots[0], _squareRoots[0]) <= _userReputation, "colony-reward-payout-invalid-parameter-user-reputation");
require(mul(_squareRoots[1], _squareRoots[1]) <= userTokens, "colony-reward-payout-invalid-parameter-user-token");
require(mul(_squareRoots[2], _squareRoots[2]) >= _totalReputation, "colony-reward-payout-invalid-parameter-total-reputation");
require(mul(_squareRoots[3], _squareRoots[3]) >= payout.totalTokens, "colony-reward-payout-invalid-parameter-total-tokens");
require(mul(_squareRoots[6], _squareRoots[6]) <= payout.amount, "colony-reward-payout-invalid-parameter-amount");
uint256 numerator = mul(_squareRoots[0], _squareRoots[1]);
uint256 denominator = mul(_squareRoots[2], _squareRoots[3]);

require(mul(_squareRoots[4], _squareRoots[4]) <= numerator, "colony-reward-payout-invalid-parametar-numerator");
require(mul(_squareRoots[5], _squareRoots[5]) <= denominator, "colony-reward-payout-invalid-parametar-denominator");
require(mul(_squareRoots[4], _squareRoots[4]) <= numerator, "colony-reward-payout-invalid-parameter-numerator");
require(mul(_squareRoots[5], _squareRoots[5]) >= denominator, "colony-reward-payout-invalid-parameter-denominator");

uint256 reward = (mul(_squareRoots[4], _squareRoots[6]) / (_squareRoots[5] + 1)) ** 2;
uint256 reward = (mul(_squareRoots[4], _squareRoots[6]) / _squareRoots[5]) ** 2;

tokenLocking.unlockTokenForUser(address(token), msg.sender, _payoutId);

Expand Down
3 changes: 1 addition & 2 deletions contracts/IColony.sol
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,9 @@ contract IColony {
/// @return Reputation root hash at the time of creation
/// @return Total colony tokens at the time of creation
/// @return Total amount of tokens taken aside for reward payout
/// @return Remaining (unclaimed) amount of tokens
/// @return Token address
/// @return Block number at the time of creation
function getRewardPayoutInfo(uint256 _payoutId) public view returns (bytes32, uint256, uint256, uint256, address, uint256);
function getRewardPayoutInfo(uint256 _payoutId) public view returns (bytes32, uint256, uint256, address, uint256);

/// @notice Finalises the reward payout. Allows creation of next reward payouts for token that has been used in `_payoutId`
/// Can only be called when reward payout cycle is finished i.e when 60 days have passed from its creation
Expand Down
13 changes: 6 additions & 7 deletions gasCosts/gasCosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ contract("All", accounts => {
});

it("when working with reward payouts", async () => {
const totalReputation = toBN(350 * 1e18);
const totalReputation = toBN(300 * 1e18);
const workerReputation = toBN(200 * 1e18);
const managerReputation = toBN(100 * 1e18);
const initialFunding = toBN(360 * 1e18);
Expand All @@ -338,7 +338,7 @@ contract("All", accounts => {
await newToken.setOwner(colonyAddress);

await fundColonyWithTokens(newColony, otherToken, initialFunding.toString());
await fundColonyWithTokens(newColony, newToken, initialFunding.toString());
await newColony.mintTokens(workerReputation.add(managerReputation).toString());

await newColony.bootstrapColony([WORKER, MANAGER], [workerReputation.toString(), managerReputation.toString()]);

Expand All @@ -357,13 +357,12 @@ contract("All", accounts => {
});

const workerReputationSqrt = bnSqrt(workerReputation);
const totalReputationSqrt = bnSqrt(workerReputation.add(managerReputation));
const totalReputationSqrt = bnSqrt(totalReputation, true);
const numeratorSqrt = bnSqrt(workerReputationSqrt.mul(workerReputationSqrt));
const denominatorSqrt = bnSqrt(totalReputationSqrt.mul(totalReputationSqrt));
const denominatorSqrt = bnSqrt(totalReputationSqrt.mul(totalReputationSqrt), true);

const info = await newColony.getRewardPayoutInfo.call(payoutId);

const amountSqrt = bnSqrt(info[2]);
const balance = await newColony.getPotBalance(0, otherToken.address);
const amountSqrt = bnSqrt(balance);

const squareRoots = [
workerReputationSqrt.toString(),
Expand Down
6 changes: 5 additions & 1 deletion helpers/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export async function createSignaturesTrezor(colony, taskId, signers, value, dat
return { sigV, sigR, sigS };
}

export function bnSqrt(bn) {
export function bnSqrt(bn, isGreater) {
let a = bn.add(web3Utils.toBN(1)).div(web3Utils.toBN(2));
let b = bn;
while (a.lt(b)) {
Expand All @@ -253,5 +253,9 @@ export function bnSqrt(bn) {
.add(a)
.div(web3Utils.toBN(2));
}

if (isGreater && b.mul(b).lt(bn)) {
b = b.addn(1);
}
return b;
}
Loading

0 comments on commit b333ae5

Please sign in to comment.