forked from gcash/neutrino
/
broadcaster.go
295 lines (247 loc) · 8 KB
/
broadcaster.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
package pushtx
import (
"errors"
"fmt"
"sync"
"time"
"github.com/dcrlabs/neutrino-bch/blockntfns"
"github.com/dcrlabs/bchwallet/wtxmgr"
"github.com/gcash/bchd/chaincfg/chainhash"
"github.com/gcash/bchd/wire"
)
var (
// ErrBroadcasterStopped is an error returned when we attempt to process a
// request to broadcast a transaction but the Broadcaster has already
// been stopped.
ErrBroadcasterStopped = errors.New("broadcaster has been stopped")
)
const (
// DefaultBroadcastTimeout is the default timeout used when broadcasting
// transactions to network peers.
DefaultBroadcastTimeout = 5 * time.Second
// DefaultRebroadcastInterval is the default period that we'll wait
// between blocks to attempt another rebroadcast.
DefaultRebroadcastInterval = time.Minute
)
// broadcastReq is an internal message the Broadcaster will use to process
// transaction broadcast requests.
type broadcastReq struct {
tx *wire.MsgTx
errChan chan error
}
// Config contains all of the external dependencies required for the Broadcaster
// to properly carry out its duties.
type Config struct {
// Broadcast broadcasts a transaction to the network. We expect certain
// BroadcastError's to be returned to handle special cases, namely
// errors with the codes Mempool and Confirmed.
Broadcast func(*wire.MsgTx) error
// SubscribeBlocks returns a block subscription that delivers block
// notifications in order. This will be used to rebroadcast all
// transactions once a new block arrives.
SubscribeBlocks func() (*blockntfns.Subscription, error)
// RebroadcastInterval is the interval that we'll continually try to
// re-broadcast transactions in-between new block arrival.
RebroadcastInterval time.Duration
}
// Broadcaster is a subsystem responsible for reliably broadcasting transactions
// to the network. Each transaction will be rebroadcast upon every new block
// being connected/disconnected to/from the chain.
type Broadcaster struct {
start sync.Once
stop sync.Once
cfg Config
// broadcastReqs is a channel through which new transaction broadcast
// requests from external callers will be streamed through.
broadcastReqs chan *broadcastReq
quit chan struct{}
wg sync.WaitGroup
}
// NewBroadcaster creates a new Broadcaster backed by the given config.
func NewBroadcaster(cfg *Config) *Broadcaster {
b := &Broadcaster{
cfg: *cfg,
broadcastReqs: make(chan *broadcastReq),
quit: make(chan struct{}),
}
return b
}
// Start starts all of the necessary steps for the Broadcaster to begin properly
// carrying out its duties.
func (b *Broadcaster) Start() error {
var err error
b.start.Do(func() {
sub, err := b.cfg.SubscribeBlocks()
if err != nil {
err = fmt.Errorf("unable to subscribe for block "+
"notifications: %v", err)
return
}
b.wg.Add(1)
go b.broadcastHandler(sub)
})
return err
}
// Stop halts the Broadcaster from rebroadcasting pending transactions.
func (b *Broadcaster) Stop() {
b.stop.Do(func() {
close(b.quit)
b.wg.Wait()
})
}
// broadcastHandler is the main event handler of the Broadcaster responsible for
// handling new broadcast requests, rebroadcasting transactions upon every new
// block, etc.
//
// NOTE: This must be run as a goroutine.
func (b *Broadcaster) broadcastHandler(sub *blockntfns.Subscription) {
defer b.wg.Done()
defer sub.Cancel()
log.Infof("Broadcaster now active")
// transactions is the set of transactions we have broadcast so far,
// and are still not confirmed.
transactions := make(map[chainhash.Hash]*wire.MsgTx)
// confChan is a channel used to notify the broadcast handler about
// confirmed transactions.
confChan := make(chan chainhash.Hash)
// The rebroadcast semaphore is used to ensure we have only one
// rebroadcast running at a time.
rebroadcastSem := make(chan struct{}, 1)
rebroadcastSem <- struct{}{}
// triggerRebroadcast is a helper method that checks whether the
// rebroadcast semaphore is available, and if it is spawns a goroutine
// to rebroadcast all pending transactions.
triggerRebroadcast := func() {
select {
// If the rebroadcast semaphore is available, start a
// new goroutine to exectue a rebroadcast.
case <-rebroadcastSem:
default:
log.Tracef("Existing rebroadcast still in " +
"progress")
return
}
// Make a copy of the current set of transactions to hand to
// the goroutine.
txs := make(map[chainhash.Hash]*wire.MsgTx)
for k, v := range transactions {
txs[k] = v.Copy()
}
b.wg.Add(1)
go func() {
defer b.wg.Done()
b.rebroadcast(txs, confChan)
rebroadcastSem <- struct{}{}
}()
}
reBroadcastTicker := time.NewTicker(b.cfg.RebroadcastInterval)
defer reBroadcastTicker.Stop()
for {
select {
// A new broadcast request was submitted by an external caller.
case req := <-b.broadcastReqs:
err := b.cfg.Broadcast(req.tx)
if err != nil && !IsBroadcastError(err, Mempool) {
log.Errorf("Broadcast attempt failed: %v", err)
req.errChan <- err
continue
}
transactions[req.tx.TxHash()] = req.tx
req.errChan <- nil
// A tx was confirmed, and we can remove it from our set of
// transactions.
case txHash := <-confChan:
delete(transactions, txHash)
// A new block notification has arrived, so we'll rebroadcast
// all of our pending transactions.
case _, ok := <-sub.Notifications:
if !ok {
log.Warn("Unable to rebroadcast transactions: " +
"block subscription was canceled")
continue
}
triggerRebroadcast()
// Between blocks, we'll also try to attempt additional
// re-broadcasts to ensure a timely confirmation.
case <-reBroadcastTicker.C:
triggerRebroadcast()
case <-b.quit:
return
}
}
}
// rebroadcast rebroadcasts all of the currently pending transactions. Care has
// been taken to ensure that the transactions are sorted in their dependency
// order to prevent peers from deeming our transactions as invalid due to
// broadcasting them before their pending dependencies.
func (b *Broadcaster) rebroadcast(txs map[chainhash.Hash]*wire.MsgTx,
confChan chan<- chainhash.Hash) {
// Return immediately if there are no transactions to re-broadcast.
if len(txs) == 0 {
return
}
log.Debugf("Re-broadcasting %d transactions", len(txs))
sortedTxs := wtxmgr.DependencySort(txs)
for _, tx := range sortedTxs {
// Before attempting to broadcast this transaction, we check
// whether we are shutting down.
select {
case <-b.quit:
return
default:
}
err := b.cfg.Broadcast(tx)
switch {
// If the transaction has already confirmed on-chain, we can
// stop broadcasting it further.
//
// TODO(wilmer); This should ideally be implemented by checking
// the chain ourselves rather than trusting our peers.
case IsBroadcastError(err, Confirmed):
log.Debugf("Re-broadcast of txid=%v, now confirmed!",
tx.TxHash())
select {
case confChan <- tx.TxHash():
case <-b.quit:
return
}
continue
// If the transaction already exists within our peers' mempool,
// we'll continue to rebroadcast it to ensure it actually
// propagates throughout the network.
//
// TODO(wilmer): Rate limit peers that have already accepted our
// transaction into their mempool to prevent resending to them
// every time.
case IsBroadcastError(err, Mempool):
log.Debugf("Re-broadcast of txid=%v, still "+
"pending...", tx.TxHash())
continue
case err != nil:
log.Errorf("Unable to rebroadcast transaction %v: %v",
tx.TxHash(), err)
continue
}
}
}
// Broadcast submits a request to the Broadcaster to reliably broadcast the
// given transaction. An error won't be returned if the transaction already
// exists within the mempool. Any transaction broadcast through this method will
// be rebroadcast upon every change of the tip of the chain.
func (b *Broadcaster) Broadcast(tx *wire.MsgTx) error {
errChan := make(chan error, 1)
select {
case b.broadcastReqs <- &broadcastReq{
tx: tx,
errChan: errChan,
}:
case <-b.quit:
return ErrBroadcasterStopped
}
select {
case err := <-errChan:
return err
case <-b.quit:
return ErrBroadcasterStopped
}
}