-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out reputation scaling function
- Loading branch information
Showing
7 changed files
with
154 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
This file is part of The Colony Network. | ||
The Colony Network is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The Colony Network is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with The Colony Network. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
pragma solidity 0.8.20; // ignore-swc-103 | ||
import "../../lib/dappsys/math.sol"; | ||
|
||
contract ScaleReputation is DSMath { | ||
// Note that scaleFactor should be a WAD. | ||
function scaleReputation(int256 reputationAmount, uint256 scaleFactor) internal pure returns (int256) { | ||
if (reputationAmount == 0 || scaleFactor == 0) { | ||
return 0; | ||
} | ||
|
||
int256 scaledReputation; | ||
int256 absAmount; | ||
|
||
if (reputationAmount == type(int256).min){ | ||
absAmount = type(int256).max; // Off by one, but best we can do - probably gets capped anyway | ||
} else { | ||
absAmount = reputationAmount >= 0 ? reputationAmount : -reputationAmount; | ||
} | ||
|
||
int256 sgnAmount = reputationAmount >= 0 ? int(1) : -1; | ||
|
||
// Guard against overflows during calculation with wmul | ||
if (type(uint256).max / scaleFactor < uint256(absAmount)){ | ||
if (sgnAmount == 1){ | ||
scaledReputation = type(int128).max; | ||
} else { | ||
scaledReputation = type(int128).min; | ||
} | ||
} else { | ||
scaledReputation = int256(wmul(scaleFactor, uint256(absAmount))) * sgnAmount; | ||
// Cap inside the range of int128, as we do for all reputations | ||
scaledReputation = imax(type(int128).min, scaledReputation); | ||
scaledReputation = imin(type(int128).max, scaledReputation); | ||
} | ||
return scaledReputation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
This file is part of The Colony Network. | ||
The Colony Network is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The Colony Network is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with The Colony Network. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
pragma solidity 0.8.20; // ignore-swc-103 | ||
import "../common/ScaleReputation.sol"; | ||
|
||
contract ScaleReputationTest is ScaleReputation { | ||
|
||
function scaleReputationPublic(int256 reputationAmount, uint256 scaleFactor) public pure returns (int256) { | ||
return scaleReputation(reputationAmount, scaleFactor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* globals artifacts */ | ||
|
||
const chai = require("chai"); | ||
const bnChai = require("bn-chai"); | ||
|
||
const { INT256_MAX, INT256_MIN, INT128_MIN, INT128_MAX, WAD } = require("../../helpers/constants"); | ||
|
||
const { expect } = chai; | ||
chai.use(bnChai(web3.utils.BN)); | ||
|
||
const ScaleReputationTest = artifacts.require("ScaleReputationTest"); | ||
|
||
let scaleReputationTest; | ||
|
||
contract("ScaleReputation", () => { | ||
before(async () => { | ||
scaleReputationTest = await ScaleReputationTest.new(); | ||
}); | ||
|
||
describe("when scaling reputation", () => { | ||
it("should scale reputation up", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(100, WAD.muln(2)); | ||
expect(scaled).to.eq.BN(200); | ||
}); | ||
|
||
it("should scale reputation down", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(100, WAD.divn(2)); | ||
expect(scaled).to.eq.BN(50); | ||
}); | ||
|
||
it("should cap negatively", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MAX.subn(10), WAD.muln(2)); | ||
expect(scaled).to.eq.BN(INT128_MAX); | ||
}); | ||
|
||
it("should cap positively", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MIN.addn(10), WAD.muln(2)); | ||
expect(scaled).to.eq.BN(INT128_MIN); | ||
}); | ||
|
||
it("deal with calculations that would arithmetically overflow", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(INT256_MAX.subn(10), WAD.subn(1)); | ||
expect(scaled).to.eq.BN(INT128_MAX); | ||
}); | ||
|
||
it("deal with calculations that would arithmetically underflow", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(INT256_MIN.addn(10), WAD.subn(1)); | ||
expect(scaled).to.eq.BN(INT128_MIN); | ||
}); | ||
|
||
it("deal with calculations that exceed our reputation cap during calculation, but not once calculation is complete", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MIN.addn(10), WAD.subn(1)); | ||
expect(scaled).to.be.gt.BN(INT128_MIN); | ||
}); | ||
|
||
it("deal with calculations that exceed our reputation cap during calculation, but not once calculation is complete", async () => { | ||
const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MAX.subn(10), WAD.subn(1)); | ||
expect(scaled).to.be.lt.BN(INT128_MAX); | ||
}); | ||
}); | ||
}); |