Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:can't switch sync mode when no snap-sync peer #848

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SnapTimeout -> FindSnapSyncPeerTimeout
a better description, During the IBD phase, if no peer node with snap-sync service enabled is found after the timeout (seconds), the node will switch to using the normal sync method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or NoSnapSyncPeerTimeout is also OK for me

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add extra defaultSnapTimeout after line 43 : defaultGBTTimeout = 800

}
if len(homeDir) > 0 {
hd, err := filepath.Abs(homeDir)
Expand Down
Loading