Skip to content

Commit

Permalink
Nonfuzzy fill (#529)
Browse files Browse the repository at this point in the history
* work in progress

* reproduced fenwick tree failures at last index

* proposed changes in light that max index is special case for prefix suim (#533)

* proposed changes in light that max index is special case for prefix suim

* removed comment

Co-authored-by: mwc <matt@ajna.finance>
Co-authored-by: Ed Noepel <ed@lpl.io>

* removed two temp tests

* ported Matt's changes to the fuzzed implementation

Co-authored-by: mattcushman <36414299+mattcushman@users.noreply.github.com>
Co-authored-by: mwc <matt@ajna.finance>
Co-authored-by: Ed Noepel <ed@lpl.io>
  • Loading branch information
4 people authored Jan 10, 2023
1 parent f16bc6c commit fad6557
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions tests/forge/utils/FenwickTreeInstance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,76 @@ contract FenwickTreeInstance is DSTestPlus {
) external {
uint256 i;
uint256 amount;
uint256 cumulativeAmount;

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

// Calculate total amount to insert
uint256 totalAmount = bound(amount_, 1 * 1e18, 9_000_000_000_000_000 * 1e18);
uint256 totalAmountDec = totalAmount;
amount_ = bound(amount_, 1 * 1e18, 9_000_000_000_000_000 * 1e18);

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

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

// Insert at random index
i = randomInRange(1, MAX_FENWICK_INDEX);

// If last iteration, insert remaining
amount = insertsDec == 1 ? totalAmountDec : (totalAmountDec % insertsDec) * randomInRange(1_000, 1 * 1e10, true);
amount = insertions_ == 1 ? amount_ : (amount_ % insertions_) * randomInRange(1_000, 1 * 1e10, true);

// Update values
add(i, amount);
totalAmountDec -= amount;
insertsDec -= 1;
amount_ -= amount;
insertions_ -= 1;
cumulativeAmount += amount;

// Verify tree sum
assertEq(deposits.treeSum(), totalAmount - totalAmountDec);
assertEq(deposits.treeSum(), cumulativeAmount);

if (trackInserts) inserts.push(i);
}

assertEq(deposits.treeSum(), totalAmount);
assertEq(deposits.treeSum(), cumulativeAmount);
}

/**
* @notice fills fenwick tree with deterministic values and tests additions.
*/
function nonFuzzyFill(
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;
uint256 cumulativeAmount;

// Initialize and print seed for randomness
setRandomSeed(seed_);

while (amount_ > 0 && insertions_ > 0) {

// Insert at random index
i = randomInRange(1, MAX_FENWICK_INDEX);

// If last iteration, insert remaining
amount = insertions_ == 1 ? amount_ : (amount_ % insertions_) * randomInRange(1_000, 1 * 1e10, true);

// Update values
add(i, amount);
amount_ -= amount;
insertions_ -= 1;
cumulativeAmount += amount;

// Verify tree sum
assertEq(deposits.treeSum(), cumulativeAmount);

if (trackInserts) inserts.push(i);
}

assertEq(deposits.treeSum(), cumulativeAmount);
}
}

0 comments on commit fad6557

Please sign in to comment.