forked from lightninglabs/neutrino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
474 lines (416 loc) · 14.3 KB
/
query.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// NOTE: THIS API IS UNSTABLE RIGHT NOW.
package neutrino
import (
"fmt"
"time"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/gcs"
"github.com/btcsuite/btcutil/gcs/builder"
"github.com/lightninglabs/neutrino/filterdb"
)
var (
// QueryTimeout specifies how long to wait for a peer to answer a
// query.
QueryTimeout = time.Second * 3
// QueryNumRetries specifies how many times to retry sending a query to
// each peer before we've concluded we aren't going to get a valid
// response. This allows to make up for missed messages in some
// instances.
QueryNumRetries = 2
// QueryPeerConnectTimeout specifies how long to wait for the
// underlying chain service to connect to a peer before giving up
// on a query in case we don't have any peers.
QueryPeerConnectTimeout = time.Second * 30
)
// queries are a set of options that can be modified per-query, unlike global
// options.
//
// TODO: Make more query options that override global options.
type queryOptions struct {
// timeout lets the query know how long to wait for a peer to answer
// the query before moving onto the next peer.
timeout time.Duration
// numRetries tells the query how many times to retry asking each peer
// the query.
numRetries uint8
// peerConnectTimeout lets the query know how long to wait for the
// underlying chain service to connect to a peer before giving up
// on a query in case we don't have any peers.
peerConnectTimeout time.Duration
// doneChan lets the query signal the caller when it's done, in case
// it's run in a goroutine.
doneChan chan<- struct{}
}
// QueryOption is a functional option argument to any of the network query
// methods, such as GetBlockFromNetwork and GetCFilter (when that resorts to a
// network query). These are always processed in order, with later options
// overriding earlier ones.
type QueryOption func(*queryOptions)
// defaultQueryOptions returns a queryOptions set to package-level defaults.
func defaultQueryOptions() *queryOptions {
return &queryOptions{
timeout: QueryTimeout,
numRetries: uint8(QueryNumRetries),
peerConnectTimeout: QueryPeerConnectTimeout,
}
}
// Timeout is a query option that lets the query know how long to wait for each
// peer we ask the query to answer it before moving on.
func Timeout(timeout time.Duration) QueryOption {
return func(qo *queryOptions) {
qo.timeout = timeout
}
}
// NumRetries is a query option that lets the query know the maximum number of
// times each peer should be queried. The default is one.
func NumRetries(numRetries uint8) QueryOption {
return func(qo *queryOptions) {
qo.numRetries = numRetries
}
}
// PeerConnectTimeout is a query option that lets the query know how long to
// wait for the underlying chain service to connect to a peer before giving up
// on a query in case we don't have any peers.
func PeerConnectTimeout(timeout time.Duration) QueryOption {
return func(qo *queryOptions) {
qo.peerConnectTimeout = timeout
}
}
// DoneChan allows the caller to pass a channel that will get closed when the
// query is finished.
func DoneChan(doneChan chan<- struct{}) QueryOption {
return func(qo *queryOptions) {
qo.doneChan = doneChan
}
}
// queryPeers is a helper function that sends a query to one or more peers and
// waits for an answer. The timeout for queries is set by the QueryTimeout
// package-level variable.
func (s *ChainService) queryPeers(
// queryMsg is the message to send to each peer selected by selectPeer.
queryMsg wire.Message,
// checkResponse is caled for every message within the timeout period.
// The quit channel lets the query know to terminate because the
// required response has been found. This is done by closing the
// channel.
checkResponse func(sp *ServerPeer, resp wire.Message,
quit chan<- struct{}),
// options takes functional options for executing the query.
options ...QueryOption) {
// Starting witht he set of default options, we'll apply any specified
// functional options to the query.
qo := defaultQueryOptions()
for _, option := range options {
option(qo)
}
// We get an initial view of our peers, to be updated each time a peer
// query times out.
curPeer := s.blockManager.SyncPeer()
peerTries := make(map[string]uint8)
// This will be state used by the peer query goroutine.
quit := make(chan struct{})
subQuit := make(chan struct{})
// Increase this number to be able to handle more queries at once as
// each channel gets results for all queries, otherwise messages can
// get mixed and there's a vicious cycle of retries causing a bigger
// message flood, more of which get missed.
msgChan := make(chan spMsg)
subscription := spMsgSubscription{
msgChan: msgChan,
quitChan: subQuit,
}
// Loop for any messages sent to us via our subscription channel and
// check them for whether they satisfy the query. Break the loop if
// it's time to quit.
peerTimeout := time.NewTicker(qo.timeout)
timeout := time.After(qo.peerConnectTimeout)
if curPeer != nil {
peerTries[curPeer.Addr()]++
curPeer.subscribeRecvMsg(subscription)
curPeer.QueueMessageWithEncoding(queryMsg, nil,
wire.WitnessEncoding)
}
checkResponses:
for {
select {
case <-timeout:
// When we time out, we're done.
if curPeer != nil {
curPeer.unsubscribeRecvMsgs(subscription)
}
break checkResponses
case <-quit:
// Same when we get a quit signal.
if curPeer != nil {
curPeer.unsubscribeRecvMsgs(subscription)
}
break checkResponses
// A message has arrived over the subscription channel, so we
// execute the checkResponses callback to see if this ends our
// query session.
case sm := <-msgChan:
// TODO: This will get stuck if checkResponse gets
// stuck. This is a caveat for callers that should be
// fixed before exposing this function for public use.
checkResponse(sm.sp, sm.msg, quit)
// The current peer we're querying has failed to answer the
// query. Time to select a new peer and query it.
case <-peerTimeout.C:
if curPeer != nil {
curPeer.unsubscribeRecvMsgs(subscription)
}
curPeer = nil
for _, curPeer = range s.Peers() {
if curPeer != nil && curPeer.Connected() &&
peerTries[curPeer.Addr()] <
qo.numRetries {
// Found a peer we can query.
peerTries[curPeer.Addr()]++
curPeer.subscribeRecvMsg(subscription)
curPeer.QueueMessageWithEncoding(
queryMsg, nil,
wire.WitnessEncoding)
break
}
}
}
}
// Close the subscription quit channel and the done channel, if any.
close(subQuit)
peerTimeout.Stop()
if qo.doneChan != nil {
close(qo.doneChan)
}
}
// GetCFilter gets a cfilter from the database. Failing that, it requests the
// cfilter from the network and writes it to the database. If extended is true,
// an extended filter will be queried for. Otherwise, we'll fetch the regular
// filter.
func (s *ChainService) GetCFilter(blockHash chainhash.Hash,
filterType wire.FilterType, options ...QueryOption) (*gcs.Filter,
error) {
// Only get one CFilter at a time to avoid redundancy from mutliple
// rescans running at once.
s.mtxCFilter.Lock()
defer s.mtxCFilter.Unlock()
// Based on if extended is true or not, we'll set up our set of
// querying, and db-write functions.
getHeader := s.RegFilterHeaders.FetchHeader
dbFilterType := filterdb.RegularFilter
if filterType == wire.GCSFilterExtended {
getHeader = s.ExtFilterHeaders.FetchHeader
dbFilterType = filterdb.ExtendedFilter
}
// First check the database to see if we already have this filter. If
// so, then we can return it an exit early.
filter, err := s.FilterDB.FetchFilter(&blockHash, dbFilterType)
if err == nil && filter != nil {
return filter, nil
}
// We didn't get the filter from the DB, so we'll set it to nil and try
// to get it from the network.
filter = nil
// In order to verify the authenticity of the filter, we'll fetch the
// target block header so we can retrieve the hash of the prior block,
// which is required to fetch the filter header for that block.
block, height, err := s.BlockHeaders.FetchHeader(&blockHash)
if err != nil {
return nil, err
}
if block.BlockHash() != blockHash {
return nil, fmt.Errorf("Couldn't get header for block %s "+
"from database", blockHash)
}
// In addition to fetching the block header, we'll fetch the filter
// headers (for this particular filter type) from the database. These
// are required in order to verify the authenticity of the filter.
curHeader, err := getHeader(&blockHash)
if err != nil {
return nil, fmt.Errorf("Couldn't get cfheader for block %s "+
"from database", blockHash)
}
prevHeader, err := getHeader(&block.PrevBlock)
if err != nil {
return nil, fmt.Errorf("Couldn't get cfheader for block %s "+
"from database", blockHash)
}
// With all the necessary items retrieved, we'll launch our concurrent
// query to the set of connected peers.
s.queryPeers(
// Send a wire.MsgGetCFilters
wire.NewMsgGetCFilters(filterType, height, &blockHash),
// Check responses and if we get one that matches, end the
// query early.
func(sp *ServerPeer, resp wire.Message, quit chan<- struct{}) {
switch response := resp.(type) {
// We're only interested in "cfilter" messages.
case *wire.MsgCFilter:
// Only keep this going if we haven't already
// found a filter, or we risk closing an
// already closed channel.
if filter != nil {
return
}
// If the response doesn't match our request.
// Ignore this message.
if blockHash != response.BlockHash ||
filterType != response.FilterType {
return
}
gotFilter, err := gcs.FromNBytes(
builder.DefaultP, response.Data)
if err != nil {
// Malformed filter data. We can ignore
// this message.
return
}
// Now that we have a proper filter, ensure
// that re-calculating the filter header hash
// for the header _after_ the filter in the
// chain checks out. If not, we can ignore this
// response.
if gotHeader, err := builder.
MakeHeaderForFilter(gotFilter,
*prevHeader); err != nil ||
gotHeader != *curHeader {
return
}
// At this point, the filter matches what we
// know about it and we declare it sane. We can
// kill the query and pass the response back to
// the caller.
filter = gotFilter
close(quit)
default:
}
},
options...,
)
// If we've found a filter, write it to the database for next time.
if filter != nil {
err := s.FilterDB.PutFilter(&blockHash, filter, dbFilterType)
if err != nil {
return nil, err
}
log.Tracef("Wrote filter for block %s, type %d",
blockHash, filterType)
}
return filter, nil
}
// GetBlockFromNetwork gets a block by requesting it from the network, one peer
// at a time, until one answers.
//
// TODO(roasbeef): add query option to indicate if the caller wants witness
// data or not.
func (s *ChainService) GetBlockFromNetwork(blockHash chainhash.Hash,
options ...QueryOption) (*btcutil.Block, error) {
// Fetch the corresponding block header from the database. If this
// isn't found, then we don't have the header for this block s we can't
// request it.
blockHeader, height, err := s.BlockHeaders.FetchHeader(&blockHash)
if err != nil || blockHeader.BlockHash() != blockHash {
return nil, fmt.Errorf("Couldn't get header for block %s "+
"from database", blockHash)
}
// Construct the appropriate getdata message to fetch the target block.
getData := wire.NewMsgGetData()
getData.AddInvVect(wire.NewInvVect(wire.InvTypeWitnessBlock,
&blockHash))
// The block is only updated from the checkResponse function argument,
// which is always called single-threadedly. We don't check the block
// until after the query is finished, so we can just write to it
// naively.
var foundBlock *btcutil.Block
s.queryPeers(
// Send a wire.GetDataMsg
getData,
// Check responses and if we get one that matches, end the
// query early.
func(sp *ServerPeer, resp wire.Message,
quit chan<- struct{}) {
switch response := resp.(type) {
// We're only interested in "block" messages.
case *wire.MsgBlock:
// Only keep this going if we haven't already
// found a block, or we risk closing an already
// closed channel.
if foundBlock != nil {
return
}
// If this isn't our block, ignore it.
if response.BlockHash() != blockHash {
return
}
block := btcutil.NewBlock(response)
// Only set height if btcutil hasn't
// automagically put one in.
if block.Height() == btcutil.BlockHeightUnknown {
block.SetHeight(int32(height))
}
// If this claims our block but doesn't pass
// the sanity check, the peer is trying to
// bamboozle us. Disconnect it.
if err := blockchain.CheckBlockSanity(
block,
// We don't need to check PoW because
// by the time we get here, it's been
// checked during header
// synchronization
s.chainParams.PowLimit,
s.timeSource,
); err != nil {
log.Warnf("Invalid block for %s "+
"received from %s -- "+
"disconnecting peer", blockHash,
sp.Addr())
sp.Disconnect()
return
}
// TODO(roasbeef): modify CheckBlockSanity to
// also check witness commitment
// At this point, the block matches what we
// know about it and we declare it sane. We can
// kill the query and pass the response back to
// the caller.
foundBlock = block
close(quit)
default:
}
},
options...,
)
if foundBlock == nil {
return nil, fmt.Errorf("Couldn't retrieve block %s from "+
"network", blockHash)
}
return foundBlock, nil
}
// SendTransaction sends a transaction to each peer. It returns an error if any
// peer rejects the transaction.
//
// TODO: Better privacy by sending to only one random peer and watching
// propagation, requires better peer selection support in query API.
func (s *ChainService) SendTransaction(tx *wire.MsgTx, options ...QueryOption) error {
var err error
s.queryPeers(
tx,
func(sp *ServerPeer, resp wire.Message, quit chan<- struct{}) {
switch response := resp.(type) {
case *wire.MsgReject:
if response.Hash == tx.TxHash() {
err = fmt.Errorf("Transaction %s "+
"rejected by %s: %s",
tx.TxHash(), sp.Addr(),
response.Reason)
log.Errorf(err.Error())
close(quit)
}
}
},
options...,
)
return err
}