Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test rollback #50

Merged
merged 8 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions contracts/blade/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract Gateway is ValidatorSetStorage, IGateway {
function receiveBatch(
BridgeMessage[] calldata batchMessages,
SignedBridgeMessageBatch calldata signedBridgeBatch
) external {
) external virtual {
if (signedBridgeBatch.isRollback) {
_verifyRollbackBatch(batchMessages);
} else {
Expand Down Expand Up @@ -125,6 +125,7 @@ contract Gateway is ValidatorSetStorage, IGateway {
* @notice Internal function that verifies the batch
* @param batch batch to verify
*/
// slither-disable-start dead-code
function _verifyBatch(BridgeMessage[] calldata batch) private view {
require(batch.length > 0, "EMPTY_BATCH");

Expand All @@ -141,7 +142,9 @@ contract Gateway is ValidatorSetStorage, IGateway {
}
}

function _verifyRollbackBatch(BridgeMessage[] calldata batch) private view {
// slither-disable-end dead-code

function _verifyRollbackBatch(BridgeMessage[] calldata batch) internal view {
require(batch.length > 0, "EMPTY_BATCH");

uint256 sourceChainId = block.chainid;
Expand All @@ -157,6 +160,7 @@ contract Gateway is ValidatorSetStorage, IGateway {
}
}

// slither-disable-start dead-code
function _executeBridgeMessage(BridgeMessage calldata message) private {
require(!processedEvents[message.id], "DestinationGateway: BRIDGE_MESSAGE_IS_ALREADY_PROCESSED");
// revert transaction if client has added flag, or receiver has no code
Expand All @@ -181,7 +185,9 @@ contract Gateway is ValidatorSetStorage, IGateway {
emit BridgeMessageResult(message.id, success, message.sourceChainId, message.destinationChainId, returnData);
}

function _executeRollbackBridgeMessage(BridgeMessage calldata message) private {
// slither-disable-end dead-code

function _executeRollbackBridgeMessage(BridgeMessage calldata message) internal {
require(
!processedEventsRollback[message.id],
"DestinationGateway: ROLLBACK_BRIDGE_MESSAGE_IS_ALREADY_PROCESSED"
Expand Down
63 changes: 63 additions & 0 deletions contracts/blade/TestRollbackGateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./Gateway.sol";

contract TestRollbackGateway is Gateway {
/**
* @notice Test function to generate error for testing rollback
* @param message Error message
*/
error TestRollbackError(string message);

/**
* @notice receives the batch of messages and executes them
* @param batchMessages batch of messages
*/
// slither-disable-next-line protected-vars
function receiveBatch(
BridgeMessage[] calldata batchMessages,
SignedBridgeMessageBatch calldata signedBridgeBatch
) external override {
if (!signedBridgeBatch.isRollback) {
revert TestRollbackError("TESTING BATCH");
}

_verifyRollbackBatch(batchMessages);

bytes memory hash = abi.encode(
keccak256(
abi.encode(
calculateMerkleRoot(batchMessages),
signedBridgeBatch.startId,
signedBridgeBatch.endId,
signedBridgeBatch.sourceChainId,
signedBridgeBatch.destinationChainId,
signedBridgeBatch.threshold,
signedBridgeBatch.isRollback
)
)
);

verifySignature(bls.hashToPoint(DOMAIN_BRIDGE, hash), signedBridgeBatch.signature, signedBridgeBatch.bitmap);

uint256 length = batchMessages.length;

for (uint256 i = 0; i < length; ) {
_executeRollbackBridgeMessage(batchMessages[i]);

unchecked {
++i;
}
}

// slither-disable-next-line reentrancy-events
emit BridgeBatchResult(
signedBridgeBatch.startId,
signedBridgeBatch.endId,
signedBridgeBatch.sourceChainId,
signedBridgeBatch.destinationChainId,
signedBridgeBatch.isRollback
);
}
}
Loading