This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
syncdag.go
152 lines (131 loc) · 4.13 KB
/
syncdag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2017-2020 The qitmeer developers
*/
package synch
import (
"context"
"errors"
"fmt"
"github.com/Qitmeer/qitmeer/core/blockdag"
"github.com/Qitmeer/qitmeer/p2p/common"
"github.com/Qitmeer/qitmeer/p2p/peers"
pb "github.com/Qitmeer/qitmeer/p2p/proto/v1"
libp2pcore "github.com/libp2p/go-libp2p-core"
"github.com/libp2p/go-libp2p-core/peer"
"strings"
"sync/atomic"
)
// MaxBlockLocatorsPerMsg is the maximum number of block locator hashes allowed
// per message.
const MaxBlockLocatorsPerMsg = 2000
func (s *Sync) sendSyncDAGRequest(ctx context.Context, id peer.ID, sd *pb.SyncDAG) (*pb.SubDAG, error) {
ctx, cancel := context.WithTimeout(ctx, ReqTimeout)
defer cancel()
stream, err := s.Send(ctx, sd, RPCSyncDAG, id)
if err != nil {
return nil, err
}
defer func() {
if err := stream.Reset(); err != nil {
log.Error(fmt.Sprintf("Failed to reset stream with protocol %s,%v", stream.Protocol(), err))
}
}()
code, errMsg, err := ReadRspCode(stream, s.Encoding())
if err != nil {
return nil, err
}
if !code.IsSuccess() {
s.Peers().IncrementBadResponses(stream.Conn().RemotePeer(), "sync DAG request rsp")
return nil, errors.New(errMsg)
}
msg := &pb.SubDAG{}
if err := s.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
return nil, err
}
return msg, err
}
func (s *Sync) syncDAGHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) *common.Error {
pe := s.peers.Get(stream.Conn().RemotePeer())
if pe == nil {
return ErrPeerUnknown
}
ctx, cancel := context.WithTimeout(ctx, HandleTimeout)
var err error
defer func() {
cancel()
}()
m, ok := msg.(*pb.SyncDAG)
if !ok {
err = fmt.Errorf("message is not type *pb.Hash")
return ErrMessage(err)
}
pe.UpdateGraphState(m.GraphState)
gs := pe.GraphState()
blocks, point := s.PeerSync().dagSync.CalcSyncBlocks(gs, changePBHashsToHashs(m.MainLocator), blockdag.SubDAGMode, MaxBlockLocatorsPerMsg)
pe.UpdateSyncPoint(point)
/* if len(blocks) <= 0 {
err = fmt.Errorf("No blocks")
return err
}*/
sd := &pb.SubDAG{SyncPoint: &pb.Hash{Hash: point.Bytes()}, GraphState: s.getGraphState(), Blocks: changeHashsToPBHashs(blocks)}
e := s.EncodeResponseMsg(stream, sd)
if e != nil {
return e
}
return nil
}
func debugSyncDAG(m *pb.SyncDAG) string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("SyncDAG: graphstate=(%v,%v,%v), ",
m.GraphState.MainOrder, m.GraphState.MainHeight, m.GraphState.Layer,
))
sb.WriteString("locator=[")
size := len(m.MainLocator)
for i, h := range m.MainLocator {
sb.WriteString(changePBHashToHash(h).String())
if i+1 < size {
sb.WriteString(",")
}
}
sb.WriteString("]")
sb.WriteString(fmt.Sprintf(", size=%d ", size))
return sb.String()
}
func (ps *PeerSync) processSyncDAGBlocks(pe *peers.Peer) error {
log.Trace(fmt.Sprintf("processSyncDAGBlocks peer=%v ", pe.GetID()))
if !ps.isSyncPeer(pe) || !pe.IsConnected() {
return fmt.Errorf("no sync peer")
}
point := pe.SyncPoint()
mainLocator := ps.dagSync.GetMainLocator(point)
sd := &pb.SyncDAG{MainLocator: changeHashsToPBHashs(mainLocator), GraphState: ps.sy.getGraphState()}
log.Trace(fmt.Sprintf("processSyncDAGBlocks sendSyncDAG point=%v, sd=%v", point.String(), debugSyncDAG(sd)))
subd, err := ps.sy.sendSyncDAGRequest(ps.sy.p2p.Context(), pe.GetID(), sd)
if err != nil {
log.Trace(fmt.Sprintf("processSyncDAGBlocks err=%v ", err.Error()))
ps.updateSyncPeer(true)
return err
}
log.Trace(fmt.Sprintf("processSyncDAGBlocks result graphstate=(%v,%v,%v), blocks=%v ",
subd.GraphState.MainOrder, subd.GraphState.MainHeight, subd.GraphState.Layer,
len(subd.Blocks)))
pe.UpdateSyncPoint(changePBHashToHash(subd.SyncPoint))
pe.UpdateGraphState(subd.GraphState)
if len(subd.Blocks) <= 0 {
ps.updateSyncPeer(true)
return fmt.Errorf("No sync dag blocks")
}
log.Trace(fmt.Sprintf("processSyncDAGBlocks do GetBlockDatas blocks=%v ", len(subd.Blocks)))
go ps.GetBlockDatas(pe, changePBHashsToHashs(subd.Blocks))
return nil
}
func (ps *PeerSync) syncDAGBlocks(pe *peers.Peer) {
if pe == nil {
return
}
// Ignore if we are shutting down.
if atomic.LoadInt32(&ps.shutdown) != 0 {
return
}
ps.msgChan <- &syncDAGBlocksMsg{pe: pe}
}