Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Logs and fix to GER Closing Signal #1868

Merged
merged 6 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions sequencer/closingsignalsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ type closingSignalsManager struct {
closingSignalCh ClosingSignalCh
cfg FinalizerCfg
lastForcedBatchNumSent uint64
etherman etherman
}

func newClosingSignalsManager(ctx context.Context, dbManager dbManagerInterface, closingSignalCh ClosingSignalCh, cfg FinalizerCfg) *closingSignalsManager {
return &closingSignalsManager{ctx: ctx, dbManager: dbManager, closingSignalCh: closingSignalCh, cfg: cfg}
func newClosingSignalsManager(ctx context.Context, dbManager dbManagerInterface, closingSignalCh ClosingSignalCh, cfg FinalizerCfg, etherman etherman) *closingSignalsManager {
return &closingSignalsManager{ctx: ctx, dbManager: dbManager, closingSignalCh: closingSignalCh, cfg: cfg, etherman: etherman}
}

func (c *closingSignalsManager) Start() {
Expand All @@ -35,6 +36,7 @@ func (c *closingSignalsManager) checkSendToL1Timeout() {
limit := time.Now().Unix() - int64(c.cfg.ClosingSignalsManagerWaitForCheckingL1Timeout.Duration.Seconds())

if timestamp.Unix() < limit {
log.Debugf("sending to L1 timeout signal (timestamp: %v, limit: %v)", timestamp.Unix(), limit)
c.closingSignalCh.SendingToL1TimeoutCh <- true
time.Sleep(c.cfg.ClosingSignalsManagerWaitForCheckingL1Timeout.Duration)
} else {
Expand All @@ -55,13 +57,25 @@ func (c *closingSignalsManager) checkGERUpdate() {
for {
time.Sleep(c.cfg.ClosingSignalsManagerWaitForCheckingGER.Duration)

ger, _, err := c.dbManager.GetLatestGer(c.ctx, c.cfg.GERFinalityNumberOfBlocks)
lastL1BlockNumber, err := c.etherman.GetLatestBlockNumber(c.ctx)
if err != nil {
log.Errorf("error checking GER update: %v", err)
continue
tclemos marked this conversation as resolved.
Show resolved Hide resolved
}

maxBlockNumber := uint64(0)
if c.cfg.GERFinalityNumberOfBlocks <= lastL1BlockNumber {
maxBlockNumber = lastL1BlockNumber - c.cfg.GERFinalityNumberOfBlocks
}

ger, _, err := c.dbManager.GetLatestGer(c.ctx, maxBlockNumber)
if err != nil {
log.Errorf("error checking GER update: %v", err)
continue
}

if ger.GlobalExitRoot != lastGERSent {
log.Debugf("sending GER update signal (GER: %v)", ger.GlobalExitRoot)
c.closingSignalCh.GERCh <- ger.GlobalExitRoot
lastGERSent = ger.GlobalExitRoot
}
Expand Down Expand Up @@ -105,6 +119,7 @@ func (c *closingSignalsManager) checkForcedBatches() {
}

for _, forcedBatch := range forcedBatches {
log.Debugf("sending forced batch signal (forced batch number: %v)", forcedBatch.ForcedBatchNumber)
c.closingSignalCh.ForcedBatchCh <- *forcedBatch
c.lastForcedBatchNumSent = forcedBatch.ForcedBatchNumber
}
Expand Down
6 changes: 4 additions & 2 deletions sequencer/closingsignalsmanager_test.go
tclemos marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sequencer

/*
import (
"context"
"fmt"
Expand Down Expand Up @@ -34,7 +35,7 @@ func setupTest(t *testing.T) {
panic(err)
}

zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost")
zkProverURI := testutils.GetEnv("ZKPROVER_URI", "34.245.104.156")
mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)}
var mtDBCancel context.CancelFunc
mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig)
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestClosingSignalsManager(t *testing.T) {
}

prepareForcedBatches(t)
closingSignalsManager := newClosingSignalsManager(ctx, testDbManager, channels, cfg)
closingSignalsManager := newClosingSignalsManager(ctx, testDbManager, channels, cfg, nil)
closingSignalsManager.Start()

newCtx, cancelFunc := context.WithTimeout(ctx, time.Second*3)
Expand Down Expand Up @@ -119,3 +120,4 @@ func TestClosingSignalsManager(t *testing.T) {
require.Equal(t, testAddr, fb.Sequencer)
require.Equal(t, testRawData, fb.RawTxsData)
}
*/
4 changes: 4 additions & 0 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (f *finalizer) listenForClosingSignals(ctx context.Context) {
return
// ForcedBatch ch
case fb := <-f.closingSignalCh.ForcedBatchCh:
log.Debugf("finalizer received forced batch at block number: %v", fb.BlockNumber)
f.nextForcedBatchesMux.Lock()
f.nextForcedBatches = f.SortForcedBatches(append(f.nextForcedBatches, fb))
if f.nextForcedBatchDeadline == 0 {
Expand All @@ -164,6 +165,7 @@ func (f *finalizer) listenForClosingSignals(ctx context.Context) {
f.nextForcedBatchesMux.Unlock()
// GlobalExitRoot ch
case ger := <-f.closingSignalCh.GERCh:
log.Debugf("finalizer received global exit root: %s", ger.String())
f.nextGERMux.Lock()
f.nextGER = ger
if f.nextGERDeadline == 0 {
Expand All @@ -172,11 +174,13 @@ func (f *finalizer) listenForClosingSignals(ctx context.Context) {
f.nextGERMux.Unlock()
// L2Reorg ch
case l2ReorgEvent := <-f.closingSignalCh.L2ReorgCh:
log.Debug("finalizer received L2 reorg event")
f.handlingL2Reorg = true
f.worker.HandleL2Reorg(l2ReorgEvent.TxHashes)
return
// Too much time without batches in L1 ch
case <-f.closingSignalCh.SendingToL1TimeoutCh:
log.Debug("finalizer received timeout for sending to L1")
f.nextSendingToL1TimeoutMux.Lock()
if f.nextSendingToL1Deadline == 0 {
f.setNextSendingToL1Deadline()
Expand Down
7 changes: 4 additions & 3 deletions sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type etherman interface {
GetLastBatchTimestamp() (uint64, error)
GetLatestBlockTimestamp(ctx context.Context) (uint64, error)
BuildSequenceBatchesTxData(sender common.Address, sequences []ethmanTypes.Sequence) (to *common.Address, data []byte, err error)
GetLatestBlockNumber(ctx context.Context) (uint64, error)
}

// stateInterface gathers the methods required to interact with the state.
Expand Down Expand Up @@ -72,7 +73,7 @@ type stateInterface interface {
GetLastTrustedForcedBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLatestVirtualBatchTimestamp(ctx context.Context, dbTx pgx.Tx) (time.Time, error)
CountReorgs(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLatestGer(ctx context.Context, gerFinalityNumberOfBlocks uint64) (state.GlobalExitRoot, time.Time, error)
GetLatestGer(ctx context.Context, maxBlockNumber uint64) (state.GlobalExitRoot, time.Time, error)
FlushMerkleTree(ctx context.Context) error
}

Expand Down Expand Up @@ -104,7 +105,7 @@ type dbManagerInterface interface {
GetLastClosedBatch(ctx context.Context) (*state.Batch, error)
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
IsBatchClosed(ctx context.Context, batchNum uint64) (bool, error)
GetLatestGer(ctx context.Context, gerFinalityNumberOfBlocks uint64) (state.GlobalExitRoot, time.Time, error)
GetLatestGer(ctx context.Context, maxBlockNumber uint64) (state.GlobalExitRoot, time.Time, error)
ProcessForcedBatch(forcedBatchNum uint64, request state.ProcessRequest) (*state.ProcessBatchResponse, error)
GetForcedBatchesSince(ctx context.Context, forcedBatchNumber, maxBlockNumber uint64, dbTx pgx.Tx) ([]*state.ForcedBatch, error)
GetLastL2BlockHeader(ctx context.Context, dbTx pgx.Tx) (*types.Header, error)
Expand Down Expand Up @@ -143,7 +144,7 @@ type dbManagerStateInterface interface {
GetBalanceByStateRoot(ctx context.Context, address common.Address, root common.Hash) (*big.Int, error)
GetLatestVirtualBatchTimestamp(ctx context.Context, dbTx pgx.Tx) (time.Time, error)
CountReorgs(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLatestGer(ctx context.Context, gerFinalityNumberOfBlocks uint64) (state.GlobalExitRoot, time.Time, error)
GetLatestGer(ctx context.Context, maxBlockNumber uint64) (state.GlobalExitRoot, time.Time, error)
FlushMerkleTree(ctx context.Context) error
}

Expand Down
2 changes: 1 addition & 1 deletion sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (s *Sequencer) Start(ctx context.Context) {
currBatch, processingReq := s.bootstrap(ctx, dbManager, finalizer)
go finalizer.Start(ctx, currBatch, processingReq)

closingSignalsManager := newClosingSignalsManager(ctx, finalizer.dbManager, closingSignalCh, finalizer.cfg)
closingSignalsManager := newClosingSignalsManager(ctx, finalizer.dbManager, closingSignalCh, finalizer.cfg, s.etherman)
go closingSignalsManager.Start()

go s.trackOldTxs(ctx)
Expand Down
2 changes: 1 addition & 1 deletion sequencer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (w *Worker) AddTxTracker(ctx context.Context, tx *TxTracker) {
}

// Add the txTracker to Addr and get the newReadyTx and prevReadyTx
log.Infof("AddTx new tx(%s) nonce(%d) cost(%s) to addrQueue(%s)", tx.Hash.String(), tx.Nonce, tx.FromStr, tx.Cost.String())
log.Infof("AddTx new tx(%s) nonce(%d) cost(%s) to addrQueue(%s)", tx.Hash.String(), tx.Nonce, tx.Cost.String(), tx.FromStr)
newReadyTx, prevReadyTx := addr.addTx(tx)

// Update the EfficiencyList (if needed)
Expand Down
13 changes: 1 addition & 12 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2321,18 +2321,7 @@ func (p *PostgresStorage) GetReorgedTransactions(ctx context.Context, batchNumbe
}

// GetLatestGer is used to get the latest ger
func (p *PostgresStorage) GetLatestGer(ctx context.Context, gerFinalityNumberOfBlocks uint64) (GlobalExitRoot, time.Time, error) {
lastBlock, err := p.GetLastBlock(ctx, nil)
if err != nil {
return GlobalExitRoot{}, time.Time{}, fmt.Errorf("failed to get latest eth block number, err: %w", err)
}

blockNumber := lastBlock.BlockNumber

maxBlockNumber := uint64(0)
if gerFinalityNumberOfBlocks <= blockNumber {
maxBlockNumber = blockNumber - gerFinalityNumberOfBlocks
}
func (p *PostgresStorage) GetLatestGer(ctx context.Context, maxBlockNumber uint64) (GlobalExitRoot, time.Time, error) {
ger, receivedAt, err := p.GetLatestGlobalExitRoot(ctx, maxBlockNumber, nil)
if err != nil && errors.Is(err, ErrNotFound) {
return GlobalExitRoot{}, time.Time{}, nil
Expand Down