-
Notifications
You must be signed in to change notification settings - Fork 465
/
api.go
338 lines (290 loc) · 9.44 KB
/
api.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package node
import (
"math/big"
"reflect"
"runtime/debug"
"sync"
"time"
"github.com/pkg/errors"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p"
"github.com/dominant-strategies/go-quai/p2p/node/peerManager"
"github.com/dominant-strategies/go-quai/p2p/node/pubsubManager"
quaiprotocol "github.com/dominant-strategies/go-quai/p2p/protocol"
"github.com/dominant-strategies/go-quai/quai"
"github.com/dominant-strategies/go-quai/trie"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/dominant-strategies/go-quai/common"
)
// Starts the node and all of its services
func (p *P2PNode) Start() error {
log.Global.Infof("starting P2P node...")
// Start any async processes belonging to this node
log.Global.Debugf("starting node processes...")
go p.eventLoop()
go p.statsLoop()
// Register the Quai protocol handler
p.peerManager.GetHost().SetStreamHandler(quaiprotocol.ProtocolVersion, func(s network.Stream) {
quaiprotocol.QuaiProtocolHandler(s, p)
})
// Start the pubsub manager
p.pubsub.SetReceiveHandler(p.handleBroadcast)
return nil
}
func (p *P2PNode) Subscribe(location common.Location, datatype interface{}) error {
err := p.pubsub.Subscribe(location, datatype)
if err != nil {
return err
}
return p.peerManager.Provide(p.ctx, location, datatype)
}
func (p *P2PNode) Unsubscribe(location common.Location, datatype interface{}) error {
return p.pubsub.Unsubscribe(location, datatype)
}
func (p *P2PNode) Broadcast(location common.Location, data interface{}) error {
return p.pubsub.Broadcast(location, data)
}
func (p *P2PNode) SetConsensusBackend(be quai.ConsensusAPI) {
p.consensus = be
p.pubsub.SetQuaiBackend(be)
}
type stopFunc func() error
// Function to gracefully shtudown all running services
func (p *P2PNode) Stop() error {
// define a list of functions to stop the services the node is running
stopFuncs := []stopFunc{
p.peerManager.GetHost().Close,
p.peerManager.Stop,
p.pubsub.Stop,
}
// create a channel to collect errors
errs := make(chan error, len(stopFuncs))
// run each stop function in a goroutine
for _, fn := range stopFuncs {
go func(fn stopFunc) {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
errs <- fn()
}(fn)
}
var allErrors []error
for i := 0; i < len(stopFuncs); i++ {
select {
case err := <-errs:
if err != nil {
log.Global.Errorf("error during shutdown: %s", err)
allErrors = append(allErrors, err)
}
case <-time.After(5 * time.Second):
err := errors.New("timeout during shutdown")
log.Global.Warnf("error: %s", err)
allErrors = append(allErrors, err)
}
}
close(errs)
if len(allErrors) > 0 {
return errors.Errorf("errors during shutdown: %v", allErrors)
} else {
return nil
}
}
func (p *P2PNode) requestFromPeers(topic *pubsubManager.Topic, requestData interface{}, respDataType interface{}, resultChan chan interface{}) {
go func() {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
defer close(resultChan)
peers := p.peerManager.GetPeers(topic, peerManager.Best)
log.Global.WithFields(log.Fields{
"peers": peers,
"topic": topic,
}).Debug("Requesting data from peers")
var requestWg sync.WaitGroup
for peerID := range peers {
requestWg.Add(1)
go func(peerID peer.ID) {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
defer requestWg.Done()
p.requestAndWait(peerID, topic, requestData, respDataType, resultChan)
}(peerID)
}
requestWg.Wait()
}()
}
func (p *P2PNode) requestAndWait(peerID peer.ID, topic *pubsubManager.Topic, reqData interface{}, respDataType interface{}, resultChan chan interface{}) {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
var recvd interface{}
var err error
if recvd, err = p.requestFromPeer(peerID, topic, reqData, respDataType); err == nil {
log.Global.WithFields(log.Fields{
"peerId": peerID,
"topic": topic.String(),
}).Trace("Received data from peer")
// Mark this peer as behaving well
p.peerManager.MarkResponsivePeer(peerID, topic)
select {
case resultChan <- recvd:
// Data sent successfully
default:
// Optionally log the missed send or handle it in another way
log.Global.WithFields(log.Fields{
"peerId": peerID,
"message": "Channel is full, data not sent",
}).Warning("Missed data send")
}
} else {
log.Global.WithFields(log.Fields{
"peerId": peerID,
"topic": topic.String(),
"err": err,
}).Error("Error requesting the data from peer")
// Mark this peer as not responding
p.peerManager.MarkUnresponsivePeer(peerID, topic)
}
}
// Request a data from the network for the specified slice
func (p *P2PNode) Request(location common.Location, requestData interface{}, responseDataType interface{}) chan interface{} {
topic, err := pubsubManager.NewTopic(p.pubsub.GetGenesis(), location, responseDataType)
if err != nil {
log.Global.WithFields(log.Fields{
"location": location.Name(),
"dataType": reflect.TypeOf(responseDataType),
"err": err,
}).Error("Error getting topic name")
panic(err)
}
resultChan := make(chan interface{}, 10)
// If it is a hash, first check to see if it is contained in the caches
if hash, ok := requestData.(common.Hash); ok {
result, ok := p.cacheGet(hash, responseDataType, location)
if ok {
resultChan <- result
return resultChan
}
}
p.requestFromPeers(topic, requestData, responseDataType, resultChan)
// TODO: optimize with waitgroups or a doneChan to only query if no peers responded
// Right now this creates too many streams, so don't call this until we have a better solution
// p.queryDHT(location, requestData, responseDataType, resultChan)
return resultChan
}
func (p *P2PNode) MarkLivelyPeer(peer p2p.PeerID, topic string) {
log.Global.WithFields(log.Fields{
"peer": peer,
"topic": topic,
}).Debug("Recording well-behaving peer")
t, err := pubsubManager.TopicFromString(p.pubsub.GetGenesis(), topic)
if err != nil {
log.Global.WithFields(log.Fields{
"topic": topic,
"err": err,
}).Error("Error getting topic name")
panic(err)
}
p.peerManager.MarkLivelyPeer(peer, t)
}
func (p *P2PNode) MarkLatentPeer(peer p2p.PeerID, topic string) {
log.Global.WithFields(log.Fields{
"peer": peer,
"topic": topic,
}).Debug("Recording misbehaving peer")
t, err := pubsubManager.TopicFromString(p.pubsub.GetGenesis(), topic)
if err != nil {
log.Global.WithFields(log.Fields{
"topic": topic,
"err": err,
}).Error("Error getting topic name")
panic(err)
}
p.peerManager.MarkLatentPeer(peer, t)
}
func (p *P2PNode) ProtectPeer(peer p2p.PeerID) {
log.Global.WithFields(log.Fields{
"peer": peer,
}).Debug("Protecting peer connection from pruning")
p.peerManager.ProtectPeer(peer)
}
func (p *P2PNode) UnprotectPeer(peer p2p.PeerID) {
log.Global.WithFields(log.Fields{
"peer": peer,
}).Debug("Unprotecting peer connection from pruning")
p.peerManager.UnprotectPeer(peer)
}
func (p *P2PNode) BanPeer(peer p2p.PeerID) {
log.Global.WithFields(log.Fields{
"peer": peer,
}).Warn("Banning peer for misbehaving")
p.peerManager.BanPeer(peer)
p.peerManager.GetHost().Network().ClosePeer(peer)
}
// Returns the list of bootpeers
func (p *P2PNode) GetBootPeers() []peer.AddrInfo {
return p.bootpeers
}
// Opens a new stream to the given peer using the given protocol ID
func (p *P2PNode) NewStream(peerID peer.ID) (network.Stream, error) {
return p.peerManager.GetStream(peerID)
}
// Connects to the given peer
func (p *P2PNode) Connect(pi peer.AddrInfo) error {
return p.peerManager.GetHost().Connect(p.ctx, pi)
}
// Search for a block in the node's cache, or query the consensus backend if it's not found in cache.
// Returns nil if the block is not found.
func (p *P2PNode) GetWorkObject(hash common.Hash, location common.Location) *types.WorkObject {
return p.consensus.LookupBlock(hash, location)
}
func (p *P2PNode) GetBlockHashByNumber(number *big.Int, location common.Location) *common.Hash {
return p.consensus.LookupBlockHashByNumber(number, location)
}
func (p *P2PNode) GetHeader(hash common.Hash, location common.Location) *types.WorkObject {
return p.consensus.GetHeader(hash, location)
}
func (p *P2PNode) GetTrieNode(hash common.Hash, location common.Location) *trie.TrieNodeResponse {
return p.consensus.GetTrieNode(hash, location)
}
func (p *P2PNode) handleBroadcast(sourcePeer peer.ID, topic string, data interface{}, nodeLocation common.Location) {
switch v := data.(type) {
case types.WorkObject:
p.cacheAdd(v.Hash(), &v, nodeLocation)
// TODO: send it to consensus
case types.WorkObjectHeaderView:
case types.Transactions:
case types.WorkObjectHeader:
default:
log.Global.Debugf("received unsupported block broadcast")
// TODO: ban the peer which sent it?
return
}
// If we made it here, pass the data on to the consensus backend
if p.consensus != nil {
p.consensus.OnNewBroadcast(sourcePeer, topic, data, nodeLocation)
}
}