-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathOPContractsManager.sol
1490 lines (1298 loc) · 66.7 KB
/
OPContractsManager.sol
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
// Libraries
import { Blueprint } from "src/libraries/Blueprint.sol";
import { Constants } from "src/libraries/Constants.sol";
import { Bytes } from "src/libraries/Bytes.sol";
import { Claim, Duration, GameType, GameTypes, OutputRoot, Hash } from "src/dispute/lib/Types.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
// Interfaces
import { ISemver } from "interfaces/universal/ISemver.sol";
import { IResourceMetering } from "interfaces/L1/IResourceMetering.sol";
import { IBigStepper } from "interfaces/dispute/IBigStepper.sol";
import { IDelayedWETH } from "interfaces/dispute/IDelayedWETH.sol";
import { IAnchorStateRegistry } from "interfaces/dispute/IAnchorStateRegistry.sol";
import { IDisputeGame } from "interfaces/dispute/IDisputeGame.sol";
import { IAddressManager } from "interfaces/legacy/IAddressManager.sol";
import { IProxyAdmin } from "interfaces/universal/IProxyAdmin.sol";
import { IDisputeGameFactory } from "interfaces/dispute/IDisputeGameFactory.sol";
import { IFaultDisputeGame } from "interfaces/dispute/IFaultDisputeGame.sol";
import { IPermissionedDisputeGame } from "interfaces/dispute/IPermissionedDisputeGame.sol";
import { ISuperchainConfig } from "interfaces/L1/ISuperchainConfig.sol";
import { IProtocolVersions } from "interfaces/L1/IProtocolVersions.sol";
import { IOptimismPortal2 as IOptimismPortal } from "interfaces/L1/IOptimismPortal2.sol";
import { ISystemConfig } from "interfaces/L1/ISystemConfig.sol";
import { IL1CrossDomainMessenger } from "interfaces/L1/IL1CrossDomainMessenger.sol";
import { IL1ERC721Bridge } from "interfaces/L1/IL1ERC721Bridge.sol";
import { IL1StandardBridge } from "interfaces/L1/IL1StandardBridge.sol";
import { IOptimismMintableERC20Factory } from "interfaces/universal/IOptimismMintableERC20Factory.sol";
import { IHasSuperchainConfig } from "interfaces/L1/IHasSuperchainConfig.sol";
import { IETHLockbox } from "interfaces/L1/IETHLockbox.sol";
contract OPContractsManagerContractsContainer {
/// @notice Addresses of the Blueprint contracts.
/// This is internal because if public the autogenerated getter method would return a tuple of
/// addresses, but we want it to return a struct.
OPContractsManager.Blueprints internal blueprint;
/// @notice Addresses of the latest implementation contracts.
OPContractsManager.Implementations internal implementation;
constructor(
OPContractsManager.Blueprints memory _blueprints,
OPContractsManager.Implementations memory _implementations
) {
blueprint = _blueprints;
implementation = _implementations;
}
function blueprints() public view returns (OPContractsManager.Blueprints memory) {
return blueprint;
}
function implementations() public view returns (OPContractsManager.Implementations memory) {
return implementation;
}
}
abstract contract OPContractsManagerBase {
/// @notice The blueprint contract addresses contract.
OPContractsManagerContractsContainer public immutable contractsContainer;
/// @notice The OPContractsManager contract that is currently being used.
OPContractsManagerBase internal immutable thisOPCM;
/// @notice Constructor to initialize the immutable thisOPCM variable and contract addresses
/// @param _contractsContainer The blueprint contract addresses and implementation contract addresses
constructor(OPContractsManagerContractsContainer _contractsContainer) {
contractsContainer = _contractsContainer;
thisOPCM = this;
}
/// @notice Retrieves the implementation addresses stored in this OPCM contract
function getImplementations() internal view returns (OPContractsManager.Implementations memory) {
return thisOPCM.implementations();
}
/// @notice Retrieves the blueprint addresses stored in this OPCM contract
function getBlueprints() internal view returns (OPContractsManager.Blueprints memory) {
return thisOPCM.blueprints();
}
/// @notice Retrieves the implementation addresses stored in this OPCM contract
function implementations() public view returns (OPContractsManager.Implementations memory) {
return contractsContainer.implementations();
}
/// @notice Retrieves the blueprint addresses stored in this OPCM contract
function blueprints() public view returns (OPContractsManager.Blueprints memory) {
return contractsContainer.blueprints();
}
/// @notice Maps an L2 chain ID to an L1 batch inbox address as defined by the standard
/// configuration's convention. This convention is `versionByte || keccak256(bytes32(chainId))[:19]`,
/// where || denotes concatenation`, versionByte is 0x00, and chainId is a uint256.
/// https://specs.optimism.io/protocol/configurability.html#consensus-parameters
function chainIdToBatchInboxAddress(uint256 _l2ChainId) public pure returns (address) {
bytes1 versionByte = 0x00;
bytes32 hashedChainId = keccak256(bytes.concat(bytes32(_l2ChainId)));
bytes19 first19Bytes = bytes19(hashedChainId);
return address(uint160(bytes20(bytes.concat(versionByte, first19Bytes))));
}
/// @notice Helper method for computing a salt that's used in CREATE2 deployments.
/// Including the contract name ensures that the resultant address from CREATE2 is unique
/// across our smart contract system. For example, we deploy multiple proxy contracts
/// with the same bytecode from this contract, so they each require a unique salt for determinism.
function computeSalt(
uint256 _l2ChainId,
string memory _saltMixer,
string memory _contractName
)
internal
pure
returns (bytes32)
{
return keccak256(abi.encode(_l2ChainId, _saltMixer, _contractName));
}
/// @notice Helper method for computing a reusable salt mixer
/// This method should be used as the salt mixer when deploying contracts when there is no user
/// provided salt mixer. This protects against a situation where multiple chains with the same
/// L2 chain ID exist, which would otherwise result in address collisions.
function reusableSaltMixer(OPContractsManager.OpChainConfig memory _opChainConfig)
internal
pure
returns (string memory)
{
return string(bytes.concat(bytes32(uint256(uint160(address(_opChainConfig.systemConfigProxy))))));
}
/// @notice Deterministically deploys a new proxy contract owned by the provided ProxyAdmin.
/// The salt is computed as a function of the L2 chain ID, the salt mixer and the contract name.
/// This is required because we deploy many identical proxies, so they each require a unique salt for determinism.
function deployProxy(
uint256 _l2ChainId,
IProxyAdmin _proxyAdmin,
string memory _saltMixer,
string memory _contractName
)
internal
returns (address)
{
bytes32 salt = computeSalt(_l2ChainId, _saltMixer, _contractName);
return Blueprint.deployFrom(getBlueprints().proxy, salt, abi.encode(_proxyAdmin));
}
/// @notice Makes an internal call to the target to initialize the proxy with the specified data.
/// First performs safety checks to ensure the target, implementation, and proxy admin are valid.
function upgradeToAndCall(
IProxyAdmin _proxyAdmin,
address _target,
address _implementation,
bytes memory _data
)
internal
{
assertValidContractAddress(_implementation);
_proxyAdmin.upgradeAndCall(payable(address(_target)), _implementation, _data);
}
function assertValidContractAddress(address _who) public view {
if (_who.code.length == 0) revert OPContractsManager.AddressHasNoCode(_who);
}
function encodePermissionlessFDGConstructor(IFaultDisputeGame.GameConstructorParams memory _params)
internal
view
virtual
returns (bytes memory)
{
bytes memory dataWithSelector = abi.encodeCall(IFaultDisputeGame.__constructor__, (_params));
return Bytes.slice(dataWithSelector, 4);
}
function encodePermissionedFDGConstructor(
IFaultDisputeGame.GameConstructorParams memory _params,
address _proposer,
address _challenger
)
internal
view
virtual
returns (bytes memory)
{
bytes memory dataWithSelector =
abi.encodeCall(IPermissionedDisputeGame.__constructor__, (_params, _proposer, _challenger));
return Bytes.slice(dataWithSelector, 4);
}
/// @notice Returns the implementation contract address for a given game type.
function getGameImplementation(
IDisputeGameFactory _disputeGameFactory,
GameType _gameType
)
internal
view
returns (IDisputeGame)
{
return _disputeGameFactory.gameImpls(_gameType);
}
/// @notice Retrieves the Anchor State Registry for a given game
function getAnchorStateRegistry(IFaultDisputeGame _disputeGame) internal view returns (IAnchorStateRegistry) {
return _disputeGame.anchorStateRegistry();
}
/// @notice Retrieves the L2 chain ID for a given game
function getL2ChainId(IFaultDisputeGame _disputeGame) internal view returns (uint256) {
return _disputeGame.l2ChainId();
}
/// @notice Retrieves the proposer address for a given game
function getProposer(IPermissionedDisputeGame _disputeGame) internal view returns (address) {
return _disputeGame.proposer();
}
/// @notice Retrieves the challenger address for a given game
function getChallenger(IPermissionedDisputeGame _disputeGame) internal view returns (address) {
return _disputeGame.challenger();
}
/// @notice Retrieves the DisputeGameFactory address for a given SystemConfig
function getDisputeGameFactory(ISystemConfig _systemConfig) internal view returns (IDisputeGameFactory) {
return IDisputeGameFactory(_systemConfig.disputeGameFactory());
}
/// @notice Retrieves the constructor params for a given game.
function getGameConstructorParams(IFaultDisputeGame _disputeGame)
internal
view
returns (IFaultDisputeGame.GameConstructorParams memory)
{
IFaultDisputeGame.GameConstructorParams memory params = IFaultDisputeGame.GameConstructorParams({
gameType: _disputeGame.gameType(),
absolutePrestate: _disputeGame.absolutePrestate(),
maxGameDepth: _disputeGame.maxGameDepth(),
splitDepth: _disputeGame.splitDepth(),
clockExtension: _disputeGame.clockExtension(),
maxClockDuration: _disputeGame.maxClockDuration(),
vm: _disputeGame.vm(),
weth: getWETH(_disputeGame),
anchorStateRegistry: getAnchorStateRegistry(_disputeGame),
l2ChainId: getL2ChainId(_disputeGame)
});
return params;
}
/// @notice Retrieves the DelayedWETH address for a given game
function getWETH(IFaultDisputeGame _disputeGame) internal view returns (IDelayedWETH) {
return _disputeGame.weth();
}
/// @notice Sets a game implementation on the dispute game factory
function setDGFImplementation(IDisputeGameFactory _dgf, GameType _gameType, IDisputeGame _newGame) internal {
_dgf.setImplementation(_gameType, _newGame);
}
}
contract OPContractsManagerGameTypeAdder is OPContractsManagerBase {
/// @notice Emitted when a new game type is added to a chain
/// @param l2ChainId Chain ID of the chain
/// @param gameType Type of the game being
/// @param newDisputeGame Address of the deployed dispute game
/// @param oldDisputeGame Address of the old dispute game
event GameTypeAdded(
uint256 indexed l2ChainId, GameType indexed gameType, IDisputeGame newDisputeGame, IDisputeGame oldDisputeGame
);
/// @notice Constructor to initialize the immutable thisOPCM variable and contract addresses
/// @param _contractsContainer The blueprint contract addresses and implementation contract addresses
constructor(OPContractsManagerContractsContainer _contractsContainer) OPContractsManagerBase(_contractsContainer) { }
/// @notice addGameType deploys a new dispute game and links it to the DisputeGameFactory. The inputted _gameConfigs
/// must be added in ascending GameType order.
function addGameType(
OPContractsManager.AddGameInput[] memory _gameConfigs,
ISuperchainConfig _superchainConfig
)
public
virtual
returns (OPContractsManager.AddGameOutput[] memory)
{
if (_gameConfigs.length == 0) revert OPContractsManager.InvalidGameConfigs();
OPContractsManager.AddGameOutput[] memory outputs = new OPContractsManager.AddGameOutput[](_gameConfigs.length);
OPContractsManager.Blueprints memory bps = getBlueprints();
// Store last game config as an int256 so that we can ensure that the same game config is not added twice.
// Using int256 generates cheaper, simpler bytecode.
int256 lastGameConfig = -1;
for (uint256 i = 0; i < _gameConfigs.length; i++) {
OPContractsManager.AddGameInput memory gameConfig = _gameConfigs[i];
// This conversion is safe because the GameType is a uint32, which will always fit in an int256.
int256 gameTypeInt = int256(uint256(gameConfig.disputeGameType.raw()));
// Ensure that the game configs are added in ascending order, and not duplicated.
if (lastGameConfig >= gameTypeInt) revert OPContractsManager.InvalidGameConfigs();
lastGameConfig = gameTypeInt;
// Grab the permissioned and fault dispute games from the SystemConfig.
// We keep the FDG type as it reduces casting below.
IFaultDisputeGame pdg = IFaultDisputeGame(
address(
getGameImplementation(getDisputeGameFactory(gameConfig.systemConfig), GameTypes.PERMISSIONED_CANNON)
)
);
// Pull out the chain ID.
uint256 l2ChainId = getL2ChainId(pdg);
// Deploy a new DelayedWETH proxy for this game if one hasn't already been specified. Leaving
/// gameConfig.delayedWETH as the zero address will cause a new DelayedWETH to be deployed for this game.
if (address(gameConfig.delayedWETH) == address(0)) {
string memory contractName = string.concat(
"DelayedWETH-",
// This is a safe cast because GameType is a uint256 under the hood and no operation has been done
// on it at this point
Strings.toString(uint256(gameTypeInt))
);
outputs[i].delayedWETH = IDelayedWETH(
payable(deployProxy(l2ChainId, gameConfig.proxyAdmin, gameConfig.saltMixer, contractName))
);
// Initialize the proxy.
upgradeToAndCall(
gameConfig.proxyAdmin,
address(outputs[i].delayedWETH),
getImplementations().delayedWETHImpl,
abi.encodeCall(IDelayedWETH.initialize, (gameConfig.proxyAdmin.owner(), _superchainConfig))
);
} else {
outputs[i].delayedWETH = gameConfig.delayedWETH;
}
// The FDG is only used for the event below, and only if it is being replaced,
// so we declare it here, but only assign it below if needed.
IFaultDisputeGame fdg;
// The below sections are functionally the same. Both deploy a new dispute game. The dispute game type is
// either permissioned or permissionless depending on game config.
if (gameConfig.permissioned) {
outputs[i].faultDisputeGame = IFaultDisputeGame(
Blueprint.deployFrom(
bps.permissionedDisputeGame1,
bps.permissionedDisputeGame2,
computeSalt(l2ChainId, gameConfig.saltMixer, "PermissionedDisputeGame"),
encodePermissionedFDGConstructor(
IFaultDisputeGame.GameConstructorParams(
gameConfig.disputeGameType,
gameConfig.disputeAbsolutePrestate,
gameConfig.disputeMaxGameDepth,
gameConfig.disputeSplitDepth,
gameConfig.disputeClockExtension,
gameConfig.disputeMaxClockDuration,
gameConfig.vm,
outputs[i].delayedWETH,
getAnchorStateRegistry(pdg),
l2ChainId
),
getProposer(IPermissionedDisputeGame(address(pdg))),
getChallenger(IPermissionedDisputeGame(address(pdg)))
)
)
);
} else {
fdg = IFaultDisputeGame(
address(getGameImplementation(getDisputeGameFactory(gameConfig.systemConfig), GameTypes.CANNON))
);
outputs[i].faultDisputeGame = IFaultDisputeGame(
Blueprint.deployFrom(
bps.permissionlessDisputeGame1,
bps.permissionlessDisputeGame2,
computeSalt(l2ChainId, gameConfig.saltMixer, "PermissionlessDisputeGame"),
encodePermissionlessFDGConstructor(
IFaultDisputeGame.GameConstructorParams(
gameConfig.disputeGameType,
gameConfig.disputeAbsolutePrestate,
gameConfig.disputeMaxGameDepth,
gameConfig.disputeSplitDepth,
gameConfig.disputeClockExtension,
gameConfig.disputeMaxClockDuration,
gameConfig.vm,
outputs[i].delayedWETH,
// We can't assume that there is an existing fault dispute game,
// so get the Anchor State Registry from the permissioned game.
getAnchorStateRegistry(pdg),
l2ChainId
)
)
)
);
}
// As a last step, register the new game type with the DisputeGameFactory. If the game type already exists,
// then its implementation will be overwritten.
IDisputeGameFactory dgf = getDisputeGameFactory(gameConfig.systemConfig);
setDGFImplementation(dgf, gameConfig.disputeGameType, IDisputeGame(address(outputs[i].faultDisputeGame)));
dgf.setInitBond(gameConfig.disputeGameType, gameConfig.initialBond);
if (gameConfig.permissioned) {
// Emit event for the newly added game type with the old permissioned dispute game
emit GameTypeAdded(
l2ChainId, gameConfig.disputeGameType, outputs[i].faultDisputeGame, IDisputeGame(address(pdg))
);
} else {
// Emit event for the newly added game type with the old fault dispute game
emit GameTypeAdded(
l2ChainId, gameConfig.disputeGameType, outputs[i].faultDisputeGame, IDisputeGame(address(fdg))
);
}
}
return outputs;
}
/// @notice Updates the prestate hash for a new game type while keeping all other parameters the same
/// @param _prestateUpdateInputs The new prestate hash to use
function updatePrestate(
OPContractsManager.OpChainConfig[] memory _prestateUpdateInputs,
ISuperchainConfig _superchainConfig
)
public
{
// Loop through each chain and prestate hash
for (uint256 i = 0; i < _prestateUpdateInputs.length; i++) {
if (Claim.unwrap(_prestateUpdateInputs[i].absolutePrestate) == bytes32(0)) {
revert OPContractsManager.PrestateRequired();
}
// Get the DisputeGameFactory and existing game implementations
IDisputeGameFactory dgf =
IDisputeGameFactory(_prestateUpdateInputs[i].systemConfigProxy.disputeGameFactory());
IFaultDisputeGame fdg = IFaultDisputeGame(address(getGameImplementation(dgf, GameTypes.CANNON)));
IPermissionedDisputeGame pdg =
IPermissionedDisputeGame(address(getGameImplementation(dgf, GameTypes.PERMISSIONED_CANNON)));
// All chains must have a permissioned game, but not all chains must have a fault dispute game.
// Whether a chain has a fault dispute game determines how many AddGameInput objects are needed.
bool hasFDG = address(fdg) != address(0);
OPContractsManager.AddGameInput[] memory inputs = new OPContractsManager.AddGameInput[](hasFDG ? 2 : 1);
OPContractsManager.AddGameInput memory pdgInput;
OPContractsManager.AddGameInput memory fdgInput;
// Get the existing game parameters and init bond for the permissioned game
IFaultDisputeGame.GameConstructorParams memory pdgParams =
getGameConstructorParams(IFaultDisputeGame(address(pdg)));
uint256 initBond = dgf.initBonds(GameTypes.PERMISSIONED_CANNON);
string memory saltMixer = reusableSaltMixer(_prestateUpdateInputs[i]);
// Create game input with updated prestate but same other params
pdgInput = OPContractsManager.AddGameInput({
disputeAbsolutePrestate: _prestateUpdateInputs[i].absolutePrestate,
saltMixer: saltMixer,
systemConfig: _prestateUpdateInputs[i].systemConfigProxy,
proxyAdmin: _prestateUpdateInputs[i].proxyAdmin,
delayedWETH: IDelayedWETH(payable(address(pdgParams.weth))),
disputeGameType: pdgParams.gameType,
disputeMaxGameDepth: pdgParams.maxGameDepth,
disputeSplitDepth: pdgParams.splitDepth,
disputeClockExtension: pdgParams.clockExtension,
disputeMaxClockDuration: pdgParams.maxClockDuration,
initialBond: initBond,
vm: pdgParams.vm,
permissioned: true
});
// If a fault dispute game exists, create a new game with the same parameters but updated prestate.
if (hasFDG) {
// Get the existing game parameters and init bond for the fault dispute game
IFaultDisputeGame.GameConstructorParams memory fdgParams =
getGameConstructorParams(IFaultDisputeGame(address(fdg)));
initBond = dgf.initBonds(GameTypes.CANNON);
// Create game input with updated prestate but same other params
fdgInput = OPContractsManager.AddGameInput({
disputeAbsolutePrestate: _prestateUpdateInputs[i].absolutePrestate,
saltMixer: saltMixer,
systemConfig: _prestateUpdateInputs[i].systemConfigProxy,
proxyAdmin: _prestateUpdateInputs[i].proxyAdmin,
delayedWETH: IDelayedWETH(payable(address(fdgParams.weth))),
disputeGameType: fdgParams.gameType,
disputeMaxGameDepth: fdgParams.maxGameDepth,
disputeSplitDepth: fdgParams.splitDepth,
disputeClockExtension: fdgParams.clockExtension,
disputeMaxClockDuration: fdgParams.maxClockDuration,
initialBond: initBond,
vm: fdgParams.vm,
permissioned: false
});
}
// Game inputs must be ordered with increasing game type values. So FDG is first if it exists.
if (hasFDG) {
inputs[0] = fdgInput;
inputs[1] = pdgInput;
} else {
inputs[0] = pdgInput;
}
// Add the new game type with updated prestate
addGameType(inputs, _superchainConfig);
}
}
}
contract OPContractsManagerUpgrader is OPContractsManagerBase {
struct UpgradeInput {
ISuperchainConfig superchainConfig;
IProtocolVersions protocolVersions;
IProxyAdmin superchainProxyAdmin;
}
/// @notice Emitted when a chain is upgraded
/// @param systemConfig Address of the chain's SystemConfig contract
/// @param upgrader Address that initiated the upgrade
event Upgraded(uint256 indexed l2ChainId, ISystemConfig indexed systemConfig, address indexed upgrader);
constructor(OPContractsManagerContractsContainer _contractsContainer) OPContractsManagerBase(_contractsContainer) { }
/// @notice Upgrades a set of chains to the latest implementation contracts
/// @param _opChainConfigs Array of OpChain structs, one per chain to upgrade
/// @dev This function is intended to be called via DELEGATECALL from the Upgrade Controller Safe
function upgrade(OPContractsManager.OpChainConfig[] memory _opChainConfigs) external virtual {
OPContractsManager.Implementations memory impls = getImplementations();
for (uint256 i = 0; i < _opChainConfigs.length; i++) {
assertValidOpChainConfig(_opChainConfigs[i]);
// Use the SystemConfig to grab the DisputeGameFactory address.
IDisputeGameFactory dgf = IDisputeGameFactory(_opChainConfigs[i].systemConfigProxy.disputeGameFactory());
// All chains have the PermissionedDisputeGame, grab that.
IPermissionedDisputeGame permissionedDisputeGame =
IPermissionedDisputeGame(address(getGameImplementation(dgf, GameTypes.PERMISSIONED_CANNON)));
// Grab the L2 chain ID from the PermissionedDisputeGame.
uint256 l2ChainId = getL2ChainId(IFaultDisputeGame(address(permissionedDisputeGame)));
// Start by upgrading the SystemConfig contract to have the l2ChainId.
upgradeToAndCall(
_opChainConfigs[i].proxyAdmin,
address(_opChainConfigs[i].systemConfigProxy),
impls.systemConfigImpl,
abi.encodeCall(ISystemConfig.upgrade, (l2ChainId))
);
// Grab chain addresses here. We need to do this after the SystemConfig upgrade or the
// addresses will be incorrect.
ISystemConfig.Addresses memory opChainAddrs = _opChainConfigs[i].systemConfigProxy.getAddresses();
// Grab the current respectedGameType from the OptimismPortal contract before the upgrade.
GameType respectedGameType = IOptimismPortal(payable(opChainAddrs.optimismPortal)).respectedGameType();
// Grab the current SuperchainConfig from the OptimismPortal contract before the upgrade.
ISuperchainConfig superchainConfig =
IOptimismPortal(payable(opChainAddrs.optimismPortal)).superchainConfig();
// Separate context to avoid stack too deep.
IAnchorStateRegistry newAnchorStateRegistryProxy;
{
// Deploy a new AnchorStateRegistry contract.
// We use the SOT suffix to avoid CREATE2 conflicts with the existing ASR.
newAnchorStateRegistryProxy = IAnchorStateRegistry(
deployProxy({
_l2ChainId: l2ChainId,
_proxyAdmin: _opChainConfigs[i].proxyAdmin,
_saltMixer: reusableSaltMixer(_opChainConfigs[i]),
_contractName: "AnchorStateRegistry-SOT"
})
);
// Separate context to avoid stack too deep.
{
// Get the existing anchor root from the old AnchorStateRegistry contract.
// Get the AnchorStateRegistry from the PermissionedDisputeGame.
(Hash root, uint256 l2BlockNumber) = getAnchorStateRegistry(
IFaultDisputeGame(address(permissionedDisputeGame))
).anchors(respectedGameType);
// Upgrade and initialize the AnchorStateRegistry contract.
// Since this is a net-new contract, we need to initialize it.
upgradeToAndCall(
_opChainConfigs[i].proxyAdmin,
address(newAnchorStateRegistryProxy),
impls.anchorStateRegistryImpl,
abi.encodeCall(
IAnchorStateRegistry.initialize,
(
superchainConfig,
dgf,
OutputRoot({ root: root, l2BlockNumber: l2BlockNumber }),
respectedGameType
)
)
);
}
}
// Upgrade the OptimismPortal contract implementation.
upgradeTo(_opChainConfigs[i].proxyAdmin, opChainAddrs.optimismPortal, impls.optimismPortalImpl);
// Separate context to avoid stack too deep.
{
// Deploy the ETHLockbox proxy.
IETHLockbox ethLockbox;
{
ethLockbox = IETHLockbox(
deployProxy({
_l2ChainId: l2ChainId,
_proxyAdmin: _opChainConfigs[i].proxyAdmin,
_saltMixer: reusableSaltMixer(_opChainConfigs[i]),
_contractName: "ETHLockbox"
})
);
// Initialize the ETHLockbox setting the OptimismPortal as an authorized portal.
IOptimismPortal[] memory portals = new IOptimismPortal[](1);
portals[0] = IOptimismPortal(payable(opChainAddrs.optimismPortal));
upgradeToAndCall(
_opChainConfigs[i].proxyAdmin,
address(ethLockbox),
impls.ethLockboxImpl,
abi.encodeCall(IETHLockbox.initialize, (superchainConfig, portals))
);
}
// Call `upgrade` on the OptimismPortal contract.
IOptimismPortal(payable(opChainAddrs.optimismPortal)).upgrade(newAnchorStateRegistryProxy, ethLockbox);
}
// We also need to redeploy the dispute games because the AnchorStateRegistry is new.
// Separate context to avoid stack too deep.
{
// Deploy and set a new permissioned game to update its prestate.
deployAndSetNewGameImpl({
_l2ChainId: l2ChainId,
_disputeGame: IDisputeGame(address(permissionedDisputeGame)),
_newAnchorStateRegistryProxy: newAnchorStateRegistryProxy,
_gameType: GameTypes.PERMISSIONED_CANNON,
_opChainConfig: _opChainConfigs[i]
});
}
// Separate context to avoid stack too deep.
{
// Now retrieve the permissionless game.
IFaultDisputeGame permissionlessDisputeGame =
IFaultDisputeGame(address(getGameImplementation(dgf, GameTypes.CANNON)));
// If it exists, replace its implementation.
if (address(permissionlessDisputeGame) != address(0)) {
// Deploy and set a new permissionless game to update its prestate
deployAndSetNewGameImpl({
_l2ChainId: l2ChainId,
_disputeGame: IDisputeGame(address(permissionlessDisputeGame)),
_newAnchorStateRegistryProxy: newAnchorStateRegistryProxy,
_gameType: GameTypes.CANNON,
_opChainConfig: _opChainConfigs[i]
});
}
}
// Emit the upgraded event with the address of the caller. Since this will be a delegatecall,
// the caller will be the value of the ADDRESS opcode.
emit Upgraded(l2ChainId, _opChainConfigs[i].systemConfigProxy, address(this));
}
}
/// @notice Retrieves the Superchain Config for a bridge contract
function getSuperchainConfig(address _hasSuperchainConfig) internal view returns (ISuperchainConfig) {
return IHasSuperchainConfig(_hasSuperchainConfig).superchainConfig();
}
/// @notice Updates the implementation of a proxy without calling the initializer.
/// First performs safety checks to ensure the target, implementation, and proxy admin are valid.
function upgradeTo(IProxyAdmin _proxyAdmin, address _target, address _implementation) internal {
assertValidContractAddress(_implementation);
_proxyAdmin.upgrade(payable(address(_target)), _implementation);
}
/// @notice Verifies that all OpChainConfig inputs are valid and reverts if any are invalid.
function assertValidOpChainConfig(OPContractsManager.OpChainConfig memory _config) internal view {
assertValidContractAddress(address(_config.systemConfigProxy));
assertValidContractAddress(address(_config.proxyAdmin));
}
/// @notice Deploys and sets a new dispute game implementation
/// @param _l2ChainId The L2 chain ID
/// @param _disputeGame The current dispute game implementation
/// @param _newAnchorStateRegistryProxy The new anchor state registry proxy
/// @param _gameType The type of game to deploy
/// @param _opChainConfig The OP chain configuration
function deployAndSetNewGameImpl(
uint256 _l2ChainId,
IDisputeGame _disputeGame,
IAnchorStateRegistry _newAnchorStateRegistryProxy,
GameType _gameType,
OPContractsManager.OpChainConfig memory _opChainConfig
)
internal
{
OPContractsManager.Blueprints memory bps = getBlueprints();
OPContractsManager.Implementations memory impls = getImplementations();
// Get the constructor params for the game
IFaultDisputeGame.GameConstructorParams memory params =
getGameConstructorParams(IFaultDisputeGame(address(_disputeGame)));
// Modify the params with the new vm values.
params.anchorStateRegistry = IAnchorStateRegistry(address(_newAnchorStateRegistryProxy));
params.vm = IBigStepper(impls.mipsImpl);
// If the prestate is set in the config, use it. If not set, we'll try to use the prestate
// that already exists on the current dispute game.
if (Claim.unwrap(_opChainConfig.absolutePrestate) != bytes32(0)) {
params.absolutePrestate = _opChainConfig.absolutePrestate;
}
// As a sanity check, if the prestate is zero here, revert.
if (params.absolutePrestate.raw() == bytes32(0)) {
revert OPContractsManager.PrestateNotSet();
}
IDisputeGame newGame;
if (GameType.unwrap(_gameType) == GameType.unwrap(GameTypes.PERMISSIONED_CANNON)) {
address proposer = getProposer(IPermissionedDisputeGame(address(_disputeGame)));
address challenger = getChallenger(IPermissionedDisputeGame(address(_disputeGame)));
newGame = IDisputeGame(
Blueprint.deployFrom(
bps.permissionedDisputeGame1,
bps.permissionedDisputeGame2,
computeSalt(_l2ChainId, reusableSaltMixer(_opChainConfig), "PermissionedDisputeGame"),
encodePermissionedFDGConstructor(params, proposer, challenger)
)
);
} else {
newGame = IDisputeGame(
Blueprint.deployFrom(
bps.permissionlessDisputeGame1,
bps.permissionlessDisputeGame2,
computeSalt(_l2ChainId, reusableSaltMixer(_opChainConfig), "PermissionlessDisputeGame"),
encodePermissionlessFDGConstructor(params)
)
);
}
// Grab the DisputeGameFactory from the SystemConfig.
IDisputeGameFactory dgf = IDisputeGameFactory(_opChainConfig.systemConfigProxy.disputeGameFactory());
// Set the new implementation.
setDGFImplementation(dgf, _gameType, IDisputeGame(newGame));
}
}
contract OPContractsManagerDeployer is OPContractsManagerBase {
/// @notice Emitted when a new OP Stack chain is deployed.
/// @param l2ChainId Chain ID of the new chain.
/// @param deployer Address that deployed the chain.
/// @param deployOutput ABI-encoded output of the deployment.
event Deployed(uint256 indexed l2ChainId, address indexed deployer, bytes deployOutput);
constructor(OPContractsManagerContractsContainer _contractsContainer) OPContractsManagerBase(_contractsContainer) { }
function deploy(
OPContractsManager.DeployInput calldata _input,
ISuperchainConfig _superchainConfig,
address _deployer
)
external
virtual
returns (OPContractsManager.DeployOutput memory)
{
assertValidInputs(_input);
OPContractsManager.DeployOutput memory output;
OPContractsManager.Blueprints memory blueprint = getBlueprints();
OPContractsManager.Implementations memory implementation = getImplementations();
// -------- Deploy Chain Singletons --------
// The AddressManager is used to store the implementation for the L1CrossDomainMessenger
// due to it's usage of the legacy ResolvedDelegateProxy.
output.addressManager = IAddressManager(
Blueprint.deployFrom(
blueprint.addressManager,
computeSalt(_input.l2ChainId, _input.saltMixer, "AddressManager"),
abi.encode()
)
);
// The ProxyAdmin is the owner of all proxies for the chain. We temporarily set the owner to
// this contract, and then transfer ownership to the specified owner at the end of deployment.
output.opChainProxyAdmin = IProxyAdmin(
Blueprint.deployFrom(
blueprint.proxyAdmin,
computeSalt(_input.l2ChainId, _input.saltMixer, "ProxyAdmin"),
abi.encode(address(this))
)
);
// Set the AddressManager on the ProxyAdmin.
output.opChainProxyAdmin.setAddressManager(output.addressManager);
// Transfer ownership of the AddressManager to the ProxyAdmin.
transferOwnership(address(output.addressManager), address(output.opChainProxyAdmin));
// -------- Deploy Proxy Contracts --------
// Deploy ERC-1967 proxied contracts.
output.l1ERC721BridgeProxy =
IL1ERC721Bridge(deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "L1ERC721Bridge"));
output.optimismPortalProxy = IOptimismPortal(
payable(deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "OptimismPortal"))
);
output.ethLockboxProxy =
IETHLockbox(deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "ETHLockbox"));
output.systemConfigProxy =
ISystemConfig(deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "SystemConfig"));
output.optimismMintableERC20FactoryProxy = IOptimismMintableERC20Factory(
deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "OptimismMintableERC20Factory")
);
output.disputeGameFactoryProxy = IDisputeGameFactory(
deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "DisputeGameFactory")
);
output.anchorStateRegistryProxy = IAnchorStateRegistry(
deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "AnchorStateRegistry")
);
// Deploy legacy proxied contracts.
output.l1StandardBridgeProxy = IL1StandardBridge(
payable(
Blueprint.deployFrom(
blueprint.l1ChugSplashProxy,
computeSalt(_input.l2ChainId, _input.saltMixer, "L1StandardBridge"),
abi.encode(output.opChainProxyAdmin)
)
)
);
output.opChainProxyAdmin.setProxyType(address(output.l1StandardBridgeProxy), IProxyAdmin.ProxyType.CHUGSPLASH);
string memory contractName = "OVM_L1CrossDomainMessenger";
output.l1CrossDomainMessengerProxy = IL1CrossDomainMessenger(
Blueprint.deployFrom(
blueprint.resolvedDelegateProxy,
computeSalt(_input.l2ChainId, _input.saltMixer, "L1CrossDomainMessenger"),
abi.encode(output.addressManager, contractName)
)
);
output.opChainProxyAdmin.setProxyType(
address(output.l1CrossDomainMessengerProxy), IProxyAdmin.ProxyType.RESOLVED
);
output.opChainProxyAdmin.setImplementationName(address(output.l1CrossDomainMessengerProxy), contractName);
// Eventually we will switch from DelayedWETHPermissionedGameProxy to DelayedWETHPermissionlessGameProxy.
output.delayedWETHPermissionedGameProxy = IDelayedWETH(
payable(
deployProxy(_input.l2ChainId, output.opChainProxyAdmin, _input.saltMixer, "DelayedWETHPermissionedGame")
)
);
// While not a proxy, we deploy the PermissionedDisputeGame here as well because it's bespoke per chain.
output.permissionedDisputeGame = IPermissionedDisputeGame(
Blueprint.deployFrom(
blueprint.permissionedDisputeGame1,
blueprint.permissionedDisputeGame2,
computeSalt(_input.l2ChainId, _input.saltMixer, "PermissionedDisputeGame"),
encodePermissionedFDGConstructor(
IFaultDisputeGame.GameConstructorParams({
gameType: _input.disputeGameType,
absolutePrestate: _input.disputeAbsolutePrestate,
maxGameDepth: _input.disputeMaxGameDepth,
splitDepth: _input.disputeSplitDepth,
clockExtension: _input.disputeClockExtension,
maxClockDuration: _input.disputeMaxClockDuration,
vm: IBigStepper(implementation.mipsImpl),
weth: IDelayedWETH(payable(address(output.delayedWETHPermissionedGameProxy))),
anchorStateRegistry: IAnchorStateRegistry(address(output.anchorStateRegistryProxy)),
l2ChainId: _input.l2ChainId
}),
_input.roles.proposer,
_input.roles.challenger
)
)
);
// -------- Set and Initialize Proxy Implementations --------
bytes memory data;
data = encodeL1ERC721BridgeInitializer(output, _superchainConfig);
upgradeToAndCall(
output.opChainProxyAdmin, address(output.l1ERC721BridgeProxy), implementation.l1ERC721BridgeImpl, data
);
data = encodeOptimismPortalInitializer(output, _superchainConfig);
upgradeToAndCall(
output.opChainProxyAdmin, address(output.optimismPortalProxy), implementation.optimismPortalImpl, data
);
// Initialize the ETHLockbox.
IOptimismPortal[] memory portals = new IOptimismPortal[](1);
portals[0] = output.optimismPortalProxy;
data = encodeETHLockboxInitializer(_superchainConfig, portals);
upgradeToAndCall(output.opChainProxyAdmin, address(output.ethLockboxProxy), implementation.ethLockboxImpl, data);
data = encodeSystemConfigInitializer(_input, output);
upgradeToAndCall(
output.opChainProxyAdmin, address(output.systemConfigProxy), implementation.systemConfigImpl, data
);
data = encodeOptimismMintableERC20FactoryInitializer(output);
upgradeToAndCall(
output.opChainProxyAdmin,
address(output.optimismMintableERC20FactoryProxy),
implementation.optimismMintableERC20FactoryImpl,
data
);
data = encodeL1CrossDomainMessengerInitializer(output, _superchainConfig);
upgradeToAndCall(
output.opChainProxyAdmin,
address(output.l1CrossDomainMessengerProxy),
implementation.l1CrossDomainMessengerImpl,
data
);
data = encodeL1StandardBridgeInitializer(output, _superchainConfig);
upgradeToAndCall(
output.opChainProxyAdmin, address(output.l1StandardBridgeProxy), implementation.l1StandardBridgeImpl, data
);
data = encodeDelayedWETHInitializer(_input, _superchainConfig);
// Eventually we will switch from DelayedWETHPermissionedGameProxy to DelayedWETHPermissionlessGameProxy.
upgradeToAndCall(
output.opChainProxyAdmin,
address(output.delayedWETHPermissionedGameProxy),
implementation.delayedWETHImpl,
data
);
// We set the initial owner to this contract, set game implementations, then transfer ownership.
data = encodeDisputeGameFactoryInitializer();
upgradeToAndCall(
output.opChainProxyAdmin,
address(output.disputeGameFactoryProxy),
implementation.disputeGameFactoryImpl,
data
);
setDGFImplementation(
output.disputeGameFactoryProxy,
GameTypes.PERMISSIONED_CANNON,
IDisputeGame(address(output.permissionedDisputeGame))
);
transferOwnership(address(output.disputeGameFactoryProxy), address(_input.roles.opChainProxyAdminOwner));
data = encodeAnchorStateRegistryInitializer(_input, _superchainConfig, output);
upgradeToAndCall(
output.opChainProxyAdmin,
address(output.anchorStateRegistryProxy),
implementation.anchorStateRegistryImpl,
data
);
// -------- Finalize Deployment --------
// Transfer ownership of the ProxyAdmin from this contract to the specified owner.
transferOwnership(address(output.opChainProxyAdmin), _input.roles.opChainProxyAdminOwner);
emit Deployed(_input.l2ChainId, _deployer, abi.encode(output));
return output;
}
/// @notice Returns default, standard config arguments for the SystemConfig initializer.
/// This is used by subclasses to reduce code duplication.
function defaultSystemConfigParams(
OPContractsManager.DeployInput memory, /* _input */
OPContractsManager.DeployOutput memory _output
)
internal
view
virtual
returns (IResourceMetering.ResourceConfig memory resourceConfig_, ISystemConfig.Addresses memory opChainAddrs_)
{
resourceConfig_ = Constants.DEFAULT_RESOURCE_CONFIG();
opChainAddrs_ = ISystemConfig.Addresses({
l1CrossDomainMessenger: address(_output.l1CrossDomainMessengerProxy),
l1ERC721Bridge: address(_output.l1ERC721BridgeProxy),
l1StandardBridge: address(_output.l1StandardBridgeProxy),
optimismPortal: address(_output.optimismPortalProxy),
optimismMintableERC20Factory: address(_output.optimismMintableERC20FactoryProxy)
});
assertValidContractAddress(opChainAddrs_.l1CrossDomainMessenger);
assertValidContractAddress(opChainAddrs_.l1ERC721Bridge);
assertValidContractAddress(opChainAddrs_.l1StandardBridge);
assertValidContractAddress(opChainAddrs_.optimismPortal);
assertValidContractAddress(opChainAddrs_.optimismMintableERC20Factory);
}