-
Notifications
You must be signed in to change notification settings - Fork 82
/
layer_two.go
139 lines (122 loc) · 4.14 KB
/
layer_two.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
package genesis
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/kroma-network/kroma/kroma-bindings/predeploys"
"github.com/kroma-network/kroma/kroma-chain-ops/immutables"
opbindings "github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-chain-ops/deployer"
"github.com/ethereum-optimism/optimism/op-chain-ops/squash"
"github.com/ethereum-optimism/optimism/op-chain-ops/state"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-service/eth"
)
// BuildL2Genesis will build the L2 genesis block.
func BuildL2Genesis(config *DeployConfig, l1StartBlock *types.Block) (*core.Genesis, error) {
genspec, err := NewL2Genesis(config, l1StartBlock)
if err != nil {
return nil, err
}
db := state.NewMemoryStateDB(genspec)
if config.FundDevAccounts {
log.Info("Funding developer accounts in L2 genesis")
FundDevAccounts(db)
}
SetPrecompileBalances(db)
storage, err := NewL2StorageConfig(config, l1StartBlock)
if err != nil {
return nil, err
}
immutableConfig, err := NewL2ImmutableConfig(config, l1StartBlock)
if err != nil {
return nil, err
}
// Set up the proxies
err = setProxies(db, predeploys.ProxyAdminAddr, BigL2PredeployNamespace, 256)
if err != nil {
return nil, err
}
// Set up the implementations that contain immutables
deployResults, err := immutables.Deploy(immutableConfig)
if err != nil {
return nil, err
}
for name, predeploy := range predeploys.Predeploys {
if predeploy.Enabled != nil && !predeploy.Enabled(config) {
log.Warn("Skipping disabled predeploy.", "name", name, "address", predeploy.Address)
continue
}
codeAddr := predeploy.Address
switch name {
case "Permit2":
deployerAddressBytes, err := opbindings.GetDeployerAddress(name)
if err != nil {
return nil, err
}
deployerAddress := common.BytesToAddress(deployerAddressBytes)
predeploys := map[string]*common.Address{
"DeterministicDeploymentProxy": &deployerAddress,
}
backend, err := deployer.NewL2BackendWithChainIDAndPredeploys(
new(big.Int).SetUint64(config.L2ChainID),
predeploys,
)
if err != nil {
return nil, err
}
deployedBin, err := deployer.DeployWithDeterministicDeployer(backend, name)
if err != nil {
return nil, err
}
deployResults[name] = deployedBin
fallthrough
case "MultiCall3", "Create2Deployer", "Safe_v130",
"SafeL2_v130", "MultiSendCallOnly_v130", "SafeSingletonFactory",
"DeterministicDeploymentProxy", "MultiSend_v130", "SenderCreator", "EntryPoint":
db.CreateAccount(codeAddr)
default:
if !predeploy.ProxyDisabled {
codeAddr, err = AddressToCodeNamespace(predeploy.Address)
if err != nil {
return nil, fmt.Errorf("error converting to code namespace: %w", err)
}
db.CreateAccount(codeAddr)
db.SetState(predeploy.Address, ImplementationSlot, eth.AddressAsLeftPaddedHash(codeAddr))
log.Info("Set proxy", "name", name, "address", predeploy.Address, "implementation", codeAddr)
}
}
if predeploy.ProxyDisabled && db.Exist(predeploy.Address) {
db.DeleteState(predeploy.Address, AdminSlot)
}
if err := setupPredeploy(db, deployResults, storage, name, predeploy.Address, codeAddr); err != nil {
return nil, err
}
code := db.GetCode(codeAddr)
if len(code) == 0 {
return nil, fmt.Errorf("code not set for %s", name)
}
}
if err := PerformUpgradeTxs(db); err != nil {
return nil, fmt.Errorf("failed to perform upgrade txs: %w", err)
}
return db.Genesis(), nil
}
func PerformUpgradeTxs(db *state.MemoryStateDB) error {
// Only the Ecotone upgrade is performed with upgrade-txs.
if !db.Genesis().Config.IsEcotone(db.Genesis().Timestamp) {
return nil
}
sim := squash.NewSimulator(db)
ecotone, err := derive.EcotoneNetworkUpgradeTransactions()
if err != nil {
return fmt.Errorf("failed to build ecotone upgrade txs: %w", err)
}
if err := sim.AddUpgradeTxs(ecotone); err != nil {
return fmt.Errorf("failed to apply ecotone upgrade txs: %w", err)
}
return nil
}