arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/synchronizer/synchronizer.go
synchronizer/synchronizer.goRaw
   1package synchronizer
   2
   3import (
   4	"context"
   5	"database/sql"
   6	"fmt"
   7	"math/big"
   8	"sync"
   9	"time"
  10
  11	"github.com/ethereum/go-ethereum"
  12	"github.com/hermeznetwork/hermez-node/common"
  13	"github.com/hermeznetwork/hermez-node/db/historydb"
  14	"github.com/hermeznetwork/hermez-node/db/statedb"
  15	"github.com/hermeznetwork/hermez-node/eth"
  16	"github.com/hermeznetwork/hermez-node/log"
  17	"github.com/hermeznetwork/tracerr"
  18)
  19
  20var (
  21// ErrNotAbleToSync is used when there is not possible to find a valid block to sync
  22// ErrNotAbleToSync = errors.New("it has not been possible to synchronize any block")
  23)
  24
  25// // SyncronizerState describes the synchronization progress of the smart contracts
  26// type SyncronizerState struct {
  27// 	LastUpdate                time.Time // last time this information was updated
  28// 	CurrentBatchNum           BatchNum  // Last batch that was forged on the blockchain
  29// 	CurrentBlockNum           uint64    // Last block that was mined on Ethereum
  30// 	CurrentToForgeL1TxsNum    uint32
  31// 	LastSyncedBatchNum        BatchNum // last batch synchronized by the coordinator
  32// 	LastSyncedBlockNum        uint64   // last Ethereum block synchronized by the coordinator
  33// 	LastSyncedToForgeL1TxsNum uint32
  34// }
  35
  36// // SyncStatus is returned by the Status method of the Synchronizer
  37// type SyncStatus struct {
  38// 	CurrentBlock      int64
  39// 	CurrentBatch      BatchNum
  40// 	CurrentForgerAddr ethCommon.Address
  41// 	NextForgerAddr    ethCommon.Address
  42// 	Synchronized      bool
  43// }
  44
  45// Stats of the syncrhonizer
  46type Stats struct {
  47	Eth struct {
  48		RefreshPeriod time.Duration
  49		Updated       time.Time
  50		FirstBlockNum int64
  51		LastBlock     common.Block
  52		LastBatch     int64
  53	}
  54	Sync struct {
  55		Updated   time.Time
  56		LastBlock common.Block
  57		LastBatch int64
  58		// LastL1BatchBlock is the last ethereum block in which an
  59		// l1Batch was forged
  60		LastL1BatchBlock  int64
  61		LastForgeL1TxsNum int64
  62		Auction           struct {
  63			CurrentSlot common.Slot
  64		}
  65	}
  66}
  67
  68// Synced returns true if the Synchronizer is up to date with the last ethereum block
  69func (s *Stats) Synced() bool {
  70	return s.Eth.LastBlock == s.Sync.LastBlock
  71}
  72
  73// TODO(Edu): Consider removing all the mutexes from StatsHolder, make
  74// Synchronizer.Stats not thread-safe, don't pass the synchronizer to the
  75// debugAPI, and have a copy of the Stats in the DebugAPI that the node passes
  76// when the Sync updates.
  77
  78// StatsHolder stores stats and that allows reading and writing them
  79// concurrently
  80type StatsHolder struct {
  81	Stats
  82	rw sync.RWMutex
  83}
  84
  85// NewStatsHolder creates a new StatsHolder
  86func NewStatsHolder(firstBlockNum int64, refreshPeriod time.Duration) *StatsHolder {
  87	stats := Stats{}
  88	stats.Eth.RefreshPeriod = refreshPeriod
  89	stats.Eth.FirstBlockNum = firstBlockNum
  90	return &StatsHolder{Stats: stats}
  91}
  92
  93// UpdateCurrentSlot updates the auction stats
  94func (s *StatsHolder) UpdateCurrentSlot(slot common.Slot) {
  95	s.rw.Lock()
  96	s.Sync.Auction.CurrentSlot = slot
  97	s.rw.Unlock()
  98}
  99
 100// UpdateSync updates the synchronizer stats
 101func (s *StatsHolder) UpdateSync(lastBlock *common.Block, lastBatch *common.BatchNum,
 102	lastL1BatchBlock *int64, lastForgeL1TxsNum *int64) {
 103	now := time.Now()
 104	s.rw.Lock()
 105	s.Sync.LastBlock = *lastBlock
 106	if lastBatch != nil {
 107		s.Sync.LastBatch = int64(*lastBatch)
 108	}
 109	if lastL1BatchBlock != nil {
 110		s.Sync.LastL1BatchBlock = *lastL1BatchBlock
 111		s.Sync.LastForgeL1TxsNum = *lastForgeL1TxsNum
 112	}
 113	s.Sync.Updated = now
 114	s.rw.Unlock()
 115}
 116
 117// UpdateEth updates the ethereum stats, only if the previous stats expired
 118func (s *StatsHolder) UpdateEth(ethClient eth.ClientInterface) error {
 119	now := time.Now()
 120	s.rw.RLock()
 121	elapsed := now.Sub(s.Eth.Updated)
 122	s.rw.RUnlock()
 123	if elapsed < s.Eth.RefreshPeriod {
 124		return nil
 125	}
 126
 127	lastBlock, err := ethClient.EthBlockByNumber(context.TODO(), -1)
 128	if err != nil {
 129		return tracerr.Wrap(err)
 130	}
 131	lastBatch, err := ethClient.RollupLastForgedBatch()
 132	if err != nil {
 133		return tracerr.Wrap(err)
 134	}
 135	s.rw.Lock()
 136	s.Eth.Updated = now
 137	s.Eth.LastBlock = *lastBlock
 138	s.Eth.LastBatch = lastBatch
 139	s.rw.Unlock()
 140	return nil
 141}
 142
 143// CopyStats returns a copy of the inner Stats
 144func (s *StatsHolder) CopyStats() *Stats {
 145	s.rw.RLock()
 146	sCopy := s.Stats
 147	if s.Sync.Auction.CurrentSlot.BidValue != nil {
 148		sCopy.Sync.Auction.CurrentSlot.BidValue =
 149			common.CopyBigInt(s.Sync.Auction.CurrentSlot.BidValue)
 150	}
 151	if s.Sync.Auction.CurrentSlot.DefaultSlotBid != nil {
 152		sCopy.Sync.Auction.CurrentSlot.DefaultSlotBid =
 153			common.CopyBigInt(s.Sync.Auction.CurrentSlot.DefaultSlotBid)
 154	}
 155	s.rw.RUnlock()
 156	return &sCopy
 157}
 158
 159func (s *StatsHolder) blocksPerc() float64 {
 160	syncLastBlockNum := s.Sync.LastBlock.Num
 161	if s.Sync.LastBlock.Num == 0 {
 162		syncLastBlockNum = s.Eth.FirstBlockNum - 1
 163	}
 164	return float64(syncLastBlockNum-(s.Eth.FirstBlockNum-1)) * 100.0 /
 165		float64(s.Eth.LastBlock.Num-(s.Eth.FirstBlockNum-1))
 166}
 167
 168func (s *StatsHolder) batchesPerc(batchNum int64) float64 {
 169	return float64(batchNum) * 100.0 /
 170		float64(s.Eth.LastBatch)
 171}
 172
 173// ConfigStartBlockNum sets the first block used to start tracking the smart
 174// contracts
 175type ConfigStartBlockNum struct {
 176	Rollup   int64 `validate:"required"`
 177	Auction  int64 `validate:"required"`
 178	WDelayer int64 `validate:"required"`
 179}
 180
 181// SCVariables joins all the smart contract variables in a single struct
 182type SCVariables struct {
 183	Rollup   common.RollupVariables   `validate:"required"`
 184	Auction  common.AuctionVariables  `validate:"required"`
 185	WDelayer common.WDelayerVariables `validate:"required"`
 186}
 187
 188// SCConsts joins all the smart contract constants in a single struct
 189type SCConsts struct {
 190	Rollup   common.RollupConstants
 191	Auction  common.AuctionConstants
 192	WDelayer common.WDelayerConstants
 193}
 194
 195// Config is the Synchronizer configuration
 196type Config struct {
 197	StartBlockNum      ConfigStartBlockNum
 198	InitialVariables   SCVariables
 199	StatsRefreshPeriod time.Duration
 200}
 201
 202// Synchronizer implements the Synchronizer type
 203type Synchronizer struct {
 204	ethClient eth.ClientInterface
 205	// auctionConstants  common.AuctionConstants
 206	// rollupConstants   common.RollupConstants
 207	// wDelayerConstants common.WDelayerConstants
 208	consts        SCConsts
 209	historyDB     *historydb.HistoryDB
 210	stateDB       *statedb.StateDB
 211	cfg           Config
 212	startBlockNum int64
 213	vars          SCVariables
 214	stats         *StatsHolder
 215	// firstSavedBlock  *common.Block
 216	// mux sync.Mutex
 217}
 218
 219// NewSynchronizer creates a new Synchronizer
 220func NewSynchronizer(ethClient eth.ClientInterface, historyDB *historydb.HistoryDB,
 221	stateDB *statedb.StateDB, cfg Config) (*Synchronizer, error) {
 222	auctionConstants, err := ethClient.AuctionConstants()
 223	if err != nil {
 224		log.Errorw("NewSynchronizer ethClient.AuctionConstants()", "err", err)
 225		return nil, tracerr.Wrap(err)
 226	}
 227	rollupConstants, err := ethClient.RollupConstants()
 228	if err != nil {
 229		log.Errorw("NewSynchronizer ethClient.RollupConstants()", "err", err)
 230		return nil, tracerr.Wrap(err)
 231	}
 232	wDelayerConstants, err := ethClient.WDelayerConstants()
 233	if err != nil {
 234		log.Errorw("NewSynchronizer ethClient.WDelayerConstants()", "err", err)
 235		return nil, tracerr.Wrap(err)
 236	}
 237
 238	// Set startBlockNum to the minimum between Auction, Rollup and
 239	// WDelayer StartBlockNum
 240	startBlockNum := cfg.StartBlockNum.Auction
 241	if startBlockNum < cfg.StartBlockNum.Rollup {
 242		startBlockNum = cfg.StartBlockNum.Rollup
 243	}
 244	if startBlockNum < cfg.StartBlockNum.WDelayer {
 245		startBlockNum = cfg.StartBlockNum.WDelayer
 246	}
 247	stats := NewStatsHolder(startBlockNum, cfg.StatsRefreshPeriod)
 248	s := &Synchronizer{
 249		ethClient: ethClient,
 250		consts: SCConsts{
 251			Rollup:   *rollupConstants,
 252			Auction:  *auctionConstants,
 253			WDelayer: *wDelayerConstants,
 254		},
 255		historyDB:     historyDB,
 256		stateDB:       stateDB,
 257		cfg:           cfg,
 258		startBlockNum: startBlockNum,
 259		stats:         stats,
 260	}
 261	return s, s.init()
 262}
 263
 264// Stats returns a copy of the Synchronizer Stats.  It is safe to call Stats()
 265// during a Sync call
 266func (s *Synchronizer) Stats() *Stats {
 267	return s.stats.CopyStats()
 268}
 269
 270// AuctionConstants returns the AuctionConstants read from the smart contract
 271func (s *Synchronizer) AuctionConstants() *common.AuctionConstants {
 272	return &s.consts.Auction
 273}
 274
 275// RollupConstants returns the RollupConstants read from the smart contract
 276func (s *Synchronizer) RollupConstants() *common.RollupConstants {
 277	return &s.consts.Rollup
 278}
 279
 280// WDelayerConstants returns the WDelayerConstants read from the smart contract
 281func (s *Synchronizer) WDelayerConstants() *common.WDelayerConstants {
 282	return &s.consts.WDelayer
 283}
 284
 285// SCVars returns a copy of the Smart Contract Variables
 286func (s *Synchronizer) SCVars() (*common.RollupVariables, *common.AuctionVariables, *common.WDelayerVariables) {
 287	return s.vars.Rollup.Copy(), s.vars.Auction.Copy(), s.vars.WDelayer.Copy()
 288}
 289
 290func (s *Synchronizer) updateCurrentSlotIfSync(batchesLen int) error {
 291	slot := common.Slot{
 292		SlotNum:    s.stats.Sync.Auction.CurrentSlot.SlotNum,
 293		BatchesLen: int(s.stats.Sync.Auction.CurrentSlot.BatchesLen),
 294	}
 295	// We want the next block because the current one is already mined
 296	blockNum := s.stats.Sync.LastBlock.Num + 1
 297	slotNum := s.consts.Auction.SlotNum(blockNum)
 298	if batchesLen == -1 {
 299		dbBatchesLen, err := s.historyDB.GetBatchesLen(slotNum)
 300		// fmt.Printf("DBG -1 from: %v, to: %v, len: %v\n", from, to, dbBatchesLen)
 301		if err != nil {
 302			log.Errorw("historyDB.GetBatchesLen", "err", err)
 303			return tracerr.Wrap(err)
 304		}
 305		slot.BatchesLen = dbBatchesLen
 306	} else if slotNum > slot.SlotNum {
 307		// fmt.Printf("DBG batchesLen Reset len: %v (%v %v)\n", batchesLen, slotNum, slot.SlotNum)
 308		slot.BatchesLen = batchesLen
 309	} else {
 310		// fmt.Printf("DBG batchesLen add len: %v: %v\n", batchesLen, slot.BatchesLen+batchesLen)
 311		slot.BatchesLen += batchesLen
 312	}
 313	slot.SlotNum = slotNum
 314	slot.StartBlock, slot.EndBlock = s.consts.Auction.SlotBlocks(slot.SlotNum)
 315	// If Synced, update the current coordinator
 316	if s.stats.Synced() {
 317		bidCoord, err := s.historyDB.GetBestBidCoordinator(slot.SlotNum)
 318		if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
 319			return tracerr.Wrap(err)
 320		}
 321		if tracerr.Unwrap(err) == sql.ErrNoRows {
 322			slot.BootCoord = true
 323			slot.Forger = s.vars.Auction.BootCoordinator
 324			slot.URL = "???"
 325		} else if err == nil {
 326			slot.BidValue = bidCoord.BidValue
 327			slot.DefaultSlotBid = bidCoord.DefaultSlotSetBid[slot.SlotNum%6]
 328			// Only if the highest bid value is higher than the
 329			// default slot bid, the bidder is the winner of the
 330			// slot.  Otherwise the boot coordinator is the winner.
 331			if slot.BidValue.Cmp(slot.DefaultSlotBid) >= 0 {
 332				slot.Bidder = bidCoord.Bidder
 333				slot.Forger = bidCoord.Forger
 334				slot.URL = bidCoord.URL
 335			} else {
 336				slot.BootCoord = true
 337				slot.Forger = s.vars.Auction.BootCoordinator
 338				slot.URL = "???"
 339			}
 340		}
 341
 342		// TODO: Remove this SANITY CHECK once this code is tested enough
 343		// BEGIN SANITY CHECK
 344		canForge, err := s.ethClient.AuctionCanForge(slot.Forger, blockNum)
 345		if err != nil {
 346			return tracerr.Wrap(err)
 347		}
 348		if !canForge {
 349			return tracerr.Wrap(fmt.Errorf("Synchronized value of forger address for closed slot "+
 350				"differs from smart contract: %+v", slot))
 351		}
 352		// END SANITY CHECK
 353	}
 354	s.stats.UpdateCurrentSlot(slot)
 355	return nil
 356}
 357
 358func (s *Synchronizer) init() error {
 359	// Update stats parameters so that they have valid values before the
 360	// first Sync call
 361	if err := s.stats.UpdateEth(s.ethClient); err != nil {
 362		return tracerr.Wrap(err)
 363	}
 364	lastBlock := &common.Block{}
 365	lastSavedBlock, err := s.historyDB.GetLastBlock()
 366	if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
 367		return tracerr.Wrap(err)
 368	}
 369	// If there's no block in the DB (or we only have the default block 0),
 370	// make sure that the stateDB is clean
 371	if tracerr.Unwrap(err) == sql.ErrNoRows || lastSavedBlock.Num == 0 {
 372		if err := s.stateDB.Reset(0); err != nil {
 373			return tracerr.Wrap(err)
 374		}
 375	} else {
 376		lastBlock = lastSavedBlock
 377	}
 378	if err := s.resetState(lastBlock); err != nil {
 379		return tracerr.Wrap(err)
 380	}
 381
 382	log.Infow("Sync init block",
 383		"syncLastBlock", s.stats.Sync.LastBlock,
 384		"syncBlocksPerc", s.stats.blocksPerc(),
 385		"ethFirstBlockNum", s.stats.Eth.FirstBlockNum,
 386		"ethLastBlock", s.stats.Eth.LastBlock,
 387	)
 388	log.Infow("Sync init batch",
 389		"syncLastBatch", s.stats.Sync.LastBatch,
 390		"syncBatchesPerc", s.stats.batchesPerc(s.stats.Sync.LastBatch),
 391		"ethLastBatch", s.stats.Eth.LastBatch,
 392	)
 393	return nil
 394}
 395
 396// Sync2 attems to synchronize an ethereum block starting from lastSavedBlock.
 397// If lastSavedBlock is nil, the lastSavedBlock value is obtained from de DB.
 398// If a block is synched, it will be returned and also stored in the DB.  If a
 399// reorg is detected, the number of discarded blocks will be returned and no
 400// synchronization will be made.
 401// TODO: Be smart about locking: only lock during the read/write operations
 402func (s *Synchronizer) Sync2(ctx context.Context, lastSavedBlock *common.Block) (*common.BlockData, *int64, error) {
 403	var nextBlockNum int64 // next block number to sync
 404	if lastSavedBlock == nil {
 405		var err error
 406		// Get lastSavedBlock from History DB
 407		lastSavedBlock, err = s.historyDB.GetLastBlock()
 408		if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
 409			return nil, nil, tracerr.Wrap(err)
 410		}
 411		// If we don't have any stored block, we must do a full sync
 412		// starting from the startBlockNum
 413		if tracerr.Unwrap(err) == sql.ErrNoRows || lastSavedBlock.Num == 0 {
 414			nextBlockNum = s.startBlockNum
 415			lastSavedBlock = nil
 416		}
 417	}
 418	if lastSavedBlock != nil {
 419		nextBlockNum = lastSavedBlock.Num + 1
 420	}
 421
 422	ethBlock, err := s.ethClient.EthBlockByNumber(ctx, nextBlockNum)
 423	if tracerr.Unwrap(err) == ethereum.NotFound {
 424		return nil, nil, nil
 425	} else if err != nil {
 426		return nil, nil, tracerr.Wrap(err)
 427	}
 428	log.Debugf("ethBlock: num: %v, parent: %v, hash: %v",
 429		ethBlock.Num, ethBlock.ParentHash.String(), ethBlock.Hash.String())
 430
 431	if err := s.stats.UpdateEth(s.ethClient); err != nil {
 432		return nil, nil, tracerr.Wrap(err)
 433	}
 434
 435	log.Debugw("Syncing...",
 436		"block", nextBlockNum,
 437		"ethLastBlock", s.stats.Eth.LastBlock,
 438	)
 439
 440	// Check that the obtianed ethBlock.ParentHash == prevEthBlock.Hash; if not, reorg!
 441	if lastSavedBlock != nil {
 442		if lastSavedBlock.Hash != ethBlock.ParentHash {
 443			// Reorg detected
 444			log.Debugw("Reorg Detected",
 445				"blockNum", ethBlock.Num,
 446				"block.parent(got)", ethBlock.ParentHash, "parent.hash(exp)", lastSavedBlock.Hash)
 447			lastDBBlockNum, err := s.reorg(lastSavedBlock)
 448			if err != nil {
 449				return nil, nil, tracerr.Wrap(err)
 450			}
 451			discarded := lastSavedBlock.Num - lastDBBlockNum
 452			return nil, &discarded, nil
 453		}
 454	}
 455
 456	// Get data from the rollup contract
 457	rollupData, err := s.rollupSync(ethBlock)
 458	if err != nil {
 459		return nil, nil, tracerr.Wrap(err)
 460	}
 461
 462	// Get data from the auction contract
 463	auctionData, err := s.auctionSync(ethBlock)
 464	if err != nil {
 465		return nil, nil, tracerr.Wrap(err)
 466	}
 467
 468	// Get data from the WithdrawalDelayer contract
 469	wDelayerData, err := s.wdelayerSync(ethBlock)
 470	if err != nil {
 471		return nil, nil, tracerr.Wrap(err)
 472	}
 473
 474	for i := range rollupData.Withdrawals {
 475		withdrawal := &rollupData.Withdrawals[i]
 476		if !withdrawal.InstantWithdraw {
 477			wDelayerTransfers := wDelayerData.DepositsByTxHash[withdrawal.TxHash]
 478			if len(wDelayerTransfers) == 0 {
 479				return nil, nil, tracerr.Wrap(fmt.Errorf("WDelayer deposit corresponding to " +
 480					"non-instant rollup withdrawal not found"))
 481			}
 482			// Pop the first wDelayerTransfer to consume them in chronological order
 483			wDelayerTransfer := wDelayerTransfers[0]
 484			wDelayerData.DepositsByTxHash[withdrawal.TxHash] =
 485				wDelayerData.DepositsByTxHash[withdrawal.TxHash][1:]
 486
 487			withdrawal.Owner = wDelayerTransfer.Owner
 488			withdrawal.Token = wDelayerTransfer.Token
 489		}
 490	}
 491
 492	// Group all the block data into the structs to save into HistoryDB
 493	blockData := common.BlockData{
 494		Block:    *ethBlock,
 495		Rollup:   *rollupData,
 496		Auction:  *auctionData,
 497		WDelayer: *wDelayerData,
 498	}
 499
 500	// log.Debugw("Sync()", "block", blockData)
 501	// err = s.historyDB.AddBlock(blockData.Block)
 502	// if err != nil {
 503	// 	return err
 504	// }
 505	err = s.historyDB.AddBlockSCData(&blockData)
 506	if err != nil {
 507		return nil, nil, tracerr.Wrap(err)
 508	}
 509
 510	batchesLen := len(rollupData.Batches)
 511	if batchesLen == 0 {
 512		s.stats.UpdateSync(ethBlock, nil, nil, nil)
 513	} else {
 514		var lastL1BatchBlock *int64
 515		var lastForgeL1TxsNum *int64
 516		for _, batchData := range rollupData.Batches {
 517			if batchData.L1Batch {
 518				lastL1BatchBlock = &batchData.Batch.EthBlockNum
 519				lastForgeL1TxsNum = batchData.Batch.ForgeL1TxsNum
 520			}
 521		}
 522		s.stats.UpdateSync(ethBlock,
 523			&rollupData.Batches[batchesLen-1].Batch.BatchNum, lastL1BatchBlock, lastForgeL1TxsNum)
 524	}
 525	if err := s.updateCurrentSlotIfSync(len(rollupData.Batches)); err != nil {
 526		return nil, nil, tracerr.Wrap(err)
 527	}
 528
 529	log.Debugw("Synced block",
 530		"syncLastBlockNum", s.stats.Sync.LastBlock.Num,
 531		"syncBlocksPerc", s.stats.blocksPerc(),
 532		"ethLastBlockNum", s.stats.Eth.LastBlock.Num,
 533	)
 534	for _, batchData := range rollupData.Batches {
 535		log.Debugw("Synced batch",
 536			"syncLastBatch", batchData.Batch.BatchNum,
 537			"syncBatchesPerc", s.stats.batchesPerc(int64(batchData.Batch.BatchNum)),
 538			"ethLastBatch", s.stats.Eth.LastBatch,
 539		)
 540	}
 541
 542	return &blockData, nil, nil
 543}
 544
 545// reorg manages a reorg, updating History and State DB as needed.  Keeps
 546// checking previous blocks from the HistoryDB against the blockchain until a
 547// block hash match is found.  All future blocks in the HistoryDB and
 548// corresponding batches in StateBD are discarded.  Returns the last valid
 549// blockNum from the HistoryDB.
 550func (s *Synchronizer) reorg(uncleBlock *common.Block) (int64, error) {
 551	blockNum := uncleBlock.Num
 552
 553	var block *common.Block
 554	for blockNum >= s.startBlockNum {
 555		ethBlock, err := s.ethClient.EthBlockByNumber(context.Background(), blockNum)
 556		if err != nil {
 557			log.Errorw("ethClient.EthBlockByNumber", "err", err)
 558			return 0, tracerr.Wrap(err)
 559		}
 560
 561		block, err = s.historyDB.GetBlock(blockNum)
 562		if err != nil {
 563			log.Errorw("historyDB.GetBlock", "err", err)
 564			return 0, tracerr.Wrap(err)
 565		}
 566		if block.Hash == ethBlock.Hash {
 567			log.Debugf("Found valid block: %v", blockNum)
 568			break
 569		}
 570		blockNum--
 571	}
 572	total := uncleBlock.Num - block.Num
 573	log.Debugw("Discarding blocks", "total", total, "from", uncleBlock.Num, "to", block.Num+1)
 574
 575	// Set History DB and State DB to the correct state
 576	err := s.historyDB.Reorg(block.Num)
 577	if err != nil {
 578		return 0, tracerr.Wrap(err)
 579	}
 580
 581	if err := s.resetState(block); err != nil {
 582		return 0, tracerr.Wrap(err)
 583	}
 584
 585	return block.Num, nil
 586}
 587
 588func (s *Synchronizer) resetState(block *common.Block) error {
 589	rollup, auction, wDelayer, err := s.historyDB.GetSCVars()
 590	// If SCVars are not in the HistoryDB, this is probably the first run
 591	// of the Synchronizer: store the initial vars taken from config
 592	if tracerr.Unwrap(err) == sql.ErrNoRows {
 593		rollup = &s.cfg.InitialVariables.Rollup
 594		auction = &s.cfg.InitialVariables.Auction
 595		wDelayer = &s.cfg.InitialVariables.WDelayer
 596		log.Info("Setting initial SCVars in HistoryDB")
 597		if err = s.historyDB.SetInitialSCVars(rollup, auction, wDelayer); err != nil {
 598			log.Errorw("historyDB.SetInitialSCVars", "err", err)
 599			return tracerr.Wrap(err)
 600		}
 601	}
 602	s.vars.Rollup = *rollup
 603	s.vars.Auction = *auction
 604	s.vars.WDelayer = *wDelayer
 605
 606	batchNum, err := s.historyDB.GetLastBatchNum()
 607	if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
 608		log.Errorw("historyDB.GetLastBatchNum", "err", err)
 609		return tracerr.Wrap(err)
 610	}
 611	if tracerr.Unwrap(err) == sql.ErrNoRows {
 612		batchNum = 0
 613	}
 614
 615	lastL1BatchBlockNum, err := s.historyDB.GetLastL1BatchBlockNum()
 616	if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
 617		log.Errorw("historyDB.GetLastL1BatchBlockNum", "err", err)
 618		return tracerr.Wrap(err)
 619	}
 620	if tracerr.Unwrap(err) == sql.ErrNoRows {
 621		lastL1BatchBlockNum = 0
 622	}
 623
 624	lastForgeL1TxsNum, err := s.historyDB.GetLastL1TxsNum()
 625	if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
 626		log.Errorw("historyDB.GetLastL1BatchBlockNum", "err", err)
 627		return tracerr.Wrap(err)
 628	}
 629	if tracerr.Unwrap(err) == sql.ErrNoRows || lastForgeL1TxsNum == nil {
 630		n := int64(-1)
 631		lastForgeL1TxsNum = &n
 632	}
 633
 634	err = s.stateDB.Reset(batchNum)
 635	if err != nil {
 636		log.Errorw("stateDB.Reset", "err", err)
 637		return tracerr.Wrap(err)
 638	}
 639
 640	s.stats.UpdateSync(block, &batchNum, &lastL1BatchBlockNum, lastForgeL1TxsNum)
 641
 642	if err := s.updateCurrentSlotIfSync(-1); err != nil {
 643		return tracerr.Wrap(err)
 644	}
 645	return nil
 646}
 647
 648// TODO: Figure out who will use the Status output, and only return what's strictly need
 649/*
 650// Status returns current status values from the Synchronizer
 651func (s *Synchronizer) Status() (*common.SyncStatus, error) {
 652	// Avoid possible inconsistencies
 653	s.mux.Lock()
 654	defer s.mux.Unlock()
 655
 656	var status *common.SyncStatus
 657
 658	// TODO: Join all queries to the DB into a single transaction so that
 659	// we can remove the mutex locking here:
 660	// - HistoryDB.GetLastBlock
 661	// - HistoryDB.GetLastBatchNum
 662	// - HistoryDB.GetCurrentForgerAddr
 663	// - HistoryDB.GetNextForgerAddr
 664
 665	// Get latest block in History DB
 666	lastSavedBlock, err := s.historyDB.GetLastBlock()
 667	if err != nil {
 668		return nil, err
 669	}
 670	status.CurrentBlock = lastSavedBlock.EthBlockNum
 671
 672	// Get latest batch in History DB
 673	lastSavedBatch, err := s.historyDB.GetLastBatchNum()
 674	if err != nil && err != sql.ErrNoRows {
 675		return nil, err
 676	}
 677	status.CurrentBatch = lastSavedBatch
 678
 679	// Get latest blockNum in blockchain
 680	latestBlockNum, err := s.ethClient.EthLastBlock()
 681	if err != nil {
 682		return nil, err
 683	}
 684
 685	// TODO: Get CurrentForgerAddr & NextForgerAddr from the Auction SC / Or from the HistoryDB
 686
 687	// Check if Synchronizer is synchronized
 688	status.Synchronized = status.CurrentBlock == latestBlockNum
 689	return status, nil
 690}
 691*/
 692
 693// rollupSync retreives all the Rollup Smart Contract Data that happened at
 694// ethBlock.blockNum with ethBlock.Hash.
 695func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*common.RollupData, error) {
 696	blockNum := ethBlock.Num
 697	var rollupData = common.NewRollupData()
 698	// var forgeL1TxsNum int64
 699
 700	// Get rollup events in the block, and make sure the block hash matches
 701	// the expected one.
 702	rollupEvents, blockHash, err := s.ethClient.RollupEventsByBlock(blockNum)
 703	if err != nil {
 704		return nil, tracerr.Wrap(err)
 705	}
 706	// No events in this block
 707	if blockHash == nil {
 708		return &rollupData, nil
 709	}
 710	if *blockHash != ethBlock.Hash {
 711		log.Errorw("Block hash mismatch in Rollup events", "expected", ethBlock.Hash.String(),
 712			"got", blockHash.String())
 713		return nil, tracerr.Wrap(eth.ErrBlockHashMismatchEvent)
 714	}
 715
 716	var nextForgeL1TxsNum int64 // forgeL1TxsNum for the next L1Batch
 717	nextForgeL1TxsNumPtr, err := s.historyDB.GetLastL1TxsNum()
 718	if err != nil {
 719		return nil, tracerr.Wrap(err)
 720	}
 721	if nextForgeL1TxsNumPtr != nil {
 722		nextForgeL1TxsNum = *nextForgeL1TxsNumPtr + 1
 723	} else {
 724		nextForgeL1TxsNum = 0
 725	}
 726
 727	// Get L1UserTX
 728	rollupData.L1UserTxs, err = getL1UserTx(rollupEvents.L1UserTx, blockNum)
 729	if err != nil {
 730		return nil, tracerr.Wrap(err)
 731	}
 732
 733	// Get ForgeBatch events to get the L1CoordinatorTxs
 734	for _, evtForgeBatch := range rollupEvents.ForgeBatch {
 735		batchData := common.NewBatchData()
 736		position := 0
 737
 738		// Get the input for each Tx
 739		forgeBatchArgs, sender, err := s.ethClient.RollupForgeBatchArgs(evtForgeBatch.EthTxHash,
 740			evtForgeBatch.L1UserTxsLen)
 741		if err != nil {
 742			return nil, tracerr.Wrap(err)
 743		}
 744
 745		batchNum := common.BatchNum(evtForgeBatch.BatchNum)
 746		var l1UserTxs []common.L1Tx
 747		// Check if this is a L1Batch to get L1 Tx from it
 748		if forgeBatchArgs.L1Batch {
 749			// Get L1UserTxs with toForgeL1TxsNum, which correspond
 750			// to the L1UserTxs that are forged in this batch, so
 751			// that stateDB can process them.
 752
 753			// First try to find them in HistoryDB.
 754			l1UserTxs, err = s.historyDB.GetUnforgedL1UserTxs(nextForgeL1TxsNum)
 755			if err != nil {
 756				return nil, tracerr.Wrap(err)
 757			}
 758			// Apart from the DB, try to find them in this block.
 759			// This could happen because in a block there could be
 760			// multiple batches with L1Batch = true (although it's
 761			// a very rare case).  If not found in the DB and the
 762			// block doesn't contain the l1UserTxs, it means that
 763			// the L1UserTxs queue with toForgeL1TxsNum was closed
 764			// empty, so we leave `l1UserTxs` as an empty slice.
 765			for _, l1UserTx := range rollupData.L1UserTxs {
 766				if *l1UserTx.ToForgeL1TxsNum == nextForgeL1TxsNum {
 767					l1UserTxs = append(l1UserTxs, l1UserTx)
 768				}
 769			}
 770
 771			position = len(l1UserTxs)
 772		}
 773		// Get L1 Coordinator Txs
 774		for i := range forgeBatchArgs.L1CoordinatorTxs {
 775			l1CoordinatorTx := forgeBatchArgs.L1CoordinatorTxs[i]
 776			l1CoordinatorTx.Position = position
 777			// l1CoordinatorTx.ToForgeL1TxsNum = &forgeL1TxsNum
 778			l1CoordinatorTx.UserOrigin = false
 779			l1CoordinatorTx.EthBlockNum = blockNum
 780			l1CoordinatorTx.BatchNum = &batchNum
 781			l1Tx, err := common.NewL1Tx(&l1CoordinatorTx)
 782			if err != nil {
 783				return nil, tracerr.Wrap(err)
 784			}
 785
 786			batchData.L1CoordinatorTxs = append(batchData.L1CoordinatorTxs, *l1Tx)
 787			position++
 788			// fmt.Println("DGB l1coordtx")
 789		}
 790
 791		// Insert all the txs forged in this batch (l1UserTxs,
 792		// L1CoordinatorTxs, PoolL2Txs) into stateDB so that they are
 793		// processed.
 794		poolL2Txs := common.L2TxsToPoolL2Txs(forgeBatchArgs.L2TxsData) // NOTE: This is a big ugly, find a better way
 795
 796		// ProcessTxs updates poolL2Txs adding: Nonce (and also TokenID, but we don't use it).
 797		//nolint:gomnd
 798		ptc := statedb.ProcessTxsConfig{ // TODO TMP
 799			NLevels:  32,
 800			MaxFeeTx: 64,
 801			MaxTx:    512,
 802			MaxL1Tx:  64,
 803		}
 804		processTxsOut, err := s.stateDB.ProcessTxs(ptc, forgeBatchArgs.FeeIdxCoordinator, l1UserTxs,
 805			batchData.L1CoordinatorTxs, poolL2Txs)
 806		if err != nil {
 807			return nil, tracerr.Wrap(err)
 808		}
 809
 810		// Set batchNum in exits
 811		for i := range processTxsOut.ExitInfos {
 812			exit := &processTxsOut.ExitInfos[i]
 813			exit.BatchNum = batchNum
 814		}
 815		batchData.ExitTree = processTxsOut.ExitInfos
 816
 817		l2Txs, err := common.PoolL2TxsToL2Txs(poolL2Txs) // NOTE: This is a big uggly, find a better way
 818		if err != nil {
 819			return nil, tracerr.Wrap(err)
 820		}
 821
 822		for i := range l2Txs {
 823			tx := &l2Txs[i]
 824			tx.Position = position
 825			tx.EthBlockNum = blockNum
 826			tx.BatchNum = batchNum
 827			nTx, err := common.NewL2Tx(tx)
 828			if err != nil {
 829				return nil, tracerr.Wrap(err)
 830			}
 831
 832			batchData.L2Txs = append(batchData.L2Txs, *nTx)
 833			position++
 834		}
 835
 836		for i := range processTxsOut.CreatedAccounts {
 837			createdAccount := &processTxsOut.CreatedAccounts[i]
 838			createdAccount.Nonce = 0
 839			createdAccount.Balance = big.NewInt(0)
 840			createdAccount.BatchNum = batchNum
 841		}
 842		batchData.CreatedAccounts = processTxsOut.CreatedAccounts
 843
 844		slotNum := int64(0)
 845		if ethBlock.Num >= s.consts.Auction.GenesisBlockNum {
 846			slotNum = (ethBlock.Num - s.consts.Auction.GenesisBlockNum) /
 847				int64(s.consts.Auction.BlocksPerSlot)
 848		}
 849
 850		// Get Batch information
 851		batch := common.Batch{
 852			BatchNum:           batchNum,
 853			EthBlockNum:        blockNum,
 854			ForgerAddr:         *sender,
 855			CollectedFees:      processTxsOut.CollectedFees,
 856			FeeIdxsCoordinator: forgeBatchArgs.FeeIdxCoordinator,
 857			StateRoot:          forgeBatchArgs.NewStRoot,
 858			NumAccounts:        len(batchData.CreatedAccounts),
 859			LastIdx:            forgeBatchArgs.NewLastIdx,
 860			ExitRoot:           forgeBatchArgs.NewExitRoot,
 861			SlotNum:            slotNum,
 862		}
 863		nextForgeL1TxsNumCpy := nextForgeL1TxsNum
 864		if forgeBatchArgs.L1Batch {
 865			batch.ForgeL1TxsNum = &nextForgeL1TxsNumCpy
 866			batchData.L1Batch = true
 867			nextForgeL1TxsNum++
 868		}
 869		batchData.Batch = batch
 870		rollupData.Batches = append(rollupData.Batches, *batchData)
 871	}
 872
 873	// Get Registered Tokens
 874	for _, evtAddToken := range rollupEvents.AddToken {
 875		var token common.Token
 876
 877		token.TokenID = common.TokenID(evtAddToken.TokenID)
 878		token.EthAddr = evtAddToken.TokenAddress
 879		token.EthBlockNum = blockNum
 880
 881		if consts, err := s.ethClient.EthERC20Consts(evtAddToken.TokenAddress); err != nil {
 882			log.Warnw("Error retreiving ERC20 token constants", "addr", evtAddToken.TokenAddress)
 883			token.Name = "ERC20_ETH_ERROR"
 884			token.Symbol = "ERROR"
 885			token.Decimals = 1
 886		} else {
 887			token.Name = cutStringMax(consts.Name, 20)
 888			token.Symbol = cutStringMax(consts.Symbol, 10)
 889			token.Decimals = consts.Decimals
 890		}
 891
 892		rollupData.AddedTokens = append(rollupData.AddedTokens, token)
 893	}
 894
 895	varsUpdate := false
 896
 897	for _, evtUpdateForgeL1L2BatchTimeout := range rollupEvents.UpdateForgeL1L2BatchTimeout {
 898		s.vars.Rollup.ForgeL1L2BatchTimeout = evtUpdateForgeL1L2BatchTimeout.NewForgeL1L2BatchTimeout
 899		varsUpdate = true
 900	}
 901
 902	for _, evtUpdateFeeAddToken := range rollupEvents.UpdateFeeAddToken {
 903		s.vars.Rollup.FeeAddToken = evtUpdateFeeAddToken.NewFeeAddToken
 904		varsUpdate = true
 905	}
 906
 907	// NOTE: WithdrawDelay update doesn't have event, so we can't track changes
 908
 909	// NOTE: Buckets update dones't have event, so we can't track changes
 910
 911	for _, evtWithdraw := range rollupEvents.Withdraw {
 912		rollupData.Withdrawals = append(rollupData.Withdrawals, common.WithdrawInfo{
 913			Idx:             common.Idx(evtWithdraw.Idx),
 914			NumExitRoot:     common.BatchNum(evtWithdraw.NumExitRoot),
 915			InstantWithdraw: evtWithdraw.InstantWithdraw,
 916			TxHash:          evtWithdraw.TxHash,
 917		})
 918	}
 919
 920	if varsUpdate {
 921		s.vars.Rollup.EthBlockNum = blockNum
 922		rollupData.Vars = s.vars.Rollup.Copy()
 923	}
 924
 925	return &rollupData, nil
 926}
 927
 928func cutStringMax(s string, max int) string {
 929	if len(s) > max {
 930		return s[:max]
 931	}
 932	return s
 933}
 934
 935// auctionSync gets information from the Auction Contract
 936func (s *Synchronizer) auctionSync(ethBlock *common.Block) (*common.AuctionData, error) {
 937	blockNum := ethBlock.Num
 938	var auctionData = common.NewAuctionData()
 939
 940	// Get auction events in the block
 941	auctionEvents, blockHash, err := s.ethClient.AuctionEventsByBlock(blockNum)
 942	if err != nil {
 943		return nil, tracerr.Wrap(err)
 944	}
 945	// No events in this block
 946	if blockHash == nil {
 947		return &auctionData, nil
 948	}
 949	if *blockHash != ethBlock.Hash {
 950		log.Errorw("Block hash mismatch in Auction events", "expected", ethBlock.Hash.String(),
 951			"got", blockHash.String())
 952		return nil, tracerr.Wrap(eth.ErrBlockHashMismatchEvent)
 953	}
 954
 955	// Get bids
 956	for _, evtNewBid := range auctionEvents.NewBid {
 957		bid := common.Bid{
 958			SlotNum:     evtNewBid.Slot,
 959			BidValue:    evtNewBid.BidAmount,
 960			Bidder:      evtNewBid.Bidder,
 961			EthBlockNum: blockNum,
 962		}
 963		auctionData.Bids = append(auctionData.Bids, bid)
 964	}
 965
 966	// Get Coordinators
 967	for _, evtSetCoordinator := range auctionEvents.SetCoordinator {
 968		coordinator := common.Coordinator{
 969			Bidder: evtSetCoordinator.BidderAddress,
 970			Forger: evtSetCoordinator.ForgerAddress,
 971			URL:    evtSetCoordinator.CoordinatorURL,
 972		}
 973		auctionData.Coordinators = append(auctionData.Coordinators, coordinator)
 974	}
 975
 976	varsUpdate := false
 977
 978	for _, evt := range auctionEvents.NewSlotDeadline {
 979		s.vars.Auction.SlotDeadline = evt.NewSlotDeadline
 980		varsUpdate = true
 981	}
 982	for _, evt := range auctionEvents.NewClosedAuctionSlots {
 983		s.vars.Auction.ClosedAuctionSlots = evt.NewClosedAuctionSlots
 984		varsUpdate = true
 985	}
 986	for _, evt := range auctionEvents.NewOutbidding {
 987		s.vars.Auction.Outbidding = evt.NewOutbidding
 988		varsUpdate = true
 989	}
 990	for _, evt := range auctionEvents.NewDonationAddress {
 991		s.vars.Auction.DonationAddress = evt.NewDonationAddress
 992		varsUpdate = true
 993	}
 994	for _, evt := range auctionEvents.NewBootCoordinator {
 995		s.vars.Auction.BootCoordinator = evt.NewBootCoordinator
 996		varsUpdate = true
 997	}
 998	for _, evt := range auctionEvents.NewOpenAuctionSlots {
 999		s.vars.Auction.OpenAuctionSlots = evt.NewOpenAuctionSlots
1000		varsUpdate = true
1001	}
1002	for _, evt := range auctionEvents.NewAllocationRatio {
1003		s.vars.Auction.AllocationRatio = evt.NewAllocationRatio
1004		varsUpdate = true
1005	}
1006	for _, evt := range auctionEvents.NewDefaultSlotSetBid {
1007		if evt.SlotSet > 6 { //nolint:gomnd
1008			return nil, tracerr.Wrap(fmt.Errorf("unexpected SlotSet in "+
1009				"auctionEvents.NewDefaultSlotSetBid: %v", evt.SlotSet))
1010		}
1011		s.vars.Auction.DefaultSlotSetBid[evt.SlotSet] = evt.NewInitialMinBid
1012		s.vars.Auction.DefaultSlotSetBidSlotNum = s.consts.Auction.SlotNum(blockNum) +
1013			int64(s.vars.Auction.ClosedAuctionSlots) + 1
1014		varsUpdate = true
1015	}
1016
1017	// NOTE: We ignore NewForgeAllocated
1018	// NOTE: We ignore NewForge because we're already tracking ForgeBatch event from Rollup
1019	// NOTE: We ignore HEZClaimed
1020
1021	if varsUpdate {
1022		s.vars.Auction.EthBlockNum = blockNum
1023		auctionData.Vars = s.vars.Auction.Copy()
1024	}
1025
1026	return &auctionData, nil
1027}
1028
1029// wdelayerSync gets information from the Withdrawal Delayer Contract
1030func (s *Synchronizer) wdelayerSync(ethBlock *common.Block) (*common.WDelayerData, error) {
1031	blockNum := ethBlock.Num
1032	wDelayerData := common.NewWDelayerData()
1033
1034	// Get wDelayer events in the block
1035	wDelayerEvents, blockHash, err := s.ethClient.WDelayerEventsByBlock(blockNum)
1036	if err != nil {
1037		return nil, tracerr.Wrap(err)
1038	}
1039	// No events in this block
1040	if blockHash == nil {
1041		return &wDelayerData, nil
1042	}
1043	if *blockHash != ethBlock.Hash {
1044		log.Errorw("Block hash mismatch in WDelayer events", "expected", ethBlock.Hash.String(),
1045			"got", blockHash.String())
1046		return nil, tracerr.Wrap(eth.ErrBlockHashMismatchEvent)
1047	}
1048
1049	for _, evt := range wDelayerEvents.Deposit {
1050		wDelayerData.Deposits = append(wDelayerData.Deposits, common.WDelayerTransfer{
1051			Owner:  evt.Owner,
1052			Token:  evt.Token,
1053			Amount: evt.Amount,
1054		})
1055		wDelayerData.DepositsByTxHash[evt.TxHash] =
1056			append(wDelayerData.DepositsByTxHash[evt.TxHash],
1057				&wDelayerData.Deposits[len(wDelayerData.Deposits)-1])
1058	}
1059	for _, evt := range wDelayerEvents.Withdraw {
1060		wDelayerData.Withdrawals = append(wDelayerData.Withdrawals, common.WDelayerTransfer{
1061			Owner:  evt.Owner,
1062			Token:  evt.Token,
1063			Amount: evt.Amount,
1064		})
1065	}
1066
1067	varsUpdate := false
1068
1069	// TODO EscapeHatchWithdrawal
1070	for range wDelayerEvents.EmergencyModeEnabled {
1071		s.vars.WDelayer.EmergencyMode = true
1072		varsUpdate = true
1073	}
1074	for _, evt := range wDelayerEvents.NewWithdrawalDelay {
1075		s.vars.WDelayer.WithdrawalDelay = evt.WithdrawalDelay
1076		varsUpdate = true
1077	}
1078	for _, evt := range wDelayerEvents.NewEmergencyCouncil {
1079		s.vars.WDelayer.EmergencyCouncilAddress = evt.NewEmergencyCouncil
1080		varsUpdate = true
1081	}
1082	for _, evt := range wDelayerEvents.NewHermezGovernanceAddress {
1083		s.vars.WDelayer.HermezGovernanceAddress = evt.NewHermezGovernanceAddress
1084		varsUpdate = true
1085	}
1086
1087	if varsUpdate {
1088		s.vars.WDelayer.EthBlockNum = blockNum
1089		wDelayerData.Vars = s.vars.WDelayer.Copy()
1090	}
1091
1092	return &wDelayerData, nil
1093}
1094
1095func getL1UserTx(eventsL1UserTx []eth.RollupEventL1UserTx, blockNum int64) ([]common.L1Tx, error) {
1096	l1Txs := make([]common.L1Tx, len(eventsL1UserTx))
1097	for i := range eventsL1UserTx {
1098		eventsL1UserTx[i].L1UserTx.EthBlockNum = blockNum
1099		// Check validity of L1UserTx
1100		l1Tx, err := common.NewL1Tx(&eventsL1UserTx[i].L1UserTx)
1101		if err != nil {
1102			return nil, tracerr.Wrap(err)
1103		}
1104		l1Txs[i] = *l1Tx
1105	}
1106	return l1Txs, nil
1107}