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
package state
import (
"errors"
"fmt"
"math/big"
"net/url"
"reflect"
"github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/artifacts"
"github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/standard"
"github.com/ethereum-optimism/optimism/op-service/ioutil"
"github.com/ethereum-optimism/optimism/op-service/jsonutil"
"github.com/ethereum/go-ethereum/common"
)
type DeploymentStrategy string
const (
DeploymentStrategyLive DeploymentStrategy = "live"
DeploymentStrategyGenesis DeploymentStrategy = "genesis"
)
func (d DeploymentStrategy) Check() error {
switch d {
case DeploymentStrategyLive, DeploymentStrategyGenesis:
return nil
default:
return fmt.Errorf("deployment strategy must be 'live' or 'genesis'")
}
}
type IntentConfigType string
const (
IntentConfigTypeStandard IntentConfigType = "standard"
IntentConfigTypeCustom IntentConfigType = "custom"
IntentConfigTypeStrict IntentConfigType = "strict"
IntentConfigTypeStandardOverrides IntentConfigType = "standard-overrides"
IntentConfigTypeStrictOverrides IntentConfigType = "strict-overrides"
)
var emptyAddress common.Address
var emptyHash common.Hash
type SuperchainProofParams struct {
WithdrawalDelaySeconds uint64 `json:"withdrawalDelaySeconds" toml:"withdrawalDelaySeconds"`
MinProposalSizeBytes uint64 `json:"minProposalSizeBytes" toml:"minProposalSizeBytes"`
ChallengePeriodSeconds uint64 `json:"challengePeriodSeconds" toml:"challengePeriodSeconds"`
ProofMaturityDelaySeconds uint64 `json:"proofMaturityDelaySeconds" toml:"proofMaturityDelaySeconds"`
DisputeGameFinalityDelaySeconds uint64 `json:"disputeGameFinalityDelaySeconds" toml:"disputeGameFinalityDelaySeconds"`
MIPSVersion uint64 `json:"mipsVersion" toml:"mipsVersion"`
}
type Intent struct {
DeploymentStrategy DeploymentStrategy `json:"deploymentStrategy" toml:"deploymentStrategy"`
ConfigType IntentConfigType `json:"configType" toml:"configType"`
L1ChainID uint64 `json:"l1ChainID" toml:"l1ChainID"`
SuperchainRoles *SuperchainRoles `json:"superchainRoles" toml:"superchainRoles,omitempty"`
FundDevAccounts bool `json:"fundDevAccounts" toml:"fundDevAccounts"`
UseInterop bool `json:"useInterop" toml:"useInterop"`
L1ContractsLocator *artifacts.Locator `json:"l1ContractsLocator" toml:"l1ContractsLocator"`
L2ContractsLocator *artifacts.Locator `json:"l2ContractsLocator" toml:"l2ContractsLocator"`
Chains []*ChainIntent `json:"chains" toml:"chains"`
GlobalDeployOverrides map[string]any `json:"globalDeployOverrides" toml:"globalDeployOverrides"`
}
type SuperchainRoles struct {
ProxyAdminOwner common.Address `json:"proxyAdminOwner" toml:"proxyAdminOwner"`
ProtocolVersionsOwner common.Address `json:"protocolVersionsOwner" toml:"protocolVersionsOwner"`
Guardian common.Address `json:"guardian" toml:"guardian"`
}
var ErrSuperchainRoleZeroAddress = errors.New("SuperchainRole is set to zero address")
var ErrL1ContractsLocatorUndefined = errors.New("L1ContractsLocator undefined")
var ErrL2ContractsLocatorUndefined = errors.New("L2ContractsLocator undefined")
func (s *SuperchainRoles) CheckNoZeroAddresses() error {
val := reflect.ValueOf(*s)
typ := reflect.TypeOf(*s)
// Iterate through all the fields
for i := 0; i < val.NumField(); i++ {
fieldValue := val.Field(i)
fieldName := typ.Field(i).Name
if fieldValue.Interface() == (common.Address{}) {
return fmt.Errorf("%w: %s", ErrSuperchainRoleZeroAddress, fieldName)
}
}
return nil
}
func (c *Intent) L1ChainIDBig() *big.Int {
return big.NewInt(int64(c.L1ChainID))
}
func (c *Intent) validateCustomConfig() error {
if c.L1ContractsLocator == nil ||
(c.L1ContractsLocator.Tag == "" && c.L1ContractsLocator.URL == &url.URL{}) {
return ErrL1ContractsLocatorUndefined
}
if c.L2ContractsLocator == nil ||
(c.L2ContractsLocator.Tag == "" && c.L2ContractsLocator.URL == &url.URL{}) {
return ErrL2ContractsLocatorUndefined
}
if c.SuperchainRoles == nil {
return errors.New("SuperchainRoles is set to nil")
}
if err := c.SuperchainRoles.CheckNoZeroAddresses(); err != nil {
return err
}
if len(c.Chains) == 0 {
return errors.New("must define at least one l2 chain")
}
for _, chain := range c.Chains {
if err := chain.Check(); err != nil {
return err
}
}
return nil
}
func (c *Intent) validateStrictConfig() error {
if err := c.validateStandardValues(); err != nil {
return err
}
challenger, _ := standard.ChallengerAddressFor(c.L1ChainID)
l1ProxyAdminOwner, _ := standard.L1ProxyAdminOwner(c.L1ChainID)
for chainIndex := range c.Chains {
if c.Chains[chainIndex].Roles.Challenger != challenger {
return fmt.Errorf("invalid challenger address for chain: %s", c.Chains[chainIndex].ID)
}
if c.Chains[chainIndex].Roles.L1ProxyAdminOwner != l1ProxyAdminOwner {
return fmt.Errorf("invalid l1ProxyAdminOwner address for chain: %s", c.Chains[chainIndex].ID)
}
}
return nil
}
// Ensures the following:
// 1. no zero-values for non-standard fields (user should have populated these)
// 2. no non-standard values for standard fields (user should not have changed these)
func (c *Intent) validateStandardValues() error {
if err := c.checkL1Prod(); err != nil {
return err
}
if err := c.checkL2Prod(); err != nil {
return err
}
standardSuperchainRoles, err := getStandardSuperchainRoles(c.L1ChainID)
if err != nil {
return fmt.Errorf("error getting standard superchain roles: %w", err)
}
if c.SuperchainRoles == nil || *c.SuperchainRoles != *standardSuperchainRoles {
return fmt.Errorf("SuperchainRoles does not match standard value")
}
for _, chain := range c.Chains {
if err := chain.Check(); err != nil {
return err
}
if chain.Eip1559DenominatorCanyon != standard.Eip1559DenominatorCanyon ||
chain.Eip1559Denominator != standard.Eip1559Denominator ||
chain.Eip1559Elasticity != standard.Eip1559Elasticity {
return fmt.Errorf("%w: chainId=%s", ErrNonStandardValue, chain.ID)
}
if len(chain.AdditionalDisputeGames) > 0 {
return fmt.Errorf("%w: chainId=%s additionalDisputeGames must be nil", ErrNonStandardValue, chain.ID)
}
}
return nil
}
func getStandardSuperchainRoles(l1ChainId uint64) (*SuperchainRoles, error) {
superCfg, err := standard.SuperchainFor(l1ChainId)
if err != nil {
return nil, fmt.Errorf("error getting superchain config: %w", err)
}
proxyAdminOwner, err := standard.L1ProxyAdminOwner(l1ChainId)
if err != nil {
return nil, fmt.Errorf("error getting L1ProxyAdminOwner: %w", err)
}
guardian, err := standard.GuardianAddressFor(l1ChainId)
if err != nil {
return nil, fmt.Errorf("error getting guardian address: %w", err)
}
superchainRoles := &SuperchainRoles{
ProxyAdminOwner: proxyAdminOwner,
ProtocolVersionsOwner: common.Address(*superCfg.Config.ProtocolVersionsAddr),
Guardian: guardian,
}
return superchainRoles, nil
}
func (c *Intent) Check() error {
if c.L1ChainID == 0 {
return fmt.Errorf("l1ChainID cannot be 0")
}
if err := c.DeploymentStrategy.Check(); err != nil {
return err
}
if c.L1ContractsLocator == nil {
return ErrL1ContractsLocatorUndefined
}
if c.L2ContractsLocator == nil {
return ErrL2ContractsLocatorUndefined
}
var err error
switch c.ConfigType {
case IntentConfigTypeStandard:
err = c.validateStandardValues()
case IntentConfigTypeCustom:
err = c.validateCustomConfig()
case IntentConfigTypeStrict:
err = c.validateStrictConfig()
case IntentConfigTypeStandardOverrides, IntentConfigTypeStrictOverrides:
err = c.validateCustomConfig()
default:
return fmt.Errorf("intent-config-type unsupported: %s", c.ConfigType)
}
if err != nil {
return fmt.Errorf("failed to validate intent-config-type=%s: %w", c.ConfigType, err)
}
return nil
}
func (c *Intent) Chain(id common.Hash) (*ChainIntent, error) {
for i := range c.Chains {
if c.Chains[i].ID == id {
return c.Chains[i], nil
}
}
return nil, fmt.Errorf("chain %d not found", id)
}
func (c *Intent) WriteToFile(path string) error {
return jsonutil.WriteTOML(c, ioutil.ToAtomicFile(path, 0o755))
}
func (c *Intent) checkL1Prod() error {
versions, err := standard.L1VersionsFor(c.L1ChainID)
if err != nil {
return err
}
if _, ok := versions.Releases[c.L1ContractsLocator.Tag]; !ok {
return fmt.Errorf("tag '%s' not found in standard versions", c.L1ContractsLocator.Tag)
}
return nil
}
func (c *Intent) checkL2Prod() error {
_, err := standard.ArtifactsURLForTag(c.L2ContractsLocator.Tag)
return err
}
func NewIntent(configType IntentConfigType, deploymentStrategy DeploymentStrategy, l1ChainId uint64, l2ChainIds []common.Hash) (Intent, error) {
switch configType {
case IntentConfigTypeCustom:
return NewIntentCustom(deploymentStrategy, l1ChainId, l2ChainIds)
case IntentConfigTypeStandard:
return NewIntentStandard(deploymentStrategy, l1ChainId, l2ChainIds)
case IntentConfigTypeStandardOverrides:
return NewIntentStandardOverrides(deploymentStrategy, l1ChainId, l2ChainIds)
case IntentConfigTypeStrict:
return NewIntentStrict(deploymentStrategy, l1ChainId, l2ChainIds)
case IntentConfigTypeStrictOverrides:
return NewIntentStrictOverrides(deploymentStrategy, l1ChainId, l2ChainIds)
default:
return Intent{}, fmt.Errorf("intent config type not supported")
}
}
// Sets all Intent fields to their zero value with the expectation that the
// user will populate the values before running 'apply'
func NewIntentCustom(deploymentStrategy DeploymentStrategy, l1ChainId uint64, l2ChainIds []common.Hash) (Intent, error) {
intent := Intent{
DeploymentStrategy: deploymentStrategy,
ConfigType: IntentConfigTypeCustom,
L1ChainID: l1ChainId,
L1ContractsLocator: &artifacts.Locator{URL: &url.URL{}},
L2ContractsLocator: &artifacts.Locator{URL: &url.URL{}},
SuperchainRoles: &SuperchainRoles{},
}
for _, l2ChainID := range l2ChainIds {
intent.Chains = append(intent.Chains, &ChainIntent{
ID: l2ChainID,
})
}
return intent, nil
}
func NewIntentStandard(deploymentStrategy DeploymentStrategy, l1ChainId uint64, l2ChainIds []common.Hash) (Intent, error) {
intent := Intent{
DeploymentStrategy: deploymentStrategy,
ConfigType: IntentConfigTypeStandard,
L1ChainID: l1ChainId,
L1ContractsLocator: artifacts.DefaultL1ContractsLocator,
L2ContractsLocator: artifacts.DefaultL2ContractsLocator,
}
superchainRoles, err := getStandardSuperchainRoles(l1ChainId)
if err != nil {
return Intent{}, fmt.Errorf("error getting standard superchain roles: %w", err)
}
intent.SuperchainRoles = superchainRoles
for _, l2ChainID := range l2ChainIds {
intent.Chains = append(intent.Chains, &ChainIntent{
ID: l2ChainID,
Eip1559DenominatorCanyon: standard.Eip1559DenominatorCanyon,
Eip1559Denominator: standard.Eip1559Denominator,
Eip1559Elasticity: standard.Eip1559Elasticity,
})
}
return intent, nil
}
func NewIntentStandardOverrides(deploymentStrategy DeploymentStrategy, l1ChainId uint64, l2ChainIds []common.Hash) (Intent, error) {
intent, err := NewIntentStandard(deploymentStrategy, l1ChainId, l2ChainIds)
if err != nil {
return Intent{}, err
}
intent.ConfigType = IntentConfigTypeStandardOverrides
return intent, nil
}
// Same as NewIntentStandard, but also sets l2 Challenger and L1ProxyAdminOwner
// addresses to standard values
func NewIntentStrict(deploymentStrategy DeploymentStrategy, l1ChainId uint64, l2ChainIds []common.Hash) (Intent, error) {
intent, err := NewIntentStandard(deploymentStrategy, l1ChainId, l2ChainIds)
if err != nil {
return Intent{}, err
}
intent.ConfigType = IntentConfigTypeStrict
challenger, _ := standard.ChallengerAddressFor(l1ChainId)
l1ProxyAdminOwner, _ := standard.L1ProxyAdminOwner(l1ChainId)
for chainIndex := range intent.Chains {
intent.Chains[chainIndex].Roles.Challenger = challenger
intent.Chains[chainIndex].Roles.L1ProxyAdminOwner = l1ProxyAdminOwner
}
return intent, nil
}
func NewIntentStrictOverrides(deploymentStrategy DeploymentStrategy, l1ChainId uint64, l2ChainIds []common.Hash) (Intent, error) {
intent, err := NewIntentStrict(deploymentStrategy, l1ChainId, l2ChainIds)
if err != nil {
return Intent{}, err
}
intent.ConfigType = IntentConfigTypeStrictOverrides
return intent, nil
}