Skip to content

Commit 7aa07f9

Browse files
committed
feat: add verify_data_root
1 parent b00f0fc commit 7aa07f9

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

blocksync/reactor.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77
"time"
88

9+
cfg "github.com/cometbft/cometbft/config"
910
"github.com/cometbft/cometbft/crypto"
1011
"github.com/cometbft/cometbft/libs/log"
1112
"github.com/cometbft/cometbft/libs/trace"
@@ -64,6 +65,7 @@ type Reactor struct {
6465
traceClient trace.Tracer
6566
blockSync bool
6667
localAddr crypto.Address
68+
config *cfg.BlockSyncConfig
6769
poolRoutineWg sync.WaitGroup
6870

6971
requestsCh <-chan BlockRequest
@@ -78,13 +80,17 @@ type Reactor struct {
7880
func NewReactor(state sm.State, blockExec *sm.BlockExecutor, store *store.BlockStore,
7981
blockSync bool, metrics *Metrics, offlineStateSyncHeight int64,
8082
) *Reactor {
81-
return NewReactorWithAddr(state, blockExec, store, blockSync, nil, metrics, offlineStateSyncHeight, trace.NoOpTracer())
83+
return NewReactorWithAddr(state, blockExec, store, blockSync, nil, metrics, offlineStateSyncHeight, trace.NoOpTracer(), nil)
8284
}
8385

8486
// Function added to keep existing API.
8587
func NewReactorWithAddr(state sm.State, blockExec *sm.BlockExecutor, store *store.BlockStore,
86-
blockSync bool, localAddr crypto.Address, metrics *Metrics, offlineStateSyncHeight int64, traceClient trace.Tracer,
88+
blockSync bool, localAddr crypto.Address, metrics *Metrics, offlineStateSyncHeight int64, traceClient trace.Tracer, config *cfg.BlockSyncConfig,
8789
) *Reactor {
90+
// Use default config if nil
91+
if config == nil {
92+
config = cfg.DefaultBlockSyncConfig()
93+
}
8894

8995
storeHeight := store.Height()
9096
if storeHeight == 0 {
@@ -121,6 +127,7 @@ func NewReactorWithAddr(state sm.State, blockExec *sm.BlockExecutor, store *stor
121127
pool: pool,
122128
blockSync: blockSync,
123129
localAddr: localAddr,
130+
config: config,
124131
requestsCh: requestsCh,
125132
errorsCh: errorsCh,
126133
metrics: metrics,
@@ -514,7 +521,7 @@ FOR_LOOP:
514521
err = bcR.blockExec.ValidateBlock(state, first)
515522
}
516523

517-
if err == nil {
524+
if err == nil && bcR.config.VerifyDataRoot {
518525
var stateMachineValid bool
519526
// Block sync doesn't check that the `Data` in a block is valid.
520527
// Since celestia-core can't determine if the `Data` in a block

config/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,13 +1035,15 @@ func (cfg *StateSyncConfig) ValidateBasic() error {
10351035

10361036
// BlockSyncConfig (formerly known as FastSync) defines the configuration for the CometBFT block sync service
10371037
type BlockSyncConfig struct {
1038-
Version string `mapstructure:"version"`
1038+
Version string `mapstructure:"version"`
1039+
VerifyDataRoot bool `mapstructure:"verify_data_root"`
10391040
}
10401041

10411042
// DefaultBlockSyncConfig returns a default configuration for the block sync service
10421043
func DefaultBlockSyncConfig() *BlockSyncConfig {
10431044
return &BlockSyncConfig{
1044-
Version: "v0",
1045+
Version: "v0",
1046+
VerifyDataRoot: true,
10451047
}
10461048
}
10471049

config/config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func TestBlockSyncConfigValidateBasic(t *testing.T) {
144144
cfg := config.TestBlockSyncConfig()
145145
assert.NoError(t, cfg.ValidateBasic())
146146

147+
// verify default value for VerifyDataRoot
148+
assert.True(t, cfg.VerifyDataRoot)
149+
147150
// tamper with version
148151
cfg.Version = "v1"
149152
assert.Error(t, cfg.ValidateBasic())

config/toml.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ chunk_fetchers = "{{ .StateSync.ChunkFetchers }}"
499499
# 1) "v0" - the default block sync implementation
500500
version = "{{ .BlockSync.Version }}"
501501
502+
# Verify data root during block sync.
503+
# When enabled, the node will verify the data root of each block during sync.
504+
# This adds extra validation but may slow down the sync process.
505+
verify_data_root = {{ .BlockSync.VerifyDataRoot }}
506+
502507
#######################################################
503508
### Consensus Configuration Options ###
504509
#######################################################

node/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func createBlocksyncReactor(config *cfg.Config,
352352
) (bcReactor p2p.Reactor, err error) {
353353
switch config.BlockSync.Version {
354354
case "v0":
355-
bcReactor = blocksync.NewReactorWithAddr(state.Copy(), blockExec, blockStore, blockSync, localAddr, metrics, offlineStateSyncHeight, tracer)
355+
bcReactor = blocksync.NewReactorWithAddr(state.Copy(), blockExec, blockStore, blockSync, localAddr, metrics, offlineStateSyncHeight, tracer, config.BlockSync)
356356
case "v1", "v2":
357357
return nil, fmt.Errorf("block sync version %s has been deprecated. Please use v0", config.BlockSync.Version)
358358
default:

0 commit comments

Comments
 (0)