-
Notifications
You must be signed in to change notification settings - Fork 458
/
p2p_services.go
161 lines (146 loc) · 4.77 KB
/
p2p_services.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
153
154
155
156
157
158
159
160
161
package node
import (
"math/big"
"runtime/debug"
"time"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/pkg/errors"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p/node/peerManager"
"github.com/dominant-strategies/go-quai/p2p/node/pubsubManager"
"github.com/dominant-strategies/go-quai/p2p/node/requestManager"
"github.com/dominant-strategies/go-quai/p2p/pb"
"github.com/dominant-strategies/go-quai/trie"
)
// Opens a stream to the given peer and request some data for the given hash at the given location
func (p *P2PNode) requestFromPeer(peerID peer.ID, topic *pubsubManager.Topic, reqData interface{}, respDataType interface{}) (interface{}, error) {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
log.Global.WithFields(log.Fields{
"peerId": peerID,
"topic": topic,
}).Trace("Requesting the data from peer")
stream, err := p.NewStream(peerID)
if err != nil {
log.Global.WithFields(log.Fields{
"peerId": peerID,
"error": err,
}).Error("Failed to open stream to peer")
return nil, err
}
// Get a new request ID
id := p.requestManager.CreateRequest()
// Remove request ID from the map of pending requests when cleaning up
defer p.requestManager.CloseRequest(id)
// Create the corresponding data request
requestBytes, err := pb.EncodeQuaiRequest(id, topic.GetLocation(), reqData, respDataType)
if err != nil {
return nil, err
}
// Send the request to the peer
err = p.GetPeerManager().WriteMessageToStream(peerID, stream, requestBytes)
if err != nil {
return nil, err
}
// Get appropriate channel and wait for response
dataChan, err := p.requestManager.GetRequestChan(id)
if err != nil {
return nil, err
}
var recvdType interface{}
select {
case recvdType = <-dataChan:
p.GetPeerManager().ClosePendingRequest(peerID)
break
case <-time.After(requestManager.C_requestTimeout):
log.Global.WithFields(log.Fields{
"requestID": id,
"peerId": peerID,
}).Warn("Peer did not respond in time")
p.peerManager.MarkUnresponsivePeer(peerID, topic)
p.GetPeerManager().ClosePendingRequest(peerID)
return nil, errors.New("peer did not respond in time")
}
if recvdType == nil {
return nil, nil
}
// Check the received data type & hash matches the request
switch respDataType.(type) {
// First, check that the recvdType is the same as the expected type
case *types.WorkObjectBlockView, *types.WorkObjectHeaderView:
switch reqData := reqData.(type) {
case common.Hash:
// Next, if it was a requestByHash, verify the hash matches
switch recvdType := recvdType.(type) {
// Finally, BlockView and HeaderView have different hash functions
case *types.WorkObjectBlockView:
if reqData == recvdType.Hash() {
return recvdType, nil
}
case *types.WorkObjectHeaderView:
if reqData == recvdType.Hash() {
return recvdType, nil
}
default:
return nil, errors.New("invalid response")
}
return nil, errors.Errorf("invalid response: got block with different hash")
case *big.Int:
// If it was a requestByNumber, just verify the number matches
nodeCtx := topic.GetLocation().Context()
switch block := recvdType.(type) {
case *types.WorkObjectBlockView:
if block.Number(nodeCtx).Cmp(reqData) == 0 {
return recvdType, nil
}
case *types.WorkObjectHeaderView:
if block.Number(nodeCtx).Cmp(reqData) == 0 {
return recvdType, nil
}
default:
return nil, errors.New("invalid response")
}
return nil, errors.Errorf("invalid response: got block with different number")
}
return nil, errors.New("block request invalid response")
case *types.Header:
if header, ok := recvdType.(*types.Header); ok && header.Hash() == reqData.(common.Hash) {
return header, nil
}
case *types.Transaction:
if tx, ok := recvdType.(*types.Transaction); ok && tx.Hash() == reqData.(common.Hash) {
return tx, nil
}
case common.Hash:
if hash, ok := recvdType.(common.Hash); ok {
return hash, nil
}
case *trie.TrieNodeRequest:
if trieNode, ok := recvdType.(*trie.TrieNodeResponse); ok {
return trieNode, nil
}
default:
log.Global.Warn("peer returned unexpected type")
}
// If this peer responded with an invalid response, ban them for misbehaving.
p.BanPeer(peerID)
return nil, errors.New("invalid response")
}
func (p *P2PNode) GetRequestManager() requestManager.RequestManager {
return p.requestManager
}
func (p *P2PNode) GetPeerManager() peerManager.PeerManager {
return p.peerManager
}
func (p *P2PNode) GetHostBackend() host.Host {
return p.peerManager.GetHost()
}