-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add L2 genesis generation support to
op-deployer
Adds support for generating L2 genesis files to `op-deployer. The L2 initialization config is generated by merging in overrides as specified in the intent into a default config with sane values. The outputted genesis file is stored in the stage as a GZIP-compressed, base64-encoded string.
- Loading branch information
Showing
15 changed files
with
367 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ain-ops/interopgen/deployers/l2genesis.go → op-chain-ops/deployer/opsm/l2genesis.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package deployers | ||
package opsm | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package pipeline | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"math/big" | ||
"os" | ||
|
||
"github.com/ethereum-optimism/optimism/op-chain-ops/deployer/opsm" | ||
"github.com/ethereum-optimism/optimism/op-chain-ops/deployer/state" | ||
"github.com/ethereum-optimism/optimism/op-chain-ops/foundry" | ||
"github.com/ethereum-optimism/optimism/op-chain-ops/script" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
func GenerateL2Genesis(ctx context.Context, env *Env, intent *state.Intent, st *state.State, chainID common.Hash) error { | ||
lgr := env.Logger.New("stage", "generate-l2-genesis") | ||
|
||
lgr.Info("generating L2 genesis", "id", chainID.Hex()) | ||
|
||
var artifactsFS foundry.StatDirFs | ||
var err error | ||
if intent.ContractArtifactsURL.Scheme == "file" { | ||
fs := os.DirFS(intent.ContractArtifactsURL.Path) | ||
artifactsFS = fs.(foundry.StatDirFs) | ||
} else { | ||
return fmt.Errorf("only file:// artifacts URLs are supported") | ||
} | ||
|
||
thisIntent, err := intent.Chain(chainID) | ||
if err != nil { | ||
return fmt.Errorf("failed to get chain intent: %w", err) | ||
} | ||
|
||
thisChainState, err := st.Chain(chainID) | ||
if err != nil { | ||
return fmt.Errorf("failed to get chain state: %w", err) | ||
} | ||
|
||
initCfg, err := state.CombineL2InitConfig(intent, thisIntent) | ||
if err != nil { | ||
return fmt.Errorf("failed to combine L2 init config: %w", err) | ||
} | ||
|
||
var dump *foundry.ForgeAllocs | ||
err = CallScriptBroadcast( | ||
ctx, | ||
CallScriptBroadcastOpts{ | ||
L1ChainID: big.NewInt(int64(intent.L1ChainID)), | ||
Logger: lgr, | ||
ArtifactsFS: artifactsFS, | ||
Deployer: env.Deployer, | ||
Signer: env.Signer, | ||
Client: env.L1Client, | ||
Broadcaster: DiscardBroadcaster, | ||
Handler: func(host *script.Host) error { | ||
err := opsm.L2Genesis(host, &opsm.L2GenesisInput{ | ||
L1Deployments: opsm.L1Deployments{ | ||
L1CrossDomainMessengerProxy: thisChainState.L1CrossDomainMessengerProxyAddress, | ||
L1StandardBridgeProxy: thisChainState.L1StandardBridgeProxyAddress, | ||
L1ERC721BridgeProxy: thisChainState.L1ERC721BridgeProxyAddress, | ||
}, | ||
L2Config: initCfg, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to call L2Genesis script: %w", err) | ||
} | ||
|
||
host.Wipe(env.Deployer) | ||
|
||
dump, err = host.StateDump() | ||
if err != nil { | ||
return fmt.Errorf("failed to dump state: %w", err) | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to call L2Genesis script: %w", err) | ||
} | ||
|
||
var buf bytes.Buffer | ||
gw := gzip.NewWriter(&buf) | ||
if err := json.NewEncoder(gw).Encode(dump); err != nil { | ||
return fmt.Errorf("failed to encode state dump: %w", err) | ||
} | ||
if err := gw.Close(); err != nil { | ||
return fmt.Errorf("failed to close gzip writer: %w", err) | ||
} | ||
thisChainState.Genesis = buf.Bytes() | ||
|
||
if err := env.WriteState(st); err != nil { | ||
return fmt.Errorf("failed to write state: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package state | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
) | ||
|
||
type Base64Bytes []byte | ||
|
||
func (b Base64Bytes) MarshalJSON() ([]byte, error) { | ||
if len(b) == 0 { | ||
return []byte(`null`), nil | ||
} | ||
|
||
encoded := base64.StdEncoding.EncodeToString(b) | ||
return []byte(`"` + encoded + `"`), nil | ||
} | ||
|
||
func (b *Base64Bytes) UnmarshalJSON(data []byte) error { | ||
var dataStr string | ||
if err := json.Unmarshal(data, &dataStr); err != nil { | ||
return err | ||
} | ||
decoded, err := base64.StdEncoding.DecodeString(dataStr) | ||
if err != nil { | ||
return err | ||
} | ||
*b = decoded | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package state | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBase64BytesMarshaling(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in Base64Bytes | ||
out string | ||
}{ | ||
{ | ||
name: "empty", | ||
in: Base64Bytes{}, | ||
out: "null", | ||
}, | ||
{ | ||
name: "non-empty", | ||
in: Base64Bytes{0x01, 0x02, 0x03}, | ||
out: `"AQID"`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
data, err := tt.in.MarshalJSON() | ||
require.NoError(t, err) | ||
require.Equal(t, tt.out, string(data)) | ||
|
||
var b Base64Bytes | ||
err = b.UnmarshalJSON(data) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.in, b) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.