Skip to content

Commit

Permalink
last block time fix
Browse files Browse the repository at this point in the history
  • Loading branch information
srene committed Nov 19, 2024
1 parent c8d0078 commit c190fe6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
3 changes: 3 additions & 0 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/dymensionxyz/gerr-cosmos/gerrc"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -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
}

Expand Down
15 changes: 8 additions & 7 deletions block/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions block/submit_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -86,7 +86,7 @@ func testSubmitLoopInner(

time.Sleep(approx(args.produceTime))

if args.batchSkew <= skewTime(lastBlockTime) {
if args.batchSkew <= skewTime() {
continue
}

Expand Down
3 changes: 3 additions & 0 deletions types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c190fe6

Please sign in to comment.