Skip to content

Commit

Permalink
fix:can't switch sync mode when no snap-sync peer
Browse files Browse the repository at this point in the history
  • Loading branch information
lochjin committed Dec 12, 2024
1 parent a09f8b8 commit 9e12bc2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions p2p/peers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type P2P interface {
IsRunning() bool
Consensus() model.Consensus
MeerServer() *p2p.QngServer
IsSnap() bool
}

type P2PRPC interface {
Expand Down
24 changes: 15 additions & 9 deletions p2p/synch/snapsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions services/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9e12bc2

Please sign in to comment.