Skip to content

Commit

Permalink
Do not run DBSync if there is already a readable app version (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Apr 26, 2023
1 parent 78ba8ab commit 46c1bb6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 5 additions & 2 deletions internal/dbsync/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Reactor struct {
stateStore sm.Store
blockStore *store.BlockStore
initialHeight int64
shouldSync bool

chainID string
config config.DBSyncConfig
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion internal/dbsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions internal/dbsync/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 12 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -361,7 +367,7 @@ func makeNode(
blockStore,
csReactor,
peerManager.Subscribe,
blockSync && !stateSync && !cfg.DBSync.Enable,
blockSync && !stateSync && !shoulddbsync,
nodeMetrics.consensus,
eventBus,
restartCh,
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 46c1bb6

Please sign in to comment.