Skip to content
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

reworked the RNG to use a deterministic seed #523

Merged
merged 4 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 12 additions & 13 deletions tests/forge/FenwickTree.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ contract FenwickTreeTest is DSTestPlus {
uint256 insertions_,
uint256 totalAmount_,
uint256 scaleIndex_,
uint256 factor_
) external {

_tree.fuzzyFill(insertions_, totalAmount_, false);
uint256 factor_,
uint256 seed_
) external {
_tree.fuzzyFill(insertions_, totalAmount_, seed_, false);

uint256 scaleIndex = bound(scaleIndex_, 2, MAX_INDEX);
uint256 subIndex = randomInRange(1, scaleIndex - 1);
Expand Down Expand Up @@ -159,10 +159,10 @@ contract FenwickTreeTest is DSTestPlus {
uint256 insertions_,
uint256 totalAmount_,
uint256 scaleIndex_,
uint256 factor_
) external {

_tree.fuzzyFill(insertions_, totalAmount_, false);
uint256 factor_,
uint256 seed_
) external {
_tree.fuzzyFill(insertions_, totalAmount_, seed_, false);

uint256 scaleIndex = bound(scaleIndex_, 2, 7388);
uint256 subIndex = randomInRange(0, scaleIndex - 1);
Expand Down Expand Up @@ -191,10 +191,10 @@ contract FenwickTreeTest is DSTestPlus {
// TODO: check random parent to verify sum post removal
function testLoadFenwickFuzzyRemoval(
uint256 insertions_,
uint256 totalAmount_
) external {

_tree.fuzzyFill(insertions_, totalAmount_, true);
uint256 totalAmount_,
uint256 seed_
) external {
_tree.fuzzyFill(insertions_, totalAmount_, seed_, true);

uint256 removalIndex = _tree.getIByInsertIndex(randomInRange(0, _tree.numInserts() - 1));
uint256 removalAmount = _tree.get(removalIndex);
Expand All @@ -208,7 +208,6 @@ contract FenwickTreeTest is DSTestPlus {
assertEq(preRemovalIndexSum - removalAmount, postRemovalIndexSum);
assertEq(preRemovalTreeSum - removalAmount, _tree.treeSum());
}

}

contract FenwickTreeGasLoadTest is DSTestPlus {
Expand Down
4 changes: 2 additions & 2 deletions tests/forge/Heap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ contract HeapTest is DSTestPlus {
assertEq(_loans.getTotalTps(), 7);
}

function testLoadHeapFuzzy(uint256 inserts_) public {
function testLoadHeapFuzzy(uint256 inserts_, uint256 seed_) public {

// test adding different TPs
_loans.fuzzyFill(inserts_, true);
_loans.fuzzyFill(inserts_, seed_, true);

// test adding different TPs
address removeAddress = _loans.getIdByInsertIndex(randomInRange(1, _loans.numInserts() - 1, true));
Expand Down
20 changes: 12 additions & 8 deletions tests/forge/utils/DSTestPlus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract contract DSTestPlus is Test, IPoolEvents {
using EnumerableSet for EnumerableSet.UintSet;

// nonce for generating random addresses
uint16 internal _nonce = 0;
uint256 internal _nonce = 0;

// mainnet address of AJNA token, because tests are forked
address internal _ajna = 0x9a96ec9B57Fb64FbC60B423d1f4da7691Bd35079;
Expand Down Expand Up @@ -1251,30 +1251,34 @@ abstract contract DSTestPlus is Test, IPoolEvents {
lup_ = _priceAt(lupIndex);
}

function setRandomSeed(uint256 seed) public {
_nonce = seed;
}

function getNextNonce() public returns (uint256) {
return _nonce == type(uint256).max ? 0 : ++_nonce;
}

function randomInRange(uint256 min, uint256 max) public returns (uint256) {
return randomInRange(min, max, false);
}

function randomInRange(uint256 min, uint256 max, bool nonZero) public returns (uint256) {
if (max == 0 && nonZero) return 1;
else if (max == min) return max;
uint256 rand = uint(keccak256(abi.encodePacked(block.timestamp, msg.sender, _nonce))) % (max - min + 1) + min;
_nonce++;
return rand;
return uint(keccak256(abi.encodePacked(msg.sender, getNextNonce()))) % (max - min + 1) + min;
}

// returns a random index between 1 and 7388
function _randomIndex() internal returns (uint256 index_) {
// calculate a random index between 1 and 7388
index_ = 1 + uint256(keccak256(abi.encodePacked(block.number, block.difficulty))) % 7387;
vm.roll(block.number + 1); // advance block to ensure that the index price is different
index_ = 1 + uint256(keccak256(abi.encodePacked(msg.sender, getNextNonce()))) % 7387;
}

// returns a random index between 1 and a given maximum
// used for testing in NFT pools where higher indexes (and lower prices) would require so many NFTs that gas and memory limits would be exceeded
function _randomIndexWithMinimumPrice(uint256 minimumPrice_) internal returns (uint256 index_) {
index_ = 1 + uint256(keccak256(abi.encodePacked(block.number, block.difficulty))) % minimumPrice_;
vm.roll(block.number + 1); // advance block to ensure that the index price is different
index_ = 1 + uint256(keccak256(abi.encodePacked(msg.sender, getNextNonce()))) % minimumPrice_;
}

// find the bucket index in array corresponding to the highest bucket price
Expand Down
13 changes: 7 additions & 6 deletions tests/forge/utils/FenwickTreeInstance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ contract FenwickTreeInstance is DSTestPlus {
* @notice fills fenwick tree with fuzzed values and tests additions.
*/
function fuzzyFill(
uint256 insertions_,
uint256 amount_,
bool trackInserts)
external {

uint256 insertions_, // number of insertions to perform
uint256 amount_, // total amount to insert
uint256 seed_, // seed for psuedorandom number generator
bool trackInserts
) external {
uint256 i;
uint256 amount;

Expand All @@ -83,6 +83,8 @@ contract FenwickTreeInstance is DSTestPlus {
uint256 totalAmount = bound(amount_, 1 * 1e18, 9_000_000_000_000_000 * 1e18);
uint256 totalAmountDec = totalAmount;

// Initialize and print seed for randomness
setRandomSeed(bound(seed_, 0, type(uint256).max - 1));

while (totalAmountDec > 0 && insertsDec > 0) {

Expand All @@ -106,4 +108,3 @@ contract FenwickTreeInstance is DSTestPlus {
assertEq(deposits.treeSum(), totalAmount);
}
}

12 changes: 7 additions & 5 deletions tests/forge/utils/HeapInstance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,20 @@ contract HeapInstance is DSTestPlus {
* @notice fills Heap with fuzzed values and tests additions.
*/
function fuzzyFill(
uint256 inserts_,
bool trackInserts_)
external {

uint256 inserts_, // number of insertions to perform
uint256 seed_, // seed for psuedorandom number generator
bool trackInserts_
) external {
uint256 tp;
address borrower;

// Calculate total insertions
uint256 totalInserts = bound(inserts_, 1000, 2000);
uint256 insertsDec = totalInserts;

// Initialize and print seed for randomness
setRandomSeed(bound(seed_, 0, type(uint256).max - 1));

while (insertsDec > 0) {

// build address and TP
Expand All @@ -91,4 +94,3 @@ contract HeapInstance is DSTestPlus {
assertEq(_heap.loans.length - 1, totalInserts);
}
}