Skip to content

Commit be5196c

Browse files
tac0turtletac0turtleManav-Aggarwal
authored
feat: Submit empty blocks to DA when no txs (#2234)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. NOTE: PR titles should follow semantic commits: https://www.conventionalcommits.org/en/v1.0.0/ --> ## Overview This pr recreates the legacy default design in which blocks are created at a set interval, even if there are no txs --------- Co-authored-by: tac0turtle <you@example.com> Co-authored-by: Manav Aggarwal <manavaggarwal1234@gmail.com>
1 parent 10ea3ae commit be5196c

14 files changed

Lines changed: 778 additions & 148 deletions

block/aggregation.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (m *Manager) AggregationLoop(ctx context.Context) {
3636
// Lazy Aggregator mode.
3737
// In Lazy Aggregator mode, blocks are built only when there are
3838
// transactions or every LazyBlockTime.
39-
if m.config.Node.LazyAggregator {
39+
if m.config.Node.LazyMode {
4040
m.lazyAggregationLoop(ctx, blockTimer)
4141
return
4242
}
@@ -55,21 +55,37 @@ func (m *Manager) lazyAggregationLoop(ctx context.Context, blockTimer *time.Time
5555
return
5656

5757
case <-lazyTimer.C:
58-
case <-blockTimer.C:
59-
}
58+
m.logger.Debug("Lazy timer triggered block production")
59+
m.produceBlock(ctx, "lazy_timer", lazyTimer, blockTimer)
6060

61-
// Reset the start time
62-
start := time.Now()
61+
case <-blockTimer.C:
62+
m.logger.Debug("Block timer triggered block production")
63+
if m.txsAvailable {
64+
m.produceBlock(ctx, "block_timer", lazyTimer, blockTimer)
65+
}
66+
m.txsAvailable = false
6367

64-
// Attempt to publish the block regardless of activity
65-
if err := m.publishBlock(ctx); err != nil && ctx.Err() == nil {
66-
m.logger.Error("error while publishing block", "error", err)
68+
case <-m.txNotifyCh:
69+
m.txsAvailable = true
6770
}
71+
}
72+
}
73+
74+
// produceBlock handles the common logic for producing a block and resetting timers
75+
func (m *Manager) produceBlock(ctx context.Context, trigger string, lazyTimer, blockTimer *time.Timer) {
76+
// Record the start time
77+
start := time.Now()
6878

69-
// Reset both timers for the next aggregation window
70-
lazyTimer.Reset(getRemainingSleep(start, m.config.Node.LazyBlockTime.Duration))
71-
blockTimer.Reset(getRemainingSleep(start, m.config.Node.BlockTime.Duration))
79+
// Attempt to publish the block
80+
if err := m.publishBlock(ctx); err != nil && ctx.Err() == nil {
81+
m.logger.Error("error while publishing block", "trigger", trigger, "error", err)
82+
} else {
83+
m.logger.Debug("Successfully published block", "trigger", trigger)
7284
}
85+
86+
// Reset both timers for the next aggregation window
87+
lazyTimer.Reset(getRemainingSleep(start, m.config.Node.LazyBlockInterval.Duration))
88+
blockTimer.Reset(getRemainingSleep(start, m.config.Node.BlockTime.Duration))
7389
}
7490

7591
func (m *Manager) normalAggregationLoop(ctx context.Context, blockTimer *time.Timer) {
@@ -87,6 +103,12 @@ func (m *Manager) normalAggregationLoop(ctx context.Context, blockTimer *time.Ti
87103
// Reset the blockTimer to signal the next block production
88104
// period based on the block time.
89105
blockTimer.Reset(getRemainingSleep(start, m.config.Node.BlockTime.Duration))
106+
107+
case <-m.txNotifyCh:
108+
// Transaction notifications are intentionally ignored in normal mode
109+
// to avoid triggering block production outside the scheduled intervals.
110+
// We just update the txsAvailable flag for tracking purposes
111+
m.txsAvailable = true
90112
}
91113
}
92114
}

block/aggregation_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func TestAggregationLoop_Normal_BasicInterval(t *testing.T) {
4545
logger: logger,
4646
config: config.Config{
4747
Node: config.NodeConfig{
48-
BlockTime: config.DurationWrapper{Duration: blockTime},
49-
LazyAggregator: false,
48+
BlockTime: config.DurationWrapper{Duration: blockTime},
49+
LazyMode: false,
5050
},
5151
DA: config.DAConfig{
5252
BlockTime: config.DurationWrapper{Duration: 1 * time.Second},
@@ -142,8 +142,8 @@ func TestAggregationLoop_Normal_PublishBlockError(t *testing.T) {
142142
logger: mockLogger,
143143
config: config.Config{
144144
Node: config.NodeConfig{
145-
BlockTime: config.DurationWrapper{Duration: blockTime},
146-
LazyAggregator: false,
145+
BlockTime: config.DurationWrapper{Duration: blockTime},
146+
LazyMode: false,
147147
},
148148
DA: config.DAConfig{
149149
BlockTime: config.DurationWrapper{Duration: 1 * time.Second},

0 commit comments

Comments
 (0)