Skip to content

Commit 91ba0a2

Browse files
authored
Move logic to functions in artifact generation (#262)
This PR moves some of the inlined logic in the artifact generation into some helper functions. I feel there is more we can do to modularise that part but this is already a good start.
1 parent d03b0e0 commit 91ba0a2

File tree

1 file changed

+65
-55
lines changed

1 file changed

+65
-55
lines changed

playground/artifacts.go

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -196,52 +196,17 @@ func (b *ArtifactsBuilder) Build() (*Artifacts, error) {
196196
gen.Config.DepositContractAddress = gethcommon.HexToAddress(config.DepositContractAddress)
197197

198198
// add pre-funded accounts
199-
prefundedBalance, _ := new(big.Int).SetString("10000000000000000000000", 16)
200-
201-
for _, privStr := range b.getPrefundedAccounts() {
202-
priv, err := getPrivKey(privStr)
203-
if err != nil {
204-
return nil, err
205-
}
206-
addr := ecrypto.PubkeyToAddress(priv.PublicKey)
207-
gen.Alloc[addr] = types.Account{
208-
Balance: prefundedBalance,
209-
Nonce: 1,
210-
}
199+
if err := appendPrefundedAccountsToAlloc(&gen.Alloc, b.getPrefundedAccounts()); err != nil {
200+
return nil, err
211201
}
212202

213203
// Apply Optimism pre-state
214204
{
215-
var state struct {
216-
L1StateDump string `json:"l1StateDump"`
217-
}
218-
if err := json.Unmarshal(opState, &state); err != nil {
219-
return nil, fmt.Errorf("failed to unmarshal opState: %w", err)
220-
}
221-
222-
decoded, err := base64.StdEncoding.DecodeString(state.L1StateDump)
223-
if err != nil {
224-
return nil, fmt.Errorf("failed to decode opState: %w", err)
225-
}
226-
227-
// Create gzip reader from the base64 decoded data
228-
gr, err := gzip.NewReader(bytes.NewReader(decoded))
205+
opAllocs, err := readOptimismL1Allocs()
229206
if err != nil {
230-
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
231-
}
232-
defer gr.Close()
233-
234-
// Read and decode the contents
235-
contents, err := io.ReadAll(gr)
236-
if err != nil {
237-
return nil, fmt.Errorf("failed to read opState: %w", err)
238-
}
239-
240-
var alloc types.GenesisAlloc
241-
if err := json.Unmarshal(contents, &alloc); err != nil {
242-
return nil, fmt.Errorf("failed to unmarshal opState: %w", err)
207+
return nil, err
243208
}
244-
maps.Copy(gen.Alloc, alloc)
209+
maps.Copy(gen.Alloc, opAllocs)
245210
}
246211

247212
block := gen.ToBlock()
@@ -306,9 +271,16 @@ func (b *ArtifactsBuilder) Build() (*Artifacts, error) {
306271
}
307272
}
308273

274+
// Update the allocs to include the same prefunded accounts as the L1 genesis.
275+
allocs := types.GenesisAlloc{}
276+
if err := appendPrefundedAccountsToAlloc(&allocs, b.getPrefundedAccounts()); err != nil {
277+
return nil, err
278+
}
279+
309280
// override l2 genesis, make the timestamp start 2 seconds after the L1 genesis
310281
input := map[string]interface{}{
311282
"timestamp": hexutil.Uint64(opTimestamp).String(),
283+
"allocs": allocs,
312284
}
313285
if forkTime != nil {
314286
// We need to enable prague on the EL to enable the engine v4 calls
@@ -318,21 +290,6 @@ func (b *ArtifactsBuilder) Build() (*Artifacts, error) {
318290
}
319291
}
320292

321-
// Update the allocs to include the same prefunded accounts as the L1 genesis.
322-
allocs := make(map[string]interface{})
323-
input["alloc"] = allocs
324-
for _, privStr := range b.getPrefundedAccounts() {
325-
priv, err := getPrivKey(privStr)
326-
if err != nil {
327-
return nil, err
328-
}
329-
addr := ecrypto.PubkeyToAddress(priv.PublicKey)
330-
allocs[addr.String()] = map[string]interface{}{
331-
"balance": "0x10000000000000000000000",
332-
"nonce": "0x1",
333-
}
334-
}
335-
336293
newOpGenesis, err := overrideJSON(opGenesis, input)
337294
if err != nil {
338295
return nil, err
@@ -769,3 +726,56 @@ func (o *output) GetEnodeAddr() *EnodeAddr {
769726

770727
return &EnodeAddr{PrivKey: privKey, Artifact: fileName}
771728
}
729+
730+
func readOptimismL1Allocs() (types.GenesisAlloc, error) {
731+
var state struct {
732+
L1StateDump string `json:"l1StateDump"`
733+
}
734+
if err := json.Unmarshal(opState, &state); err != nil {
735+
return nil, fmt.Errorf("failed to unmarshal opState: %w", err)
736+
}
737+
738+
decoded, err := base64.StdEncoding.DecodeString(state.L1StateDump)
739+
if err != nil {
740+
return nil, fmt.Errorf("failed to decode opState: %w", err)
741+
}
742+
743+
// Create gzip reader from the base64 decoded data
744+
gr, err := gzip.NewReader(bytes.NewReader(decoded))
745+
if err != nil {
746+
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
747+
}
748+
defer gr.Close()
749+
750+
// Read and decode the contents
751+
contents, err := io.ReadAll(gr)
752+
if err != nil {
753+
return nil, fmt.Errorf("failed to read opState: %w", err)
754+
}
755+
756+
var alloc types.GenesisAlloc
757+
if err := json.Unmarshal(contents, &alloc); err != nil {
758+
return nil, fmt.Errorf("failed to unmarshal opState: %w", err)
759+
}
760+
761+
return alloc, nil
762+
}
763+
764+
var (
765+
prefundedBalance, _ = new(big.Int).SetString("10000000000000000000000", 16)
766+
)
767+
768+
func appendPrefundedAccountsToAlloc(allocs *types.GenesisAlloc, privKeys []string) error {
769+
for _, privStr := range privKeys {
770+
priv, err := getPrivKey(privStr)
771+
if err != nil {
772+
return err
773+
}
774+
addr := ecrypto.PubkeyToAddress(priv.PublicKey)
775+
(*allocs)[addr] = types.Account{
776+
Balance: prefundedBalance,
777+
Nonce: 1,
778+
}
779+
}
780+
return nil
781+
}

0 commit comments

Comments
 (0)