arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/coordinator/coordinator.go
coordinator/coordinator.goRaw
  1package coordinator
  2
  3import (
  4	"context"
  5	"fmt"
  6	"math/big"
  7	"os"
  8	"sync"
  9	"time"
 10
 11	ethCommon "github.com/ethereum/go-ethereum/common"
 12	"github.com/hermeznetwork/hermez-node/batchbuilder"
 13	"github.com/hermeznetwork/hermez-node/common"
 14	"github.com/hermeznetwork/hermez-node/config"
 15	"github.com/hermeznetwork/hermez-node/db/historydb"
 16	"github.com/hermeznetwork/hermez-node/db/l2db"
 17	"github.com/hermeznetwork/hermez-node/eth"
 18	"github.com/hermeznetwork/hermez-node/log"
 19	"github.com/hermeznetwork/hermez-node/prover"
 20	"github.com/hermeznetwork/hermez-node/synchronizer"
 21	"github.com/hermeznetwork/hermez-node/txprocessor"
 22	"github.com/hermeznetwork/hermez-node/txselector"
 23	"github.com/hermeznetwork/tracerr"
 24)
 25
 26var (
 27	errLastL1BatchNotSynced  = fmt.Errorf("last L1Batch not synced yet")
 28	errForgeNoTxsBeforeDelay = fmt.Errorf("no txs to forge and we haven't reached the forge no txs delay")
 29	errForgeBeforeDelay      = fmt.Errorf("we haven't reached the forge delay")
 30)
 31
 32const (
 33	queueLen         = 16
 34	longWaitDuration = 999 * time.Hour
 35	zeroDuration     = 0 * time.Second
 36)
 37
 38// Config contains the Coordinator configuration
 39type Config struct {
 40	// ForgerAddress is the address under which this coordinator is forging
 41	ForgerAddress ethCommon.Address
 42	// ConfirmBlocks is the number of confirmation blocks to wait for sent
 43	// ethereum transactions before forgetting about them
 44	ConfirmBlocks int64
 45	// L1BatchTimeoutPerc is the portion of the range before the L1Batch
 46	// timeout that will trigger a schedule to forge an L1Batch
 47	L1BatchTimeoutPerc float64
 48	// StartSlotBlocksDelay is the number of blocks of delay to wait before
 49	// starting the pipeline when we reach a slot in which we can forge.
 50	StartSlotBlocksDelay int64
 51	// ScheduleBatchBlocksAheadCheck is the number of blocks ahead in which
 52	// the forger address is checked to be allowed to forge (apart from
 53	// checking the next block), used to decide when to stop scheduling new
 54	// batches (by stopping the pipeline).
 55	// For example, if we are at block 10 and ScheduleBatchBlocksAheadCheck
 56	// is 5, eventhough at block 11 we canForge, the pipeline will be
 57	// stopped if we can't forge at block 15.
 58	// This value should be the expected number of blocks it takes between
 59	// scheduling a batch and having it mined.
 60	ScheduleBatchBlocksAheadCheck int64
 61	// SendBatchBlocksMarginCheck is the number of margin blocks ahead in
 62	// which the coordinator is also checked to be allowed to forge, apart
 63	// from the next block; used to decide when to stop sending batches to
 64	// the smart contract.
 65	// For example, if we are at block 10 and SendBatchBlocksMarginCheck is
 66	// 5, eventhough at block 11 we canForge, the batch will be discarded
 67	// if we can't forge at block 15.
 68	// This value should be the expected number of blocks it takes between
 69	// sending a batch and having it mined.
 70	SendBatchBlocksMarginCheck int64
 71	// EthClientAttempts is the number of attempts to do an eth client RPC
 72	// call before giving up
 73	EthClientAttempts int
 74	// ForgeRetryInterval is the waiting interval between calls forge a
 75	// batch after an error
 76	ForgeRetryInterval time.Duration
 77	// ForgeDelay is the delay after which a batch is forged if the slot is
 78	// already committed.  If set to 0s, the coordinator will continuously
 79	// forge at the maximum rate.
 80	ForgeDelay time.Duration
 81	// ForgeNoTxsDelay is the delay after which a batch is forged even if
 82	// there are no txs to forge if the slot is already committed.  If set
 83	// to 0s, the coordinator will continuously forge even if the batches
 84	// are empty.
 85	ForgeNoTxsDelay time.Duration
 86	// SyncRetryInterval is the waiting interval between calls to the main
 87	// handler of a synced block after an error
 88	SyncRetryInterval time.Duration
 89	// PurgeByExtDelInterval is the waiting interval between calls
 90	// to the PurgeByExternalDelete function of the l2db which deletes
 91	// pending txs externally marked by the column `external_delete`
 92	PurgeByExtDelInterval time.Duration
 93	// EthClientAttemptsDelay is delay between attempts do do an eth client
 94	// RPC call
 95	EthClientAttemptsDelay time.Duration
 96	// EthTxResendTimeout is the timeout after which a non-mined ethereum
 97	// transaction will be resent (reusing the nonce) with a newly
 98	// calculated gas price
 99	EthTxResendTimeout time.Duration
100	// EthNoReuseNonce disables reusing nonces of pending transactions for
101	// new replacement transactions
102	EthNoReuseNonce bool
103	// MaxGasPrice is the maximum gas price allowed for ethereum
104	// transactions
105	MaxGasPrice *big.Int
106	// GasPriceIncPerc is the percentage increase of gas price set in an
107	// ethereum transaction from the suggested gas price by the ehtereum
108	// node
109	GasPriceIncPerc int64
110	// TxManagerCheckInterval is the waiting interval between receipt
111	// checks of ethereum transactions in the TxManager
112	TxManagerCheckInterval time.Duration
113	// DebugBatchPath if set, specifies the path where batchInfo is stored
114	// in JSON in every step/update of the pipeline
115	DebugBatchPath string
116	Purger         PurgerCfg
117	// VerifierIdx is the index of the verifier contract registered in the
118	// smart contract
119	VerifierIdx uint8
120	// ForgeBatchGasCost contains the cost of each action in the
121	// ForgeBatch transaction.
122	ForgeBatchGasCost config.ForgeBatchGasCost
123	TxProcessorConfig txprocessor.Config
124}
125
126func (c *Config) debugBatchStore(batchInfo *BatchInfo) {
127	if c.DebugBatchPath != "" {
128		if err := batchInfo.DebugStore(c.DebugBatchPath); err != nil {
129			log.Warnw("Error storing debug BatchInfo",
130				"path", c.DebugBatchPath, "err", err)
131		}
132	}
133}
134
135type fromBatch struct {
136	BatchNum   common.BatchNum
137	ForgerAddr ethCommon.Address
138	StateRoot  *big.Int
139}
140
141// Coordinator implements the Coordinator type
142type Coordinator struct {
143	// State
144	pipelineNum       int       // Pipeline sequential number.  The first pipeline is 1
145	pipelineFromBatch fromBatch // batch from which we started the pipeline
146	provers           []prover.Client
147	consts            synchronizer.SCConsts
148	vars              synchronizer.SCVariables
149	stats             synchronizer.Stats
150	started           bool
151
152	cfg Config
153
154	historyDB    *historydb.HistoryDB
155	l2DB         *l2db.L2DB
156	txSelector   *txselector.TxSelector
157	batchBuilder *batchbuilder.BatchBuilder
158
159	msgCh  chan interface{}
160	ctx    context.Context
161	wg     sync.WaitGroup
162	cancel context.CancelFunc
163
164	// mutexL2DBUpdateDelete protects updates to the L2DB so that
165	// these two processes always happen exclusively:
166	// - Pipeline taking pending txs, running through the TxProcessor and
167	//   marking selected txs as forging
168	// - Coordinator deleting pending txs that have been marked with
169	//   `external_delete`.
170	// Without this mutex, the coordinator could delete a pending txs that
171	// has just been selected by the TxProcessor in the pipeline.
172	mutexL2DBUpdateDelete sync.Mutex
173	pipeline              *Pipeline
174	lastNonFailedBatchNum common.BatchNum
175
176	purger    *Purger
177	txManager *TxManager
178}
179
180// NewCoordinator creates a new Coordinator
181func NewCoordinator(cfg Config,
182	historyDB *historydb.HistoryDB,
183	l2DB *l2db.L2DB,
184	txSelector *txselector.TxSelector,
185	batchBuilder *batchbuilder.BatchBuilder,
186	serverProofs []prover.Client,
187	ethClient eth.ClientInterface,
188	scConsts *synchronizer.SCConsts,
189	initSCVars *synchronizer.SCVariables,
190) (*Coordinator, error) {
191	// nolint reason: hardcoded `1.0`, by design the percentage can't be over 100%
192	if cfg.L1BatchTimeoutPerc >= 1.0 { //nolint:gomnd
193		return nil, tracerr.Wrap(fmt.Errorf("invalid value for Config.L1BatchTimeoutPerc (%v >= 1.0)",
194			cfg.L1BatchTimeoutPerc))
195	}
196	if cfg.EthClientAttempts < 1 {
197		return nil, tracerr.Wrap(fmt.Errorf("invalid value for Config.EthClientAttempts (%v < 1)",
198			cfg.EthClientAttempts))
199	}
200
201	if cfg.DebugBatchPath != "" {
202		if err := os.MkdirAll(cfg.DebugBatchPath, 0744); err != nil {
203			return nil, tracerr.Wrap(err)
204		}
205	}
206
207	purger := Purger{
208		cfg:                 cfg.Purger,
209		lastPurgeBlock:      0,
210		lastPurgeBatch:      0,
211		lastInvalidateBlock: 0,
212		lastInvalidateBatch: 0,
213	}
214
215	ctx, cancel := context.WithCancel(context.Background())
216	c := Coordinator{
217		pipelineNum: 0,
218		pipelineFromBatch: fromBatch{
219			BatchNum:   0,
220			ForgerAddr: ethCommon.Address{},
221			StateRoot:  big.NewInt(0),
222		},
223		provers: serverProofs,
224		consts:  *scConsts,
225		vars:    *initSCVars,
226
227		cfg: cfg,
228
229		historyDB:    historyDB,
230		l2DB:         l2DB,
231		txSelector:   txSelector,
232		batchBuilder: batchBuilder,
233
234		purger: &purger,
235
236		msgCh: make(chan interface{}),
237		ctx:   ctx,
238		// wg
239		cancel: cancel,
240	}
241	ctxTimeout, ctxTimeoutCancel := context.WithTimeout(ctx, 1*time.Second)
242	defer ctxTimeoutCancel()
243	txManager, err := NewTxManager(ctxTimeout, &cfg, ethClient, l2DB, &c,
244		scConsts, initSCVars)
245	if err != nil {
246		return nil, tracerr.Wrap(err)
247	}
248	c.txManager = txManager
249	// Set Eth LastBlockNum to -1 in stats so that stats.Synced() is
250	// guaranteed to return false before it's updated with a real stats
251	c.stats.Eth.LastBlock.Num = -1
252	return &c, nil
253}
254
255// TxSelector returns the inner TxSelector
256func (c *Coordinator) TxSelector() *txselector.TxSelector {
257	return c.txSelector
258}
259
260// BatchBuilder returns the inner BatchBuilder
261func (c *Coordinator) BatchBuilder() *batchbuilder.BatchBuilder {
262	return c.batchBuilder
263}
264
265func (c *Coordinator) newPipeline(ctx context.Context) (*Pipeline, error) {
266	c.pipelineNum++
267	return NewPipeline(ctx, c.cfg, c.pipelineNum, c.historyDB, c.l2DB, c.txSelector,
268		c.batchBuilder, &c.mutexL2DBUpdateDelete, c.purger, c, c.txManager,
269		c.provers, &c.consts)
270}
271
272// MsgSyncBlock indicates an update to the Synchronizer stats
273type MsgSyncBlock struct {
274	Stats   synchronizer.Stats
275	Batches []common.BatchData
276	// Vars contains each Smart Contract variables if they are updated, or
277	// nil if they haven't changed.
278	Vars synchronizer.SCVariablesPtr
279}
280
281// MsgSyncReorg indicates a reorg
282type MsgSyncReorg struct {
283	Stats synchronizer.Stats
284	Vars  synchronizer.SCVariablesPtr
285}
286
287// MsgStopPipeline indicates a signal to reset the pipeline
288type MsgStopPipeline struct {
289	Reason string
290	// FailedBatchNum indicates the first batchNum that failed in the
291	// pipeline.  If FailedBatchNum is 0, it should be ignored.
292	FailedBatchNum common.BatchNum
293}
294
295// SendMsg is a thread safe method to pass a message to the Coordinator
296func (c *Coordinator) SendMsg(ctx context.Context, msg interface{}) {
297	select {
298	case c.msgCh <- msg:
299	case <-ctx.Done():
300	}
301}
302
303func updateSCVars(vars *synchronizer.SCVariables, update synchronizer.SCVariablesPtr) {
304	if update.Rollup != nil {
305		vars.Rollup = *update.Rollup
306	}
307	if update.Auction != nil {
308		vars.Auction = *update.Auction
309	}
310	if update.WDelayer != nil {
311		vars.WDelayer = *update.WDelayer
312	}
313}
314
315func (c *Coordinator) syncSCVars(vars synchronizer.SCVariablesPtr) {
316	updateSCVars(&c.vars, vars)
317}
318
319func canForge(auctionConstants *common.AuctionConstants, auctionVars *common.AuctionVariables,
320	currentSlot *common.Slot, nextSlot *common.Slot, addr ethCommon.Address, blockNum int64) bool {
321	if blockNum < auctionConstants.GenesisBlockNum {
322		log.Infow("canForge: requested blockNum is < genesis", "blockNum", blockNum,
323			"genesis", auctionConstants.GenesisBlockNum)
324		return false
325	}
326	var slot *common.Slot
327	if currentSlot.StartBlock <= blockNum && blockNum <= currentSlot.EndBlock {
328		slot = currentSlot
329	} else if nextSlot.StartBlock <= blockNum && blockNum <= nextSlot.EndBlock {
330		slot = nextSlot
331	} else {
332		log.Warnw("canForge: requested blockNum is outside current and next slot",
333			"blockNum", blockNum, "currentSlot", currentSlot,
334			"nextSlot", nextSlot,
335		)
336		return false
337	}
338	anyoneForge := false
339	if !slot.ForgerCommitment &&
340		auctionConstants.RelativeBlock(blockNum) >= int64(auctionVars.SlotDeadline) {
341		log.Debugw("canForge: anyone can forge in the current slot (slotDeadline passed)",
342			"block", blockNum)
343		anyoneForge = true
344	}
345	if slot.Forger == addr || anyoneForge {
346		return true
347	}
348	log.Debugw("canForge: can't forge", "slot.Forger", slot.Forger)
349	return false
350}
351
352func (c *Coordinator) canForgeAt(blockNum int64) bool {
353	return canForge(&c.consts.Auction, &c.vars.Auction,
354		&c.stats.Sync.Auction.CurrentSlot, &c.stats.Sync.Auction.NextSlot,
355		c.cfg.ForgerAddress, blockNum)
356}
357
358func (c *Coordinator) canForge() bool {
359	blockNum := c.stats.Eth.LastBlock.Num + 1
360	return canForge(&c.consts.Auction, &c.vars.Auction,
361		&c.stats.Sync.Auction.CurrentSlot, &c.stats.Sync.Auction.NextSlot,
362		c.cfg.ForgerAddress, blockNum)
363}
364
365func (c *Coordinator) syncStats(ctx context.Context, stats *synchronizer.Stats) error {
366	nextBlock := c.stats.Eth.LastBlock.Num + 1
367	canForge := c.canForgeAt(nextBlock)
368	if c.cfg.ScheduleBatchBlocksAheadCheck != 0 && canForge {
369		canForge = c.canForgeAt(nextBlock + c.cfg.ScheduleBatchBlocksAheadCheck)
370	}
371	if c.pipeline == nil {
372		relativeBlock := c.consts.Auction.RelativeBlock(nextBlock)
373		if canForge && relativeBlock < c.cfg.StartSlotBlocksDelay {
374			log.Debugf("Coordinator: delaying pipeline start due to "+
375				"relativeBlock (%v) < cfg.StartSlotBlocksDelay (%v)",
376				relativeBlock, c.cfg.StartSlotBlocksDelay)
377		} else if canForge {
378			log.Infow("Coordinator: forging state begin", "block",
379				stats.Eth.LastBlock.Num+1, "batch", stats.Sync.LastBatch.BatchNum)
380			fromBatch := fromBatch{
381				BatchNum:   stats.Sync.LastBatch.BatchNum,
382				ForgerAddr: stats.Sync.LastBatch.ForgerAddr,
383				StateRoot:  stats.Sync.LastBatch.StateRoot,
384			}
385			if c.lastNonFailedBatchNum > fromBatch.BatchNum {
386				fromBatch.BatchNum = c.lastNonFailedBatchNum
387				fromBatch.ForgerAddr = c.cfg.ForgerAddress
388				fromBatch.StateRoot = big.NewInt(0)
389			}
390			var err error
391			if c.pipeline, err = c.newPipeline(ctx); err != nil {
392				return tracerr.Wrap(err)
393			}
394			c.pipelineFromBatch = fromBatch
395			if err := c.pipeline.Start(fromBatch.BatchNum, stats, &c.vars); err != nil {
396				c.pipeline = nil
397				return tracerr.Wrap(err)
398			}
399		}
400	} else {
401		if !canForge {
402			log.Infow("Coordinator: forging state end", "block", stats.Eth.LastBlock.Num+1)
403			c.pipeline.Stop(c.ctx)
404			c.pipeline = nil
405		}
406	}
407	if c.pipeline == nil {
408		if _, err := c.purger.InvalidateMaybe(c.l2DB, c.txSelector.LocalAccountsDB(),
409			stats.Sync.LastBlock.Num, int64(stats.Sync.LastBatch.BatchNum)); err != nil {
410			return tracerr.Wrap(err)
411		}
412		if _, err := c.purger.PurgeMaybe(c.l2DB, stats.Sync.LastBlock.Num,
413			int64(stats.Sync.LastBatch.BatchNum)); err != nil {
414			return tracerr.Wrap(err)
415		}
416	}
417	return nil
418}
419
420func (c *Coordinator) handleMsgSyncBlock(ctx context.Context, msg *MsgSyncBlock) error {
421	c.stats = msg.Stats
422	c.syncSCVars(msg.Vars)
423	c.txManager.SetSyncStatsVars(ctx, &msg.Stats, &msg.Vars)
424	if c.pipeline != nil {
425		c.pipeline.SetSyncStatsVars(ctx, &msg.Stats, &msg.Vars)
426	}
427	if !c.stats.Synced() {
428		return nil
429	}
430	return c.syncStats(ctx, &c.stats)
431}
432
433func (c *Coordinator) handleReorg(ctx context.Context, msg *MsgSyncReorg) error {
434	c.stats = msg.Stats
435	c.syncSCVars(msg.Vars)
436	c.txManager.SetSyncStatsVars(ctx, &msg.Stats, &msg.Vars)
437	if c.pipeline != nil {
438		c.pipeline.SetSyncStatsVars(ctx, &msg.Stats, &msg.Vars)
439	}
440	if c.stats.Sync.LastBatch.ForgerAddr != c.cfg.ForgerAddress &&
441		(c.stats.Sync.LastBatch.StateRoot == nil || c.pipelineFromBatch.StateRoot == nil ||
442			c.stats.Sync.LastBatch.StateRoot.Cmp(c.pipelineFromBatch.StateRoot) != 0) {
443		// There's been a reorg and the batch state root from which the
444		// pipeline was started has changed (probably because it was in
445		// a block that was discarded), and it was sent by a different
446		// coordinator than us.  That batch may never be in the main
447		// chain, so we stop the pipeline  (it will be started again
448		// once the node is in sync).
449		log.Infow("Coordinator.handleReorg StopPipeline sync.LastBatch.ForgerAddr != cfg.ForgerAddr "+
450			"& sync.LastBatch.StateRoot != pipelineFromBatch.StateRoot",
451			"sync.LastBatch.StateRoot", c.stats.Sync.LastBatch.StateRoot,
452			"pipelineFromBatch.StateRoot", c.pipelineFromBatch.StateRoot)
453		c.txManager.DiscardPipeline(ctx, c.pipelineNum)
454		if err := c.handleStopPipeline(ctx, "reorg", 0); err != nil {
455			return tracerr.Wrap(err)
456		}
457	}
458	return nil
459}
460
461// handleStopPipeline handles stopping the pipeline.  If failedBatchNum is 0,
462// the next pipeline will start from the last state of the synchronizer,
463// otherwise, it will state from failedBatchNum-1.
464func (c *Coordinator) handleStopPipeline(ctx context.Context, reason string, failedBatchNum common.BatchNum) error {
465	batchNum := c.stats.Sync.LastBatch.BatchNum
466	if failedBatchNum != 0 {
467		batchNum = failedBatchNum - 1
468	}
469	if c.pipeline != nil {
470		c.pipeline.Stop(c.ctx)
471		c.pipeline = nil
472	}
473	if err := c.l2DB.Reorg(batchNum); err != nil {
474		return tracerr.Wrap(err)
475	}
476	c.lastNonFailedBatchNum = batchNum
477	return nil
478}
479
480func (c *Coordinator) handleMsg(ctx context.Context, msg interface{}) error {
481	switch msg := msg.(type) {
482	case MsgSyncBlock:
483		if err := c.handleMsgSyncBlock(ctx, &msg); err != nil {
484			return tracerr.Wrap(fmt.Errorf("Coordinator.handleMsgSyncBlock error: %w", err))
485		}
486	case MsgSyncReorg:
487		if err := c.handleReorg(ctx, &msg); err != nil {
488			return tracerr.Wrap(fmt.Errorf("Coordinator.handleReorg error: %w", err))
489		}
490	case MsgStopPipeline:
491		log.Infow("Coordinator received MsgStopPipeline", "reason", msg.Reason)
492		if err := c.handleStopPipeline(ctx, msg.Reason, msg.FailedBatchNum); err != nil {
493			return tracerr.Wrap(fmt.Errorf("Coordinator.handleStopPipeline: %w", err))
494		}
495	default:
496		log.Fatalw("Coordinator Unexpected Coordinator msg of type %T: %+v", msg, msg)
497	}
498	return nil
499}
500
501// Start the coordinator
502func (c *Coordinator) Start() {
503	if c.started {
504		log.Fatal("Coordinator already started")
505	}
506	c.started = true
507	c.wg.Add(1)
508	go func() {
509		c.txManager.Run(c.ctx)
510		c.wg.Done()
511	}()
512
513	c.wg.Add(1)
514	go func() {
515		timer := time.NewTimer(longWaitDuration)
516		for {
517			select {
518			case <-c.ctx.Done():
519				log.Info("Coordinator done")
520				c.wg.Done()
521				return
522			case msg := <-c.msgCh:
523				if err := c.handleMsg(c.ctx, msg); c.ctx.Err() != nil {
524					continue
525				} else if err != nil {
526					log.Errorw("Coordinator.handleMsg", "err", err)
527					if !timer.Stop() {
528						<-timer.C
529					}
530					timer.Reset(c.cfg.SyncRetryInterval)
531					continue
532				}
533			case <-timer.C:
534				timer.Reset(longWaitDuration)
535				if !c.stats.Synced() {
536					continue
537				}
538				if err := c.syncStats(c.ctx, &c.stats); c.ctx.Err() != nil {
539					continue
540				} else if err != nil {
541					log.Errorw("Coordinator.syncStats", "err", err)
542					if !timer.Stop() {
543						<-timer.C
544					}
545					timer.Reset(c.cfg.SyncRetryInterval)
546					continue
547				}
548			}
549		}
550	}()
551
552	c.wg.Add(1)
553	go func() {
554		for {
555			select {
556			case <-c.ctx.Done():
557				log.Info("Coordinator L2DB.PurgeByExternalDelete loop done")
558				c.wg.Done()
559				return
560			case <-time.After(c.cfg.PurgeByExtDelInterval):
561				c.mutexL2DBUpdateDelete.Lock()
562				if err := c.l2DB.PurgeByExternalDelete(); err != nil {
563					log.Errorw("L2DB.PurgeByExternalDelete", "err", err)
564				}
565				c.mutexL2DBUpdateDelete.Unlock()
566			}
567		}
568	}()
569}
570
571const stopCtxTimeout = 200 * time.Millisecond
572
573// Stop the coordinator
574func (c *Coordinator) Stop() {
575	if !c.started {
576		log.Fatal("Coordinator already stopped")
577	}
578	c.started = false
579	log.Infow("Stopping Coordinator...")
580	c.cancel()
581	c.wg.Wait()
582	if c.pipeline != nil {
583		ctx, cancel := context.WithTimeout(context.Background(), stopCtxTimeout)
584		defer cancel()
585		c.pipeline.Stop(ctx)
586		c.pipeline = nil
587	}
588}