forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_test.go
437 lines (388 loc) · 13.2 KB
/
migration_test.go
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
package op_e2e
import (
"context"
"encoding/json"
"fmt"
"math/big"
"os"
"os/exec"
"path"
"testing"
"time"
bss "github.com/ethereum-optimism/optimism/op-batcher/batcher"
"github.com/ethereum-optimism/optimism/op-node/sources"
l2os "github.com/ethereum-optimism/optimism/op-proposer/proposer"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-bindings/hardhat"
"github.com/ethereum-optimism/optimism/op-chain-ops/genesis"
"github.com/ethereum-optimism/optimism/op-chain-ops/genesis/migration_action"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/metrics"
"github.com/ethereum-optimism/optimism/op-node/node"
"github.com/ethereum-optimism/optimism/op-node/p2p"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
"github.com/ethereum-optimism/optimism/op-node/testlog"
"github.com/ethereum-optimism/optimism/op-service/backoff"
)
type migrationTestConfig struct {
enabled bool
l1URL string
l2Path string
ovmAddrsPath string
evmAddrsPath string
ovmAllowancesPath string
ovmMessagesPath string
evmMessagesPath string
}
var config migrationTestConfig
var cwd string
func init() {
if os.Getenv("OP_E2E_MIGRATION_ENABLED") != "true" {
return
}
iCwd, err := os.Getwd()
if err != nil {
panic("failed to get cwd")
}
cwd = iCwd
config.enabled = true
config.l1URL = os.Getenv("OP_E2E_MIGRATION_L1_URL")
if config.l1URL == "" {
panic("must specify an L1 url")
}
config.l2Path = os.Getenv("OP_E2E_MIGRATION_L2_DATA_PATH")
if config.l2Path == "" {
panic("must specify an l2 data path")
}
migrationDataDir := path.Join(cwd, "..", "packages", "migration-data", "data")
config.ovmAddrsPath = path.Join(migrationDataDir, "ovm-addresses.json")
config.evmAddrsPath = path.Join(migrationDataDir, "evm-addresses.json")
config.ovmAllowancesPath = path.Join(migrationDataDir, "ovm-allowances.json")
config.ovmMessagesPath = path.Join(migrationDataDir, "ovm-messages.json")
config.evmMessagesPath = path.Join(migrationDataDir, "evm-messages.json")
}
type storageSlot struct {
addr string
slot string
value string
}
const (
networkName = "mainnet-forked"
hardhatImage = "docker.io/ethereumoptimism/hardhat-node:latest"
forkedL1URL = "http://127.0.0.1:8545"
)
var hardcodedSlots = []storageSlot{
// Address manager owner
{
"0xdE1FCfB0851916CA5101820A69b13a4E276bd81F",
"0x0",
"0x000000000000000000000000f39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
},
// L1SB Proxy Owner
{
"0x99C9fc46f92E8a1c0deC1b1747d010903E884bE1",
"0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103",
"0x000000000000000000000000f39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
},
// L1XDM Owner
{
"0x25ace71c97B33Cc4729CF772ae268934F7ab5fA1",
"0x33",
"0x000000000000000000000000f39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
},
}
func TestMigration(t *testing.T) {
if !config.enabled {
t.Skipf("skipping migration tests")
return
}
lgr := testlog.Logger(t, log.LvlDebug)
lgr.Info("starting forked L1")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dkr, err := client.NewClientWithOpts(client.FromEnv)
require.NoError(t, err, "error connecting to Docker")
//_, err = dkr.ImagePull(context.Background(), hardhatImage, types.ImagePullOptions{})
//require.NoError(t, err, "error pulling hardhat image")
realL1Client, err := ethclient.Dial(config.l1URL)
require.NoError(t, err)
headBlock, err := realL1Client.HeaderByNumber(ctx, nil)
require.NoError(t, err)
// Have to specify a small confirmation depth here to prevent the Hardhat fork
// from timing out in the middle of contract deployments.
forkBlock, err := realL1Client.BlockByNumber(ctx, new(big.Int).Sub(headBlock.Number, big.NewInt(10)))
require.NoError(t, err)
forkBlockNumber := forkBlock.NumberU64()
lgr.Info("writing deploy config")
deployCfg := e2eutils.ForkedDeployConfig(t, e2eutils.DefaultMnemonicConfig, forkBlock)
deployCfgPath := path.Join(cwd, "..", "packages", "contracts-bedrock", "deploy-config", "mainnet-forked.json")
f, err := os.OpenFile(deployCfgPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o744)
require.NoError(t, err)
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
require.NoError(t, enc.Encode(deployCfg))
ctnr, err := dkr.ContainerCreate(ctx, &container.Config{
Image: hardhatImage,
Env: []string{
fmt.Sprintf("FORK_STARTING_BLOCK=%d", forkBlockNumber),
fmt.Sprintf("FORK_URL=%s", config.l1URL),
"FORK_CHAIN_ID=1",
},
}, &container.HostConfig{
PortBindings: nat.PortMap{
"8545/tcp": []nat.PortBinding{
{
HostIP: "127.0.0.1", HostPort: "8545",
},
},
},
}, nil, nil, "")
require.NoError(t, err, "error creating hardhat container")
err = dkr.ContainerStart(ctx, ctnr.ID, types.ContainerStartOptions{})
require.NoError(t, err)
t.Cleanup(func() {
timeout := 5 * time.Second
err = dkr.ContainerStop(context.Background(), ctnr.ID, &timeout)
require.NoError(t, err)
})
var forkedL1RPC *rpc.Client
var forkedL1Client *ethclient.Client
require.NoError(t, backoff.Do(10, backoff.Exponential(), func() error {
forkedL1RPC, err = rpc.Dial(forkedL1URL)
if err != nil {
lgr.Warn("error connecting to forked L1, trying again", "err", err)
return err
}
forkedL1Client = ethclient.NewClient(forkedL1RPC)
_, err = forkedL1Client.ChainID(ctx)
if err != nil {
lgr.Warn("error connecting to forked L1, trying again", "err", err)
}
return err
}), "error connecting to forked L1")
for _, slot := range hardcodedSlots {
lgr.Info("setting storage slot", "addr", slot.addr, "slot", slot.slot)
require.NoError(t, forkedL1RPC.Call(nil, "hardhat_setStorageAt", slot.addr, slot.slot, slot.value))
}
tag := rpc.BlockNumberOrHash(*deployCfg.L1StartingBlockTag)
l1BlockHash, ok := tag.Hash()
require.True(t, ok, "invalid l1 starting block tag")
l1Block, err := forkedL1Client.BlockByHash(ctx, l1BlockHash)
require.NoError(t, err)
workdir := "/tmp/migration-tmp-workdir"
require.NoError(t, os.MkdirAll(workdir, 0o755))
lgr.Info("performing L1 migration")
t.Cleanup(func() {
// Clean up the mainnet-forked deployment artifacts
require.NoError(t, os.RemoveAll(path.Join(cwd, "..", "packages", "contracts-bedrock", "deployments", networkName)))
})
migrateL1(t)
lgr.Info("l1 successfully migrated!")
hh, err := hardhat.New(networkName, []string{}, []string{
path.Join(cwd, "..", "packages", "contracts-bedrock", "deployments"),
path.Join(cwd, "..", "packages", "contracts-periphery", "deployments"),
path.Join(cwd, "..", "packages", "contracts", "deployments"),
})
require.NoError(t, err)
require.NoError(t, deployCfg.GetDeployedAddresses(hh))
go makeBlocks(ctx, forkedL1RPC, lgr)
lgr.Info("extracting L2 datadir")
untar(t, config.l2Path, workdir)
lgr.Info("performing L2 migration")
migRes := migrateL2(t, workdir, deployCfg, l1Block.NumberU64())
lgr.Info("starting new L2 system")
portal, err := hh.GetDeployment("OptimismPortalProxy")
require.NoError(t, err)
sysConfig, err := hh.GetDeployment("SystemConfigProxy")
require.NoError(t, err)
l2OS, err := hh.GetDeployment("L2OutputOracleProxy")
require.NoError(t, err)
jwt := writeDefaultJWT(t)
nodeCfg := defaultNodeConfig("geth", jwt)
nodeCfg.DataDir = workdir
ethCfg := ðconfig.Config{
NetworkId: deployCfg.L2ChainID,
}
gethNode, _, err := createGethNode(true, nodeCfg, ethCfg, nil)
require.NoError(t, err)
require.NoError(t, gethNode.Start())
t.Cleanup(func() {
require.NoError(t, gethNode.Close())
})
secrets, err := e2eutils.DefaultMnemonicConfig.Secrets()
require.NoError(t, err)
// Don't log state snapshots in test output
snapLog := log.New()
snapLog.SetHandler(log.DiscardHandler())
rollupNodeConfig := &node.Config{
L1: &node.L1EndpointConfig{
L1NodeAddr: forkedL1URL,
L1TrustRPC: false,
L1RPCKind: sources.RPCKindBasic,
},
L2: &node.L2EndpointConfig{
L2EngineAddr: gethNode.HTTPAuthEndpoint(),
L2EngineJWTSecret: testingJWTSecret,
},
Driver: driver.Config{
VerifierConfDepth: 0,
SequencerConfDepth: 0,
SequencerEnabled: true,
},
Rollup: rollup.Config{
Genesis: rollup.Genesis{
L1: eth.BlockID{
Hash: forkBlock.Hash(),
Number: forkBlock.NumberU64(),
},
L2: eth.BlockID{
Hash: migRes.TransitionBlockHash,
Number: migRes.TransitionHeight,
},
L2Time: migRes.TransitionTimestamp,
SystemConfig: e2eutils.SystemConfigFromDeployConfig(deployCfg),
},
BlockTime: deployCfg.L2BlockTime,
MaxSequencerDrift: deployCfg.MaxSequencerDrift,
SeqWindowSize: deployCfg.SequencerWindowSize,
ChannelTimeout: deployCfg.ChannelTimeout,
L1ChainID: new(big.Int).SetUint64(deployCfg.L1ChainID),
L2ChainID: new(big.Int).SetUint64(deployCfg.L2ChainID),
BatchInboxAddress: deployCfg.BatchInboxAddress,
DepositContractAddress: portal.Address,
L1SystemConfigAddress: sysConfig.Address,
},
P2PSigner: &p2p.PreparedSigner{Signer: p2p.NewLocalSigner(secrets.SequencerP2P)},
RPC: node.RPCConfig{
ListenAddr: "127.0.0.1",
ListenPort: 0,
EnableAdmin: true,
},
L1EpochPollInterval: 4 * time.Second,
}
rollupNode, err := node.New(ctx, rollupNodeConfig, log.New(), snapLog, "", metrics.NewMetrics(""))
require.NoError(t, err)
require.NoError(t, rollupNode.Start(ctx))
t.Cleanup(func() {
require.NoError(t, rollupNode.Close())
})
batcher, err := bss.NewBatchSubmitterFromCLIConfig(bss.CLIConfig{
L1EthRpc: forkedL1URL,
L2EthRpc: gethNode.WSEndpoint(),
RollupRpc: rollupNode.HTTPEndpoint(),
MaxL1TxSize: 120_000,
TargetL1TxSize: 624,
TargetNumFrames: 1,
ApproxComprRatio: 1.0,
SubSafetyMargin: testSafetyMargin(deployCfg),
PollInterval: 50 * time.Millisecond,
NumConfirmations: 1,
ResubmissionTimeout: 5 * time.Second,
SafeAbortNonceTooLowCount: 3,
LogConfig: oplog.CLIConfig{
Level: "info",
Format: "text",
},
PrivateKey: hexPriv(secrets.Batcher),
}, lgr.New("module", "batcher"))
require.NoError(t, err)
t.Cleanup(func() {
batcher.Stop()
})
proposer, err := l2os.NewL2OutputSubmitterFromCLIConfig(l2os.CLIConfig{
L1EthRpc: forkedL1URL,
RollupRpc: rollupNode.HTTPEndpoint(),
L2OOAddress: l2OS.Address.String(),
PollInterval: 50 * time.Millisecond,
NumConfirmations: 1,
ResubmissionTimeout: 3 * time.Second,
SafeAbortNonceTooLowCount: 3,
AllowNonFinalized: true,
LogConfig: oplog.CLIConfig{
Level: "info",
Format: "text",
},
PrivateKey: hexPriv(secrets.Proposer),
}, lgr.New("module", "proposer"))
require.NoError(t, err)
t.Cleanup(func() {
proposer.Stop()
})
}
func untar(t *testing.T, src, dst string) {
cmd := exec.Command("tar", "-xzvf", src, "--strip-components=6", "-C", dst)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run(), "error untarring data")
}
func migrateL1(t *testing.T) {
cmd := exec.Command(
"yarn",
"hardhat",
"--network",
networkName,
"deploy",
"--tags",
"migration",
)
cmd.Env = os.Environ()
cmd.Env = append(
cmd.Env,
"CHAIN_ID=1",
"L1_RPC=http://127.0.0.1:8545",
"PRIVATE_KEY_DEPLOYER=ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = path.Join(cwd, "..", "packages", "contracts-bedrock")
require.NoError(t, cmd.Run(), "error migrating L1")
}
func migrateL2(t *testing.T, workdir string, deployConfig *genesis.DeployConfig, startingBlockNumber uint64) *genesis.MigrationResult {
migCfg := &migration_action.Config{
DeployConfig: deployConfig,
OVMAddressesPath: config.ovmAddrsPath,
EVMAddressesPath: config.evmAddrsPath,
OVMAllowancesPath: config.ovmAllowancesPath,
OVMMessagesPath: config.ovmMessagesPath,
EVMMessagesPath: config.evmMessagesPath,
Network: "mainnet",
HardhatDeployments: []string{
path.Join(cwd, "..", "packages", "contracts", "deployments"),
path.Join(cwd, "..", "packages", "contracts-periphery", "deployments"),
},
L1URL: config.l1URL,
StartingL1BlockNumber: startingBlockNumber,
L2DBPath: workdir,
DryRun: false,
}
res, err := migration_action.Migrate(migCfg)
require.NoError(t, err)
return res
}
func makeBlocks(ctx context.Context, rpcClient *rpc.Client, lgr log.Logger) {
blockTick := time.NewTicker(12 * time.Second)
for {
select {
case <-blockTick.C:
err := rpcClient.CallContext(ctx, nil, "evm_mine")
if err != nil {
lgr.Error("error mining new block", "err", err)
continue
}
lgr.Debug("mined block")
case <-ctx.Done():
return
}
}
}