synchronizer

package
v1.2.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package synchronizer synchronizes the hermez network state by querying events emitted by the three smart contracts: `Hermez.sol` (referred as Rollup here), `HermezAuctionProtocol.sol` (referred as Auction here) and `WithdrawalDelayer.sol` (referred as WDelayer here).

The main entry point for synchronization is the `Sync` function, which at most will synchronize one ethereum block, and all the hermez events that happened in that block. During a `Sync` call, a reorg can be detected; in such case, uncle blocks will be discarded, and only in a future `Sync` call correct blocks will be synced.

The synchronization of the events in each smart contracts are done in the methods `rollupSync`, `auctionSync` and `wdelayerSync`, which in turn use the interface code to read each smart contract state and events found in "github.com/hermeznetwork/hermez-node/eth". After these three methods are called, an object of type `common.BlockData` is built containing all the updates and events that happened in that block, and it is inserted in the HistoryDB in a single SQL transaction.

`rollupSync` is the method that synchronizes batches sent via the `forgeBatch` transaction in `Hermez.sol`. In `rollupSync`, for every batch, the accounts state is updated in the StateDB by processing all transactions that have been forged in that batch.

The consistency of the stored data is guaranteed by the HistoryDB: All the block information is inserted in a single SQL transaction at the end of the `Sync` method, once the StateDB has been updated. And every time the Synchronizer starts, it continues from the last block in the HistoryDB. The StateDB stores updates organized by checkpoints for every batch, and each batch is only accessed if it appears in the HistoryDB.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownBlock is the error returned by the Synchronizer when a
	// block is queried by hash but the ethereum node doesn't find it due
	// to it being discarded from a reorg.
	ErrUnknownBlock = fmt.Errorf("unknown block")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	StatsUpdateBlockNumDiffThreshold uint16
	StatsUpdateFrequencyDivider      uint16
	ChainID                          uint16
}

Config is the Synchronizer configuration

type StartBlockNums

type StartBlockNums struct {
	Rollup   int64
	Auction  int64
	WDelayer int64
}

StartBlockNums sets the first block used to start tracking the smart contracts

type Stats

type Stats struct {
	Eth struct {
		UpdateBlockNumDiffThreshold uint16
		UpdateFrequencyDivider      uint16
		FirstBlockNum               int64
		LastBlock                   common.Block
		LastBatchNum                int64
	}
	Sync struct {
		Updated   time.Time
		LastBlock common.Block
		LastBatch common.Batch
		// LastL1BatchBlock is the last ethereum block in which an
		// l1Batch was forged
		LastL1BatchBlock  int64
		LastForgeL1TxsNum int64
		Auction           struct {
			CurrentSlot common.Slot
			NextSlot    common.Slot
		}
	}
}

Stats of the synchronizer

func (*Stats) Synced

func (s *Stats) Synced() bool

Synced returns true if the Synchronizer is up to date with the last ethereum block

type StatsHolder

type StatsHolder struct {
	Stats
	// contains filtered or unexported fields
}

StatsHolder stores stats and that allows reading and writing them concurrently

func NewStatsHolder

func NewStatsHolder(firstBlockNum int64, updateBlockNumDiffThreshold uint16, updateFrequencyDivider uint16) *StatsHolder

NewStatsHolder creates a new StatsHolder

func (*StatsHolder) CopyStats

func (s *StatsHolder) CopyStats() *Stats

CopyStats returns a copy of the inner Stats

func (*StatsHolder) UpdateCurrentNextSlot

func (s *StatsHolder) UpdateCurrentNextSlot(current *common.Slot, next *common.Slot)

UpdateCurrentNextSlot updates the auction stats

func (*StatsHolder) UpdateEth

func (s *StatsHolder) UpdateEth(ethClient eth.ClientInterface) error

UpdateEth updates the ethereum stats, only if the previous stats expired

func (*StatsHolder) UpdateSync

func (s *StatsHolder) UpdateSync(lastBlock *common.Block, lastBatch *common.Batch,
	lastL1BatchBlock *int64, lastForgeL1TxsNum *int64)

UpdateSync updates the synchronizer stats

type Synchronizer

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

Synchronizer implements the Synchronizer type

func NewSynchronizer

func NewSynchronizer(ethClient eth.ClientInterface, historyDB *historydb.HistoryDB,
	l2DB *l2db.L2DB, stateDB *statedb.StateDB, cfg Config) (*Synchronizer, error)

NewSynchronizer creates a new Synchronizer

func (*Synchronizer) AuctionConstants

func (s *Synchronizer) AuctionConstants() *common.AuctionConstants

AuctionConstants returns the AuctionConstants read from the smart contract

func (*Synchronizer) RollupConstants

func (s *Synchronizer) RollupConstants() *common.RollupConstants

RollupConstants returns the RollupConstants read from the smart contract

func (*Synchronizer) SCVars

func (s *Synchronizer) SCVars() *common.SCVariables

SCVars returns a copy of the Smart Contract Variables

func (*Synchronizer) StateDB

func (s *Synchronizer) StateDB() *statedb.StateDB

StateDB returns the inner StateDB

func (*Synchronizer) Stats

func (s *Synchronizer) Stats() *Stats

Stats returns a copy of the Synchronizer Stats. It is safe to call Stats() during a Sync call

func (*Synchronizer) Sync

func (s *Synchronizer) Sync(ctx context.Context,
	lastSavedBlock *common.Block) (blockData *common.BlockData, discarded *int64, err error)

Sync attempts to synchronize an ethereum block starting from lastSavedBlock. If lastSavedBlock is nil, the lastSavedBlock value is obtained from de DB. If a block is synced, it will be returned and also stored in the DB. If a reorg is detected, the number of discarded blocks will be returned and no synchronization will be made. TODO: Be smart about locking: only lock during the read/write operations

func (*Synchronizer) WDelayerConstants

func (s *Synchronizer) WDelayerConstants() *common.WDelayerConstants

WDelayerConstants returns the WDelayerConstants read from the smart contract

Jump to

Keyboard shortcuts

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