From c190fe6c75e9495c9ab46799bcc961e5a76de56d Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 19 Nov 2024 16:16:39 +0100 Subject: [PATCH] last block time fix --- block/block.go | 3 +++ block/manager.go | 16 ++++++++++++++-- block/submit.go | 15 ++++++++------- block/submit_loop_test.go | 8 ++++---- types/state.go | 3 +++ 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/block/block.go b/block/block.go index 30349bdf5..e009462c6 100644 --- a/block/block.go +++ b/block/block.go @@ -124,6 +124,9 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta // Update the state with the new app hash, and store height from the commit. // Every one of those, if happens before commit, prevents us from re-executing the block in case failed during commit. m.Executor.UpdateStateAfterCommit(m.State, responses, appHash, block.Header.Height, block.Header.Hash()) + + // update last block time + m.State.LastBlockTime = block.Header.GetTimestamp() } // Update the store: diff --git a/block/manager.go b/block/manager.go index d338ea0ae..b075c3021 100644 --- a/block/manager.go +++ b/block/manager.go @@ -6,6 +6,7 @@ import ( "fmt" "sync" "sync/atomic" + "time" "github.com/dymensionxyz/gerr-cosmos/gerrc" "golang.org/x/sync/errgroup" @@ -311,12 +312,23 @@ func (m *Manager) updateFromLastSettlementState() error { return err } - m.LastSettlementHeight.Store(latestHeight) - if latestHeight >= m.State.NextHeight() { m.UpdateTargetHeight(latestHeight) } + + 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.State.LastBlockTime = block.Header.GetTimestamp() + } else { + m.State.LastBlockTime = time.Now() + } + return nil } diff --git a/block/submit.go b/block/submit.go index ba4a0419f..34e08da3b 100644 --- a/block/submit.go +++ b/block/submit.go @@ -46,7 +46,7 @@ func SubmitLoopInner( bytesProduced chan int, // a channel of block and commit bytes produced maxBatchSkew time.Duration, // max time between last submitted block and last produced block allowed. if this threshold is reached block production is stopped. unsubmittedBlocksNum func() uint64, - skewTime func(time.Time) time.Duration, + skewTime func() time.Duration, maxBatchTime time.Duration, // max time to allow between batches maxBatchBytes uint64, // max size of serialised batch in bytes createAndSubmitBatch func(maxSizeBytes uint64) (sizeBlocksCommits uint64, err error), @@ -72,11 +72,11 @@ func SubmitLoopInner( types.RollappPendingSubmissionsSkewBytes.Set(float64(pendingBytes.Load())) types.RollappPendingSubmissionsSkewBlocks.Set(float64(unsubmittedBlocksNum())) - types.RollappPendingSubmissionsSkewTimeMinutes.Set(float64(skewTime(time.Now()).Minutes())) + types.RollappPendingSubmissionsSkewTimeMinutes.Set(float64(skewTime().Minutes())) submitter.Nudge() - if maxBatchSkew < skewTime(time.Now()) { + if maxBatchSkew < skewTime() { // too much stuff is pending submission // we block here until we get a progress nudge from the submitter thread select { @@ -103,14 +103,14 @@ func SubmitLoopInner( pending := pendingBytes.Load() types.RollappPendingSubmissionsSkewBytes.Set(float64(pending)) types.RollappPendingSubmissionsSkewBlocks.Set(float64(unsubmittedBlocksNum())) - types.RollappPendingSubmissionsSkewTimeMinutes.Set(float64(skewTime(time.Now()).Minutes())) + types.RollappPendingSubmissionsSkewTimeMinutes.Set(float64(skewTime().Minutes())) // while there are accumulated blocks, create and submit batches!! for { done := ctx.Err() != nil nothingToSubmit := pending == 0 - lastSubmissionIsRecent := skewTime(time.Now()) < maxBatchTime + lastSubmissionIsRecent := skewTime() < maxBatchTime maxDataNotExceeded := pending <= maxBatchBytes if done || nothingToSubmit || (lastSubmissionIsRecent && maxDataNotExceeded) { break @@ -310,6 +310,7 @@ func (m *Manager) UpdateLastSubmittedHeight(event pubsub.Message) { } // GetSkewTime returns the time between the last produced block and the last block submitted to SL -func (m *Manager) GetSkewTime(lastBlockTime time.Time) time.Duration { - return lastBlockTime.Sub(m.State.LastBlockTimeInSettlement) +func (m *Manager) GetSkewTime() time.Duration { + + return m.State.LastBlockTime.Sub(m.State.LastBlockTimeInSettlement) } diff --git a/block/submit_loop_test.go b/block/submit_loop_test.go index 7bbaa4262..b1a623fdf 100644 --- a/block/submit_loop_test.go +++ b/block/submit_loop_test.go @@ -62,8 +62,8 @@ func testSubmitLoopInner( lastSettlementBlockTime := time.Now() lastBlockTime := time.Now() - skewTime := func(time time.Time) time.Duration { - return time.Sub(lastSettlementBlockTime) + skewTime := func() time.Duration { + return lastBlockTime.Sub(lastSettlementBlockTime) } go func() { // simulate block production go func() { // another thread to check system properties @@ -74,7 +74,7 @@ func testSubmitLoopInner( default: } // producer shall not get too far ahead - require.True(t, skewTime(lastBlockTime) < args.batchSkew+args.skewMargin, "last produced blocks time not less than maximum skew time", "produced block skew time", skewTime(time.Now()), "max skew time", args.batchSkew) + require.True(t, skewTime() < args.batchSkew+args.skewMargin, "last produced blocks time not less than maximum skew time", "produced block skew time", skewTime(), "max skew time", args.batchSkew) } }() for { @@ -86,7 +86,7 @@ func testSubmitLoopInner( time.Sleep(approx(args.produceTime)) - if args.batchSkew <= skewTime(lastBlockTime) { + if args.batchSkew <= skewTime() { continue } diff --git a/types/state.go b/types/state.go index 7e83512bf..2204d7d57 100644 --- a/types/state.go +++ b/types/state.go @@ -47,6 +47,9 @@ type State struct { // LastBlockTimeInSettlement is the time of last submitted block used to calculated skew time LastBlockTimeInSettlement time.Time + + // LastBlockTimeInSettlement is the time of last produced block used to calculated skew time + LastBlockTime time.Time } func (s *State) GetProposer() *Sequencer {