Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch to mtsitrin/324: simplify empty timing #836

Merged
45 changes: 14 additions & 31 deletions block/produce.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,21 @@
func (m *Manager) ProduceBlockLoop(ctx context.Context) {
m.logger.Debug("Started produce loop")

// Setup variables for empty block production
var (
produceEmptyBlock = true // Allow the initial block to be empty
resetEmptyBlocksFn = func(bool) {}
nextEmptyBlock = time.Time{}
)

// Setup ticker for empty blocks if enabled
// if disabled, produceEmptyBlock will remain true and the block will be produced every block time
if 0 < m.Conf.MaxIdleTime {
//define a reset function to reset the timer
resetEmptyBlocksFn = func(proofRequired bool) {
produceEmptyBlock = false
if proofRequired {
nextEmptyBlock = time.Now().Add(m.Conf.MaxProofTime)
} else {
nextEmptyBlock = time.Now().Add(m.Conf.MaxIdleTime)
}
}
resetEmptyBlocksFn(false)
}

// Main ticker for block production
ticker := time.NewTicker(m.Conf.BlockTime)
defer ticker.Stop()

var nextEmptyBlock time.Time
firstBlock := true

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
//check if we can produce a block even if there are no transactions
//if the time has passed, we will produce an empty block
if !nextEmptyBlock.IsZero() && nextEmptyBlock.Before(time.Now()) {
m.logger.Debug("no transactions, producing empty block")
produceEmptyBlock = true
}

// if empty blocks are configured to be enabled, and one is scheduled...
produceEmptyBlock := firstBlock || 0 < m.Conf.MaxProofTime && nextEmptyBlock.Before(time.Now())
Fixed Show fixed Hide fixed
firstBlock = false

block, commit, err := m.ProduceAndGossipBlock(ctx, produceEmptyBlock)
if errors.Is(err, context.Canceled) {
Expand All @@ -73,11 +51,16 @@
m.logger.Error("produce and gossip: uncategorized, assuming recoverable", "error", err)
continue
}

// If IBC transactions are present, set proof required to true
// This will set a shorter timer for the next block
// currently we set it for all txs as we don't have a way to determine if an IBC tx is present (https://github.com/dymensionxyz/dymint/issues/709)
proofRequired := len(block.Data.Txs) != 0
resetEmptyBlocksFn(proofRequired)
nextEmptyBlock = time.Now().Add(m.Conf.MaxIdleTime)
Dismissed Show dismissed Hide dismissed
if 0 < len(block.Data.Txs) {
nextEmptyBlock = time.Now().Add(m.Conf.MaxProofTime)
Dismissed Show dismissed Hide dismissed
} else {
m.logger.Info("produced empty block")
}

// Send the size to the accumulated size channel
// This will block in case the submitter is too slow and it's buffer is full
Expand Down
Loading