Skip to content

Commit 9af9291

Browse files
Use Owner pattern to switch betweern batcher.
1 parent 692ea92 commit 9af9291

File tree

6 files changed

+261
-15
lines changed

6 files changed

+261
-15
lines changed

op-batcher/bindings/batch_authenticator.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

op-batcher/bindings/batch_inbox.go

Lines changed: 230 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contracts-bedrock/interfaces/L1/IBatchInbox.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ interface IBatchInbox {
66

77
function version() external view returns (string memory);
88

9-
function __constructor__(address _nonTeeBatcher, address _batchAuthenticator) external;
9+
function __constructor__(address _nonTeeBatcher, address _batchAuthenticator, address _owner) external;
1010
}

packages/contracts-bedrock/scripts/deploy/DeployEspresso.s.sol

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ contract DeployEspresso is Script {
7676
function run(DeployEspressoInput input, DeployEspressoOutput output, address deployerAddress) public {
7777
IEspressoTEEVerifier teeVerifier = deployTEEVerifier(input);
7878
IBatchAuthenticator batchAuthenticator = deployBatchAuthenticator(input, output, teeVerifier, deployerAddress);
79-
deployBatchInbox(input, output, batchAuthenticator);
79+
deployBatchInbox(input, output, batchAuthenticator, deployerAddress);
8080
checkOutput(output);
8181
}
8282

@@ -123,7 +123,8 @@ contract DeployEspresso is Script {
123123
function deployBatchInbox(
124124
DeployEspressoInput input,
125125
DeployEspressoOutput output,
126-
IBatchAuthenticator batchAuthenticator
126+
IBatchAuthenticator batchAuthenticator,
127+
address owner
127128
)
128129
public
129130
{
@@ -134,7 +135,7 @@ contract DeployEspresso is Script {
134135
_name: "BatchInbox",
135136
_salt: salt,
136137
_args: DeployUtils.encodeConstructor(
137-
abi.encodeCall(IBatchInbox.__constructor__, (input.nonTeeBatcher(), address(batchAuthenticator)))
138+
abi.encodeCall(IBatchInbox.__constructor__, (input.nonTeeBatcher(), address(batchAuthenticator), owner))
138139
)
139140
})
140141
);

packages/contracts-bedrock/src/L1/BatchInbox.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.28;
33

4+
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
45
import { IBatchAuthenticator } from "interfaces/L1/IBatchAuthenticator.sol";
56

67
/// @title BatchInbox
78
/// @notice Receives batches from either a TEE batcher or a non-TEE batcher and enforces
89
/// that TEE batches are authenticated by the configured batch authenticator.
9-
contract BatchInbox {
10+
contract BatchInbox is Ownable {
1011
/// @notice Address of the TEE-based batcher.
1112
address public immutable teeBatcher;
1213

@@ -24,16 +25,17 @@ contract BatchInbox {
2425
/// and the batch authenticator.
2526
/// @param _nonTeeBatcher Address of the non-TEE batcher.
2627
/// @param _batchAuthenticator Address of the batch authenticator contract.
27-
constructor(address _nonTeeBatcher, IBatchAuthenticator _batchAuthenticator) {
28+
constructor(address _nonTeeBatcher, IBatchAuthenticator _batchAuthenticator, address _owner) Ownable() {
2829
require(_nonTeeBatcher != address(0), "BatchInbox: zero address for non tee batcher");
2930
nonTeeBatcher = _nonTeeBatcher;
3031
batchAuthenticator = _batchAuthenticator;
3132
// By default, start with the TEE batcher active
3233
activeIsTee = true;
34+
_transferOwnership(_owner);
3335
}
3436

3537
/// @notice Toggles the active batcher between the TEE and non-TEE batcher.
36-
function switchBatcher() external {
38+
function switchBatcher() external onlyOwner {
3739
activeIsTee = !activeIsTee;
3840
}
3941

packages/contracts-bedrock/test/L1/BatchInbox.t.sol

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ contract BatchInbox_Test is Test {
3535

3636
function setUp() public virtual {
3737
authenticator = new MockBatchAuthenticator();
38-
inbox = new BatchInbox(nonTeeBatcher, IBatchAuthenticator(address(authenticator)));
38+
inbox = new BatchInbox(nonTeeBatcher, IBatchAuthenticator(address(authenticator)), deployer);
3939
}
4040
}
4141

@@ -44,16 +44,17 @@ contract BatchInbox_Test is Test {
4444
contract BatchInbox_Constructor_Test is Test {
4545
address nonTeeBatcher = address(0x5678);
4646
address batchAuthenticator = address(0x9ABC);
47+
address owner = address(0xABCD);
4748

4849
/// @notice Test that constructor reverts when non-TEE batcher is zero address
4950
function test_constructor_revertsWhenNonTeeBatcherIsZero() external {
5051
vm.expectRevert("BatchInbox: zero address for non tee batcher");
51-
new BatchInbox(address(0), IBatchAuthenticator(batchAuthenticator));
52+
new BatchInbox(address(0), IBatchAuthenticator(batchAuthenticator), owner);
5253
}
5354

5455
/// @notice Test that constructor succeeds with valid addresses
5556
function test_constructor_succeedsWithValidAddresses() external {
56-
BatchInbox testInbox = new BatchInbox(nonTeeBatcher, IBatchAuthenticator(batchAuthenticator));
57+
BatchInbox testInbox = new BatchInbox(nonTeeBatcher, IBatchAuthenticator(batchAuthenticator), owner);
5758

5859
assertEq(testInbox.nonTeeBatcher(), nonTeeBatcher, "Non-TEE batcher should match");
5960
assertEq(address(testInbox.batchAuthenticator()), batchAuthenticator, "Batch authenticator should match");
@@ -70,13 +71,25 @@ contract BatchInbox_SwitchBatcher_Test is BatchInbox_Test {
7071
assertTrue(inbox.activeIsTee(), "Should start with TEE batcher active");
7172

7273
// Switch to non-TEE batcher
74+
vm.prank(deployer);
7375
inbox.switchBatcher();
7476
assertFalse(inbox.activeIsTee(), "Should switch to non-TEE batcher");
7577

7678
// Switch back to TEE batcher
79+
vm.prank(deployer);
7780
inbox.switchBatcher();
7881
assertTrue(inbox.activeIsTee(), "Should switch back to TEE batcher");
7982
}
83+
84+
/// @notice Test that only the owner can switch the active batcher
85+
function test_switchBatcher_revertsForNonOwner() external {
86+
// Initially TEE batcher is active
87+
assertTrue(inbox.activeIsTee(), "Should start with TEE batcher active");
88+
89+
vm.prank(unauthorized);
90+
vm.expectRevert("Ownable: caller is not the owner");
91+
inbox.switchBatcher();
92+
}
8093
}
8194

8295
/// @title BatchInbox_Fallback_Test
@@ -85,6 +98,7 @@ contract BatchInbox_Fallback_Test is BatchInbox_Test {
8598
/// @notice Test that non-TEE batcher can post after switching
8699
function test_fallback_nonTeeBatcherCanPostAfterSwitch() external {
87100
// Switch to non-TEE batcher
101+
vm.prank(deployer);
88102
inbox.switchBatcher();
89103

90104
// Non-TEE batcher should be able to post
@@ -96,6 +110,7 @@ contract BatchInbox_Fallback_Test is BatchInbox_Test {
96110
/// @notice Test that inactive batcher reverts
97111
function test_fallback_inactiveBatcherReverts() external {
98112
// Switch to non-TEE batcher (making TEE batcher inactive)
113+
vm.prank(deployer);
99114
inbox.switchBatcher();
100115

101116
// TEE batcher (now inactive) should revert
@@ -143,6 +158,7 @@ contract BatchInbox_Fallback_Test is BatchInbox_Test {
143158
/// @notice Test that non-TEE batcher doesn't require authentication
144159
function test_fallback_nonTeeBatcherDoesNotRequireAuth() external {
145160
// Switch to non-TEE batcher
161+
vm.prank(deployer);
146162
inbox.switchBatcher();
147163

148164
bytes memory data = "no-auth-needed";
@@ -157,6 +173,7 @@ contract BatchInbox_Fallback_Test is BatchInbox_Test {
157173
/// @notice Test that unauthorized address cannot post
158174
function test_fallback_unauthorizedAddressReverts() external {
159175
// Switch to non-TEE batcher. In this case the batch inbox should revert if the batcher is not authorized.
176+
vm.prank(deployer);
160177
inbox.switchBatcher();
161178
vm.prank(unauthorized);
162179
(bool success,) = address(inbox).call("unauthorized");

0 commit comments

Comments
 (0)