Skip to content

Commit

Permalink
feat+test: remove local chains and fix tryBulkDepositFor on RoninGate…
Browse files Browse the repository at this point in the history
…wayV3
  • Loading branch information
huyhuynh3103 committed Feb 2, 2024
1 parent aad0891 commit 0ffb0d5
Show file tree
Hide file tree
Showing 18 changed files with 211 additions and 180 deletions.
14 changes: 7 additions & 7 deletions script/Migration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ contract Migration is BaseMigrationV2, Utils {
// Undefined
} else if (network() == DefaultNetwork.RoninTestnet.key()) {
// Undefined
} else if (network() == Network.RoninLocal.key() || network() == Network.EthLocal.key()) {
} else if (
network() == Network.RoninLocal.key() || network() == Network.EthLocal.key()
|| network() == DefaultNetwork.Local.key()
) {
// test
param.test.numberOfBlocksInEpoch = 200;
param.test.proxyAdmin = makeAddr("proxy-admin");
param.test.dposGA = makeAddr("governance-admin");
param.test.mainchainChainId = Network.EthLocal.chainId();
param.test.roninChainId = Network.RoninLocal.chainId();

// tokens
param.weth.name = "Wrapped WETH";
Expand Down Expand Up @@ -95,7 +97,7 @@ contract Migration is BaseMigrationV2, Utils {
param.roninBridgeManager.num = 2;
param.roninBridgeManager.denom = 4;
param.roninBridgeManager.roninChainId = 0;
param.roninBridgeManager.roninChainId = param.test.roninChainId;
param.roninBridgeManager.roninChainId = block.chainid;
param.roninBridgeManager.expiryDuration = 60 * 60 * 24 * 14; // 14 days
param.roninBridgeManager.bridgeOperators = operatorAddrs;
param.roninBridgeManager.governors = governorAddrs;
Expand All @@ -108,7 +110,7 @@ contract Migration is BaseMigrationV2, Utils {
param.mainchainPauseEnforcer.sentries = wrapAddress(makeAddr("pause-enforcer-sentry"));

// Mainchain Gateway V3
param.mainchainGatewayV3.roninChainId = param.test.roninChainId;
param.mainchainGatewayV3.roninChainId = block.chainid;
param.mainchainGatewayV3.numerator = 1;
param.mainchainGatewayV3.highTierVWNumerator = 10;
param.mainchainGatewayV3.denominator = 10;
Expand All @@ -117,14 +119,12 @@ contract Migration is BaseMigrationV2, Utils {
param.mainchainBridgeManager.num = 2;
param.mainchainBridgeManager.denom = 4;
param.mainchainBridgeManager.roninChainId = 0;
param.mainchainBridgeManager.roninChainId = param.test.roninChainId;
param.mainchainBridgeManager.roninChainId = block.chainid;
param.mainchainBridgeManager.bridgeOperators = operatorAddrs;
param.mainchainBridgeManager.governors = governorAddrs;
param.mainchainBridgeManager.voteWeights = voteWeights;
param.mainchainBridgeManager.targetOptions = options;
param.mainchainBridgeManager.targets = targets;
} else if (network() == DefaultNetwork.Local.key()) {
// Undefined
} else {
revert("Migration: Network Unknown Shared Parameters Unimplemented!");
}
Expand Down
127 changes: 64 additions & 63 deletions src/ronin/gateway/RoninGatewayV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ contract RoninGatewayV3 is
* @dev Reverts if the method caller is not bridge operator.
*/
function _requireBridgeOperator() internal view {
if (!IBridgeManager(getContract(ContractType.BRIDGE_MANAGER)).isBridgeOperator(msg.sender))
if (!IBridgeManager(getContract(ContractType.BRIDGE_MANAGER)).isBridgeOperator(msg.sender)) {
revert ErrUnauthorized(msg.sig, RoleAccess.__DEPRECATED_BRIDGE_OPERATOR);
}
}

/**
Expand All @@ -88,7 +89,7 @@ contract RoninGatewayV3 is
uint256 _denominator,
uint256 _trustedNumerator,
uint256 _trustedDenominator,
address[] calldata /* _withdrawalMigrators */,
address[] calldata, /* _withdrawalMigrators */
// _packedAddresses[0]: roninTokens
// _packedAddresses[1]: mainchainTokens
address[][2] calldata _packedAddresses,
Expand Down Expand Up @@ -122,12 +123,13 @@ contract RoninGatewayV3 is
/**
* @inheritdoc IRoninGatewayV3
*/
function getWithdrawalSignatures(
uint256 withdrawalId,
address[] calldata operators
) external view returns (bytes[] memory _signatures) {
function getWithdrawalSignatures(uint256 withdrawalId, address[] calldata operators)
external
view
returns (bytes[] memory _signatures)
{
_signatures = new bytes[](operators.length);
for (uint256 _i = 0; _i < operators.length; ) {
for (uint256 _i = 0; _i < operators.length;) {
_signatures[_i] = _withdrawalSig[withdrawalId][operators[_i]];

unchecked {
Expand All @@ -143,25 +145,25 @@ contract RoninGatewayV3 is
address _sender = msg.sender;
_depositFor(_receipt, _sender, minimumVoteWeight());
IBridgeTracking(getContract(ContractType.BRIDGE_TRACKING)).recordVote(
IBridgeTracking.VoteKind.Deposit,
_receipt.id,
_sender
IBridgeTracking.VoteKind.Deposit, _receipt.id, _sender
);
}

/**
* @inheritdoc IRoninGatewayV3
*/
function tryBulkAcknowledgeMainchainWithdrew(
uint256[] calldata _withdrawalIds
) external onlyBridgeOperator returns (bool[] memory _executedReceipts) {
function tryBulkAcknowledgeMainchainWithdrew(uint256[] calldata _withdrawalIds)
external
onlyBridgeOperator
returns (bool[] memory _executedReceipts)
{
address _governor = msg.sender;
uint256 _minVoteWeight = minimumVoteWeight();

uint256 _withdrawalId;
_executedReceipts = new bool[](_withdrawalIds.length);
IBridgeTracking _bridgeTrackingContract = IBridgeTracking(getContract(ContractType.BRIDGE_TRACKING));
for (uint256 _i; _i < _withdrawalIds.length; ) {
for (uint256 _i; _i < _withdrawalIds.length;) {
_withdrawalId = _withdrawalIds[_i];
_bridgeTrackingContract.recordVote(IBridgeTracking.VoteKind.MainchainWithdrawal, _withdrawalId, _governor);
if (mainchainWithdrew(_withdrawalId)) {
Expand All @@ -187,23 +189,26 @@ contract RoninGatewayV3 is
/**
* @inheritdoc IRoninGatewayV3
*/
function tryBulkDepositFor(
Transfer.Receipt[] calldata _receipts
) external whenNotPaused onlyBridgeOperator returns (bool[] memory _executedReceipts) {
function tryBulkDepositFor(Transfer.Receipt[] calldata _receipts)
external
whenNotPaused
onlyBridgeOperator
returns (bool[] memory _executedReceipts)
{
address _sender = msg.sender;

Transfer.Receipt memory _receipt;
_executedReceipts = new bool[](_receipts.length);
uint256 _minVoteWeight = minimumVoteWeight();
IBridgeTracking _bridgeTrackingContract = IBridgeTracking(getContract(ContractType.BRIDGE_TRACKING));
for (uint256 _i; _i < _receipts.length; ) {
for (uint256 _i; _i < _receipts.length;) {
_receipt = _receipts[_i];
_bridgeTrackingContract.recordVote(IBridgeTracking.VoteKind.Deposit, _receipt.id, _sender);
if (depositVote[_receipt.mainchain.chainId][_receipt.id].status == VoteStatus.Executed) {
_executedReceipts[_i] = true;
} else {
_depositFor(_receipt, _sender, _minVoteWeight);
}
_bridgeTrackingContract.recordVote(IBridgeTracking.VoteKind.Deposit, _receipt.id, _sender);

unchecked {
++_i;
Expand All @@ -224,7 +229,7 @@ contract RoninGatewayV3 is
function bulkRequestWithdrawalFor(Transfer.Request[] calldata _requests, uint256 _chainId) external whenNotPaused {
if (_requests.length == 0) revert ErrEmptyArray();

for (uint256 _i; _i < _requests.length; ) {
for (uint256 _i; _i < _requests.length;) {
_requestWithdrawalFor(_requests[_i], msg.sender, _chainId);
unchecked {
++_i;
Expand All @@ -249,10 +254,11 @@ contract RoninGatewayV3 is
/**
* @inheritdoc IRoninGatewayV3
*/
function bulkSubmitWithdrawalSignatures(
uint256[] calldata withdrawals,
bytes[] calldata signatures
) external whenNotPaused onlyBridgeOperator {
function bulkSubmitWithdrawalSignatures(uint256[] calldata withdrawals, bytes[] calldata signatures)
external
whenNotPaused
onlyBridgeOperator
{
address operator = msg.sender;

if (!(withdrawals.length > 0 && withdrawals.length == signatures.length)) {
Expand All @@ -263,7 +269,7 @@ contract RoninGatewayV3 is

uint256 id;
IBridgeTracking _bridgeTrackingContract = IBridgeTracking(getContract(ContractType.BRIDGE_TRACKING));
for (uint256 _i; _i < withdrawals.length; ) {
for (uint256 _i; _i < withdrawals.length;) {
id = withdrawals[_i];
_withdrawalSig[id][operator] = signatures[_i];
_bridgeTrackingContract.recordVote(IBridgeTracking.VoteKind.Withdrawal, id, operator);
Expand Down Expand Up @@ -338,10 +344,11 @@ contract RoninGatewayV3 is
uint256[] calldata _chainIds,
Token.Standard[] calldata _standards
) internal {
if (!(_roninTokens.length == _mainchainTokens.length && _roninTokens.length == _chainIds.length))
if (!(_roninTokens.length == _mainchainTokens.length && _roninTokens.length == _chainIds.length)) {
revert ErrLengthMismatch(msg.sig);
}

for (uint256 _i; _i < _roninTokens.length; ) {
for (uint256 _i; _i < _roninTokens.length;) {
_mainchainToken[_roninTokens[_i]][_chainIds[_i]].tokenAddr = _mainchainTokens[_i];
_mainchainToken[_roninTokens[_i]][_chainIds[_i]].erc = _standards[_i];

Expand All @@ -368,8 +375,9 @@ contract RoninGatewayV3 is

MappedToken memory _token = getMainchainToken(receipt.ronin.tokenAddr, receipt.mainchain.chainId);

if (!(_token.erc == receipt.info.erc && _token.tokenAddr == receipt.mainchain.tokenAddr))
if (!(_token.erc == receipt.info.erc && _token.tokenAddr == receipt.mainchain.tokenAddr)) {
revert ErrInvalidReceipt();
}

IsolatedGovernance.Vote storage _proposal = depositVote[receipt.mainchain.chainId][id];
bytes32 _receiptHash = receipt.hash();
Expand All @@ -379,8 +387,7 @@ contract RoninGatewayV3 is
_proposal.status = VoteStatus.Executed;
receipt.info.handleAssetTransfer(payable(receipt.ronin.addr), receipt.ronin.tokenAddr, IWETH(address(0)));
IBridgeTracking(getContract(ContractType.BRIDGE_TRACKING)).handleVoteApproved(
IBridgeTracking.VoteKind.Deposit,
receipt.id
IBridgeTracking.VoteKind.Deposit, receipt.id
);
emit Deposited(_receiptHash, receipt);
}
Expand Down Expand Up @@ -418,12 +425,8 @@ contract RoninGatewayV3 is
address _mainchainTokenAddr
) internal returns (uint256 _withdrawalId) {
_withdrawalId = withdrawalCount++;
Transfer.Receipt memory _receipt = _request.into_withdrawal_receipt(
_requester,
_withdrawalId,
_mainchainTokenAddr,
_chainId
);
Transfer.Receipt memory _receipt =
_request.into_withdrawal_receipt(_requester, _withdrawalId, _mainchainTokenAddr, _chainId);
withdrawal[_withdrawalId] = _receipt;
emit WithdrawalRequested(_receipt.hash(), _receipt);
}
Expand All @@ -450,12 +453,11 @@ contract RoninGatewayV3 is
* - The voter has not voted for the round.
*
*/
function _castIsolatedVote(
IsolatedGovernance.Vote storage _v,
address _voter,
uint256 _minVoteWeight,
bytes32 _hash
) internal virtual returns (VoteStatus _status) {
function _castIsolatedVote(IsolatedGovernance.Vote storage _v, address _voter, uint256 _minVoteWeight, bytes32 _hash)
internal
virtual
returns (VoteStatus _status)
{
_v.castVote(_voter, _hash);
uint256 _totalWeight = _getVoteWeight(_v, _hash);
return _v.syncVoteStatus(_minVoteWeight, _totalWeight, _hash);
Expand All @@ -464,27 +466,29 @@ contract RoninGatewayV3 is
/**
* @dev Returns the vote weight for a specified hash.
*/
function _getVoteWeight(
IsolatedGovernance.Vote storage _v,
bytes32 _hash
) internal view returns (uint256 _totalWeight) {
(, address[] memory bridgeOperators, uint96[] memory weights) = IBridgeManager(
getContract(ContractType.BRIDGE_MANAGER)
).getFullBridgeOperatorInfos();
function _getVoteWeight(IsolatedGovernance.Vote storage _v, bytes32 _hash)
internal
view
returns (uint256 _totalWeight)
{
(, address[] memory bridgeOperators, uint96[] memory weights) =
IBridgeManager(getContract(ContractType.BRIDGE_MANAGER)).getFullBridgeOperatorInfos();
uint256 length = bridgeOperators.length;
unchecked {
for (uint _i; _i < length; ++_i) {
for (uint256 _i; _i < length; ++_i) {
if (_v.voteHashOf[bridgeOperators[_i]] == _hash) {
_totalWeight += weights[_i];
}
}
}
}

function setTrustedThreshold(
uint256 _trustedNumerator,
uint256 _trustedDenominator
) external virtual onlyAdmin returns (uint256, uint256) {
function setTrustedThreshold(uint256 _trustedNumerator, uint256 _trustedDenominator)
external
virtual
onlyAdmin
returns (uint256, uint256)
{
return _setTrustedThreshold(_trustedNumerator, _trustedDenominator);
}

Expand All @@ -501,10 +505,11 @@ contract RoninGatewayV3 is
* Emits the `TrustedThresholdUpdated` event.
*
*/
function _setTrustedThreshold(
uint256 _trustedNumerator,
uint256 _trustedDenominator
) internal virtual returns (uint256 _previousTrustedNum, uint256 _previousTrustedDenom) {
function _setTrustedThreshold(uint256 _trustedNumerator, uint256 _trustedDenominator)
internal
virtual
returns (uint256 _previousTrustedNum, uint256 _previousTrustedDenom)
{
if (_trustedNumerator > _trustedDenominator) revert ErrInvalidTrustedThreshold();

_previousTrustedNum = _num;
Expand All @@ -513,11 +518,7 @@ contract RoninGatewayV3 is
_trustedDenom = _trustedDenominator;
unchecked {
emit TrustedThresholdUpdated(
nonce++,
_trustedNumerator,
_trustedDenominator,
_previousTrustedNum,
_previousTrustedDenom
nonce++, _trustedNumerator, _trustedDenominator, _previousTrustedNum, _previousTrustedDenom
);
}
}
Expand Down
Loading

0 comments on commit 0ffb0d5

Please sign in to comment.