Skip to content

Commit

Permalink
Merge pull request #779 from matter-labs/sb-sync-reorg-with-stable
Browse files Browse the repository at this point in the history
Sync reorg with stable
  • Loading branch information
StanislavBreadless authored Sep 6, 2024
2 parents b6766b0 + 23f5e3c commit cb42ae4
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 66 deletions.
12 changes: 1 addition & 11 deletions l1-contracts/contracts/bridgehub/Bridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {BridgehubL2TransactionRequest, L2Message, L2Log, TxStatus} from "../comm
import {AddressAliasHelper} from "../vendor/AddressAliasHelper.sol";
import {IMessageRoot} from "./IMessageRoot.sol";
import {ICTMDeploymentTracker} from "./ICTMDeploymentTracker.sol";
import {L2CanonicalTransaction} from "../common/Messaging.sol";
import {AssetHandlerNotRegistered, ZKChainLimitReached, Unauthorized, CTMAlreadyRegistered, CTMNotRegistered, ZeroChainId, ChainIdTooBig, SharedBridgeNotSet, BridgeHubAlreadyRegistered, AddressTooLow, MsgValueMismatch, WrongMagicValue, ZeroAddress} from "../common/L1ContractErrors.sol";

/// @author Matter Labs
Expand Down Expand Up @@ -569,25 +568,16 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus

/// @notice Used to forward a transaction on the gateway to the chains mailbox (from L1).
/// @param _chainId the chainId of the chain
/// @param _transaction the transaction to be forwarded
/// @param _factoryDeps the factory dependencies for the transaction
/// @param _canonicalTxHash the canonical transaction hash
/// @param _expirationTimestamp the expiration timestamp for the transaction
function forwardTransactionOnGateway(
uint256 _chainId,
L2CanonicalTransaction calldata _transaction,
bytes[] calldata _factoryDeps,
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) external override onlySettlementLayerRelayedSender {
require(L1_CHAIN_ID != block.chainid, "BH: not in sync layer mode");
address zkChain = zkChainMap.get(_chainId);
IZKChain(zkChain).bridgehubRequestL2TransactionOnGateway(
_transaction,
_factoryDeps,
_canonicalTxHash,
_expirationTimestamp
);
IZKChain(zkChain).bridgehubRequestL2TransactionOnGateway(_canonicalTxHash, _expirationTimestamp);
}

/// @notice forwards function call to Mailbox based on ChainId
Expand Down
4 changes: 1 addition & 3 deletions l1-contracts/contracts/bridgehub/IBridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pragma solidity ^0.8.21;

import {IL1AssetRouter} from "../bridge/interfaces/IL1AssetRouter.sol";
import {L2CanonicalTransaction, L2Message, L2Log, TxStatus} from "../common/Messaging.sol";
import {L2Message, L2Log, TxStatus} from "../common/Messaging.sol";
import {IL1AssetHandler} from "../bridge/interfaces/IL1AssetHandler.sol";
import {ICTMDeploymentTracker} from "./ICTMDeploymentTracker.sol";
import {IMessageRoot} from "./IMessageRoot.sol";
Expand Down Expand Up @@ -207,8 +207,6 @@ interface IBridgehub is IL1AssetHandler {

function forwardTransactionOnGateway(
uint256 _chainId,
L2CanonicalTransaction calldata _transaction,
bytes[] calldata _factoryDeps,
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) external;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ contract GettersFacet is ZKChainBase, IGetters, ILegacyGetters {

/// @inheritdoc IGetters
function getTotalPriorityTxs() external view returns (uint256) {
if (s.priorityQueue.getFirstUnprocessedPriorityTx() >= s.priorityTree.startIndex) {
return s.priorityTree.getTotalPriorityTxs();
} else {
return s.priorityQueue.getTotalPriorityTxs();
}
return _getTotalPriorityTxs();
}

/// @inheritdoc IGetters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,6 @@ contract MailboxFacet is ZKChainBase, IMailbox {
/// @inheritdoc IMailbox
function requestL2TransactionToGatewayMailbox(
uint256 _chainId,
L2CanonicalTransaction calldata _transaction,
bytes[] calldata _factoryDeps,
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) external override onlyL1 returns (bytes32 canonicalTxHash) {
Expand All @@ -376,8 +374,6 @@ contract MailboxFacet is ZKChainBase, IMailbox {

BridgehubL2TransactionRequest memory wrappedRequest = _wrapRequest({
_chainId: _chainId,
_transaction: _transaction,
_factoryDeps: _factoryDeps,
_canonicalTxHash: _canonicalTxHash,
_expirationTimestamp: _expirationTimestamp
});
Expand All @@ -386,25 +382,22 @@ contract MailboxFacet is ZKChainBase, IMailbox {

/// @inheritdoc IMailbox
function bridgehubRequestL2TransactionOnGateway(
L2CanonicalTransaction calldata _transaction,
bytes[] calldata _factoryDeps,
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) external override onlyBridgehub {
_writePriorityOp(_transaction, _factoryDeps, _canonicalTxHash, _expirationTimestamp);
_writePriorityOpHash(_canonicalTxHash, _expirationTimestamp);
emit NewRelayedPriorityTransaction(_getTotalPriorityTxs(), _canonicalTxHash, _expirationTimestamp);
}

function _wrapRequest(
uint256 _chainId,
L2CanonicalTransaction calldata _transaction,
bytes[] calldata _factoryDeps,
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) internal view returns (BridgehubL2TransactionRequest memory) {
// solhint-disable-next-line func-named-parameters
bytes memory data = abi.encodeCall(
IBridgehub(s.bridgehub).forwardTransactionOnGateway,
(_chainId, _transaction, _factoryDeps, _canonicalTxHash, _expirationTimestamp)
(_chainId, _canonicalTxHash, _expirationTimestamp)
);
return
BridgehubL2TransactionRequest({
Expand Down Expand Up @@ -492,8 +485,6 @@ contract MailboxFacet is ZKChainBase, IMailbox {
// slither-disable-next-line unused-return
IMailbox(s.settlementLayer).requestL2TransactionToGatewayMailbox({
_chainId: s.chainId,
_transaction: transaction,
_factoryDeps: _params.request.factoryDeps,
_canonicalTxHash: canonicalTxHash,
_expirationTimestamp: _params.expirationTimestamp
});
Expand Down Expand Up @@ -569,6 +560,14 @@ contract MailboxFacet is ZKChainBase, IMailbox {
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) internal {
_writePriorityOpHash(_canonicalTxHash, _expirationTimestamp);

// Data that is needed for the operator to simulate priority queue offchain
// solhint-disable-next-line func-named-parameters
emit NewPriorityRequest(_transaction.nonce, _canonicalTxHash, _expirationTimestamp, _transaction, _factoryDeps);
}

function _writePriorityOpHash(bytes32 _canonicalTxHash, uint64 _expirationTimestamp) internal {
if (s.priorityTree.startIndex > s.priorityQueue.getFirstUnprocessedPriorityTx()) {
s.priorityQueue.pushBack(
PriorityOperation({
Expand All @@ -579,10 +578,6 @@ contract MailboxFacet is ZKChainBase, IMailbox {
);
}
s.priorityTree.push(_canonicalTxHash);

// Data that is needed for the operator to simulate priority queue offchain
// solhint-disable-next-line func-named-parameters
emit NewPriorityRequest(_transaction.nonce, _canonicalTxHash, _expirationTimestamp, _transaction, _factoryDeps);
}

/// @notice Hashes the L2 bytecodes and returns them in the format in which they are processed by the bootloader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ pragma solidity 0.8.24;

import {ZKChainStorage} from "../ZKChainStorage.sol";
import {ReentrancyGuard} from "../../../common/ReentrancyGuard.sol";

import {PriorityQueue} from "../../libraries/PriorityQueue.sol";
import {PriorityTree} from "../../libraries/PriorityTree.sol";
import {Unauthorized} from "../../../common/L1ContractErrors.sol";

/// @title Base contract containing functions accessible to the other facets.
/// @author Matter Labs
/// @custom:security-contact [email protected]
contract ZKChainBase is ReentrancyGuard {
using PriorityQueue for PriorityQueue.Queue;
using PriorityTree for PriorityTree.Tree;

// slither-disable-next-line uninitialized-state
ZKChainStorage internal s;

Expand Down Expand Up @@ -64,4 +68,12 @@ contract ZKChainBase is ReentrancyGuard {
}
_;
}

function _getTotalPriorityTxs() internal view returns (uint256) {
if (s.priorityQueue.getFirstUnprocessedPriorityTx() >= s.priorityTree.startIndex) {
return s.priorityTree.getTotalPriorityTxs();
} else {
return s.priorityQueue.getTotalPriorityTxs();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,14 @@ interface IMailbox is IZKChainBase {
) external returns (bytes32 canonicalTxHash);

/// @dev On the Gateway the chain's mailbox receives the tx from the bridgehub.
function bridgehubRequestL2TransactionOnGateway(
L2CanonicalTransaction calldata _transaction,
bytes[] calldata _factoryDeps,
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) external;
function bridgehubRequestL2TransactionOnGateway(bytes32 _canonicalTxHash, uint64 _expirationTimestamp) external;

/// @dev On L1 we have to forward to the Gateway's mailbox which sends to the Bridgehub on the Gw
/// @param _chainId the chainId of the chain
/// @param _transaction the transaction to be relayed
/// @param _factoryDeps the factory dependencies
/// @param _canonicalTxHash the canonical transaction hash
/// @param _expirationTimestamp the expiration timestamp
function requestL2TransactionToGatewayMailbox(
uint256 _chainId,
L2CanonicalTransaction calldata _transaction,
bytes[] calldata _factoryDeps,
bytes32 _canonicalTxHash,
uint64 _expirationTimestamp
) external returns (bytes32 canonicalTxHash);
Expand Down Expand Up @@ -175,4 +166,13 @@ interface IMailbox is IZKChainBase {
L2CanonicalTransaction transaction,
bytes[] factoryDeps
);

/// @notice New relayed priority request event. It is emitted on a chain that is deployed
/// on top of the gateway when it receives a request relayed via the Bridgehub.
/// @dev IMPORTANT: this event most likely will be removed in the future, so
/// no one should rely on it for indexing purposes.
/// @param txId Serial number of the priority operation
/// @param txHash keccak256 hash of encoded transaction representation
/// @param expirationTimestamp Timestamp up to which priority request should be processed
event NewRelayedPriorityTransaction(uint256 txId, bytes32 txHash, uint64 expirationTimestamp);
}
21 changes: 1 addition & 20 deletions l1-contracts/test/foundry/integration/GatewayTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,9 @@ contract GatewayTests is L1ContractDeployer, ZKChainDeployer, TokenDeployer, L2T
finishMoveChain();

IBridgehub bridgehub = IBridgehub(l1Script.getBridgehubProxyAddress());
L2CanonicalTransaction memory tx = L2CanonicalTransaction({
txType: 255,
from: uint256(0),
to: uint256(0),
gasLimit: 72000000,
gasPerPubdataByteLimit: 800,
maxFeePerGas: 1,
maxPriorityFeePerGas: 0,
paymaster: 0,
// Note, that the priority operation id is used as "nonce" for L1->L2 transactions
nonce: 0,
value: 0,
reserved: [uint256(0), 0, 0, 0],
data: "0x",
signature: new bytes(0),
factoryDeps: new uint256[](0),
paymasterInput: "0x",
reservedDynamic: "0x"
});
vm.chainId(12345);
vm.startBroadcast(SETTLEMENT_LAYER_RELAY_SENDER);
bridgehub.forwardTransactionOnGateway(mintChainId, tx, new bytes[](0), bytes32(0), 0);
bridgehub.forwardTransactionOnGateway(mintChainId, bytes32(0), 0);
vm.stopBroadcast();
}

Expand Down
Loading

0 comments on commit cb42ae4

Please sign in to comment.