Skip to content

Commit

Permalink
New Logs and fix to GER Closing Signal (#1868)
Browse files Browse the repository at this point in the history
* fix log

* add closing signals logs

* fix new ger signal

* fix log

* fix and enable test

* fix finalizer log
  • Loading branch information
ToniRamirezM authored Mar 20, 2023
1 parent 743b904 commit 7e9e385
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 36 deletions.
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 getting latest L1 block number: %v", err)
continue
}

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
18 changes: 17 additions & 1 deletion sequencer/closingsignalsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/test/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,6 +25,10 @@ var (
testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b")
)

type mocks struct {
Etherman *EthermanMock
}

func setupTest(t *testing.T) {
initOrResetDB()

Expand Down Expand Up @@ -83,13 +88,24 @@ func prepareForcedBatches(t *testing.T) {
}

func TestClosingSignalsManager(t *testing.T) {
m := mocks{
Etherman: NewEthermanMock(t),
}

ctxMatchBy := mock.MatchedBy(func(ctx context.Context) bool { return ctx != nil })
lastL1BlockNumber := uint64(1)

m.Etherman.
On("GetLatestBlockNumber", ctxMatchBy).
Return(lastL1BlockNumber, nil)

setupTest(t)
channels := ClosingSignalCh{
ForcedBatchCh: make(chan state.ForcedBatch),
}

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

newCtx, cancelFunc := context.WithTimeout(ctx, time.Second*3)
Expand Down
6 changes: 5 additions & 1 deletion 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 Expand Up @@ -269,7 +273,7 @@ func (f *finalizer) newWIPBatch(ctx context.Context) (*WipBatch, error) {
if err != nil || !processBatchResponse.IsBatchProcessed {
log.Info("restarting the sequencer node because of a reprocessing error")
if err != nil {
log.Fatal("failed to reprocess batch, err: %v", err)
log.Fatalf("failed to reprocess batch, err: %v", err)
} else {
log.Fatal("Out of counters during reprocessFullBath")
}
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
14 changes: 7 additions & 7 deletions sequencer/mock_db_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e9e385

Please sign in to comment.