Skip to content

Commit

Permalink
test: revamp mock validator set
Browse files Browse the repository at this point in the history
  • Loading branch information
huyhuynh3103 committed Jan 27, 2024
1 parent d6a6b47 commit 79984fc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.0;

contract MockValidatorContract_OnlyTiming_ForHardhatTest {
event WrappedUpEpoch(uint256 newPeriod, uint256 newEpoch, bool periodEnding);
event CurrentPeriodUpdated(uint256 previousPeriod, uint256 currentPeriod);

uint256 public constant PERIOD_DURATION = 1 days;
/// @dev The number of blocks in a epoch
Expand All @@ -21,7 +22,6 @@ contract MockValidatorContract_OnlyTiming_ForHardhatTest {
}

function wrapUpEpoch() external payable {
require(epochEndingAt(block.number), "Tach");
uint256 _newPeriod = _computePeriod(block.timestamp);
bool _periodEnding = _isPeriodEnding(_newPeriod);

Expand All @@ -33,7 +33,9 @@ contract MockValidatorContract_OnlyTiming_ForHardhatTest {
}

_periodOf[_nextEpoch] = _newPeriod;
_lastUpdatedPeriod = _newPeriod;

setCurrentPeriod(_newPeriod);

emit WrappedUpEpoch(_newPeriod, _nextEpoch, _periodEnding);
}

Expand Down Expand Up @@ -86,7 +88,9 @@ contract MockValidatorContract_OnlyTiming_ForHardhatTest {
return _lastUpdatedPeriod;
}

function setCurrentPeriod(uint256 period) external {
function setCurrentPeriod(uint256 period) public {
emit CurrentPeriodUpdated(_lastUpdatedPeriod, period);

_lastUpdatedPeriod = period;
}

Expand Down
30 changes: 27 additions & 3 deletions test/bridge/integration/BaseIntegration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { MockValidatorContract_OnlyTiming_ForHardhatTest } from
import { PauseEnforcer } from "@ronin/contracts/ronin/gateway/PauseEnforcer.sol";
import { IPauseTarget } from "@ronin/contracts/interfaces/IPauseTarget.sol";
import { GatewayV3 } from "@ronin/contracts/extensions/GatewayV3.sol";
import { IBridgeManagerCallbackRegister } from "@ronin/contracts/interfaces/bridge/IBridgeManagerCallbackRegister.sol";

import { RoninBridgeManagerDeploy } from "@ronin/script/contracts/RoninBridgeManagerDeploy.s.sol";
import { RoninGatewayV3Deploy } from "@ronin/script/contracts/RoninGatewayV3Deploy.s.sol";
Expand Down Expand Up @@ -149,7 +150,7 @@ contract BaseIntegration_Test is Base_Test {
function _initializeRonin() internal {
_config.switchTo(Network.RoninLocal.key());

_validatorSet.setCurrentPeriod(block.timestamp / 1 days);
_validatorSet.setCurrentPeriod(block.timestamp / _validatorSet.PERIOD_DURATION() - 2);

_bridgeRewardInitialize();
_bridgeTrackingInitialize();
Expand Down Expand Up @@ -350,6 +351,26 @@ contract BaseIntegration_Test is Base_Test {
vm.prank(_param.roninBridgeManager.governors[0]);
_roninBridgeManager.proposeGlobalProposalStructAndCastVotes(globalProposal, supports_, signatures);
}

{
// set callback register
bytes memory calldata_ =
abi.encodeCall(IBridgeManagerCallbackRegister.registerCallbacks, (param.callbackRegisters));
GlobalProposal.GlobalProposalDetail memory globalProposal = _roninProposalUtils.createGlobalProposal({
expiryTimestamp: block.timestamp + 10,
targetOption: GlobalProposal.TargetOption.BridgeManager,
value: 0,
calldata_: calldata_,
gasAmount: 500_000,
nonce: _roninNonce++
});

SignatureConsumer.Signature[] memory signatures =
_roninProposalUtils.generateSignaturesGlobal(globalProposal, _param.test.governorPKs);

vm.prank(_param.roninBridgeManager.governors[0]);
_roninBridgeManager.proposeGlobalProposalStructAndCastVotes(globalProposal, supports_, signatures);
}
}

function _constructForMainchainBridgeManager() internal {
Expand Down Expand Up @@ -525,12 +546,15 @@ contract BaseIntegration_Test is Base_Test {

function _wrapUpEpoch() internal {
uint256 multiplier = _validatorSet.numberOfBlocksInEpoch();
console.log(block.number);
console.log("Before roll block.number: ", block.number);

vm.roll((block.number / multiplier + 1) * (multiplier) - 1);
vm.roll((block.number / multiplier + 1) * multiplier - 1);
console.log("After roll block.number: ", block.number);

vm.prank(block.coinbase);
console.log("Before wrap up", _validatorSet.currentPeriod());
_validatorSet.wrapUpEpoch();
console.log("After wrap up", _validatorSet.currentPeriod());
}

function _setTimestampToPeriodEnding() internal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ contract DepositVote_RoninGatewayV3_Test is BaseIntegration_Test {
}

function test_bulkDeposit_100Txs() public {
_setTimestampToPeriodEnding();
_wrapUpEpochAndMine();
_wrapUpEpochAndMine();
_setTimestampToPeriodEnding();

vm.deal(address(_bridgeReward), 10 ether);
address newBridgeOperator = makeAddr("new-bridge-operator");
Expand Down Expand Up @@ -124,16 +124,17 @@ contract DepositVote_RoninGatewayV3_Test is BaseIntegration_Test {

vm.prank(_param.roninBridgeManager.governors[0]);
_roninBridgeManager.updateBridgeOperator(newBridgeOperator);

_param.roninBridgeManager.bridgeOperators[0] = newBridgeOperator;

for (uint256 i; i < _numOperatorsForVoteExecuted; i++) {
vm.prank(_param.roninBridgeManager.bridgeOperators[i]);
_roninGatewayV3.tryBulkDepositFor(second50Receipts);
}

_setTimestampToPeriodEnding();
_wrapUpEpochAndMine();
_wrapUpEpochAndMine();
_setTimestampToPeriodEnding();

sampleReceipt.id = 101;

Expand Down

0 comments on commit 79984fc

Please sign in to comment.