diff --git a/config/config.go b/config/config.go index 585d7514..3c35a176 100644 --- a/config/config.go +++ b/config/config.go @@ -144,6 +144,8 @@ type Config struct { // TODO: It will soon be discarded in the near future DevSnapSync bool `long:"devsnapsync" description:"Enable snap sync for P2P that only exist in development mode"` + + SnapTimeout int `long:"snaptimeout" description:"Unable to find a peer that supports snap-sync service for setting IBD timeout(seconds), it will switch to the normal sync method"` } func (c *Config) GetMinningAddrs() []types.Address { diff --git a/p2p/peers/interface.go b/p2p/peers/interface.go index c0003ddc..7aaf876a 100644 --- a/p2p/peers/interface.go +++ b/p2p/peers/interface.go @@ -40,6 +40,7 @@ type P2P interface { IsRunning() bool Consensus() model.Consensus MeerServer() *p2p.QngServer + IsSnap() bool } type P2PRPC interface { diff --git a/p2p/synch/snapsync.go b/p2p/synch/snapsync.go index 679c4053..9710506f 100644 --- a/p2p/synch/snapsync.go +++ b/p2p/synch/snapsync.go @@ -9,7 +9,6 @@ import ( "github.com/Qitmeer/qng/core/blockchain/utxo" "github.com/Qitmeer/qng/core/event" "github.com/Qitmeer/qng/core/json" - "github.com/Qitmeer/qng/core/protocol" "github.com/Qitmeer/qng/core/types" "github.com/Qitmeer/qng/meerdag" "github.com/Qitmeer/qng/p2p/common" @@ -83,8 +82,7 @@ func (ps *PeerSync) GetSnapSyncInfo() *json.SnapSyncInfo { } func (ps *PeerSync) startSnapSync() bool { - snap := protocol.HasServices(ps.sy.p2p.Config().Services, protocol.Snap) - if !snap { + if !ps.sy.p2p.IsSnap() { if ps.IsSnapSync() { log.Error("There is an unfinished snap-sync, please enable the snap service") } @@ -105,8 +103,11 @@ func (ps *PeerSync) startSnapSync() bool { return false } if !isValidSnapPeer(bestPeer) { - snapPeer := ps.getSnapSyncPeer() + snapPeer, change := ps.getSnapSyncPeer(ps.sy.p2p.Consensus().Config().SnapTimeout) if snapPeer == nil { + if change { + return false + } return true } bestPeer = snapPeer @@ -210,7 +211,7 @@ func (ps *PeerSync) continueSnapSync() bool { log.Info("Continue snap-sync", "point", ps.snapStatus.syncPoint.GetHash().String(), "point_order", ps.snapStatus.GetSyncPoint().GetOrder(), "status", fmt.Sprintf("%v", ps.snapStatus.ToInfo())) best := ps.Chain().BestSnapshot() - bestPeer := ps.getSnapSyncPeer() + bestPeer, _ := ps.getSnapSyncPeer(0) if bestPeer == nil { return true } @@ -291,7 +292,7 @@ func (ps *PeerSync) trySyncSnapStatus(pe *peers.Peer) *pb.SnapSyncRsp { return ret } log.Warn("Try change snap peer") - newPeer := ps.getSnapSyncPeer() + newPeer, _ := ps.getSnapSyncPeer(0) if newPeer == nil { return nil } @@ -385,20 +386,25 @@ func (ps *PeerSync) processRsp(ssr *pb.SnapSyncRsp) ([]*blockchain.SnapData, err return ret, nil } -func (ps *PeerSync) getSnapSyncPeer() *peers.Peer { +func (ps *PeerSync) getSnapSyncPeer(timeout int) (*peers.Peer, bool) { start := time.Now() var pe *peers.Peer for pe == nil { pe = ps.getBestPeer(true) if pe == nil { + if timeout > 0 { + if time.Since(start) > time.Duration(timeout)*time.Second { + return nil, true + } + } log.Debug("Try to get snap-sync peer", "cost", time.Since(start).String()) time.Sleep(SnapSyncReqInterval) } if !ps.IsRunning() { - return nil + return nil, false } } - return pe + return pe, false } func (s *Sync) sendSnapSyncRequest(stream network.Stream, pe *peers.Peer) (*pb.SnapSyncRsp, *common.Error) { diff --git a/services/common/flags.go b/services/common/flags.go index 2c2dac8b..6e51cdd9 100644 --- a/services/common/flags.go +++ b/services/common/flags.go @@ -650,6 +650,11 @@ var ( Usage: "Generate (mine) coins using the CPU on develop mode whithout gap", Destination: &cfg.GenerateNoDevGap, }, + &cli.IntFlag{ + Name: "snaptimeout", + Usage: "Unable to find a peer that supports snap-sync service for setting IBD timeout(seconds), it will switch to the normal sync method", + Destination: &cfg.SnapTimeout, + }, } ) @@ -686,6 +691,7 @@ func DefaultConfig(homeDir string) *config.Config { SubmitNoSynced: false, DevNextGDB: true, GBTTimeOut: defaultGBTTimeout, + SnapTimeout: 300, } if len(homeDir) > 0 { hd, err := filepath.Abs(homeDir)