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	var numAccounts int
300
301	// Get rollup events in the block, and make sure the block hash matches
302	// the expected one.
303	rollupEvents, blockHash, err := s.ethClient.RollupEventsByBlock(blockNum)
304	if err != nil {
305		return nil, err
306	}
307	if *blockHash != ethBlock.Hash {
308		return nil, eth.ErrBlockHashMismatchEvent
309	}
310
311	var nextForgeL1TxsNum int64 // forgeL1TxsNum for the next L1Batch
312	nextForgeL1TxsNumPtr, err := s.historyDB.GetLastL1TxsNum()
313	if err != nil {
314		return nil, err
315	}
316	if nextForgeL1TxsNumPtr != nil {
317		nextForgeL1TxsNum = *nextForgeL1TxsNumPtr + 1
318	} else {
319		nextForgeL1TxsNum = 0
320	}
321
322	// Get newLastIdx that will be used to complete the accounts
323	// idx, err := s.getIdx(rollupEvents)
324	// if err != nil {
325	// 	return nil, err
326	// }
327
328	// Get L1UserTX
329	rollupData.l1UserTxs, err = getL1UserTx(rollupEvents.L1UserTx, blockNum)
330	if err != nil {
331		return nil, err
332	}
333
334	// Get ForgeBatch events to get the L1CoordinatorTxs
335	for _, evtForgeBatch := range rollupEvents.ForgeBatch {
336		batchData := common.NewBatchData()
337		position := 0
338
339		// Get the input for each Tx
340		forgeBatchArgs, sender, err := s.ethClient.RollupForgeBatchArgs(evtForgeBatch.EthTxHash)
341		if err != nil {
342			return nil, err
343		}
344
345		batchNum := common.BatchNum(evtForgeBatch.BatchNum)
346		forgeL1TxsNum := nextForgeL1TxsNum
347		var l1UserTxs []common.L1Tx
348		// Check if this is a L1Batch to get L1 Tx from it
349		if forgeBatchArgs.L1Batch {
350			// Get L1UserTxs with toForgeL1TxsNum, which correspond
351			// to the L1UserTxs that are forged in this batch, so
352			// that stateDB can process them.
353
354			// First try to find them in HistoryDB.
355			l1UserTxs, err := s.historyDB.GetL1UserTxs(forgeL1TxsNum)
356			if len(l1UserTxs) == 0 {
357				// If not found in the DB, try to find them in
358				// this block.  This could happen because in a
359				// block there could be multiple batches with
360				// L1Batch = true (although it's a very rare
361				// case).
362				// If not found in the DB and the block doesn't
363				// contain the l1UserTxs, it means that the
364				// L1UserTxs queue with toForgeL1TxsNum was
365				// closed empty, so we leave `l1UserTxs` as an
366				// empty slice.
367				for _, l1UserTx := range rollupData.l1UserTxs {
368					if *l1UserTx.ToForgeL1TxsNum == forgeL1TxsNum {
369						l1UserTxs = append(l1UserTxs, l1UserTx)
370					}
371				}
372			}
373			if err != nil {
374				return nil, err
375			}
376
377			position = len(l1UserTxs)
378
379			// Get L1 Coordinator Txs
380			for i := range forgeBatchArgs.L1CoordinatorTxs {
381				l1CoordinatorTx := forgeBatchArgs.L1CoordinatorTxs[i]
382				l1CoordinatorTx.Position = position
383				l1CoordinatorTx.ToForgeL1TxsNum = &forgeL1TxsNum
384				l1CoordinatorTx.UserOrigin = false
385				l1CoordinatorTx.EthBlockNum = blockNum
386				l1CoordinatorTx.BatchNum = &batchNum
387				l1Tx, err := common.NewL1Tx(&l1CoordinatorTx)
388				if err != nil {
389					return nil, err
390				}
391
392				batchData.L1CoordinatorTxs = append(batchData.L1CoordinatorTxs, *l1Tx)
393				position++
394			}
395			nextForgeL1TxsNum++
396		}
397
398		// Insert all the txs forged in this batch (l1UserTxs,
399		// L1CoordinatorTxs, PoolL2Txs) into stateDB so that they are
400		// processed.
401		poolL2Txs := common.L2TxsToPoolL2Txs(forgeBatchArgs.L2TxsData) // TODO: This is a big ugly, find a better way
402
403		// TODO: Get createdAccounts from ProcessTxs()
404		// TODO: Get CollectedFees from ProcessTxs()
405		// TODO: Pass forgeBatchArgs.FeeIdxCoordinator to ProcessTxs()
406		// ProcessTxs updates poolL2Txs adding: Nonce, TokenID
407		_, exitInfo, err := s.stateDB.ProcessTxs(l1UserTxs, batchData.L1CoordinatorTxs, poolL2Txs)
408		if err != nil {
409			return nil, err
410		}
411
412		l2Txs, err := common.PoolL2TxsToL2Txs(poolL2Txs) // TODO: This is a big uggly, find a better way
413		if err != nil {
414			return nil, err
415		}
416
417		for i := range l2Txs {
418			_l2Tx := l2Txs[i]
419			_l2Tx.Position = position
420			_l2Tx.EthBlockNum = blockNum
421			_l2Tx.BatchNum = batchNum
422			l2Tx, err := common.NewL2Tx(&_l2Tx)
423			if err != nil {
424				return nil, err
425			}
426
427			batchData.L2Txs = append(batchData.L2Txs, *l2Tx)
428			position++
429		}
430
431		batchData.ExitTree = exitInfo
432
433		slotNum := int64(0)
434		if ethBlock.EthBlockNum >= s.auctionConstants.GenesisBlockNum {
435			slotNum = (ethBlock.EthBlockNum - s.auctionConstants.GenesisBlockNum) /
436				int64(s.auctionConstants.BlocksPerSlot)
437		}
438
439		// Get Batch information
440		batch := common.Batch{
441			BatchNum:    batchNum,
442			EthBlockNum: blockNum,
443			ForgerAddr:  *sender,
444			// CollectedFees: , TODO: Clarify where to get them if they are still needed
445			StateRoot:   forgeBatchArgs.NewStRoot,
446			NumAccounts: numAccounts,
447			ExitRoot:    forgeBatchArgs.NewExitRoot,
448			SlotNum:     slotNum,
449		}
450		if forgeBatchArgs.L1Batch {
451			batch.ForgeL1TxsNum = &forgeL1TxsNum
452		}
453		batchData.Batch = batch
454		rollupData.batches = append(rollupData.batches, *batchData)
455	}
456
457	// Get Registered Tokens
458	for _, evtAddToken := range rollupEvents.AddToken {
459		var token common.Token
460
461		token.TokenID = common.TokenID(evtAddToken.TokenID)
462		token.EthAddr = evtAddToken.TokenAddress
463		token.EthBlockNum = blockNum
464
465		if consts, err := s.ethClient.EthERC20Consts(evtAddToken.TokenAddress); err != nil {
466			log.Warnw("Error retreiving ERC20 token constants", "addr", evtAddToken.TokenAddress)
467			// TODO: Add external information consulting SC about it using Address
468			token.Name = "ERC20_ETH_ERROR"
469			token.Symbol = "ERROR"
470			token.Decimals = 1
471		} else {
472			token.Name = cutStringMax(consts.Name, 20)
473			token.Symbol = cutStringMax(consts.Symbol, 10)
474			token.Decimals = consts.Decimals
475		}
476
477		rollupData.addTokens = append(rollupData.addTokens, token)
478	}
479
480	// TODO: rollupEvents.UpdateForgeL1L2BatchTimeout
481	// TODO: rollupEvents.UpdateFeeAddToken
482	// TODO: rollupEvents.WithdrawEvent
483
484	// TODO: Emergency Mechanism
485	// TODO: Variables
486	// TODO: Constants
487
488	return &rollupData, nil
489}
490
491func cutStringMax(s string, max int) string {
492	if len(s) > max {
493		return s[:max]
494	}
495	return s
496}
497
498// auctionSync gets information from the Auction Contract
499func (s *Synchronizer) auctionSync(ethBlock *common.Block) (*auctionData, error) {
500	blockNum := ethBlock.EthBlockNum
501	var auctionData = newAuctionData()
502
503	// Get auction events in the block
504	auctionEvents, blockHash, err := s.ethClient.AuctionEventsByBlock(blockNum)
505	if err != nil {
506		return nil, err
507	}
508	if *blockHash != ethBlock.Hash {
509		return nil, eth.ErrBlockHashMismatchEvent
510	}
511
512	// Get bids
513	for _, evtNewBid := range auctionEvents.NewBid {
514		bid := common.Bid{
515			SlotNum:     evtNewBid.Slot,
516			BidValue:    evtNewBid.BidAmount,
517			Bidder:      evtNewBid.Bidder,
518			EthBlockNum: blockNum,
519		}
520		auctionData.bids = append(auctionData.bids, bid)
521	}
522
523	// Get Coordinators
524	for _, evtSetCoordinator := range auctionEvents.SetCoordinator {
525		coordinator := common.Coordinator{
526			Bidder: evtSetCoordinator.BidderAddress,
527			Forger: evtSetCoordinator.ForgerAddress,
528			URL:    evtSetCoordinator.CoordinatorURL,
529		}
530		auctionData.coordinators = append(auctionData.coordinators, coordinator)
531	}
532
533	// TODO: NewSlotDeadline
534	// TODO: NewClosedAuctionSlots
535	// TODO: NewOutbidding
536	// TODO: NewDonationAddress
537	// TODO: NewBootCoordinator
538	// TODO: NewOpenAuctionSlots
539	// TODO: NewAllocationRatio
540	// TODO: NewForgeAllocated
541	// TODO: NewDefaultSlotSetBid
542	// TODO: NewForge
543	// TODO: HEZClaimed
544
545	// TODO: VARS
546	// TODO: CONSTANTS
547
548	return auctionData, nil
549}
550
551// wdelayerSync gets information from the Withdrawal Delayer Contract
552func (s *Synchronizer) wdelayerSync(ethBlock *common.Block) (*wdelayerData, error) {
553	// blockNum := ethBlock.EthBlockNum
554	// TODO: VARS
555	// TODO: CONSTANTS
556
557	return &wdelayerData{
558		vars: nil,
559	}, nil
560}
561
562// func (s *Synchronizer) getIdx(rollupEvents *eth.RollupEvents) (int64, error) {
563// 	// TODO: FIXME: There will be an error here when `len(rollupEvents.ForgeBatch) == 0`
564// 	lastForgeBatch := rollupEvents.ForgeBatch[len(rollupEvents.ForgeBatch)-1]
565//
566// 	// TODO: RollupForgeBatchArgs is already called in `rollupSync`.
567// 	// Ideally it should not need to be called twice for the same batch.
568// 	// Get the input for forgeBatch
569// 	forgeBatchArgs, err := s.ethClient.RollupForgeBatchArgs(lastForgeBatch.EthTxHash)
570// 	if err != nil {
571// 		return 0, err
572// 	}
573//
574// 	return forgeBatchArgs.NewLastIdx + 1, nil
575// }
576
577func getL1UserTx(eventsL1UserTx []eth.RollupEventL1UserTx, blockNum int64) ([]common.L1Tx, error) {
578	l1Txs := make([]common.L1Tx, len(eventsL1UserTx))
579	for i := range eventsL1UserTx {
580		eventsL1UserTx[i].L1UserTx.EthBlockNum = blockNum
581		// Check validity of L1UserTx
582		l1Tx, err := common.NewL1Tx(&eventsL1UserTx[i].L1UserTx)
583		if err != nil {
584			return nil, err
585		}
586		l1Txs[i] = *l1Tx
587	}
588	return l1Txs, nil
589}