Skip to content

Commit

Permalink
#133 tstamp, #114 add lastExecution information
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Sep 17, 2024
1 parent 7263154 commit 89ee9a9
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 18 deletions.
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -70,6 +71,14 @@ func LoadFileFromString(configFileData string, configType string) (*Config, erro
return cfg, nil
}

func SaveConfigToString(cfg Config) (string, error) {
b, err := json.Marshal(cfg)
if err != nil {
return "", err
}
return string(b), nil
}

// Load loads the configuration
func LoadFile(configFilePath string) (*Config, error) {
_, fileName := filepath.Split(configFilePath)
Expand Down
2 changes: 1 addition & 1 deletion etherman/sequence_batches_decode_banana_validium.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
//https://github.com/0xPolygonHermez/zkevm-contracts/blob/a9b4f742f66bd4f3bcd98a3a188422480ffe0d4e/contracts/v2/consensus/validium/PolygonValidiumEtrog.sol#L91
function sequenceBatchesValidium(
ValidiumBatchData[] calldata batches,
uint32 indexL1InfoRoot,
uint32 l1InfoTreeLeafCount,
uint64 maxSequenceTimestamp,
bytes32 expectedFinalAccInputHash,
address l2Coinbase,
Expand Down
16 changes: 16 additions & 0 deletions state/entities/last_execution_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package entities

import (
"fmt"
"time"
)

type LastExecutionData struct {
SyncVersion string
LastStart time.Time
Configuration string
}

func (s *LastExecutionData) String() string {
return fmt.Sprintf("{SyncVersion: %s, LastStart: %s}", s.SyncVersion, s.LastStart.String())
}
2 changes: 2 additions & 0 deletions state/model/kv_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type StorageKVInterface = storage.KvStorer
const (
// keyStorageContentBound is the name of the KEY for sanity storage
keyStorageContentBound KVKey = "ContentBound"
// keyStorageLastExecution is the name of the KEY for last run
keyStorageLastExecution KVKey = "LastExecution"
)

// GetKVHelper is a helper function to get a value from the KV storage, if not found returns nil
Expand Down
58 changes: 58 additions & 0 deletions state/model/last_execution_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package model

import (
"context"
"time"

"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/entities"
)

type LastExecutionState struct {
storage storageKVInterface
lastExecutionCache *lastExecutionBoundData
currentExecution *lastExecutionBoundData
}

type lastExecutionBoundData = entities.LastExecutionData

func NewLastExecutionState(storage storageKVInterface,
configString string, syncVesion string) *LastExecutionState {
res := &LastExecutionState{
storage: storage,
}
last, err := res.getCurrentExecutionData(context.Background(), nil, storage)
if err != nil {
log.Error("Error getting current execution data", "error", err)
return nil
}
res.lastExecutionCache = last
res.currentExecution = &lastExecutionBoundData{
Configuration: configString,
SyncVersion: syncVesion,
}

return res
}

func (l *LastExecutionState) GetPreviousExecutionData(ctx context.Context, dbTx storageTxType, storage storageKVInterface) (*lastExecutionBoundData, error) {
return l.lastExecutionCache, nil
}

func (l *LastExecutionState) StartingSynchronization(ctx context.Context, dbTx storageTxType) error {
l.currentExecution.LastStart = time.Now()
return l.SetCurrentExecutionData(ctx, dbTx, l.storage, l.currentExecution)
}

func (l *LastExecutionState) SetCurrentExecutionData(ctx context.Context, dbTx storageTxType, storage storageKVInterface, data *lastExecutionBoundData) error {
err := l.storage.KVSetJson(ctx, keyStorageLastExecution, data, nil, dbTx)
if err != nil {
return err
}
l.lastExecutionCache = data
return nil
}

func (l *LastExecutionState) getCurrentExecutionData(ctx context.Context, dbTx storageTxType, storage storageKVInterface) (*lastExecutionBoundData, error) {
return GetKVHelper[lastExecutionBoundData](ctx, keyStorageLastExecution, storage, dbTx)
}
3 changes: 2 additions & 1 deletion synchronizer/actions/banana/processor_l1_sequence_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ func (p *ProcessorL1SequenceBatchesBanana) ProcessSequenceBatches(ctx context.Co
if sequencedBatches[0].Metadata != nil {
seqSource = seqSource + "/" + sequencedBatches[0].Metadata.RollupFlavor + "/" + sequencedBatches[0].Metadata.ForkName
}
seqTimeStamp := time.Unix(int64(sequencedBatches[0].BananaData.MaxSequenceTimestamp), 0)
seq.Sequence = *entities.NewSequencedBatches(
sequencedBatches[0].BatchNumber, sequencedBatches[len(sequencedBatches)-1].BatchNumber,
blockNumber, uint64(forkId),
l1BlockTimestamp, time.Now(),
seqTimeStamp, time.Now(),
l1inforoot, seqSource)

for _, sequencedBatch := range sequencedBatches {
Expand Down
38 changes: 24 additions & 14 deletions synchronizer/internal/synchronizer_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ type SynchronizerImpl struct {
networkID uint
synced bool

blockRangeProcessor syncinterfaces.BlockRangeProcessor
l1Sync syncinterfaces.L1Syncer
storageChecker syncinterfaces.StorageCompatibilityChecker
blockRangeProcessor syncinterfaces.BlockRangeProcessor
l1Sync syncinterfaces.L1Syncer
storageChecker syncinterfaces.StorageCompatibilityChecker
lastExecutionChecker syncinterfaces.LastExecutionChecker

reorgCallback func(nreorgData ReorgExecutionResult)
rollbackBatchesCallback func(data RollbackBatchesData)
Expand All @@ -48,6 +49,7 @@ func NewSynchronizerImpl(
state syncinterfaces.StateInterface,
ethMan syncinterfaces.EthermanFullInterface,
storageChecker syncinterfaces.StorageCompatibilityChecker,
lastExecutionChecker syncinterfaces.LastExecutionChecker,
cfg syncconfig.Config) (*SynchronizerImpl, error) {
ctx, cancel := context.WithCancel(ctx)
networkID := uint(0)
Expand Down Expand Up @@ -99,17 +101,18 @@ func NewSynchronizerImpl(
})

sync := SynchronizerImpl{
storage: storage,
state: state,
etherMan: ethMan,
ctx: ctx,
cancelCtx: cancel,
genBlockNumber: genesisBlockNumber,
cfg: cfg,
networkID: networkID,
storageChecker: storageChecker,
l1Sync: l1SequentialSync,
blockRangeProcessor: blockRangeProcessor,
storage: storage,
state: state,
etherMan: ethMan,
ctx: ctx,
cancelCtx: cancel,
genBlockNumber: genesisBlockNumber,
cfg: cfg,
networkID: networkID,
storageChecker: storageChecker,
lastExecutionChecker: lastExecutionChecker,
l1Sync: l1SequentialSync,
blockRangeProcessor: blockRangeProcessor,
}
state.AddOnReorgCallback(sync.OnReorgExecuted)
state.AddOnRollbackBatchesCallback(sync.OnRollbackBatchesExecuted)
Expand Down Expand Up @@ -234,6 +237,13 @@ func (s *SynchronizerImpl) Sync(executionFlags SyncExecutionFlags) error {
log.Infof("networkID: %d, continuing from the last block stored on DB. lastBlockSynced: %+v", s.networkID, lastBlockSynced)
}
log.Infof("NetworkID: %d, initial lastBlockSynced: %+v", s.networkID, lastBlockSynced)
err = s.lastExecutionChecker.StartingSynchronization(s.ctx, nil)
if err != nil {

err := fmt.Errorf("error checking last execution data. Error: %w", err)
log.Errorf(" %s", err)
return err
}
for {
select {
case <-s.ctx.Done():
Expand Down
13 changes: 12 additions & 1 deletion synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"errors"
"time"

zkevm_synchronizer_l1 "github.com/0xPolygonHermez/zkevm-synchronizer-l1"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/config"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/etherman"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/rpcsync"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/state"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/model"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/storage"
internal "github.com/0xPolygonHermez/zkevm-synchronizer-l1/synchronizer/internal"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -165,7 +167,8 @@ func NewSynchronizer(ctx context.Context, config config.Config) (Synchronizer, e
log.Debugf("Creating state")
state := state.NewState(storage)
storageCompatibilityChecker := internal.NewSanityStorageCheckerImpl(state, etherman, config.Synchronizer.OverrideStorageCheck)
sync, err := internal.NewSynchronizerImpl(ctx, storage, state, etherman, storageCompatibilityChecker, config.Synchronizer)
LastExecutionChecker := newLastExecutionState(storage, config)
sync, err := internal.NewSynchronizerImpl(ctx, storage, state, etherman, storageCompatibilityChecker, LastExecutionChecker, config.Synchronizer)
if err != nil {
log.Error("Error creating synchronizer", err)
return nil, err
Expand All @@ -177,3 +180,11 @@ func NewSynchronizer(ctx context.Context, config config.Config) (Synchronizer, e

return syncAdapter, nil
}

func newLastExecutionState(storage storage.KvStorer, cfg config.Config) *model.LastExecutionState {
configStr, err := config.SaveConfigToString(cfg)
if err != nil {
log.Fatal("Error saving config to string", err)
}
return model.NewLastExecutionState(storage, configStr, zkevm_synchronizer_l1.Version)
}
11 changes: 11 additions & 0 deletions synchronizer/syncinterfaces/last_execution_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package syncinterfaces

import (
"context"

"github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/entities"
)

type LastExecutionChecker interface {
StartingSynchronization(ctx context.Context, dbTx entities.Tx) error
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
Version = "v1.0.0"
Version = "v1.0.1"
)

// PrintVersion prints version info into the provided io.Writer.
Expand Down

0 comments on commit 89ee9a9

Please sign in to comment.