From 684abd28c6b715f3f0ea49baef3e0a657bfa1145 Mon Sep 17 00:00:00 2001 From: codchen Date: Wed, 26 Apr 2023 10:20:40 +0800 Subject: [PATCH] Do not run DBSync if there is already a readable app version --- internal/dbsync/reactor.go | 7 +++++-- internal/dbsync/syncer.go | 3 ++- internal/dbsync/syncer_test.go | 1 + node/node.go | 17 ++++++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/dbsync/reactor.go b/internal/dbsync/reactor.go index eff715679..17525e76a 100644 --- a/internal/dbsync/reactor.go +++ b/internal/dbsync/reactor.go @@ -107,6 +107,7 @@ type Reactor struct { stateStore sm.Store blockStore *store.BlockStore initialHeight int64 + shouldSync bool chainID string config config.DBSyncConfig @@ -138,6 +139,7 @@ func NewReactor( initialHeight int64, chainID string, eventBus *eventbus.EventBus, + shouldSync bool, postSyncHook func(context.Context, sm.State) error, ) *Reactor { reactor := &Reactor{ @@ -152,8 +154,9 @@ func NewReactor( eventBus: eventBus, config: config, postSyncHook: postSyncHook, + shouldSync: shouldSync, } - syncer := NewSyncer(logger, config, baseConfig, reactor.requestMetadata, reactor.requestFile, reactor.commitState, reactor.postSync, defaultResetDirFn) + syncer := NewSyncer(logger, config, baseConfig, shouldSync, reactor.requestMetadata, reactor.requestFile, reactor.commitState, reactor.postSync, defaultResetDirFn) reactor.syncer = syncer reactor.BaseService = *service.NewBaseService(logger, "DBSync", reactor) @@ -187,7 +190,7 @@ func (r *Reactor) OnStart(ctx context.Context) error { go r.processFileCh(ctx, r.fileChannel) go r.processLightBlockCh(ctx, r.lightBlockChannel) go r.processParamsCh(ctx, r.paramsChannel) - if r.config.Enable { + if r.shouldSync { to := light.TrustOptions{ Period: r.config.TrustPeriod, Height: r.config.TrustHeight, diff --git a/internal/dbsync/syncer.go b/internal/dbsync/syncer.go index 8e74f9581..be54f0e56 100644 --- a/internal/dbsync/syncer.go +++ b/internal/dbsync/syncer.go @@ -70,6 +70,7 @@ func NewSyncer( logger log.Logger, dbsyncConfig config.DBSyncConfig, baseConfig config.BaseConfig, + enable bool, metadataRequestFn func(context.Context) error, fileRequestFn func(context.Context, types.NodeID, uint64, string) error, commitStateFn func(context.Context, uint64) (sm.State, *types.Commit, error), @@ -78,7 +79,7 @@ func NewSyncer( ) *Syncer { return &Syncer{ logger: logger, - active: dbsyncConfig.Enable, + active: enable, timeoutInSeconds: time.Duration(dbsyncConfig.TimeoutInSeconds) * time.Second, fileQueue: []*dstypes.FileResponse{}, applicationDBDirectory: path.Join(baseConfig.DBDir(), ApplicationDBSubdirectory), diff --git a/internal/dbsync/syncer_test.go b/internal/dbsync/syncer_test.go index 8fcf3df29..0f2787d55 100644 --- a/internal/dbsync/syncer_test.go +++ b/internal/dbsync/syncer_test.go @@ -24,6 +24,7 @@ func getTestSyncer(t *testing.T) *Syncer { log.NewNopLogger(), *dbsyncConfig, baseConfig, + true, func(ctx context.Context) error { return nil }, func(ctx context.Context, ni types.NodeID, u uint64, s string) error { return nil }, func(ctx context.Context, u uint64) (state.State, *types.Commit, error) { diff --git a/node/node.go b/node/node.go index 0a9ff3366..06bf82ca7 100644 --- a/node/node.go +++ b/node/node.go @@ -282,10 +282,16 @@ func makeNode( node.rpcEnv.EvidencePool = evPool node.evPool = evPool + info, err := client.Info(ctx, &abci.RequestInfo{}) + if err != nil { + return nil, err + } + shoulddbsync := cfg.DBSync.Enable && info.LastBlockHeight == 0 + mpReactor, mp := createMempoolReactor(logger, cfg, proxyApp, stateStore, nodeMetrics.mempool, peerManager.Subscribe, peerManager) node.router.AddChDescToBeAdded(mempool.GetChannelDescriptor(cfg.Mempool), mpReactor.SetChannel) - if !cfg.DBSync.Enable { + if !shoulddbsync { mpReactor.MarkReadyToStart() } node.rpcEnv.Mempool = mp @@ -310,14 +316,14 @@ func makeNode( stateSync = false } - if stateSync && cfg.DBSync.Enable { + if stateSync && shoulddbsync { panic("statesync and dbsync cannot be turned on at the same time") } // Determine whether we should do block sync. This must happen after the handshake, since the // app may modify the validator set, specifying ourself as the only validator. blockSync := !onlyValidatorIsUs(state, pubKey) - waitSync := stateSync || blockSync || cfg.DBSync.Enable + waitSync := stateSync || blockSync || shoulddbsync csState, err := consensus.NewState(logger.With("module", "consensus"), cfg.Consensus, @@ -361,7 +367,7 @@ func makeNode( blockStore, csReactor, peerManager.Subscribe, - blockSync && !stateSync && !cfg.DBSync.Enable, + blockSync && !stateSync && !shoulddbsync, nodeMetrics.consensus, eventBus, restartCh, @@ -429,7 +435,7 @@ func makeNode( cfg.SelfRemediation, ) - node.shouldHandshake = !stateSync && !cfg.DBSync.Enable + node.shouldHandshake = !stateSync && !shoulddbsync node.services = append(node.services, ssReactor) node.router.AddChDescToBeAdded(statesync.GetSnapshotChannelDescriptor(), ssReactor.SetSnapshotChannel) node.router.AddChDescToBeAdded(statesync.GetChunkChannelDescriptor(), ssReactor.SetChunkChannel) @@ -446,6 +452,7 @@ func makeNode( genDoc.InitialHeight, genDoc.ChainID, eventBus, + shoulddbsync, func(ctx context.Context, state sm.State) error { if _, err := client.LoadLatest(ctx, &abci.RequestLoadLatest{}); err != nil { return err