Skip to content

Commit

Permalink
test: try deposit 100 txs
Browse files Browse the repository at this point in the history
  • Loading branch information
huyhuynh3103 committed Jan 25, 2024
1 parent d34969d commit 5817a36
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

- name: Run Forge tests
run: |
anvil --chain-id 2024 --port 8546 --block-time 1 &
anvil --chain-id 2 --port 8547 --block-time 1 &
anvil --chain-id 2024 --port 8546 --block-time 3 &
anvil --chain-id 2 --port 8547 --block-time 3 &
forge test -vvv
id: test
2 changes: 1 addition & 1 deletion script/Migration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract Migration is BaseMigrationV2, Utils {
// Undefined
} else if (network() == Network.RoninLocal.key() || network() == Network.EthLocal.key()) {
// test
param.test.numberOfBlocksInEpoch = 600;
param.test.numberOfBlocksInEpoch = 200;
param.test.proxyAdmin = makeAddr("proxy-admin");
param.test.dposGA = makeAddr("governance-admin");
param.test.mainchainChainId = Network.EthLocal.chainId();
Expand Down
57 changes: 33 additions & 24 deletions src/mocks/ronin/MockValidatorContract_OnlyTiming_ForHardhatTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pragma solidity ^0.8.0;

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

uint256 public constant PERIOD_DURATION = 1 days;
/// @dev The number of blocks in a epoch
uint256 internal _numberOfBlocksInEpoch;
Expand All @@ -11,14 +13,15 @@ contract MockValidatorContract_OnlyTiming_ForHardhatTest {
uint256 internal _lastUpdatedPeriod;
/// @dev The starting block of the last updated period
uint256 internal _currentPeriodStartAtBlock;
uint256[] internal _epochs;
/// @dev Mapping from epoch index => period index
mapping(uint256 => uint256) internal _periodOf;

constructor(uint256 __numberOfBlocksInEpoch) {
_numberOfBlocksInEpoch = __numberOfBlocksInEpoch;
_epochs.push(0);
}

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

Expand All @@ -31,39 +34,45 @@ contract MockValidatorContract_OnlyTiming_ForHardhatTest {

_periodOf[_nextEpoch] = _newPeriod;
_lastUpdatedPeriod = _newPeriod;
emit WrappedUpEpoch(_newPeriod, _nextEpoch, _periodEnding);
}

function endEpoch() external {
_epochs.push(block.number);
}
// function endEpoch() external {
// // _epochs.push(block.number);
// uint nextEpoch = epochOf(block.number) + 1;
// uint startBlockOfNextEpoch = nextEpoch * _numberOfBlocksInEpoch;
// uint currPeriod = _computePeriod(block.timestamp);
// for (uint i = block.number; i < startBlockOfNextEpoch; i++) {

function epochOf(uint256 _block) public view returns (uint256 _epoch) {
for (uint256 _i = _epochs.length; _i > 0; _i--) {
if (_block > _epochs[_i - 1]) {
return _i;
}
}
}
// }
// }

// function epochOf(uint256 _block) public view returns (uint256 _epoch) {
// for (uint256 _i = _epochs.length; _i > 0; _i--) {
// if (_block > _epochs[_i - 1]) {
// return _i;
// }
// }
// }

function epochEndingAt(uint256 _block) public view returns (bool) {
for (uint256 _i = 0; _i < _epochs.length; _i++) {
if (_block == _epochs[_i]) {
return true;
}
}
return false;
// for (uint256 _i = 0; _i < _epochs.length; _i++) {
// if (_block == _epochs[_i]) {
// return true;
// }
// }
// return false;

return (_block + 1) % _numberOfBlocksInEpoch == 0;
}

/// @dev Mapping from epoch index => period index
mapping(uint256 => uint256) internal _periodOf;

function getLastUpdatedBlock() external view returns (uint256) {
return _lastUpdatedBlock;
}

// function epochOf(uint256 _block) public view virtual returns (uint256) {
// return _block / _numberOfBlocksInEpoch + 1;
// }
function epochOf(uint256 _block) public view virtual returns (uint256) {
return _block / _numberOfBlocksInEpoch + 1;
}

function tryGetPeriodOfEpoch(uint256 _epoch) external view returns (bool _filled, uint256 _periodNumber) {
return (_epoch <= epochOf(block.number) || _periodOf[_epoch] > 0, _periodOf[_epoch]);
Expand Down
24 changes: 23 additions & 1 deletion test/bridge/integration/BaseIntegration.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { console2 as console } from "forge-std/console2.sol";
import { Base_Test } from "../../Base.t.sol";
import { LibSharedAddress } from "foundry-deployment-kit/libraries/LibSharedAddress.sol";
import { ISharedArgument } from "@ronin/script/interfaces/ISharedArgument.sol";
Expand Down Expand Up @@ -148,6 +149,8 @@ contract BaseIntegration_Test is Base_Test {
function _initializeRonin() internal {
_config.switchTo(Network.RoninLocal.key());

_validatorSet.setCurrentPeriod(block.timestamp / 1 days);

_bridgeRewardInitialize();
_bridgeTrackingInitialize();
_bridgeSlashInitialize();
Expand Down Expand Up @@ -204,7 +207,6 @@ contract BaseIntegration_Test is Base_Test {
param.rewardPerPeriod
);

_validatorSet.setCurrentPeriod(10);
vm.prank(_param.test.dposGA);
_bridgeReward.initializeREP2();
}
Expand Down Expand Up @@ -514,4 +516,24 @@ contract BaseIntegration_Test is Base_Test {
deployCodeTo("GeneralConfig.sol", type(GeneralConfig).creationCode, LibSharedAddress.CONFIG);
_config = IGeneralConfig(LibSharedAddress.CONFIG);
}

function _wrapUpEpochAndMine() internal {
_wrapUpEpoch();
// mine a dummy block
vm.roll(block.number + 1);
}

function _wrapUpEpoch() internal {
uint256 multiplier = _validatorSet.numberOfBlocksInEpoch();
console.log(block.number);

vm.roll((block.number / multiplier + 1) * (multiplier) - 1);

vm.prank(block.coinbase);
_validatorSet.wrapUpEpoch();
}

function _setTimestampToPeriodEnding() internal {
vm.warp(((block.timestamp / 1 days) + 1) * 1 days);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ contract EpochE1_VoteIsApprovedInLastEpoch_BridgeTracking_Test is BaseIntegratio
_config.switchTo(Network.RoninLocal.key());
vm.coinbase(makeAddr("coin-base-addr"));

_operators.push(_param.roninBridgeManager.bridgeOperators[0]);
_operators.push(_param.roninBridgeManager.bridgeOperators[1]);
_receiptId = 1;
_receiptKind = IBridgeTracking.VoteKind.Withdrawal;

// upgrade ronin gateway v3
_mockRoninGatewayV3 = new MockGatewayForTracking(address(_bridgeTracking));

Expand All @@ -32,11 +37,6 @@ contract EpochE1_VoteIsApprovedInLastEpoch_BridgeTracking_Test is BaseIntegratio
_wrapUpEpochAndMine();

_period = _validatorSet.currentPeriod();
_receiptId = 1;
_receiptKind = IBridgeTracking.VoteKind.Withdrawal;

_operators.push(_param.roninBridgeManager.bridgeOperators[0]);
_operators.push(_param.roninBridgeManager.bridgeOperators[1]);
}

// Epoch e-1: Vote & Approve & Vote > Should not record when not approved yet. Vote in last epoch (e-1).
Expand Down Expand Up @@ -78,13 +78,13 @@ contract EpochE1_VoteIsApprovedInLastEpoch_BridgeTracking_Test is BaseIntegratio
function test_epochE_notRecordForCurrentPeriod_WhenWrappingUpPeriod() public {
test_epochE1_notRecordVoteAndBallot_voteInLastEpoch();

uint256 lastPeriod = _period;
_setTimestampToPeriodEnding();
_wrapUpEpochAndMine();

uint256 lastPeriod = _period;
uint256 newPeriod = _validatorSet.currentPeriod();
_period = newPeriod;
assertTrue(newPeriod != lastPeriod);
assertEq(newPeriod, lastPeriod + 1);

assertEq(_bridgeTracking.totalVote(lastPeriod), 0);
assertEq(_bridgeTracking.totalBallot(lastPeriod), 0);
Expand Down Expand Up @@ -194,16 +194,4 @@ contract EpochE1_VoteIsApprovedInLastEpoch_BridgeTracking_Test is BaseIntegratio
assertEq(_bridgeTracking.totalBallotOf(newPeriod, _param.roninBridgeManager.bridgeOperators[4]), 0);
assertEq(_bridgeTracking.totalBallotOf(newPeriod, _param.roninBridgeManager.bridgeOperators[5]), 0);
}

function _wrapUpEpochAndMine() internal {
_validatorSet.endEpoch();
vm.prank(block.coinbase);
_validatorSet.wrapUpEpoch();
// mine a dummy block
vm.roll(block.number + 1);
}

function _setTimestampToPeriodEnding() internal {
vm.warp(((block.timestamp / 86400) + 1) * 86400);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,4 @@ contract EpochE2_VoteIsNotApprovedInLastEpoch_BridgeTracking_Test is BaseIntegra
assertEq(_bridgeTracking.totalBallotOf(newPeriod, _param.roninBridgeManager.bridgeOperators[2]), 0);
assertEq(_bridgeTracking.totalBallotOf(newPeriod, _param.roninBridgeManager.bridgeOperators[3]), 0);
}

function _wrapUpEpochAndMine() internal {
_validatorSet.endEpoch();
vm.prank(block.coinbase);
_validatorSet.wrapUpEpoch();
// mine a dummy block
vm.roll(block.number + 1);
}

function _setTimestampToPeriodEnding() internal {
vm.warp(((block.timestamp / 86400) + 1) * 86400);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { console2 as console } from "forge-std/console2.sol";
import { Transfer } from "@ronin/contracts/libraries/Transfer.sol";
import { Token } from "@ronin/contracts/libraries/Token.sol";
import { ContractType } from "@ronin/contracts/utils/ContractType.sol";
Expand All @@ -14,6 +15,8 @@ contract DepositVote_RoninGatewayV3_Test is BaseIntegration_Test {

Transfer.Receipt[] _depositReceipts;
uint256 _numOperatorsForVoteExecuted;
Transfer.Receipt[] first50Receipts;
Transfer.Receipt[] second50Receipts;

function setUp() public virtual override {
super.setUp();
Expand Down Expand Up @@ -84,4 +87,59 @@ contract DepositVote_RoninGatewayV3_Test is BaseIntegration_Test {
assertEq(totalWeight, (_numOperatorsForVoteExecuted) * 100);
}
}

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

vm.deal(address(_bridgeReward), 10 ether);
address newBridgeOperator = makeAddr("new-bridge-operator");
Transfer.Receipt memory sampleReceipt = Transfer.Receipt({
id: 0,
kind: Transfer.Kind.Deposit,
ronin: Token.Owner({ addr: makeAddr("recipient"), tokenAddr: address(_roninWeth), chainId: _param.test.roninChainId }),
mainchain: Token.Owner({
addr: makeAddr("requester"),
tokenAddr: address(_mainchainWeth),
chainId: _param.test.mainchainChainId
}),
info: Token.Info({ erc: Token.Standard.ERC20, id: 0, quantity: 100 })
});

uint256 id = 1;
for (uint256 i; i < 50; i++) {
first50Receipts.push(sampleReceipt);
second50Receipts.push(sampleReceipt);
first50Receipts[i].id = id;
second50Receipts[i].id = id + 50;

id++;
}

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

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);
}

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

sampleReceipt.id = 101;

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

0 comments on commit 5817a36

Please sign in to comment.