l2_shared

package
v0.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 15 Imported by: 0

Documentation

Overview

// https://www.digitalocean.com/community/tutorials/how-to-add-extra-information-to-errors-in-go

This error DeSyncPermissionlessAndTrustedNodeError have a field L1BlockNumber that contains the block number where the discrepancy is.

object TrustedBatchesRetrieve: - It get all pending batches from trusted node to be synchronized

You must implements BatchProcessor with the code to process the batches

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBatchDataIsNotIncremental is returned when the new batch has different data than the one in node and is not possible to sync
	ErrBatchDataIsNotIncremental = errors.New("the new batch has different data than the one in node")
)
View Source
var (
	// ErrFatalBatchDesynchronized is the error when the batch is desynchronized
	ErrFatalBatchDesynchronized = fmt.Errorf("batch desynchronized")
)

Functions

func AreEqualStateBatchAndTrustedBatch

func AreEqualStateBatchAndTrustedBatch(stateBatch *state.Batch, trustedBatch *types.Batch, flags CompareBatchFlags) (bool, string)

AreEqualStateBatchAndTrustedBatch check is are equal, it response true|false and a debug string you could pass some flags to ignore some fields

func ThereAreNewBatchL2Data

func ThereAreNewBatchL2Data(previousData []byte, incommingData types.ArgBytes) (bool, error)

ThereAreNewBatchL2Data check if there are new batch data and if the previous data are compatible

Types

type BatchProcessMode

type BatchProcessMode string

BatchProcessMode is the mode for process a batch (full, incremental, reprocess, nothing)

const (
	// FullProcessMode This batch is not on database, so is the first time we process it
	FullProcessMode BatchProcessMode = "full"
	// IncrementalProcessMode We have processed this batch before, and we have the intermediate state root, so is going to be process only new Tx
	IncrementalProcessMode BatchProcessMode = "incremental"
	// ReprocessProcessMode We have processed this batch before, but we don't have the intermediate state root, so we need to reprocess it
	ReprocessProcessMode BatchProcessMode = "reprocess"
	// NothingProcessMode The batch is already synchronized, so we don't need to process it
	NothingProcessMode BatchProcessMode = "nothing"
)

type BatchProcessor

type BatchProcessor interface {
	// ProcessTrustedBatch processes a trusted batch
	ProcessTrustedBatch(ctx context.Context, trustedBatch *types.Batch, status TrustedState, dbTx pgx.Tx, debugPrefix string) (*TrustedState, error)
}

BatchProcessor is a interface with the ProcessTrustedBatch methor

this method is responsible to process a trusted batch

type CompareBatchFlags

type CompareBatchFlags int

CompareBatchFlags is a flag to ignore some fields when comparing batches

const (
	CMP_BATCH_NONE          CompareBatchFlags = 0x0 // CMP_BATCH_NONE No flag
	CMP_BATCH_IGNORE_WIP    CompareBatchFlags = 0x1 // CMP_BATCH_IGNORE_WIP Ignore WIP field
	CMP_BATCH_IGNORE_TSTAMP CompareBatchFlags = 0x2 // CMP_BATCH_IGNORE_TSTAMP Ignore Timestamp field
)

func (CompareBatchFlags) IsSet

IsSet check if a flag is set. example of usage: v.IsSet(CMP_BATCH_IGNORE_WIP)

type DeSyncPermissionlessAndTrustedNodeError

type DeSyncPermissionlessAndTrustedNodeError struct {
	L1BlockNumber uint64
	Err           error
}

DeSyncPermissionlessAndTrustedNodeError is an error type that contains the Block where is the discrepancy

func NewDeSyncPermissionlessAndTrustedNodeError

func NewDeSyncPermissionlessAndTrustedNodeError(L1BlockNumber uint64) *DeSyncPermissionlessAndTrustedNodeError

NewDeSyncPermissionlessAndTrustedNodeError returns a new instance of DeSyncPermissionlessAndTrustedNodeError

func (*DeSyncPermissionlessAndTrustedNodeError) Error

func (*DeSyncPermissionlessAndTrustedNodeError) Unwrap

type L1SyncGlobalExitRootChecker added in v0.6.1

type L1SyncGlobalExitRootChecker interface {
	CheckL1SyncGlobalExitRootEnoughToProcessBatch(ctx context.Context, batchNumber uint64, globalExitRoot common.Hash, dbTx pgx.Tx) error
}

L1SyncGlobalExitRootChecker is the interface to check if the required GlobalExitRoot is already synced from L1

type PostClosedBatchCheckL2Block added in v0.6.1

type PostClosedBatchCheckL2Block struct {
	// contains filtered or unexported fields
}

PostClosedBatchCheckL2Block is a struct that implements the PostClosedBatchChecker interface and check the las L2Block hash on close batch

func NewPostClosedBatchCheckL2Block added in v0.6.1

func NewPostClosedBatchCheckL2Block(state statePostClosedBatchCheckL2Block) *PostClosedBatchCheckL2Block

NewPostClosedBatchCheckL2Block creates a new PostClosedBatchCheckL2Block

func (*PostClosedBatchCheckL2Block) CheckPostClosedBatch added in v0.6.1

func (p *PostClosedBatchCheckL2Block) CheckPostClosedBatch(ctx context.Context, processData ProcessData, dbTx pgx.Tx) error

CheckPostClosedBatch checks the last L2Block hash on close batch

type PostClosedBatchChecker added in v0.6.1

type PostClosedBatchChecker interface {
	CheckPostClosedBatch(ctx context.Context, processData ProcessData, dbTx pgx.Tx) error
}

PostClosedBatchChecker is the interface to implement a checker post closed batch

type ProcessData

type ProcessData struct {
	BatchNumber       uint64
	Mode              BatchProcessMode
	OldStateRoot      common.Hash
	OldAccInputHash   common.Hash
	BatchMustBeClosed bool
	// TrustedBatch The batch in trusted node, it NEVER will be nil
	TrustedBatch *types.Batch
	// StateBatch Current batch in state DB, it could be nil
	StateBatch *state.Batch
	// PreviousStateBatch Previous batch in state DB (BatchNumber - 1), it could be nil
	PreviousStateBatch *state.Batch
	Now                time.Time
	Description        string
	// DebugPrefix is used to log, must prefix all logs entries
	DebugPrefix string
}

ProcessData contains the data required to process a batch

type ProcessResponse

type ProcessResponse struct {
	// ProcessBatchResponse have the NewStateRoot
	ProcessBatchResponse *state.ProcessBatchResponse
	// ClearCache force to clear cache for next execution
	ClearCache bool
	// UpdateBatch  update the batch for next execution
	UpdateBatch *state.Batch
	// UpdateBatchWithProcessBatchResponse update the batch (if not nil) with the data in ProcessBatchResponse
	UpdateBatchWithProcessBatchResponse bool
}

ProcessResponse contains the response of the process of a batch

func NewProcessResponse

func NewProcessResponse() ProcessResponse

NewProcessResponse creates a new ProcessResponse

func (*ProcessResponse) CheckSanity

func (p *ProcessResponse) CheckSanity() error

CheckSanity check the sanity of the response

func (*ProcessResponse) DiscardCache

func (p *ProcessResponse) DiscardCache()

DiscardCache set to discard cache for next execution

func (*ProcessResponse) UpdateCurrentBatch

func (p *ProcessResponse) UpdateCurrentBatch(UpdateBatch *state.Batch)

UpdateCurrentBatch update the current batch for next execution

func (*ProcessResponse) UpdateCurrentBatchWithExecutionResult

func (p *ProcessResponse) UpdateCurrentBatchWithExecutionResult(UpdateBatch *state.Batch, ProcessBatchResponse *state.ProcessBatchResponse)

UpdateCurrentBatchWithExecutionResult update the current batch for next execution with the data in ProcessBatchResponse

type ProcessorTrustedBatchSync

type ProcessorTrustedBatchSync struct {
	Steps SyncTrustedBatchExecutor

	Cfg l2_sync.Config
	// contains filtered or unexported fields
}

ProcessorTrustedBatchSync is a template to sync trusted state. It classify what kind of update is needed and call to SyncTrustedStateBatchExecutorSteps

  that is the one that execute the sync process

	the real implementation of the steps is in the SyncTrustedStateBatchExecutorSteps interface that known how to process a batch

func NewProcessorTrustedBatchSync

func NewProcessorTrustedBatchSync(steps SyncTrustedBatchExecutor,
	timeProvider syncCommon.TimeProvider, l1SyncChecker L1SyncGlobalExitRootChecker, cfg l2_sync.Config) *ProcessorTrustedBatchSync

NewProcessorTrustedBatchSync creates a new SyncTrustedStateBatchExecutorTemplate

func (*ProcessorTrustedBatchSync) AddPostChecker added in v0.6.1

func (s *ProcessorTrustedBatchSync) AddPostChecker(checker PostClosedBatchChecker)

AddPostChecker add a post closed batch checker

func (*ProcessorTrustedBatchSync) ExecuteProcessBatch

func (s *ProcessorTrustedBatchSync) ExecuteProcessBatch(ctx context.Context, processMode *ProcessData, dbTx pgx.Tx) (*ProcessResponse, error)

ExecuteProcessBatch execute the batch and process it

func (*ProcessorTrustedBatchSync) GetCurrentAndPreviousBatchFromCache

func (s *ProcessorTrustedBatchSync) GetCurrentAndPreviousBatchFromCache(status *TrustedState) (*state.Batch, *state.Batch)

GetCurrentAndPreviousBatchFromCache returns the current and previous batch from cache

func (*ProcessorTrustedBatchSync) GetModeForProcessBatch

func (s *ProcessorTrustedBatchSync) GetModeForProcessBatch(trustedNodeBatch *types.Batch, stateBatch *state.Batch, statePreviousBatch *state.Batch, debugPrefix string) (ProcessData, error)

GetModeForProcessBatch returns the mode for process a batch

func (*ProcessorTrustedBatchSync) GetNextStatus

func (s *ProcessorTrustedBatchSync) GetNextStatus(status TrustedState, processBatchResp *ProcessResponse, closedBatch bool, debugPrefix string) (*TrustedState, error)

GetNextStatus returns the next cache for use in the next run it could be nil, that means discard current cache

func (*ProcessorTrustedBatchSync) ProcessTrustedBatch

func (s *ProcessorTrustedBatchSync) ProcessTrustedBatch(ctx context.Context, trustedBatch *types.Batch, status TrustedState, dbTx pgx.Tx, debugPrefix string) (*TrustedState, error)

ProcessTrustedBatch processes a trusted batch and return the new state

type StateInterface

type StateInterface interface {
	BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
	GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
}

StateInterface contains the methods required to interact with the state.

type SyncTrustedBatchExecutor

type SyncTrustedBatchExecutor interface {
	// FullProcess process a batch that is not on database, so is the first time we process it
	FullProcess(ctx context.Context, data *ProcessData, dbTx pgx.Tx) (*ProcessResponse, error)
	// IncrementalProcess process a batch that we have processed before, and we have the intermediate state root, so is going to be process only new Tx
	IncrementalProcess(ctx context.Context, data *ProcessData, dbTx pgx.Tx) (*ProcessResponse, error)
	// ReProcess process a batch that we have processed before, but we don't have the intermediate state root, so we need to reprocess it
	ReProcess(ctx context.Context, data *ProcessData, dbTx pgx.Tx) (*ProcessResponse, error)
	// NothingProcess process a batch that is already synchronized, so we don't need to process it
	NothingProcess(ctx context.Context, data *ProcessData, dbTx pgx.Tx) (*ProcessResponse, error)
}

SyncTrustedBatchExecutor is the interface that known how to process a batch

type SyncTrustedStateExecutorSelector added in v0.5.4

type SyncTrustedStateExecutorSelector struct {
	// contains filtered or unexported fields
}

SyncTrustedStateExecutorSelector Implements SyncTrustedStateExecutor

func NewSyncTrustedStateExecutorSelector added in v0.5.4

func NewSyncTrustedStateExecutorSelector(
	supportedForks map[uint64]syncinterfaces.SyncTrustedStateExecutor,
	state stateSyncTrustedStateExecutorSelector) *SyncTrustedStateExecutorSelector

NewSyncTrustedStateExecutorSelector creates a new SyncTrustedStateExecutorSelector that implements SyncTrustedStateExecutor

func (*SyncTrustedStateExecutorSelector) CleanTrustedState added in v0.5.4

func (s *SyncTrustedStateExecutorSelector) CleanTrustedState()

CleanTrustedState clean cache of Batches and StateRoot

func (*SyncTrustedStateExecutorSelector) GetCachedBatch added in v0.5.4

func (s *SyncTrustedStateExecutorSelector) GetCachedBatch(batchNumber uint64) *state.Batch

GetCachedBatch implements syncinterfaces.SyncTrustedStateExecutor. Returns a cached batch

func (*SyncTrustedStateExecutorSelector) GetExecutor added in v0.5.4

func (s *SyncTrustedStateExecutorSelector) GetExecutor(latestSyncedBatch uint64, maximumBatchNumberToProcess uint64) (syncinterfaces.SyncTrustedStateExecutor, uint64)

GetExecutor returns the executor that should be used for the given batch, could be nil it returns the executor and the maximum batch number that the executor can process

func (*SyncTrustedStateExecutorSelector) SyncTrustedState added in v0.5.4

func (s *SyncTrustedStateExecutorSelector) SyncTrustedState(ctx context.Context, latestSyncedBatch uint64, maximumBatchNumberToProcess uint64) error

SyncTrustedState syncs the trusted state with the permissionless state. In this case choose which executor must use

type TrustedBatchesRetrieve

type TrustedBatchesRetrieve struct {
	TrustedStateMngr TrustedStateManager
	// contains filtered or unexported fields
}

TrustedBatchesRetrieve it gets pending batches from Trusted node. It calls for each batch to BatchExecutor

and for each new batch calls the ProcessTrustedBatch method of the BatchExecutor interface

func NewTrustedBatchesRetrieve

NewTrustedBatchesRetrieve creates a new SyncTrustedStateTemplate

func (*TrustedBatchesRetrieve) CleanTrustedState

func (s *TrustedBatchesRetrieve) CleanTrustedState()

CleanTrustedState Clean cache of TrustedBatches and StateRoot

func (*TrustedBatchesRetrieve) GetCachedBatch added in v0.5.4

func (s *TrustedBatchesRetrieve) GetCachedBatch(batchNumber uint64) *state.Batch

GetCachedBatch implements syncinterfaces.SyncTrustedStateExecutor. Returns a cached batch

func (*TrustedBatchesRetrieve) SyncTrustedState

func (s *TrustedBatchesRetrieve) SyncTrustedState(ctx context.Context, latestSyncedBatch uint64, maximumBatchNumberToProcess uint64) error

SyncTrustedState sync trusted state from latestSyncedBatch to lastTrustedStateBatchNumber

type TrustedState

type TrustedState struct {
	// LastTrustedBatches [0] -> Current  batch, [1] -> previous batch
	LastTrustedBatches []*state.Batch
}

TrustedState is the trusted state, basically contains the batch cache for a concrete batch

func (*TrustedState) GetCurrentBatch added in v0.5.4

func (ts *TrustedState) GetCurrentBatch() *state.Batch

GetCurrentBatch returns the current batch or nil

func (*TrustedState) GetPreviousBatch added in v0.5.4

func (ts *TrustedState) GetPreviousBatch() *state.Batch

GetPreviousBatch returns the previous batch or nil

func (*TrustedState) IsEmpty

func (ts *TrustedState) IsEmpty() bool

IsEmpty returns true if the trusted state is empty

type TrustedStateManager

type TrustedStateManager struct {
	Cache *common.Cache[uint64, *state.Batch]
}

TrustedStateManager is the trusted state manager, basically contains the batch cache and create the TrustedState

func NewTrustedStateManager

func NewTrustedStateManager(timerProvider common.TimeProvider, timeOfLiveItems time.Duration) *TrustedStateManager

NewTrustedStateManager creates a new TrustedStateManager

func (*TrustedStateManager) Clear

func (ts *TrustedStateManager) Clear()

Clear clears the cache

func (*TrustedStateManager) GetStateForWorkingBatch

func (ts *TrustedStateManager) GetStateForWorkingBatch(ctx context.Context, batchNumber uint64, stateGetBatch syncinterfaces.StateGetBatchByNumberInterface, dbTx pgx.Tx) (*TrustedState, error)

GetStateForWorkingBatch returns the trusted state for the working batch

func (*TrustedStateManager) Set

func (ts *TrustedStateManager) Set(resultBatch *state.Batch)

Set sets the result batch in the cache

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL