Skip to content

Commit

Permalink
feat: mock valid epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaaschmidt committed Sep 19, 2024
1 parent bec818b commit ca84231
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 50 deletions.
15 changes: 11 additions & 4 deletions contracts/test/Deployment.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ contract DeploymentTest is Test, DeploymentFixture {
/// @notice Test that the deployment setup is as intended
function testCheckDeployment(uint256 _salt) public {
// Withdraw
assertEq(WithdrawV1(address(withdrawProxy)).getRiver(), address(riverProxy), "Withdraw: river contract incorrectly set");

assertEq(
WithdrawV1(address(withdrawProxy)).getRiver(),
address(riverProxy),
"Withdraw: river contract incorrectly set"
);

// Allowlist
assert(address(allowlistFirewall) != address(0));
assertEq(allowlistFirewall.executor(), executor, "AllowlistFirewall: executor address mismatch");
Expand All @@ -49,6 +53,9 @@ contract DeploymentTest is Test, DeploymentFixture {
// Oracle
address riverOracle = OracleManagerV1(payable(address(riverProxy))).getOracle(); // should return proxy
assertTrue(riverOracle == address(oracleProxy), "River: oracle incorrectly set.");
assertTrue(OracleManagerV1(payable(address(riverProxy))).getOracle() == address(oracleProxy), "River: oracle incorrectly set.");
assertTrue(
OracleManagerV1(payable(address(riverProxy))).getOracle() == address(oracleProxy),
"River: oracle incorrectly set."
);
}
}
}
147 changes: 125 additions & 22 deletions contracts/test/OracleIntegration.1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "./utils/RiverHelper.sol";
import "./utils/events/OracleEvents.sol";
import "../src/libraries/LibUnstructuredStorage.sol";
import "../src/state/river/OracleAddress.sol";
import "../src/state/river/LastConsensusLayerReport.sol";
// contracts
import "../src/Allowlist.1.sol";
import "../src/River.1.sol";
Expand Down Expand Up @@ -74,9 +75,49 @@ contract OracleIntegrationTest is Test, DeploymentFixture, RiverHelper, OracleEv
clr.validatorsExitingBalance = 0;
clr.validatorsSkimmedBalance = 0;
clr.validatorsExitedBalance = 0;
clr.epoch = framesBetween * epochsPerFrame;

vm.warp((clr.epoch + epochsUntilFinal) * (secondsPerSlot * slotsPerEpoch));

{

uint256 blockTimestamp = 1726660451;
// set the current epoch:
// vm.roll(blockNumber);
vm.warp(blockTimestamp);
// 1st condition _currentEpoch(_cls) >= _epoch + _cls.epochsToAssumedFinality
uint256 currentEpoch = oracleManager.getCurrentEpochId();
// uint256 currentEpoch = oracleManager.getCurrentEpochId();
uint256 tentativeEpoch = currentEpoch + epochsPerFrame -54 -225;
clr.epoch = 312075;

// uint256 expectedEpoch = oracleManager.getExpectedEpochId();
// uint256 currentEpoch = oracleManager.getCurrentEpochId();
// // currentEpoch >= inputEpoch + epochsToAssumedFinalty
// // AND inputEpoch > LastConsensusLayerEpoch
// // AND inputEpoch % epochsPerFrame == 0 (divisible by 225)
// uint256 tentativeEpoch = currentEpoch + epochsPerFrame -54 -225;
// clr.epoch = tentativeEpoch;
uint256 lastConsensusEpoch = oracleManager.getLastCompletedEpochId();
// assert(currentEpoch >= tentativeEpoch+epochsToAssumedFinality);
assert(tentativeEpoch > lastConsensusEpoch);
assert(tentativeEpoch % epochsPerFrame == 0);
assert(oracleManager.isValidEpoch(tentativeEpoch));
// uint256 expectedEpoch = oracleManager.getExpectedEpochId();
// uint256 currentEpoch = oracleManager.getCurrentEpochId();
// // currentEpoch >= inputEpoch + epochsToAssumedFinalty
// // AND inputEpoch > LastConsensusLayerEpoch
// // AND inputEpoch % epochsPerFrame == 0 (divisible by 225)
// uint256 lastConsensusEpoch = oracleManager.getLastCompletedEpochId();
// uint256 tentativeEpoch = lastConsensusEpoch + epochsPerFrame -54 -225;
// clr.epoch = tentativeEpoch;
// // uint256 lastConsensusEpoch = oracleManager.getLastCompletedEpochId();
}
// clr.epoch = 312075;
// // framesBetween * epochsPerFrame;
// vm.warp(20777406);
// vm.warp(1641070800);
// vm.warp((clr.epoch + epochsUntilFinal) * (secondsPerSlot * slotsPerEpoch));
// internal epoch: 312075

// vm.warp((clr.epoch + epochsUntilFinal) * (secondsPerSlot * slotsPerEpoch));
vm.deal(address(elFeeRecipientProxy), maxIncrease);
uint256 committedAmount = riverImpl.getCommittedBalance();
uint256 depositAmount = riverImpl.getBalanceToDeposit();
Expand All @@ -97,34 +138,96 @@ contract OracleIntegrationTest is Test, DeploymentFixture, RiverHelper, OracleEv
uint256 riverBalanceBefore = address(riverImpl).balance;

// Oracle level
vm.expectEmit(true, true, true, true);
emit ReportedConsensusLayerData(address(member), keccak256(abi.encode(clr)), clr, 1, 1);
// vm.expectEmit(true, true, true, true);
// emit ReportedConsensusLayerData(address(member), keccak256(abi.encode(clr)), clr, 1, 1);

// Oracle manager level
vm.expectEmit(true, false, false, false);
IOracleManagerV1.ConsensusLayerDataReportingTrace memory newStruct;
emit ProcessedConsensusLayerReport(clr, newStruct);
// // Oracle manager level
// vm.expectEmit(true, false, false, false);
// IOracleManagerV1.ConsensusLayerDataReportingTrace memory newStruct;
// emit ProcessedConsensusLayerReport(clr, newStruct);

uint256 supplyBeforeReport = riverImpl.totalSupply();

// call as oracle member
vm.prank(member);
// IOracleManagerV1.StoredConsensusLayerReport memory mockReport;
// mockReport.epoch = 123456; // Example value
// vm.mockCall(
// address(river),
// abi.encodeWithSelector(bytes4(keccak256("get()"))),
// abi.encode(mockReport)
// );
bytes32 baseSlot = LastConsensusLayerReport.LAST_CONSENSUS_LAYER_REPORT_SLOT;

// Mock the storage for LastConsensusLayerReport.get().epoch
uint256 mockEpochValue = clr.epoch; // Example value
vm.store(address(oracleManager), baseSlot, bytes32(mockEpochValue));

uint256 lastConsensusEpoch = oracleManager.getLastCompletedEpochId();
OracleV1(address(oracleProxy)).reportConsensusLayerData(clr);

// check river balance increased upon reporting
uint256 elBalanceAfter = address(elFeeRecipient).balance;
uint256 riverBalanceAfter = address(river).balance;

// pulls committed amounts from ELFeeRecipient into River
uint256 elBalanceDecrease = elBalanceBefore - elBalanceAfter;
uint256 riverBalanceIncrease = riverBalanceAfter - riverBalanceBefore;
assert(riverBalanceIncrease == elBalanceDecrease);

// assert rewards shares were minted token supply increased
uint256 supplyAfterReport = riverImpl.totalSupply();
assert(supplyAfterReport == supplyBeforeReport);
// // check river balance increased upon reporting
// uint256 elBalanceAfter = address(elFeeRecipient).balance;
// uint256 riverBalanceAfter = address(river).balance;

// // pulls committed amounts from ELFeeRecipient into River
// uint256 elBalanceDecrease = elBalanceBefore - elBalanceAfter;
// uint256 riverBalanceIncrease = riverBalanceAfter - riverBalanceBefore;
// assert(riverBalanceIncrease == elBalanceDecrease);

// // assert rewards shares were minted token supply increased
// uint256 supplyAfterReport = riverImpl.totalSupply();
// assert(supplyAfterReport == supplyBeforeReport);

assertEq(riverImpl.getCommittedBalance() % 32 ether, 0);
// assertEq(riverImpl.getCommittedBalance() % 32 ether, 0);
}


// function getEpochsReady(OracleManagerV1 oracleManager) public view returns {
// // 1st condition _currentEpoch(_cls) >= _epoch + _cls.epochsToAssumedFinality
// uint256 currentEpoch = oracleManager.getCurrentEpochId();
// // 2nd condition _epoch > LastConsensusLayerReport.get().epoch

// // 3rd condition _epoch % _cls.epochsPerFrame == 0
// }

function testEpoch() external {
uint256 currentEpoch = oracleManager.getCurrentEpochId();
console.log("currentEpoch", currentEpoch);
// uint256 blockNumber = 20777406;
uint256 blockTimestamp = 1726660451;
// set the current epoch:
// vm.roll(blockNumber);
vm.warp(blockTimestamp);
// 1st condition _currentEpoch(_cls) >= _epoch + _cls.epochsToAssumedFinality
currentEpoch = oracleManager.getCurrentEpochId();
console.log("currentEpoch", currentEpoch);
// 2nd condition _epoch > LastConsensusLayerReport.get().epoch

// IOracleManagerV1.StoredConsensusLayerReport memory mockReport;
// mockReport.epoch = 123456; // Example value
// vm.mockCall(
// address(river),
// abi.encodeWithSelector(bytes4(keccak256("get()"))),
// abi.encode(mockReport)
// );

// bytes32 baseSlot = LastConsensusLayerReport.LAST_CONSENSUS_LAYER_REPORT_SLOT;
// bytes32 epochSlot = keccak256(abi.encodePacked(baseSlot));

// // Mock the storage for LastConsensusLayerReport.get().epoch
// uint256 mockEpochValue = 123456; // Example value
// vm.store(address(oracleManager), epochSlot, bytes32(mockEpochValue));

bytes32 baseSlot = LastConsensusLayerReport.LAST_CONSENSUS_LAYER_REPORT_SLOT;

// Mock the storage for LastConsensusLayerReport.get().epoch
uint256 mockEpochValue = 123456; // Example value
vm.store(address(oracleManager), baseSlot, bytes32(mockEpochValue));

uint256 lastConsensusEpoch = oracleManager.getLastCompletedEpochId();
console.log("consensus", lastConsensusEpoch);
// bytes32(uint256(0))
// 3rd condition _epoch % _cls.epochsPerFrame == 0
}
}
3 changes: 1 addition & 2 deletions contracts/test/River.1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import "forge-std/Test.sol";
import "./fixtures/RiverV1ForceCommittable.sol";
import "./fixtures/OperatorsRegistryWithOverridesV1.sol";
import "./fixtures/RiverV1TestBase.sol";

// mocks
// mocks
import "./mocks/DepositContractMock.sol";
// contracts
import "../src/libraries/LibAllowlistMasks.sol";
Expand Down
4 changes: 0 additions & 4 deletions contracts/test/fixtures/RiverV1ForceCommittable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ contract RiverV1ForceCommittable is RiverV1 {
_setCommittedBalance(CommittedBalance.get() + BalanceToDeposit.get());
_setBalanceToDeposit(0);
}

function _isValidEpoch(CLSpec.CLSpecStruct memory _cls, uint256 _epoch) internal view override returns (bool) {
return true;
}
}
32 changes: 18 additions & 14 deletions contracts/test/utils/RiverHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ abstract contract RiverHelperTestBase is Test, BytesGenerator {
/// @title RiverHelper
/// @notice Helper functions for testing River contract functionality
contract RiverHelper is RiverHelperTestBase {

function _next(uint256 _salt) internal pure returns (uint256 _newSalt) {
return uint256(keccak256(abi.encode(_salt)));
}
Expand All @@ -39,8 +38,15 @@ contract RiverHelper is RiverHelperTestBase {
vm.stopPrank();
}

function _depositValidators(IAllowlistV1 allowlist, address allower, IOperatorsRegistryV1 operatorsRegistry,
RiverV1ForceCommittable river, address admin, uint256 count, uint256 _salt) internal returns (uint256) {
function _depositValidators(
IAllowlistV1 allowlist,
address allower,
IOperatorsRegistryV1 operatorsRegistry,
RiverV1ForceCommittable river,
address admin,
uint256 count,
uint256 _salt
) internal returns (uint256) {
address depositor = uf._new(_salt);
_salt = _next(_salt);
_allow(allowlist, allower, depositor);
Expand Down Expand Up @@ -79,19 +85,17 @@ contract RiverHelper is RiverHelperTestBase {
clr.stoppedValidatorCountPerOperator[0] = 0;
}

function _generateEmptyReport(uint256 stoppedValidatorsCountElements)
internal
pure
returns (IOracleManagerV1.ConsensusLayerReport memory clr)
{
function _generateEmptyReport(
uint256 stoppedValidatorsCountElements
) internal pure returns (IOracleManagerV1.ConsensusLayerReport memory clr) {
clr.stoppedValidatorCountPerOperator = new uint32[](stoppedValidatorsCountElements);
}

function debug_maxIncrease(ReportBounds.ReportBoundsStruct memory rb, uint256 _prevTotalEth, uint256 _timeElapsed)
internal
pure
returns (uint256)
{
function debug_maxIncrease(
ReportBounds.ReportBoundsStruct memory rb,
uint256 _prevTotalEth,
uint256 _timeElapsed
) internal pure returns (uint256) {
return (_prevTotalEth * rb.annualAprUpperBound * _timeElapsed) / (LibBasisPoints.BASIS_POINTS_MAX * 365 days);
}
}
}
2 changes: 1 addition & 1 deletion contracts/test/utils/events/OracleEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ contract OracleEvents {
uint256 _newTotalUnderlyingBalance,
uint256 _newTotalSupply
);
}
}
6 changes: 3 additions & 3 deletions contracts/test/utils/events/RiverEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ contract RiverEvents {
event SetOperatorsRegistry(address indexed operatorsRegistry);
event SetBalanceToDeposit(uint256 oldAmount, uint256 newAmount);
event ProcessedConsensusLayerReport(
IOracleManagerV1.ConsensusLayerReport report, IOracleManagerV1.ConsensusLayerDataReportingTrace trace
IOracleManagerV1.ConsensusLayerReport report,
IOracleManagerV1.ConsensusLayerDataReportingTrace trace
);
event ReportedConsensusLayerData(
address indexed member,
Expand All @@ -28,5 +29,4 @@ contract RiverEvents {
uint256 voteCount,
uint256 quorum
);

}
}

0 comments on commit ca84231

Please sign in to comment.