-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZoneFactory.sol
More file actions
222 lines (186 loc) · 8.71 KB
/
ZoneFactory.sol
File metadata and controls
222 lines (186 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import { TempoUtilities } from "../TempoUtilities.sol";
import { IZoneFactory, ZoneInfo } from "./IZone.sol";
import { Verifier } from "./Verifier.sol";
import { ZoneMessenger } from "./ZoneMessenger.sol";
import { ZonePortal } from "./ZonePortal.sol";
/// @title ZoneFactory
/// @notice Creates zones and registers parameters
contract ZoneFactory is IZoneFactory {
/*//////////////////////////////////////////////////////////////
STORAGE
//////////////////////////////////////////////////////////////*/
/// @notice Minimum gas required for zone creation.
/// @dev Prevents low-cost zone spam. The caller must supply at least this much gas.
uint256 public constant ZONE_CREATION_GAS = 15_000_000;
/// @notice Next zone ID to be assigned
/// @dev Starts at 1, reserving zone ID 0 for potential future use (e.g., mainnet as zone 0)
uint32 internal _nextZoneId = 1;
mapping(uint32 => ZoneInfo) internal _zones;
mapping(address => bool) internal _isZonePortal;
mapping(address => bool) internal _isZoneMessenger;
mapping(address => bool) internal _validVerifiers;
/// @notice Current active verifier (pre-fork or promoted from previous forkVerifier)
address public verifier;
/// @notice Post-fork verifier (address(0) if no fork yet)
address public forkVerifier;
/// @notice L1 block number at which forkVerifier was set (0 = no fork)
uint64 public forkActivationBlock;
/// @notice Protocol version counter, incremented at each hard fork
uint64 public protocolVersion;
/// @notice Tracks deployment count for CREATE address prediction
/// @dev Contracts start with nonce 1, not 0. Nonce 1 is used by the Verifier deployment
/// in the constructor, so zone deployments start at nonce 2.
uint256 internal _deploymentNonce = 2;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor() {
address v = address(new Verifier());
_validVerifiers[v] = true;
verifier = v;
}
/*//////////////////////////////////////////////////////////////
ZONE CREATION
//////////////////////////////////////////////////////////////*/
function createZone(CreateZoneParams calldata params)
external
returns (uint32 zoneId, address portal)
{
// Validate initial token is a TIP-20
if (!TempoUtilities.isTIP20(params.initialToken)) revert InvalidToken();
if (params.sequencer == address(0)) revert InvalidSequencer();
if (gasleft() < ZONE_CREATION_GAS) revert InsufficientGas();
zoneId = _nextZoneId;
if (zoneId == type(uint32).max) revert ZoneIdOverflow();
_nextZoneId = zoneId + 1;
// We deploy messenger first, then portal.
// Messenger needs portal's address at construction (immutable).
// Solution: predict portal's address based on CREATE address formula.
//
// CREATE addresses: address = keccak256(rlp([sender, nonce]))[12:]
// - messenger will be at nonce N
// - portal will be at nonce N+1
//
// We track our own nonce since contract nonce isn't accessible.
uint256 currentNonce = _deploymentNonce;
_deploymentNonce += 2; // We'll deploy 2 contracts
// Compute portal's address (will be deployed at nonce+1)
address predictedPortal = _computeCreateAddress(address(this), currentNonce + 1);
// Deploy messenger with predicted portal address (no token needed -- portal grants approval per-token)
ZoneMessenger messengerContract = new ZoneMessenger(predictedPortal);
address messengerAddress = address(messengerContract);
// Deploy portal with messenger address and initial token
// The portal constructor enables the initial token automatically
ZonePortal portalContract = new ZonePortal(
zoneId,
params.initialToken,
messengerAddress,
params.sequencer,
address(this),
params.zoneParams.genesisBlockHash,
params.zoneParams.genesisTempoBlockNumber
);
portal = address(portalContract);
// Verify our prediction was correct
require(portal == predictedPortal, "Portal address mismatch - nonce tracking error");
// Store zone info
_zones[zoneId] = ZoneInfo({
zoneId: zoneId,
portal: portal,
messenger: messengerAddress,
initialToken: params.initialToken,
sequencer: params.sequencer,
genesisBlockHash: params.zoneParams.genesisBlockHash,
genesisTempoBlockHash: params.zoneParams.genesisTempoBlockHash,
genesisTempoBlockNumber: params.zoneParams.genesisTempoBlockNumber
});
_isZonePortal[portal] = true;
_isZoneMessenger[messengerAddress] = true;
emit ZoneCreated(
zoneId,
portal,
messengerAddress,
params.initialToken,
params.sequencer,
params.zoneParams.genesisBlockHash,
params.zoneParams.genesisTempoBlockHash,
params.zoneParams.genesisTempoBlockNumber
);
}
/// @notice Compute the address of a contract deployed with CREATE
/// @dev address = keccak256(rlp([sender, nonce]))[12:]
function _computeCreateAddress(address deployer, uint256 nonce)
internal
pure
returns (address)
{
bytes memory data;
if (nonce == 0x00) {
data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, bytes1(0x80));
} else if (nonce <= 0x7f) {
data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, uint8(nonce));
} else if (nonce <= 0xff) {
data =
abi.encodePacked(bytes1(0xd7), bytes1(0x94), deployer, bytes1(0x81), uint8(nonce));
} else if (nonce <= 0xffff) {
data =
abi.encodePacked(bytes1(0xd8), bytes1(0x94), deployer, bytes1(0x82), uint16(nonce));
} else if (nonce <= 0xffffff) {
data =
abi.encodePacked(bytes1(0xd9), bytes1(0x94), deployer, bytes1(0x83), uint24(nonce));
} else {
data =
abi.encodePacked(bytes1(0xda), bytes1(0x94), deployer, bytes1(0x84), uint32(nonce));
}
return address(uint160(uint256(keccak256(data))));
}
/*//////////////////////////////////////////////////////////////
VERIFIER MANAGEMENT
//////////////////////////////////////////////////////////////*/
/// @notice Rotate verifiers and increment protocolVersion.
/// @dev Called as an L1 system transaction at hard fork time. O(1) — no per-zone iteration.
/// On first fork: verifier keeps its value, forkVerifier is set.
/// On subsequent forks: previous forkVerifier promoted to verifier, new one installed.
function setForkVerifier(address newForkVerifier) external {
if (forkVerifier != address(0)) {
verifier = forkVerifier;
}
forkVerifier = newForkVerifier;
forkActivationBlock = uint64(block.number);
_validVerifiers[newForkVerifier] = true;
protocolVersion++;
}
/// @notice Validate that targetVerifier is recognized and allowed for tempoBlockNumber.
/// @dev Called by portals during submitBatch.
function validateVerifier(address targetVerifier, uint64 tempoBlockNumber) external view {
if (targetVerifier != verifier && targetVerifier != forkVerifier) {
revert UnknownVerifier();
}
if (targetVerifier == verifier && forkActivationBlock != 0) {
if (tempoBlockNumber >= forkActivationBlock) {
revert UseForkVerifier();
}
}
}
/*//////////////////////////////////////////////////////////////
VIEWS
//////////////////////////////////////////////////////////////*/
/// @notice Returns the number of zones created (not including reserved zone 0)
function zoneCount() external view returns (uint32) {
return _nextZoneId - 1;
}
function zones(uint32 zoneId) external view returns (ZoneInfo memory) {
return _zones[zoneId];
}
function isZonePortal(address portal) external view returns (bool) {
return _isZonePortal[portal];
}
function isZoneMessenger(address messenger) external view returns (bool) {
return _isZoneMessenger[messenger];
}
function isValidVerifier(address v) external view returns (bool) {
return _validVerifiers[v];
}
}