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