Skip to content

Commit

Permalink
feat: introduce cli flag, JSON unmarshal logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Oct 11, 2024
1 parent fe753f2 commit 02befea
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 18 deletions.
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,11 @@ var (
Usage: "The multiplier to reduce the SMT depth by when calculating virtual counters",
Value: 0.6,
}
SovereignChainInitParams = cli.StringFlag{
Name: "zkevm.sovereign-chain.init-params",
Usage: "The file name that contains the initial (injected) batch data. It is applicable only for sovereign chains.",
Value: "",
}
DebugTimers = cli.BoolFlag{
Name: "debug.timers",
Usage: "Enable debug timers",
Expand Down
2 changes: 2 additions & 0 deletions eth/ethconfig/config_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type Zk struct {
ExecutorPayloadOutput string

TxPoolRejectSmartContractDeployments bool

SovereignChainInitParams string
}

var DefaultZkConfig = &Zk{}
Expand Down
1 change: 1 addition & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,5 @@ var DefaultFlags = []cli.Flag{
&utils.DisableVirtualCounters,
&utils.DAUrl,
&utils.VirtualCountersSmtReduction,
&utils.SovereignChainInitParams,
}
1 change: 1 addition & 0 deletions turbo/cli/flags_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
DataStreamWriteTimeout: ctx.Duration(utils.DataStreamWriteTimeout.Name),
DataStreamInactivityTimeout: ctx.Duration(utils.DataStreamInactivityTimeout.Name),
VirtualCountersSmtReduction: ctx.Float64(utils.VirtualCountersSmtReduction.Name),
SovereignChainInitParams: ctx.String(utils.SovereignChainInitParams.Name),
}

utils2.EnableTimer(cfg.DebugTimers)
Expand Down
6 changes: 1 addition & 5 deletions zk/stages/stage_l1_sequencer_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,7 @@ func HandleInitialSequenceBatches(
Transaction: txData,
}

if err = db.WriteL1InjectedBatch(ib); err != nil {
return err
}

return nil
return db.WriteL1InjectedBatch(ib)
}

func UnwindL1SequencerSyncStage(u *stagedsync.UnwindState, tx kv.RwTx, cfg L1SequencerSyncCfg, ctx context.Context) error {
Expand Down
57 changes: 51 additions & 6 deletions zk/stages/stage_sequence_execute_injected_batch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package stages

import (
"encoding/json"
"fmt"
"math"
"os"

"errors"

Expand All @@ -26,6 +29,25 @@ func processInjectedInitialBatch(
batchContext *BatchContext,
batchState *BatchState,
) error {
var (
injected *zktypes.L1InjectedBatch
err error
)

if injectedBatchFileName := batchContext.cfg.zk.SovereignChainInitParams; injectedBatchFileName != "" {
// import injected batch from file
injected, err = loadInjectedBatchDataFromFile(injectedBatchFileName)
if err != nil {
return err
}
} else {
// retrieve injected batch from the database
injected, err = batchContext.sdb.hermezDb.GetL1InjectedBatch(0)
if err != nil {
return err
}
}

// set the block height for the fork we're running at to ensure contract interactions are correct
if err := utils.RecoverySetBlockConfigForks(injectedBatchBlockNumber, batchState.forkId, batchContext.cfg.chainConfig, batchContext.s.LogPrefix()); err != nil {
return err
Expand All @@ -42,11 +64,6 @@ func processInjectedInitialBatch(
getHashFn := core.GetHashFn(header, getHeader)
blockContext := core.NewEVMBlockContext(header, getHashFn, batchContext.cfg.engine, &batchContext.cfg.zk.AddressSequencer)

injected, err := batchContext.sdb.hermezDb.GetL1InjectedBatch(0)
if err != nil {
return err
}

fakeL1TreeUpdate := &zktypes.L1InfoTreeUpdate{
GER: injected.LastGlobalExitRoot,
ParentHash: injected.L1ParentHash,
Expand Down Expand Up @@ -80,7 +97,7 @@ func processInjectedInitialBatch(
return err
}

return err
return nil
}

func handleInjectedBatch(
Expand Down Expand Up @@ -114,3 +131,31 @@ func handleInjectedBatch(

return &decodedBlocks[0].Transactions[0], receipt, execResult, effectiveGas, nil
}

// loadInjectedBatchDataFromFile loads data from a file, unmarshals it, and converts it to L1InjectedBatch
func loadInjectedBatchDataFromFile(fileName string) (*zktypes.L1InjectedBatch, error) {
// Check if the file exists
fileInfo, err := os.Stat(fileName)
if os.IsNotExist(err) {
return nil, fmt.Errorf("file %s does not exist", fileName)
}

if fileInfo.IsDir() {
return nil, fmt.Errorf("%s is a directory, not a file", fileName)
}

// Open the file
file, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %v", fileName, err)
}

// Unmarshal the JSON into InjectedBatch
var injectedBatch zktypes.L1InjectedBatch
err = json.Unmarshal(file, &injectedBatch)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON from file %s: %v", fileName, err)
}

return &injectedBatch, nil
}
32 changes: 32 additions & 0 deletions zk/stages/stage_sequence_execute_injected_batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package stages

import (
"encoding/json"
"testing"

"github.com/ledgerwatch/erigon-lib/chain"
zktx "github.com/ledgerwatch/erigon/zk/tx"
zktypes "github.com/ledgerwatch/erigon/zk/types"
"github.com/stretchr/testify/require"
)

func TestUnmarshalInjectedJSONData(t *testing.T) {
injectedBatchDataJSON := []byte(`
{
"batchL2Data": "0xf9010380808401c9c38094af97e3fe01decff90f26d266668be9f49d8df0d880b8e4f811bff7000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a40d5f56745a118d0906a34e69aec8c0db1cb8fa000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005ca1ab1e0000000000000000000000000000000000000000000000000000000005ca1ab1e1bff",
"globalExitRoot": "0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5",
"timestamp": 1728653072,
"sequencer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"l1BlockNumber": 68,
"l1BlockHash": "0x37dc5f24c65fe9d013953b9b13075293c1dc8a55bc6c0d2116cc4047f90c2068",
"l1ParentHash": "0xec78665685fe91d9445f5f366b7f4473b6aa6a2b7a54cbbf52b52e6db9f1b317"
}`)

var injectedBatchData zktypes.L1InjectedBatch
err := json.Unmarshal(injectedBatchDataJSON, &injectedBatchData)
require.NoError(t, err)

decodedL2Data, err := zktx.DecodeBatchL2Blocks([]byte(injectedBatchData.Transaction), uint64(chain.ForkID12Banana))
require.NoError(t, err)
require.NotNil(t, decodedL2Data)
}
41 changes: 34 additions & 7 deletions zk/types/zk_types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package types

import (
"strings"
"time"

"github.com/ledgerwatch/erigon-lib/common"

"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/holiman/uint256"
Expand Down Expand Up @@ -78,13 +81,13 @@ func (l *L1InfoTreeUpdate) Unmarshall(input []byte) {
}

type L1InjectedBatch struct {
L1BlockNumber uint64
Timestamp uint64
L1BlockHash common.Hash
L1ParentHash common.Hash
LastGlobalExitRoot common.Hash
Sequencer common.Address
Transaction []byte
L1BlockNumber uint64 `json:"l1BlockNumber"`
Timestamp uint64 `json:"timestamp"`
L1BlockHash common.Hash `json:"l1BlockHash"`
L1ParentHash common.Hash `json:"l1ParentHash"`
LastGlobalExitRoot common.Hash `json:"globalExitRoot"`
Sequencer common.Address `json:"sequencer"`
Transaction []byte `json:"batchL2Data"`
}

func (ib *L1InjectedBatch) Marshall() []byte {
Expand Down Expand Up @@ -119,6 +122,30 @@ func (ib *L1InjectedBatch) Unmarshall(input []byte) error {
return nil
}

func (ib *L1InjectedBatch) UnmarshalJSON(data []byte) error {
type Alias L1InjectedBatch
aux := &struct {
BatchL2Data string `json:"batchL2Data"`
*Alias
}{
Alias: (*Alias)(ib),
}

// Unmarshal into the intermediate struct first
if err := json.Unmarshal(data, aux); err != nil {
return err
}

decodedData, err := hex.DecodeString(strings.TrimPrefix(aux.BatchL2Data, "0x"))
if err != nil {
return err
}

ib.Transaction = decodedData

return nil
}

type ForkInterval struct {
ForkID uint64
FromBatchNumber uint64
Expand Down

0 comments on commit 02befea

Please sign in to comment.