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
package genesis
import (
"context"
"encoding/json"
"fmt"
"math/big"
"os"
"path/filepath"
"github.com/urfave/cli"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum-optimism/optimism/op-bindings/hardhat"
"github.com/ethereum-optimism/optimism/op-chain-ops/genesis"
)
var Subcommands = cli.Commands{
{
Name: "devnet",
Usage: "Initialize new L1 and L2 genesis files and rollup config suitable for a local devnet",
Flags: []cli.Flag{
cli.StringFlag{
Name: "deploy-config",
Usage: "Path to hardhat deploy config file",
},
cli.StringFlag{
Name: "outfile.l1",
Usage: "Path to L1 genesis output file",
},
cli.StringFlag{
Name: "outfile.l2",
Usage: "Path to L2 genesis output file",
},
cli.StringFlag{
Name: "outfile.rollup",
Usage: "Path to rollup output file",
},
},
Action: func(ctx *cli.Context) error {
deployConfig := ctx.String("deploy-config")
config, err := genesis.NewDeployConfig(deployConfig)
if err != nil {
return err
}
// Add the developer L1 addresses to the config
if err := config.InitDeveloperDeployedAddresses(); err != nil {
return err
}
if err := config.Check(); err != nil {
return err
}
l1Genesis, err := genesis.BuildL1DeveloperGenesis(config)
if err != nil {
return err
}
l1StartBlock := l1Genesis.ToBlock()
l2Genesis, err := genesis.BuildL2DeveloperGenesis(config, l1StartBlock)
if err != nil {
return err
}
l2GenesisBlock := l2Genesis.ToBlock()
rollupConfig, err := config.RollupConfig(l1StartBlock, l2GenesisBlock.Hash(), l2GenesisBlock.Number().Uint64())
if err != nil {
return err
}
if err := writeGenesisFile(ctx.String("outfile.l1"), l1Genesis); err != nil {
return err
}
if err := writeGenesisFile(ctx.String("outfile.l2"), l2Genesis); err != nil {
return err
}
return writeGenesisFile(ctx.String("outfile.rollup"), rollupConfig)
},
},
{
Name: "l2",
Usage: "Generates an L2 genesis file and rollup config suitable for a deployed network",
Flags: []cli.Flag{
cli.StringFlag{
Name: "l1-rpc",
Usage: "L1 RPC URL",
},
cli.StringFlag{
Name: "deploy-config",
Usage: "Path to hardhat deploy config file",
},
cli.StringFlag{
Name: "deployment-dir",
Usage: "Path to deployment directory",
},
cli.StringFlag{
Name: "outfile.l2",
Usage: "Path to L2 genesis output file",
},
cli.StringFlag{
Name: "outfile.rollup",
Usage: "Path to rollup output file",
},
},
Action: func(ctx *cli.Context) error {
deployConfig := ctx.String("deploy-config")
config, err := genesis.NewDeployConfig(deployConfig)
if err != nil {
return err
}
depPath, network := filepath.Split(ctx.String("deployment-dir"))
hh, err := hardhat.New(network, nil, []string{depPath})
if err != nil {
return err
}
// Read the appropriate deployment addresses from disk
if err := config.GetDeployedAddresses(hh); err != nil {
return err
}
// Sanity check the config
if err := config.Check(); err != nil {
return err
}
client, err := ethclient.Dial(ctx.String("l1-rpc"))
if err != nil {
return fmt.Errorf("cannot dial %s: %w", ctx.String("l1-rpc"), err)
}
var l1StartBlock *types.Block
if config.L1StartingBlockTag.BlockHash != nil {
l1StartBlock, err = client.BlockByHash(context.Background(), *config.L1StartingBlockTag.BlockHash)
} else if config.L1StartingBlockTag.BlockNumber != nil {
l1StartBlock, err = client.BlockByNumber(context.Background(), big.NewInt(config.L1StartingBlockTag.BlockNumber.Int64()))
}
if err != nil {
return fmt.Errorf("error getting l1 start block: %w", err)
}
// Build the developer L2 genesis block
l2Genesis, err := genesis.BuildL2DeveloperGenesis(config, l1StartBlock)
if err != nil {
return fmt.Errorf("error creating l2 developer genesis: %w", err)
}
l2GenesisBlock := l2Genesis.ToBlock()
rollupConfig, err := config.RollupConfig(l1StartBlock, l2GenesisBlock.Hash(), l2GenesisBlock.Number().Uint64())
if err != nil {
return err
}
if err := rollupConfig.Check(); err != nil {
return fmt.Errorf("generated rollup config does not pass validation: %w", err)
}
if err := writeGenesisFile(ctx.String("outfile.l2"), l2Genesis); err != nil {
return err
}
return writeGenesisFile(ctx.String("outfile.rollup"), rollupConfig)
},
},
}
func writeGenesisFile(outfile string, input any) error {
f, err := os.OpenFile(outfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
return enc.Encode(input)
}