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 || lastSavedBlock.EthBlockNum == 0 {
120 nextBlockNum = s.auctionConstants.GenesisBlockNum
121 lastSavedBlock = nil
122 }
123 }
124 if lastSavedBlock != nil {
125 nextBlockNum = lastSavedBlock.EthBlockNum + 1
126 }
127
128 ethBlock, err := s.ethClient.EthBlockByNumber(ctx, nextBlockNum)
129 if err == ethereum.NotFound {
130 return nil, nil, nil
131 } else if err != nil {
132 return nil, nil, err
133 }
134
135 log.Debugw("Syncing...", "block", nextBlockNum)
136
137 // Check that the obtianed ethBlock.ParentHash == prevEthBlock.Hash; if not, reorg!
138 if lastSavedBlock != nil {
139 if lastSavedBlock.Hash != ethBlock.ParentHash {
140 // Reorg detected
141 log.Debugw("Reorg Detected",
142 "blockNum", ethBlock.EthBlockNum,
143 "block.parent", ethBlock.ParentHash, "parent.hash", lastSavedBlock.Hash)
144 lastDBBlockNum, err := s.reorg(lastSavedBlock)
145 if err != nil {
146 return nil, nil, err
147 }
148 discarded := lastSavedBlock.EthBlockNum - lastDBBlockNum
149 return nil, &discarded, nil
150 }
151 }
152
153 // Get data from the rollup contract
154 rollupData, err := s.rollupSync(ethBlock)
155 if err != nil {
156 return nil, nil, err
157 }
158
159 // Get data from the auction contract
160 auctionData, err := s.auctionSync(ethBlock)
161 if err != nil {
162 return nil, nil, err
163 }
164
165 // Get data from the WithdrawalDelayer contract
166 wdelayerData, err := s.wdelayerSync(ethBlock)
167 if err != nil {
168 return nil, nil, err
169 }
170
171 // Group all the block data into the structs to save into HistoryDB
172 var blockData common.BlockData
173
174 blockData.Block = *ethBlock
175
176 blockData.L1UserTxs = rollupData.l1UserTxs
177 blockData.Batches = rollupData.batches
178 // blockData.withdrawals = rollupData.withdrawals // TODO
179 blockData.AddedTokens = rollupData.addTokens
180 blockData.RollupVars = rollupData.vars
181
182 blockData.Bids = auctionData.bids
183 blockData.Coordinators = auctionData.coordinators
184 blockData.AuctionVars = auctionData.vars
185
186 blockData.WithdrawDelayerVars = wdelayerData.vars
187
188 // log.Debugw("Sync()", "block", blockData)
189 // err = s.historyDB.AddBlock(blockData.Block)
190 // if err != nil {
191 // return err
192 // }
193 err = s.historyDB.AddBlockSCData(&blockData)
194 if err != nil {
195 return nil, nil, err
196 }
197
198 return &blockData, nil, nil
199}
200
201// reorg manages a reorg, updating History and State DB as needed. Keeps
202// checking previous blocks from the HistoryDB against the blockchain until a
203// block hash match is found. All future blocks in the HistoryDB and
204// corresponding batches in StateBD are discarded. Returns the last valid
205// blockNum from the HistoryDB.
206func (s *Synchronizer) reorg(uncleBlock *common.Block) (int64, error) {
207 var block *common.Block
208 blockNum := uncleBlock.EthBlockNum
209
210 for blockNum >= s.auctionConstants.GenesisBlockNum {
211 ethBlock, err := s.ethClient.EthBlockByNumber(context.Background(), blockNum)
212 if err != nil {
213 return 0, err
214 }
215
216 block, err = s.historyDB.GetBlock(blockNum)
217 if err != nil {
218 return 0, err
219 }
220 if block.Hash == ethBlock.Hash {
221 log.Debugf("Found valid block: %v", blockNum)
222 break
223 }
224 blockNum--
225 }
226 total := uncleBlock.EthBlockNum - blockNum
227 log.Debugw("Discarding blocks", "total", total, "from", uncleBlock.EthBlockNum, "to", blockNum+1)
228
229 // Set History DB and State DB to the correct state
230 err := s.historyDB.Reorg(block.EthBlockNum)
231 if err != nil {
232 return 0, err
233 }
234
235 batchNum, err := s.historyDB.GetLastBatchNum()
236 if err != nil && err != sql.ErrNoRows {
237 return 0, err
238 }
239 if batchNum != 0 {
240 err = s.stateDB.Reset(batchNum)
241 if err != nil {
242 return 0, err
243 }
244 }
245
246 return block.EthBlockNum, nil
247}
248
249// TODO: Figure out who will use the Status output, and only return what's strictly need
250/*
251// Status returns current status values from the Synchronizer
252func (s *Synchronizer) Status() (*common.SyncStatus, error) {
253 // Avoid possible inconsistencies
254 s.mux.Lock()
255 defer s.mux.Unlock()
256
257 var status *common.SyncStatus
258
259 // TODO: Join all queries to the DB into a single transaction so that
260 // we can remove the mutex locking here:
261 // - HistoryDB.GetLastBlock
262 // - HistoryDB.GetLastBatchNum
263 // - HistoryDB.GetCurrentForgerAddr
264 // - HistoryDB.GetNextForgerAddr
265
266 // Get latest block in History DB
267 lastSavedBlock, err := s.historyDB.GetLastBlock()
268 if err != nil {
269 return nil, err
270 }
271 status.CurrentBlock = lastSavedBlock.EthBlockNum
272
273 // Get latest batch in History DB
274 lastSavedBatch, err := s.historyDB.GetLastBatchNum()
275 if err != nil && err != sql.ErrNoRows {
276 return nil, err
277 }
278 status.CurrentBatch = lastSavedBatch
279
280 // Get latest blockNum in blockchain
281 latestBlockNum, err := s.ethClient.EthCurrentBlock()
282 if err != nil {
283 return nil, err
284 }
285
286 // TODO: Get CurrentForgerAddr & NextForgerAddr from the Auction SC / Or from the HistoryDB
287
288 // Check if Synchronizer is synchronized
289 status.Synchronized = status.CurrentBlock == latestBlockNum
290 return status, nil
291}
292*/
293
294// rollupSync retreives all the Rollup Smart Contract Data that happened at
295// ethBlock.blockNum with ethBlock.Hash.
296func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*rollupData, error) {
297 blockNum := ethBlock.EthBlockNum
298 var rollupData = newRollupData()
299 // var forgeL1TxsNum int64
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 // ProcessTxs updates poolL2Txs adding: Nonce (and also TokenID, but we don't use it).
397 processTxsOut, err := s.stateDB.ProcessTxs(forgeBatchArgs.FeeIdxCoordinator, l1UserTxs,
398 batchData.L1CoordinatorTxs, poolL2Txs)
399 if err != nil {
400 return nil, err
401 }
402
403 // Set batchNum in exits
404 for i := range processTxsOut.ExitInfos {
405 exit := &processTxsOut.ExitInfos[i]
406 exit.BatchNum = batchNum
407 }
408 batchData.ExitTree = processTxsOut.ExitInfos
409
410 l2Txs, err := common.PoolL2TxsToL2Txs(poolL2Txs) // TODO: This is a big uggly, find a better way
411 if err != nil {
412 return nil, err
413 }
414
415 for i := range l2Txs {
416 tx := &l2Txs[i]
417 tx.Position = position
418 tx.EthBlockNum = blockNum
419 tx.BatchNum = batchNum
420 nTx, err := common.NewL2Tx(tx)
421 if err != nil {
422 return nil, err
423 }
424
425 batchData.L2Txs = append(batchData.L2Txs, *nTx)
426 position++
427 }
428
429 for i := range processTxsOut.CreatedAccounts {
430 createdAccount := &processTxsOut.CreatedAccounts[i]
431 createdAccount.BatchNum = batchNum
432 }
433 batchData.CreatedAccounts = processTxsOut.CreatedAccounts
434
435 slotNum := int64(0)
436 if ethBlock.EthBlockNum >= s.auctionConstants.GenesisBlockNum {
437 slotNum = (ethBlock.EthBlockNum - s.auctionConstants.GenesisBlockNum) /
438 int64(s.auctionConstants.BlocksPerSlot)
439 }
440
441 // Get Batch information
442 batch := common.Batch{
443 BatchNum: batchNum,
444 EthBlockNum: blockNum,
445 ForgerAddr: *sender,
446 CollectedFees: processTxsOut.CollectedFees,
447 StateRoot: forgeBatchArgs.NewStRoot,
448 NumAccounts: len(batchData.CreatedAccounts),
449 LastIdx: forgeBatchArgs.NewLastIdx,
450 ExitRoot: forgeBatchArgs.NewExitRoot,
451 SlotNum: slotNum,
452 }
453 nextForgeL1TxsNumCpy := nextForgeL1TxsNum
454 if forgeBatchArgs.L1Batch {
455 batch.ForgeL1TxsNum = &nextForgeL1TxsNumCpy
456 batchData.L1Batch = true
457 nextForgeL1TxsNum++
458 }
459 batchData.Batch = batch
460 rollupData.batches = append(rollupData.batches, *batchData)
461 }
462
463 // Get Registered Tokens
464 for _, evtAddToken := range rollupEvents.AddToken {
465 var token common.Token
466
467 token.TokenID = common.TokenID(evtAddToken.TokenID)
468 token.EthAddr = evtAddToken.TokenAddress
469 token.EthBlockNum = blockNum
470
471 if consts, err := s.ethClient.EthERC20Consts(evtAddToken.TokenAddress); err != nil {
472 log.Warnw("Error retreiving ERC20 token constants", "addr", evtAddToken.TokenAddress)
473 token.Name = "ERC20_ETH_ERROR"
474 token.Symbol = "ERROR"
475 token.Decimals = 1
476 } else {
477 token.Name = cutStringMax(consts.Name, 20)
478 token.Symbol = cutStringMax(consts.Symbol, 10)
479 token.Decimals = consts.Decimals
480 }
481
482 rollupData.addTokens = append(rollupData.addTokens, token)
483 }
484
485 // TODO: rollupEvents.UpdateForgeL1L2BatchTimeout
486 // TODO: rollupEvents.UpdateFeeAddToken
487 // TODO: rollupEvents.WithdrawEvent
488
489 // TODO: Emergency Mechanism
490 // TODO: Variables
491 // TODO: Constants
492
493 return &rollupData, nil
494}
495
496func cutStringMax(s string, max int) string {
497 if len(s) > max {
498 return s[:max]
499 }
500 return s
501}
502
503// auctionSync gets information from the Auction Contract
504func (s *Synchronizer) auctionSync(ethBlock *common.Block) (*auctionData, error) {
505 blockNum := ethBlock.EthBlockNum
506 var auctionData = newAuctionData()
507
508 // Get auction events in the block
509 auctionEvents, blockHash, err := s.ethClient.AuctionEventsByBlock(blockNum)
510 if err != nil {
511 return nil, err
512 }
513 if *blockHash != ethBlock.Hash {
514 return nil, eth.ErrBlockHashMismatchEvent
515 }
516
517 // Get bids
518 for _, evtNewBid := range auctionEvents.NewBid {
519 bid := common.Bid{
520 SlotNum: evtNewBid.Slot,
521 BidValue: evtNewBid.BidAmount,
522 Bidder: evtNewBid.Bidder,
523 EthBlockNum: blockNum,
524 }
525 auctionData.bids = append(auctionData.bids, bid)
526 }
527
528 // Get Coordinators
529 for _, evtSetCoordinator := range auctionEvents.SetCoordinator {
530 coordinator := common.Coordinator{
531 Bidder: evtSetCoordinator.BidderAddress,
532 Forger: evtSetCoordinator.ForgerAddress,
533 URL: evtSetCoordinator.CoordinatorURL,
534 }
535 auctionData.coordinators = append(auctionData.coordinators, coordinator)
536 }
537
538 // TODO: NewSlotDeadline
539 // TODO: NewClosedAuctionSlots
540 // TODO: NewOutbidding
541 // TODO: NewDonationAddress
542 // TODO: NewBootCoordinator
543 // TODO: NewOpenAuctionSlots
544 // TODO: NewAllocationRatio
545 // TODO: NewForgeAllocated
546 // TODO: NewDefaultSlotSetBid
547 // TODO: NewForge
548 // TODO: HEZClaimed
549
550 // TODO: VARS
551 // TODO: CONSTANTS
552
553 return auctionData, nil
554}
555
556// wdelayerSync gets information from the Withdrawal Delayer Contract
557func (s *Synchronizer) wdelayerSync(ethBlock *common.Block) (*wdelayerData, error) {
558 // blockNum := ethBlock.EthBlockNum
559 // TODO: VARS
560 // TODO: CONSTANTS
561
562 return &wdelayerData{
563 vars: nil,
564 }, nil
565}
566
567// func (s *Synchronizer) getIdx(rollupEvents *eth.RollupEvents) (int64, error) {
568// // TODO: FIXME: There will be an error here when `len(rollupEvents.ForgeBatch) == 0`
569// lastForgeBatch := rollupEvents.ForgeBatch[len(rollupEvents.ForgeBatch)-1]
570//
571// // TODO: RollupForgeBatchArgs is already called in `rollupSync`.
572// // Ideally it should not need to be called twice for the same batch.
573// // Get the input for forgeBatch
574// forgeBatchArgs, err := s.ethClient.RollupForgeBatchArgs(lastForgeBatch.EthTxHash)
575// if err != nil {
576// return 0, err
577// }
578//
579// return forgeBatchArgs.NewLastIdx + 1, nil
580// }
581
582func getL1UserTx(eventsL1UserTx []eth.RollupEventL1UserTx, blockNum int64) ([]common.L1Tx, error) {
583 l1Txs := make([]common.L1Tx, len(eventsL1UserTx))
584 for i := range eventsL1UserTx {
585 eventsL1UserTx[i].L1UserTx.EthBlockNum = blockNum
586 // Check validity of L1UserTx
587 l1Tx, err := common.NewL1Tx(&eventsL1UserTx[i].L1UserTx)
588 if err != nil {
589 return nil, err
590 }
591 l1Txs[i] = *l1Tx
592 }
593 return l1Txs, nil
594}