From 79984fcea3a0442efa199f50725b5b5f57e6ef81 Mon Sep 17 00:00:00 2001 From: huyhuynh3103 Date: Sat, 27 Jan 2024 18:40:54 +0700 Subject: [PATCH] test: revamp mock validator set --- ...atorContract_OnlyTiming_ForHardhatTest.sol | 10 +++++-- test/bridge/integration/BaseIntegration.t.sol | 30 +++++++++++++++++-- .../depositVote.RoninGatewayV3.t.sol | 5 ++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/mocks/ronin/MockValidatorContract_OnlyTiming_ForHardhatTest.sol b/src/mocks/ronin/MockValidatorContract_OnlyTiming_ForHardhatTest.sol index 09382fe4..6c3d439e 100644 --- a/src/mocks/ronin/MockValidatorContract_OnlyTiming_ForHardhatTest.sol +++ b/src/mocks/ronin/MockValidatorContract_OnlyTiming_ForHardhatTest.sol @@ -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 @@ -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); @@ -33,7 +33,9 @@ contract MockValidatorContract_OnlyTiming_ForHardhatTest { } _periodOf[_nextEpoch] = _newPeriod; - _lastUpdatedPeriod = _newPeriod; + + setCurrentPeriod(_newPeriod); + emit WrappedUpEpoch(_newPeriod, _nextEpoch, _periodEnding); } @@ -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; } diff --git a/test/bridge/integration/BaseIntegration.t.sol b/test/bridge/integration/BaseIntegration.t.sol index f9390ed5..b9ba4536 100644 --- a/test/bridge/integration/BaseIntegration.t.sol +++ b/test/bridge/integration/BaseIntegration.t.sol @@ -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"; @@ -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(); @@ -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 { @@ -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 { diff --git a/test/bridge/integration/ronin-gateway/depositVote.RoninGatewayV3.t.sol b/test/bridge/integration/ronin-gateway/depositVote.RoninGatewayV3.t.sol index 6e121e32..907554ba 100644 --- a/test/bridge/integration/ronin-gateway/depositVote.RoninGatewayV3.t.sol +++ b/test/bridge/integration/ronin-gateway/depositVote.RoninGatewayV3.t.sol @@ -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"); @@ -124,6 +124,7 @@ 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++) { @@ -131,9 +132,9 @@ contract DepositVote_RoninGatewayV3_Test is BaseIntegration_Test { _roninGatewayV3.tryBulkDepositFor(second50Receipts); } + _setTimestampToPeriodEnding(); _wrapUpEpochAndMine(); _wrapUpEpochAndMine(); - _setTimestampToPeriodEnding(); sampleReceipt.id = 101;