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
  7	"github.com/ethereum/go-ethereum"
  8	"github.com/hermeznetwork/hermez-node/common"
  9	"github.com/hermeznetwork/hermez-node/db/historydb"
 10	"github.com/hermeznetwork/hermez-node/db/statedb"
 11	"github.com/hermeznetwork/hermez-node/eth"
 12	"github.com/hermeznetwork/hermez-node/log"
 13)
 14
 15var (
 16// ErrNotAbleToSync is used when there is not possible to find a valid block to sync
 17// ErrNotAbleToSync = errors.New("it has not been possible to synchronize any block")
 18)
 19
 20// // SyncronizerState describes the synchronization progress of the smart contracts
 21// type SyncronizerState struct {
 22// 	LastUpdate                time.Time // last time this information was updated
 23// 	CurrentBatchNum           BatchNum  // Last batch that was forged on the blockchain
 24// 	CurrentBlockNum           uint64    // Last block that was mined on Ethereum
 25// 	CurrentToForgeL1TxsNum    uint32
 26// 	LastSyncedBatchNum        BatchNum // last batch synchronized by the coordinator
 27// 	LastSyncedBlockNum        uint64   // last Ethereum block synchronized by the coordinator
 28// 	LastSyncedToForgeL1TxsNum uint32
 29// }
 30
 31// // SyncStatus is returned by the Status method of the Synchronizer
 32// type SyncStatus struct {
 33// 	CurrentBlock      int64
 34// 	CurrentBatch      BatchNum
 35// 	CurrentForgerAddr ethCommon.Address
 36// 	NextForgerAddr    ethCommon.Address
 37// 	Synchronized      bool
 38// }
 39
 40// rollupData contains information returned by the Rollup SC
 41type rollupData struct {
 42	l1UserTxs []common.L1Tx
 43	batches   []common.BatchData
 44	// withdrawals      []*common.ExitInfo
 45	addTokens []common.Token
 46	vars      *common.RollupVars
 47}
 48
 49// NewRollupData creates an empty rollupData with the slices initialized.
 50func newRollupData() rollupData {
 51	return rollupData{
 52		l1UserTxs: make([]common.L1Tx, 0),
 53		batches:   make([]common.BatchData, 0),
 54		// withdrawals:      make([]*common.ExitInfo, 0),
 55		addTokens: make([]common.Token, 0),
 56	}
 57}
 58
 59// auctionData contains information returned by the Action SC
 60type auctionData struct {
 61	bids         []common.Bid
 62	coordinators []common.Coordinator
 63	vars         *common.AuctionVars
 64}
 65
 66// newAuctionData creates an empty auctionData with the slices initialized.
 67func newAuctionData() *auctionData {
 68	return &auctionData{
 69		bids:         make([]common.Bid, 0),
 70		coordinators: make([]common.Coordinator, 0),
 71	}
 72}
 73
 74type wdelayerData struct {
 75	vars *common.WithdrawDelayerVars
 76}
 77
 78// Synchronizer implements the Synchronizer type
 79type Synchronizer struct {
 80	ethClient        eth.ClientInterface
 81	auctionConstants eth.AuctionConstants
 82	historyDB        *historydb.HistoryDB
 83	stateDB          *statedb.StateDB
 84	// firstSavedBlock  *common.Block
 85	// mux sync.Mutex
 86}
 87
 88// NewSynchronizer creates a new Synchronizer
 89func NewSynchronizer(ethClient eth.ClientInterface, historyDB *historydb.HistoryDB, stateDB *statedb.StateDB) (*Synchronizer, error) {
 90	auctionConstants, err := ethClient.AuctionConstants()
 91	if err != nil {
 92		log.Errorw("NewSynchronizer", "err", err)
 93		return nil, err
 94	}
 95	return &Synchronizer{
 96		ethClient:        ethClient,
 97		auctionConstants: *auctionConstants,
 98		historyDB:        historyDB,
 99		stateDB:          stateDB,
100	}, nil
101}
102
103// Sync2 attems to synchronize an ethereum block starting from lastSavedBlock.
104// If lastSavedBlock is nil, the lastSavedBlock value is obtained from de DB.
105// If a block is synched, it will be returned and also stored in the DB.  If a
106// reorg is detected, the number of discarded blocks will be returned and no
107// synchronization will be made.
108// TODO: Be smart about locking: only lock during the read/write operations
109func (s *Synchronizer) Sync2(ctx context.Context, lastSavedBlock *common.Block) (*common.BlockData, *int64, error) {
110	var nextBlockNum int64 // next block number to sync
111	if lastSavedBlock == nil {
112		var err error
113		// Get lastSavedBlock from History DB
114		lastSavedBlock, err = s.historyDB.GetLastBlock()
115		if err != nil && err != sql.ErrNoRows {
116			return nil, nil, err
117		}
118		// If we don't have any stored block, we must do a full sync starting from the rollup genesis block
119		if err == sql.ErrNoRows {
120			nextBlockNum = s.auctionConstants.GenesisBlockNum
121		}
122	}
123	if lastSavedBlock != nil {
124		nextBlockNum = lastSavedBlock.EthBlockNum + 1
125	}
126
127	ethBlock, err := s.ethClient.EthBlockByNumber(ctx, nextBlockNum)
128	if err == ethereum.NotFound {
129		return nil, nil, nil
130	} else if err != nil {
131		return nil, nil, err
132	}
133
134	log.Debugw("Syncing...", "block", nextBlockNum)
135
136	// Check that the obtianed ethBlock.ParentHash == prevEthBlock.Hash; if not, reorg!
137	if lastSavedBlock != nil {
138		if lastSavedBlock.Hash != ethBlock.ParentHash {
139			// Reorg detected
140			log.Debugw("Reorg Detected",
141				"blockNum", ethBlock.EthBlockNum,
142				"block.parent", ethBlock.ParentHash, "parent.hash", lastSavedBlock.Hash)
143			lastDBBlockNum, err := s.reorg(lastSavedBlock)
144			if err != nil {
145				return nil, nil, err
146			}
147			discarded := lastSavedBlock.EthBlockNum - lastDBBlockNum
148			return nil, &discarded, nil
149		}
150	}
151
152	// Get data from the rollup contract
153	rollupData, err := s.rollupSync(ethBlock)
154	if err != nil {
155		return nil, nil, err
156	}
157
158	// Get data from the auction contract
159	auctionData, err := s.auctionSync(ethBlock)
160	if err != nil {
161		return nil, nil, err
162	}
163
164	// Get data from the WithdrawalDelayer contract
165	wdelayerData, err := s.wdelayerSync(ethBlock)
166	if err != nil {
167		return nil, nil, err
168	}
169
170	// Group all the block data into the structs to save into HistoryDB
171	var blockData common.BlockData
172
173	blockData.Block = *ethBlock
174
175	blockData.L1UserTxs = rollupData.l1UserTxs
176	blockData.Batches = rollupData.batches
177	// blockData.withdrawals = rollupData.withdrawals // TODO
178	blockData.AddedTokens = rollupData.addTokens
179	blockData.RollupVars = rollupData.vars
180
181	blockData.Bids = auctionData.bids
182	blockData.Coordinators = auctionData.coordinators
183	blockData.AuctionVars = auctionData.vars
184
185	blockData.WithdrawDelayerVars = wdelayerData.vars
186
187	// log.Debugw("Sync()", "block", blockData)
188	// err = s.historyDB.AddBlock(blockData.Block)
189	// if err != nil {
190	// 	return err
191	// }
192	err = s.historyDB.AddBlockSCData(&blockData)
193	if err != nil {
194		return nil, nil, err
195	}
196
197	return &blockData, nil, nil
198}
199
200// reorg manages a reorg, updating History and State DB as needed.  Keeps
201// checking previous blocks from the HistoryDB against the blockchain until a
202// block hash match is found.  All future blocks in the HistoryDB and
203// corresponding batches in StateBD are discarded.  Returns the last valid
204// blockNum from the HistoryDB.
205func (s *Synchronizer) reorg(uncleBlock *common.Block) (int64, error) {
206	var block *common.Block
207	blockNum := uncleBlock.EthBlockNum
208
209	for blockNum >= s.auctionConstants.GenesisBlockNum {
210		ethBlock, err := s.ethClient.EthBlockByNumber(context.Background(), blockNum)
211		if err != nil {
212			return 0, err
213		}
214
215		block, err = s.historyDB.GetBlock(blockNum)
216		if err != nil {
217			return 0, err
218		}
219		if block.Hash == ethBlock.Hash {
220			log.Debugf("Found valid block: %v", blockNum)
221			break
222		}
223		blockNum--
224	}
225	total := uncleBlock.EthBlockNum - blockNum
226	log.Debugw("Discarding blocks", "total", total, "from", uncleBlock.EthBlockNum, "to", blockNum+1)
227
228	// Set History DB and State DB to the correct state
229	err := s.historyDB.Reorg(block.EthBlockNum)
230	if err != nil {
231		return 0, err
232	}
233
234	batchNum, err := s.historyDB.GetLastBatchNum()
235	if err != nil && err != sql.ErrNoRows {
236		return 0, err
237	}
238	if batchNum != 0 {
239		err = s.stateDB.Reset(batchNum)
240		if err != nil {
241			return 0, err
242		}
243	}
244
245	return block.EthBlockNum, nil
246}
247
248// TODO: Figure out who will use the Status output, and only return what's strictly need
249/*
250// Status returns current status values from the Synchronizer
251func (s *Synchronizer) Status() (*common.SyncStatus, error) {
252	// Avoid possible inconsistencies
253	s.mux.Lock()
254	defer s.mux.Unlock()
255
256	var status *common.SyncStatus
257
258	// TODO: Join all queries to the DB into a single transaction so that
259	// we can remove the mutex locking here:
260	// - HistoryDB.GetLastBlock
261	// - HistoryDB.GetLastBatchNum
262	// - HistoryDB.GetCurrentForgerAddr
263	// - HistoryDB.GetNextForgerAddr
264
265	// Get latest block in History DB
266	lastSavedBlock, err := s.historyDB.GetLastBlock()
267	if err != nil {
268		return nil, err
269	}
270	status.CurrentBlock = lastSavedBlock.EthBlockNum
271
272	// Get latest batch in History DB
273	lastSavedBatch, err := s.historyDB.GetLastBatchNum()
274	if err != nil && err != sql.ErrNoRows {
275		return nil, err
276	}
277	status.CurrentBatch = lastSavedBatch
278
279	// Get latest blockNum in blockchain
280	latestBlockNum, err := s.ethClient.EthCurrentBlock()
281	if err != nil {
282		return nil, err
283	}
284
285	// TODO: Get CurrentForgerAddr & NextForgerAddr from the Auction SC / Or from the HistoryDB
286
287	// Check if Synchronizer is synchronized
288	status.Synchronized = status.CurrentBlock == latestBlockNum
289	return status, nil
290}
291*/
292
293// rollupSync retreives all the Rollup Smart Contract Data that happened at
294// ethBlock.blockNum with ethBlock.Hash.
295func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*rollupData, error) {
296	blockNum := ethBlock.EthBlockNum
297	var rollupData = newRollupData()
298	// var forgeL1TxsNum int64
299
300	// Get rollup events in the block, and make sure the block hash matches
301	// the expected one.
302	rollupEvents, blockHash, err := s.ethClient.RollupEventsByBlock(blockNum)
303	if err != nil {
304		return nil, err
305	}
306	if *blockHash != ethBlock.Hash {
307		return nil, eth.ErrBlockHashMismatchEvent
308	}
309
310	var nextForgeL1TxsNum int64 // forgeL1TxsNum for the next L1Batch
311	nextForgeL1TxsNumPtr, err := s.historyDB.GetLastL1TxsNum()
312	if err != nil {
313		return nil, err
314	}
315	if nextForgeL1TxsNumPtr != nil {
316		nextForgeL1TxsNum = *nextForgeL1TxsNumPtr + 1
317	} else {
318		nextForgeL1TxsNum = 0
319	}
320
321	// Get newLastIdx that will be used to complete the accounts
322	// idx, err := s.getIdx(rollupEvents)
323	// if err != nil {
324	// 	return nil, err
325	// }
326
327	// Get L1UserTX
328	rollupData.l1UserTxs, err = getL1UserTx(rollupEvents.L1UserTx, blockNum)
329	if err != nil {
330		return nil, err
331	}
332
333	// Get ForgeBatch events to get the L1CoordinatorTxs
334	for _, evtForgeBatch := range rollupEvents.ForgeBatch {
335		batchData := common.NewBatchData()
336		position := 0
337
338		// Get the input for each Tx
339		forgeBatchArgs, sender, err := s.ethClient.RollupForgeBatchArgs(evtForgeBatch.EthTxHash)
340		if err != nil {
341			return nil, err
342		}
343
344		batchNum := common.BatchNum(evtForgeBatch.BatchNum)
345		var l1UserTxs []common.L1Tx
346		// Check if this is a L1Batch to get L1 Tx from it
347		if forgeBatchArgs.L1Batch {
348			// Get L1UserTxs with toForgeL1TxsNum, which correspond
349			// to the L1UserTxs that are forged in this batch, so
350			// that stateDB can process them.
351
352			// First try to find them in HistoryDB.
353			l1UserTxs, err = s.historyDB.GetL1UserTxs(nextForgeL1TxsNum)
354			if err != nil {
355				return nil, err
356			}
357			// Apart from the DB, try to find them in this block.
358			// This could happen because in a block there could be
359			// multiple batches with L1Batch = true (although it's
360			// a very rare case).  If not found in the DB and the
361			// block doesn't contain the l1UserTxs, it means that
362			// the L1UserTxs queue with toForgeL1TxsNum was closed
363			// empty, so we leave `l1UserTxs` as an empty slice.
364			for _, l1UserTx := range rollupData.l1UserTxs {
365				if *l1UserTx.ToForgeL1TxsNum == nextForgeL1TxsNum {
366					l1UserTxs = append(l1UserTxs, l1UserTx)
367				}
368			}
369
370			position = len(l1UserTxs)
371		}
372		// Get L1 Coordinator Txs
373		for i := range forgeBatchArgs.L1CoordinatorTxs {
374			l1CoordinatorTx := forgeBatchArgs.L1CoordinatorTxs[i]
375			l1CoordinatorTx.Position = position
376			// l1CoordinatorTx.ToForgeL1TxsNum = &forgeL1TxsNum
377			l1CoordinatorTx.UserOrigin = false
378			l1CoordinatorTx.EthBlockNum = blockNum
379			l1CoordinatorTx.BatchNum = &batchNum
380			l1Tx, err := common.NewL1Tx(&l1CoordinatorTx)
381			if err != nil {
382				return nil, err
383			}
384
385			batchData.L1CoordinatorTxs = append(batchData.L1CoordinatorTxs, *l1Tx)
386			position++
387			// fmt.Println("DGB l1coordtx")
388		}
389
390		// Insert all the txs forged in this batch (l1UserTxs,
391		// L1CoordinatorTxs, PoolL2Txs) into stateDB so that they are
392		// processed.
393		poolL2Txs := common.L2TxsToPoolL2Txs(forgeBatchArgs.L2TxsData) // TODO: This is a big ugly, find a better way
394
395		// ProcessTxs updates poolL2Txs adding: Nonce (and also TokenID, but we don't use it).
396		processTxsOut, err := s.stateDB.ProcessTxs(forgeBatchArgs.FeeIdxCoordinator, l1UserTxs,
397			batchData.L1CoordinatorTxs, poolL2Txs)
398		if err != nil {
399			return nil, err
400		}
401
402		// Set batchNum in exits
403		for i := range processTxsOut.ExitInfos {
404			exit := &processTxsOut.ExitInfos[i]
405			exit.BatchNum = batchNum
406		}
407		batchData.ExitTree = processTxsOut.ExitInfos
408
409		l2Txs, err := common.PoolL2TxsToL2Txs(poolL2Txs) // TODO: This is a big uggly, find a better way
410		if err != nil {
411			return nil, err
412		}
413
414		for i := range l2Txs {
415			tx := &l2Txs[i]
416			tx.Position = position
417			tx.EthBlockNum = blockNum
418			tx.BatchNum = batchNum
419			nTx, err := common.NewL2Tx(tx)
420			if err != nil {
421				return nil, err
422			}
423
424			batchData.L2Txs = append(batchData.L2Txs, *nTx)
425			position++
426		}
427
428		for i := range processTxsOut.CreatedAccounts {
429			createdAccount := &processTxsOut.CreatedAccounts[i]
430			createdAccount.BatchNum = batchNum
431		}
432		batchData.CreatedAccounts = processTxsOut.CreatedAccounts
433
434		slotNum := int64(0)
435		if ethBlock.EthBlockNum >= s.auctionConstants.GenesisBlockNum {
436			slotNum = (ethBlock.EthBlockNum - s.auctionConstants.GenesisBlockNum) /
437				int64(s.auctionConstants.BlocksPerSlot)
438		}
439
440		// Get Batch information
441		batch := common.Batch{
442			BatchNum:      batchNum,
443			EthBlockNum:   blockNum,
444			ForgerAddr:    *sender,
445			CollectedFees: processTxsOut.CollectedFees,
446			StateRoot:     forgeBatchArgs.NewStRoot,
447			NumAccounts:   len(batchData.CreatedAccounts),
448			LastIdx:       forgeBatchArgs.NewLastIdx,
449			ExitRoot:      forgeBatchArgs.NewExitRoot,
450			SlotNum:       slotNum,
451		}
452		nextForgeL1TxsNumCpy := nextForgeL1TxsNum
453		if forgeBatchArgs.L1Batch {
454			batch.ForgeL1TxsNum = &nextForgeL1TxsNumCpy
455			batchData.L1Batch = true
456			nextForgeL1TxsNum++
457		}
458		batchData.Batch = batch
459		rollupData.batches = append(rollupData.batches, *batchData)
460	}
461
462	// Get Registered Tokens
463	for _, evtAddToken := range rollupEvents.AddToken {
464		var token common.Token
465
466		token.TokenID = common.TokenID(evtAddToken.TokenID)
467		token.EthAddr = evtAddToken.TokenAddress
468		token.EthBlockNum = blockNum
469
470		if consts, err := s.ethClient.EthERC20Consts(evtAddToken.TokenAddress); err != nil {
471			log.Warnw("Error retreiving ERC20 token constants", "addr", evtAddToken.TokenAddress)
472			token.Name = "ERC20_ETH_ERROR"
473			token.Symbol = "ERROR"
474			token.Decimals = 1
475		} else {
476			token.Name = cutStringMax(consts.Name, 20)
477			token.Symbol = cutStringMax(consts.Symbol, 10)
478			token.Decimals = consts.Decimals
479		}
480
481		rollupData.addTokens = append(rollupData.addTokens, token)
482	}
483
484	// TODO: rollupEvents.UpdateForgeL1L2BatchTimeout
485	// TODO: rollupEvents.UpdateFeeAddToken
486	// TODO: rollupEvents.WithdrawEvent
487
488	// TODO: Emergency Mechanism
489	// TODO: Variables
490	// TODO: Constants
491
492	return &rollupData, nil
493}
494
495func cutStringMax(s string, max int) string {
496	if len(s) > max {
497		return s[:max]
498	}
499	return s
500}
501
502// auctionSync gets information from the Auction Contract
503func (s *Synchronizer) auctionSync(ethBlock *common.Block) (*auctionData, error) {
504	blockNum := ethBlock.EthBlockNum
505	var auctionData = newAuctionData()
506
507	// Get auction events in the block
508	auctionEvents, blockHash, err := s.ethClient.AuctionEventsByBlock(blockNum)
509	if err != nil {
510		return nil, err
511	}
512	if *blockHash != ethBlock.Hash {
513		return nil, eth.ErrBlockHashMismatchEvent
514	}
515
516	// Get bids
517	for _, evtNewBid := range auctionEvents.NewBid {
518		bid := common.Bid{
519			SlotNum:     evtNewBid.Slot,
520			BidValue:    evtNewBid.BidAmount,
521			Bidder:      evtNewBid.Bidder,
522			EthBlockNum: blockNum,
523		}
524		auctionData.bids = append(auctionData.bids, bid)
525	}
526
527	// Get Coordinators
528	for _, evtSetCoordinator := range auctionEvents.SetCoordinator {
529		coordinator := common.Coordinator{
530			Bidder: evtSetCoordinator.BidderAddress,
531			Forger: evtSetCoordinator.ForgerAddress,
532			URL:    evtSetCoordinator.CoordinatorURL,
533		}
534		auctionData.coordinators = append(auctionData.coordinators, coordinator)
535	}
536
537	// TODO: NewSlotDeadline
538	// TODO: NewClosedAuctionSlots
539	// TODO: NewOutbidding
540	// TODO: NewDonationAddress
541	// TODO: NewBootCoordinator
542	// TODO: NewOpenAuctionSlots
543	// TODO: NewAllocationRatio
544	// TODO: NewForgeAllocated
545	// TODO: NewDefaultSlotSetBid
546	// TODO: NewForge
547	// TODO: HEZClaimed
548
549	// TODO: VARS
550	// TODO: CONSTANTS
551
552	return auctionData, nil
553}
554
555// wdelayerSync gets information from the Withdrawal Delayer Contract
556func (s *Synchronizer) wdelayerSync(ethBlock *common.Block) (*wdelayerData, error) {
557	// blockNum := ethBlock.EthBlockNum
558	// TODO: VARS
559	// TODO: CONSTANTS
560
561	return &wdelayerData{
562		vars: nil,
563	}, nil
564}
565
566// func (s *Synchronizer) getIdx(rollupEvents *eth.RollupEvents) (int64, error) {
567// 	// TODO: FIXME: There will be an error here when `len(rollupEvents.ForgeBatch) == 0`
568// 	lastForgeBatch := rollupEvents.ForgeBatch[len(rollupEvents.ForgeBatch)-1]
569//
570// 	// TODO: RollupForgeBatchArgs is already called in `rollupSync`.
571// 	// Ideally it should not need to be called twice for the same batch.
572// 	// Get the input for forgeBatch
573// 	forgeBatchArgs, err := s.ethClient.RollupForgeBatchArgs(lastForgeBatch.EthTxHash)
574// 	if err != nil {
575// 		return 0, err
576// 	}
577//
578// 	return forgeBatchArgs.NewLastIdx + 1, nil
579// }
580
581func getL1UserTx(eventsL1UserTx []eth.RollupEventL1UserTx, blockNum int64) ([]common.L1Tx, error) {
582	l1Txs := make([]common.L1Tx, len(eventsL1UserTx))
583	for i := range eventsL1UserTx {
584		eventsL1UserTx[i].L1UserTx.EthBlockNum = blockNum
585		// Check validity of L1UserTx
586		l1Tx, err := common.NewL1Tx(&eventsL1UserTx[i].L1UserTx)
587		if err != nil {
588			return nil, err
589		}
590		l1Txs[i] = *l1Tx
591	}
592	return l1Txs, nil
593}