Skip to content

Commit

Permalink
test: use inequality in inv test
Browse files Browse the repository at this point in the history
test: remove delay from store
  • Loading branch information
andreivladbrg committed Oct 16, 2024
1 parent 4ebdf2c commit 60fdb8f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 65 deletions.
23 changes: 4 additions & 19 deletions tests/invariant/Flow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,8 @@ contract Flow_Invariant_Test is Base_Test {

// Skip the voided streams.
if (!flow.isVoided(streamId)) {
(uint256 totalStreamedAmount, uint256 totalStreamedAmountWithDelay) =
calculateTotalStreamedAmounts(flowStore.streamIds(i), flow.getTokenDecimals(streamId));

assertGe(
totalStreamedAmount,
totalStreamedAmountWithDelay,
"Invariant violation: total streamed amount without delay >= total streamed amount with delay"
);

assertEq(
totalStreamedAmountWithDelay,
assertLe(
calculateTotalStreamedAmount(flowStore.streamIds(i), flow.getTokenDecimals(streamId)),
flow.totalDebtOf(streamId) + flowStore.withdrawnAmounts(streamId),
"Invariant violation: total streamed amount with delay = total debt + withdrawn amount"
);
Expand All @@ -361,13 +352,13 @@ contract Flow_Invariant_Test is Base_Test {
}

/// @dev Calculates the total streamed amounts by iterating over each period.
function calculateTotalStreamedAmounts(
function calculateTotalStreamedAmount(
uint256 streamId,
uint8 decimals
)
internal
view
returns (uint256 totalStreamedAmount, uint256 totalStreamedAmountWithDelay)
returns (uint256 totalStreamedAmount)
{
uint256 totalDelayedAmount;
uint256 periodsCount = flowStore.getPeriods(streamId).length;
Expand All @@ -380,12 +371,6 @@ contract Flow_Invariant_Test is Base_Test {

// Calculate the total streamed amount for the current period.
totalStreamedAmount += getDescaledAmount(period.ratePerSecond * elapsed, decimals);

// Calculate the total delayed amount for the current period.
totalDelayedAmount += getDescaledAmount(period.delay * period.ratePerSecond, decimals);
}

// Calculate the total streamed amount with delay.
totalStreamedAmountWithDelay = totalStreamedAmount - totalDelayedAmount;
}
}
6 changes: 0 additions & 6 deletions tests/invariant/handlers/FlowHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ contract FlowHandler is BaseHandler {
// Adjust the rate per second.
flow.adjustRatePerSecond(currentStreamId, newRatePerSecond);

flowStore.updateDelay(currentStreamId, previousRatePerSecond, decimals);
flowStore.pushPeriod(currentStreamId, newRatePerSecond.unwrap(), "adjustRatePerSecond");
}

Expand Down Expand Up @@ -161,10 +160,6 @@ contract FlowHandler is BaseHandler {
// Paused streams cannot be paused again.
vm.assume(!flow.isPaused(currentStreamId));

flowStore.updateDelay(
currentStreamId, flow.getRatePerSecond(currentStreamId).unwrap(), flow.getTokenDecimals(currentStreamId)
);

// Pause the stream.
flow.pause(currentStreamId);

Expand Down Expand Up @@ -291,7 +286,6 @@ contract FlowHandler is BaseHandler {
// If the stream isn't paused, update the delay:
uint128 ratePerSecond = flow.getRatePerSecond(currentStreamId).unwrap();
if (ratePerSecond > 0) {
flowStore.updateDelay(currentStreamId, ratePerSecond, flow.getTokenDecimals(currentStreamId));
flowStore.pushPeriod(currentStreamId, ratePerSecond, "withdraw");
}
}
Expand Down
42 changes: 2 additions & 40 deletions tests/invariant/stores/FlowStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ contract FlowStore {
/// @param ratePerSecond The rate per second for this period.
/// @param start The start time of the period.
/// @param end The end time of the period.
/// @param delay The delay for the period.
struct Period {
string typeOfPeriod;
uint128 ratePerSecond;
uint40 start;
uint40 end;
uint40 delay;
}

/// @dev Each stream is mapped to an array of periods. This is used to calculate the total streamed amount.
Expand Down Expand Up @@ -75,13 +73,7 @@ contract FlowStore {
// Store the stream id and the period during which provided ratePerSecond applies.
streamIds.push(streamId);
periods[streamId].push(
Period({
typeOfPeriod: "create",
ratePerSecond: ratePerSecond,
start: uint40(block.timestamp),
end: 0,
delay: 0
})
Period({ typeOfPeriod: "create", ratePerSecond: ratePerSecond, start: uint40(block.timestamp), end: 0 })
);

// Update the last stream id.
Expand All @@ -94,40 +86,10 @@ contract FlowStore {

// Push the new period with the provided rate per second.
periods[streamId].push(
Period({
ratePerSecond: ratePerSecond,
start: uint40(block.timestamp),
end: 0,
delay: 0,
typeOfPeriod: typeOfPeriod
})
Period({ ratePerSecond: ratePerSecond, start: uint40(block.timestamp), end: 0, typeOfPeriod: typeOfPeriod })
);
}

function updateDelay(uint256 streamId, uint128 ratePerSecond, uint8 decimals) external {
// Skip the delay update if the decimals are 18.
if (decimals == 18) {
return;
}

uint256 periodCount = periods[streamId].length - 1;
uint256 factor = uint128(10 ** (18 - decimals));
uint256 blockTimestamp = uint40(block.timestamp);
uint256 start = periods[streamId][periodCount].start;

uint256 rescaledStreamedAmount = ratePerSecond * (blockTimestamp - start) / factor * factor;

uint40 delay;
if (rescaledStreamedAmount > ratePerSecond) {
delay = uint40(blockTimestamp - start - (rescaledStreamedAmount / ratePerSecond));
// Since we are reverse engineering the delay, we need to subtract 1 from the delay, which would normally be
// added in the constant interval calculation
delay = delay > 1 ? delay - 1 : 0;
}

periods[streamId][periodCount].delay += delay;
}

function updatePreviousValues(
uint256 streamId,
uint40 snapshotTime,
Expand Down

0 comments on commit 60fdb8f

Please sign in to comment.