Skip to content

Commit

Permalink
add epoch unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmovses committed May 10, 2024
1 parent 782e2af commit 8f32226
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion protocol-units/dispute/src/MCR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ contract MCR {
uint256 public delta = 1 * SECONDS_IN_DAY;
uint256 public p = 1 * SECONDS_IN_MINUTE;
uint256 public supermajorityStake;
uint256 public epochStartTimestamp;

mapping(address => Validator) public validators;
mapping(bytes32 => Dispute) public disputes;
Expand All @@ -76,7 +77,7 @@ contract MCR {
constructor(
uint256 _delta,
uint256 _supermajorityStake,
uint256 _epochDurationInDays,
uint256 _epochDurationInDays
) {
delta = _delta;
supermajorityStake = _supermajorityStake;
Expand All @@ -88,6 +89,7 @@ contract MCR {
if (epochsPassed > 0) {
currentEpoch += epochsPassed;
epochStartTimestamp += epochsPassed * epochDuration;
epochStartTimestamp = block.timestamp;
}
}

Expand Down
36 changes: 36 additions & 0 deletions protocol-units/dispute/test/MCR.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../src/MCR.sol";

contract MCRTest is Test {
MCR public mcr;
uint256 public epochDuration = 7 days;

function setUp() public {
mcr = new MCR(1 days, 100 ether, epochDuration / 1 days);
}

function testUpdateEpoch() public {
// Test initial epoch
assertEq(mcr.currentEpoch(), 0);
assertEq(mcr.epochStartTimestamp(), block.timestamp);

// Advance time by 3 epochs
vm.warp(block.timestamp + 3 * epochDuration);

// Call updateEpoch and check updated values
mcr.updateEpoch();
assertEq(mcr.currentEpoch(), 3);
assertEq(mcr.epochStartTimestamp(), block.timestamp);

// Advance time by 1 epoch and 1 day
vm.warp(block.timestamp + epochDuration + 1 days);

// Call updateEpoch and check updated values
mcr.updateEpoch();
assertEq(mcr.currentEpoch(), 4);
assertEq(mcr.epochStartTimestamp(), block.timestamp - 1 days);
}
}

0 comments on commit 8f32226

Please sign in to comment.