From 6c8aa7767c0baeb2bdd36eac0520935dbb035be7 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Mon, 16 Dec 2024 16:21:13 +0100 Subject: [PATCH] skew time using first block when no state update --- block/block.go | 2 -- block/manager.go | 26 +------------------------- block/produce.go | 4 ++-- block/submit.go | 23 +++++++++++++++++------ block/submit_test.go | 2 -- block/sync.go | 3 --- 6 files changed, 20 insertions(+), 40 deletions(-) diff --git a/block/block.go b/block/block.go index 4b4562794..a1d7b90e5 100644 --- a/block/block.go +++ b/block/block.go @@ -127,8 +127,6 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta } - // save last block time used to calculate batch skew time - m.LastBlockTime.Store(block.Header.GetTimestamp().UTC().UnixNano()) // Update the store: // 1. Save the proposer for the current height to the store. // 2. Update the proposer in the state in case of rotation. diff --git a/block/manager.go b/block/manager.go index 61d74a6ab..bc25b64b6 100644 --- a/block/manager.go +++ b/block/manager.go @@ -72,12 +72,6 @@ type Manager struct { Cancel context.CancelFunc Ctx context.Context - // LastBlockTimeInSettlement is the time of last submitted block, used to measure batch skew time - LastBlockTimeInSettlement atomic.Int64 - - // LastBlockTime is the time of last produced block, used to measure batch skew time - LastBlockTime atomic.Int64 - // mutex used to avoid stopping node when fork is detected but proposer is creating/sending fork batch forkMu sync.Mutex /* @@ -316,7 +310,7 @@ func (m *Manager) updateFromLastSettlementState() error { // The SL hasn't got any batches for this chain yet. m.logger.Info("No batches for chain found in SL.") m.LastSettlementHeight.Store(uint64(m.Genesis.InitialHeight - 1)) //nolint:gosec // height is non-negative and falls in int64 - m.LastBlockTimeInSettlement.Store(m.Genesis.GenesisTime.UTC().UnixNano()) + return nil } if err != nil { @@ -331,14 +325,6 @@ func (m *Manager) updateFromLastSettlementState() error { m.LastSettlementHeight.Store(latestHeight) - // init last block in settlement time in dymint state to calculate batch submit skew time - m.SetLastBlockTimeInSettlementFromHeight(latestHeight) - - // init last block time in dymint state to calculate batch submit skew time - block, err := m.Store.LoadBlock(m.State.Height()) - if err == nil { - m.LastBlockTime.Store(block.Header.GetTimestamp().UTC().UnixNano()) - } return nil } @@ -420,13 +406,3 @@ func (m *Manager) freezeNode(err error) { uevent.MustPublish(m.Ctx, m.Pubsub, &events.DataHealthStatus{Error: err}, events.HealthStatusList) m.Cancel() } - -// SetLastBlockTimeInSettlementFromHeight is used to initialize LastBlockTimeInSettlement from rollapp height in settlement -func (m *Manager) SetLastBlockTimeInSettlementFromHeight(lastSettlementHeight uint64) { - block, err := m.Store.LoadBlock(lastSettlementHeight) - if err != nil { - // if settlement height block is not found it will be updated after, when syncing - return - } - m.LastBlockTimeInSettlement.Store(block.Header.GetTimestamp().UTC().UnixNano()) -} diff --git a/block/produce.go b/block/produce.go index 9a67fe77b..fad7b362c 100644 --- a/block/produce.go +++ b/block/produce.go @@ -82,9 +82,9 @@ func (m *Manager) ProduceBlockLoop(ctx context.Context, bytesProducedC chan int) return nil case bytesProducedC <- bytesProducedN: default: - evt := &events.DataHealthStatus{Error: fmt.Errorf("Block production paused. Time between last block produced and last block submitted higher than max skew time: %s last block in settlement time: %s %w", m.Conf.MaxSkewTime, time.Unix(0, m.LastBlockTimeInSettlement.Load()), gerrc.ErrResourceExhausted)} + evt := &events.DataHealthStatus{Error: fmt.Errorf("Block production paused. Time between last block produced and last block submitted higher than max skew time: %s last block in settlement time: %s %w", m.Conf.MaxSkewTime, m.GetLastBlockInSettlementTime(), gerrc.ErrResourceExhausted)} uevent.MustPublish(ctx, m.Pubsub, evt, events.HealthStatusList) - m.logger.Error("Pausing block production until new batch is submitted.", "Batch skew time", m.GetBatchSkewTime(), "Max batch skew time", m.Conf.MaxSkewTime, "Last block in settlement time", time.Unix(0, m.LastBlockTimeInSettlement.Load())) + m.logger.Error("Pausing block production until new batch is submitted.", "Batch skew time", m.GetBatchSkewTime(), "Max batch skew time", m.Conf.MaxSkewTime, "Last block in settlement time", m.GetLastBlockInSettlementTime()) select { case <-ctx.Done(): return nil diff --git a/block/submit.go b/block/submit.go index 3ee4e2dc4..41da8192e 100644 --- a/block/submit.go +++ b/block/submit.go @@ -256,9 +256,6 @@ func (m *Manager) SubmitBatch(batch *types.Batch) error { types.RollappHubHeightGauge.Set(float64(batch.EndHeight())) m.LastSettlementHeight.Store(batch.EndHeight()) - // update last submitted block time with batch last block (used to calculate max skew time) - m.LastBlockTimeInSettlement.Store(batch.Blocks[len(batch.Blocks)-1].Header.GetTimestamp().UTC().UnixNano()) - return err } @@ -314,11 +311,25 @@ func (m *Manager) UpdateLastSubmittedHeight(event pubsub.Message) { } } +func (m *Manager) GetLastBlockInSettlementTime() time.Time { + lastBlockInSettlement, err := m.Store.LoadBlock(m.LastSettlementHeight.Load()) + if err != nil { + firstBlock, err := m.Store.LoadBlock(uint64(m.Genesis.InitialHeight)) + if err != nil { + return time.Now() + } + return firstBlock.Header.GetTimestamp() + } + return lastBlockInSettlement.Header.GetTimestamp() +} + // GetBatchSkewTime returns the time between the last produced block and the last block submitted to SL func (m *Manager) GetBatchSkewTime() time.Duration { - lastProducedTime := time.Unix(0, m.LastBlockTime.Load()) - lastSubmittedTime := time.Unix(0, m.LastBlockTimeInSettlement.Load()) - return lastProducedTime.Sub(lastSubmittedTime) + lastBlockProduced, err := m.Store.LoadBlock(m.State.Height()) + if err != nil { + return 0 + } + return lastBlockProduced.Header.GetTimestamp().Sub(m.GetLastBlockInSettlementTime()) } func UpdateBatchSubmissionGauges(skewBytes uint64, skewBlocks uint64, skewTime time.Duration) { diff --git a/block/submit_test.go b/block/submit_test.go index 59017b00f..306edaecd 100644 --- a/block/submit_test.go +++ b/block/submit_test.go @@ -268,7 +268,6 @@ func TestSubmissionByTime(t *testing.T) { manager.DAClient = testutil.GetMockDALC(log.TestingLogger()) manager.Retriever = manager.DAClient.(da.BatchRetriever) - manager.LastBlockTimeInSettlement.Store(time.Now().UTC().UnixNano()) // Check initial height initialHeight := uint64(0) require.Equal(initialHeight, manager.State.Height()) @@ -341,7 +340,6 @@ func TestSubmissionByBatchSize(t *testing.T) { managerConfig.BatchSubmitBytes = c.blockBatchMaxSizeBytes manager, err := testutil.GetManager(managerConfig, nil, 1, 1, 0, proxyApp, nil) require.NoError(err) - manager.LastBlockTimeInSettlement.Store(time.Now().UTC().UnixNano()) manager.DAClient = testutil.GetMockDALC(log.TestingLogger()) manager.Retriever = manager.DAClient.(da.BatchRetriever) diff --git a/block/sync.go b/block/sync.go index 9c3605669..89082691d 100644 --- a/block/sync.go +++ b/block/sync.go @@ -76,9 +76,6 @@ func (m *Manager) SettlementSyncLoop(ctx context.Context) error { } m.logger.Info("Retrieved state update from SL.", "state_index", settlementBatch.StateIndex) - // we update LastBlockTimeInSettlement to be able to measure batch skew time with last block time in settlement - m.LastBlockTimeInSettlement.Store(settlementBatch.BlockDescriptors[len(settlementBatch.BlockDescriptors)-1].GetTimestamp().UTC().UnixNano()) - err = m.ApplyBatchFromSL(settlementBatch.Batch) // this will keep sync loop alive when DA is down or retrievals are failing because DA issues.