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

feat(consensus/cometbft): stable block time #2422

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions consensus/cometbft/service/block_delay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package cometbft

import (

Check failure on line 23 in consensus/cometbft/service/block_delay.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gci)
"fmt"
"time"

"encoding/json"
)

// maxDelayBetweenBlocks is the maximum delay between two consecutive blocks.
// If the last block time minus the previous block time is greater than
// maxDelayBetweenBlocks, then we reset `FinalizeBlockResponse.NextBlockDelay`
// to default.
//
// This is needed because the network may stall for a long time and we don't
// want to rush in new blocks as the network resumes its operation.
const maxDelayBetweenBlocks = 30 * time.Minute
melekes marked this conversation as resolved.
Show resolved Hide resolved

type blockDelay struct {
// - genesis time if height is 0 OR
// - block time of the last block if height > 0 and time between blocks is
// greater than maxDelayBetweenBlocks.
InitialTime time.Time `json:"initial_time"`

// - InitChainRequest.InitialHeight OR
// - last block height if time between blocks is greater than maxDelayBetweenBlocks.
InitialHeight int64 `json:"initial_height"`

// PreviousBlockTime is the time of the previous block.
PreviousBlockTime time.Time `json:"previous_block_time"`
}

// CONTRACT: called only once upon genesis during or after InitChain.
func blockDelayUponGenesis(genesisTime time.Time, initialHeight int64) *blockDelay {
return &blockDelay{
InitialTime: genesisTime,
InitialHeight: initialHeight,
PreviousBlockTime: genesisTime,
}
}

func blockDelayFromBytes(
bz []byte,
) *blockDelay {
var d blockDelay
if err := json.Unmarshal(bz, &d); err != nil {
panic(fmt.Errorf("failed to unmarshal blockDelay: %w", err))
}
return &d
}

// Next returns the duration to wait before proposing the next block.
func (d *blockDelay) Next(curBlockTime time.Time, curBlockHeight int64,
targetBlockTime time.Duration) time.Duration {
// Until `timeout_commit` is removed from the CometBFT config,
// `FinalizeBlockResponse.NextBlockDelay` can't be exactly 0. If it's set to
// 0, then `timeout_commit` from the config will be used, which is not what
// we want since we're trying to control the block time.
const noDelay = 1 * time.Microsecond

// Reset the initial time and height if the time between blocks is greater
// than maxDelayBetweenBlocks.
if curBlockTime.Sub(d.PreviousBlockTime) > maxDelayBetweenBlocks {
d.InitialTime = curBlockTime
d.InitialHeight = curBlockHeight - 1
}

t := d.InitialTime.Add(targetBlockTime * time.Duration(curBlockHeight-d.InitialHeight))
if curBlockTime.Before(t) {
return t.Sub(curBlockTime)
}

// Update the previous block time.
d.PreviousBlockTime = curBlockTime

return noDelay
}

// ToBytes converts the blockDelay to bytes.
func (d *blockDelay) ToBytes() []byte {
bz, err := json.Marshal(d)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably sub-optimal

if err != nil {
panic(fmt.Errorf("failed to marshal blockDelay: %w", err))
}
return bz
}
113 changes: 113 additions & 0 deletions consensus/cometbft/service/block_delay_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package cometbft

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestBlockDelayUponGenesis(t *testing.T) {

Check failure on line 31 in consensus/cometbft/service/block_delay_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestBlockDelayUponGenesis missing the call to method parallel (paralleltest)
genesisTime := time.Now()
initialHeight := int64(1)

d := blockDelayUponGenesis(genesisTime, initialHeight)

assert.Equal(t, genesisTime, d.InitialTime)
assert.Equal(t, initialHeight, d.InitialHeight)
assert.Equal(t, genesisTime, d.PreviousBlockTime)
}

func TestBlockDelayFromBytes(t *testing.T) {

Check failure on line 42 in consensus/cometbft/service/block_delay_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestBlockDelayFromBytes missing the call to method parallel (paralleltest)
d1 := &blockDelay{
InitialTime: time.Now().Add(-10 * time.Minute),
InitialHeight: 5,
PreviousBlockTime: time.Now().Add(-5 * time.Minute),
}

b := d1.ToBytes()
d2 := blockDelayFromBytes(b)

assert.Equal(t, d1.InitialTime.Unix(), d2.InitialTime.Unix())
assert.Equal(t, d1.InitialHeight, d2.InitialHeight)
assert.Equal(t, d1.PreviousBlockTime.Unix(), d2.PreviousBlockTime.Unix())
}

func TestBlockDelayNext_NoDelay(t *testing.T) {

Check failure on line 57 in consensus/cometbft/service/block_delay_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestBlockDelayNext_NoDelay missing the call to method parallel (paralleltest)
genesisTime := time.Now()
initialHeight := int64(1)
d := blockDelayUponGenesis(genesisTime, initialHeight)

curBlockTime := genesisTime.Add(10 * time.Second)
curBlockHeight := int64(2)
targetBlockTime := 5 * time.Second
delay := d.Next(curBlockTime, curBlockHeight, targetBlockTime)

assert.Equal(t, 1*time.Microsecond, delay)
}

func TestBlockDelayNext_WithDelay(t *testing.T) {

Check failure on line 70 in consensus/cometbft/service/block_delay_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestBlockDelayNext_WithDelay missing the call to method parallel (paralleltest)
genesisTime := time.Now()
initialHeight := int64(1)
d := blockDelayUponGenesis(genesisTime, initialHeight)

curBlockTime := genesisTime.Add(8 * time.Second)
curBlockHeight := int64(3)
targetBlockTime := 5 * time.Second
delay := d.Next(curBlockTime, curBlockHeight, targetBlockTime)

assert.Equal(t, delay, 2*time.Second)

Check failure on line 80 in consensus/cometbft/service/block_delay_test.go

View workflow job for this annotation

GitHub Actions / lint

expected-actual: need to reverse actual and expected values (testifylint)
}

func TestBlockDelayNext_ResetOnStall(t *testing.T) {

Check failure on line 83 in consensus/cometbft/service/block_delay_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestBlockDelayNext_ResetOnStall missing the call to method parallel (paralleltest)
genesisTime := time.Now()
initialHeight := int64(1)
d := blockDelayUponGenesis(genesisTime, initialHeight)

curBlockTime := genesisTime.Add(maxDelayBetweenBlocks + 1*time.Minute)
curBlockHeight := int64(10)
targetBlockTime := 5 * time.Second

delay := d.Next(curBlockTime, curBlockHeight, targetBlockTime)

assert.Equal(t, d.InitialTime, curBlockTime)
assert.Equal(t, d.InitialHeight, curBlockHeight-1)
assert.Equal(t, delay, targetBlockTime)
}

func TestBlockDelaySerialization(t *testing.T) {

Check failure on line 99 in consensus/cometbft/service/block_delay_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestBlockDelaySerialization missing the call to method parallel (paralleltest)
d := &blockDelay{
InitialTime: time.Now(),
InitialHeight: 10,
PreviousBlockTime: time.Now().Add(-1 * time.Minute),
}

b := d.ToBytes()
var d2 blockDelay
err := json.Unmarshal(b, &d2)
assert.NoError(t, err)
assert.Equal(t, d.InitialTime.Unix(), d2.InitialTime.Unix())
assert.Equal(t, d.InitialHeight, d2.InitialHeight)
assert.Equal(t, d.PreviousBlockTime.Unix(), d2.PreviousBlockTime.Unix())
}
4 changes: 4 additions & 0 deletions consensus/cometbft/service/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (s *Service) commit(

s.finalizeBlockState = nil

if err := s.sm.SaveBlockDelay(s.blockDelay.ToBytes()); err != nil {
panic(fmt.Errorf("failed to save block delay: %w", err))
}

return &cmtabci.CommitResponse{
RetainHeight: retainHeight,
}, nil
Expand Down
11 changes: 2 additions & 9 deletions consensus/cometbft/service/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const ( // appeases mnd
minTimeoutPropose = 2000 * time.Millisecond
minTimeoutPrevote = 2000 * time.Millisecond
minTimeoutPrecommit = 2000 * time.Millisecond
minTimeoutCommit = 500 * time.Millisecond

maxBlockSize = 100 * 1024 * 1024

Expand Down Expand Up @@ -80,7 +79,8 @@ func DefaultConfig() *cmtcfg.Config {
consensus.TimeoutPropose = minTimeoutPropose
consensus.TimeoutPrevote = minTimeoutPrevote
consensus.TimeoutPrecommit = minTimeoutPrecommit
consensus.TimeoutCommit = minTimeoutCommit
// DEPRECATED: we use NextBlockDelay now
consensus.TimeoutCommit = 0

cfg.Storage.DiscardABCIResponses = true

Expand Down Expand Up @@ -149,13 +149,6 @@ func validateConfig(cfg *cmtcfg.Config) error {
)
}

if cfg.Consensus.TimeoutCommit < minTimeoutCommit {
return fmt.Errorf("%w, config timeout propose %v, min requested %v",
ErrInvalidaConfig,
cfg.Consensus.TimeoutCommit,
minTimeoutCommit,
)
}
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions consensus/cometbft/service/finalize_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,29 @@ func (s *Service) finalizeBlockInternal(
return nil, err
}

// Special case: height > 0, but blockDelay record doesn't exist in DB.
//
// NOTE: all nodes must enable this feature from the same height (requires
// coordinated upgrade).
if s.blockDelay == nil {
s.blockDelay = blockDelayUponGenesis(
req.Time,
req.Height-1,
)
}

// Calculate the delay for the next block.
delay := s.blockDelay.Next(
req.Time,
req.Height,
s.targetBlockTime)

cp := s.cmtConsensusParams.ToProto()
return &cmtabci.FinalizeBlockResponse{
TxResults: txResults,
ValidatorUpdates: valUpdates,
ConsensusParamUpdates: &cp,
NextBlockDelay: delay,
}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions consensus/cometbft/service/init_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func (s *Service) initChain(
return nil, err
}

s.blockDelay = blockDelayUponGenesis(
req.Time,
req.InitialHeight,
)

// NOTE: We don't commit, but FinalizeBlock for block InitialHeight starts
// from
// this FinalizeBlockState.
Expand Down
10 changes: 10 additions & 0 deletions consensus/cometbft/service/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package cometbft

import (
"time"

pruningtypes "cosmossdk.io/store/pruning/types"
storetypes "cosmossdk.io/store/types"
)
Expand Down Expand Up @@ -68,3 +70,11 @@ func SetInterBlockCache(cache storetypes.MultiStorePersistentCache) func(*Servic
func SetChainID(chainID string) func(*Service) {
return func(s *Service) { s.chainID = chainID }
}

// SetTargetBlockTime returns a Service option function that sets the desired
// block time (e.g., 2s). Note that it CAN'T be lower than the minimal (floor)
// block time in the network, which is comprised of the time to a) propose a
// new block b) gather 2/3+ prevotes c) gather 2/3+ precommits.
func SetTargetBlockTime(t time.Duration) func(*Service) {
return func(bs *Service) { bs.setTargetBlockTime(t) }
}
26 changes: 26 additions & 0 deletions consensus/cometbft/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"errors"
"fmt"
"time"

storetypes "cosmossdk.io/store/types"
"github.com/berachain/beacon-kit/beacon/blockchain"
Expand Down Expand Up @@ -96,6 +97,14 @@ type Service struct {
minRetainBlocks uint64

chainID string

// calculates block delay for the next block
melekes marked this conversation as resolved.
Show resolved Hide resolved
//
// NOTE: may be nil until either InitChain or FinalizeBlock is called.
blockDelay *blockDelay

// targetBlockTime is the desired block time. Doesn't change after start.
targetBlockTime time.Duration
}

func NewService(
Expand Down Expand Up @@ -143,6 +152,18 @@ func NewService(
panic(fmt.Errorf("failed loading latest version: %w", err))
}

// Load block delay
//
// If not found and height == 0, we will initialize it in InitChain.
// Otherwise - in FinalizeBlock.
bz, err := s.sm.LoadBlockDelay()
if err != nil {
panic(fmt.Errorf("failed loading block delay: %w", err))
}
if bz != nil {
s.blockDelay = blockDelayFromBytes(bz)
}
fridrik01 marked this conversation as resolved.
Show resolved Hide resolved

return s
}

Expand Down Expand Up @@ -259,6 +280,11 @@ func (s *Service) setMinRetainBlocks(minRetainBlocks uint64) {
s.minRetainBlocks = minRetainBlocks
}

// setTargetBlockTime sets the desired block time.
func (s *Service) setTargetBlockTime(t time.Duration) {
s.targetBlockTime = t
}

func (s *Service) setInterBlockCache(
cache storetypes.MultiStorePersistentCache,
) {
Expand Down
Loading
Loading