-
Notifications
You must be signed in to change notification settings - Fork 217
/
gossiper.go
456 lines (410 loc) · 12.2 KB
/
gossiper.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
// (c) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"math/big"
"sync"
"time"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/subnet-evm/peer"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/state"
"github.com/ava-labs/subnet-evm/core/txpool"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/plugin/evm/message"
)
const (
// We allow [recentCacheSize] to be fairly large because we only store hashes
// in the cache, not entire transactions.
recentCacheSize = 512
// [txsGossipInterval] is how often we attempt to gossip newly seen
// transactions to other nodes.
txsGossipInterval = 500 * time.Millisecond
// [minGossipBatchInterval] is the minimum amount of time that must pass
// before our last gossip to peers.
minGossipBatchInterval = 50 * time.Millisecond
)
// Gossiper handles outgoing gossip of transactions
type Gossiper interface {
// GossipTxs sends AppGossip message containing the given [txs]
GossipTxs(txs []*types.Transaction) error
}
// pushGossiper is used to gossip transactions to the network
type pushGossiper struct {
ctx *snow.Context
config Config
client peer.NetworkClient
blockchain *core.BlockChain
txPool *txpool.TxPool
// We attempt to batch transactions we need to gossip to avoid runaway
// amplification of mempol chatter.
txsToGossipChan chan []*types.Transaction
txsToGossip map[common.Hash]*types.Transaction
lastGossiped time.Time
shutdownChan chan struct{}
shutdownWg *sync.WaitGroup
// [recentTxs] prevent us from over-gossiping the
// same transaction in a short period of time.
recentTxs *cache.LRU[common.Hash, interface{}]
codec codec.Manager
signer types.Signer
stats GossipSentStats
}
// createGossiper constructs and returns a pushGossiper or noopGossiper
// based on whether vm.chainConfig.SubnetEVMTimestamp is set
func (vm *VM) createGossiper(stats GossipStats) Gossiper {
net := &pushGossiper{
ctx: vm.ctx,
config: vm.config,
client: vm.client,
blockchain: vm.blockChain,
txPool: vm.txPool,
txsToGossipChan: make(chan []*types.Transaction),
txsToGossip: make(map[common.Hash]*types.Transaction),
shutdownChan: vm.shutdownChan,
shutdownWg: &vm.shutdownWg,
recentTxs: &cache.LRU[common.Hash, interface{}]{Size: recentCacheSize},
codec: vm.networkCodec,
signer: types.LatestSigner(vm.blockChain.Config()),
stats: stats,
}
net.awaitEthTxGossip()
return net
}
// addrStatus used to track the metadata of addresses being queued for
// regossip.
type addrStatus struct {
nonce uint64
txsAdded int
}
// queueExecutableTxs attempts to select up to [maxTxs] from the tx pool for
// regossiping (with at most [maxAcctTxs] per account).
//
// We assume that [txs] contains an array of nonce-ordered transactions for a given
// account. This array of transactions can have gaps and start at a nonce lower
// than the current state of an account.
func (n *pushGossiper) queueExecutableTxs(
state *state.StateDB,
baseFee *big.Int,
txs map[common.Address]types.Transactions,
regossipFrequency Duration,
maxTxs int,
maxAcctTxs int,
) types.Transactions {
var (
stxs = types.NewTransactionsByPriceAndNonce(n.signer, txs, baseFee)
statuses = make(map[common.Address]*addrStatus)
queued = make([]*types.Transaction, 0, maxTxs)
)
// Iterate over possible transactions until there are none left or we have
// hit the regossip target.
for len(queued) < maxTxs {
next := stxs.Peek()
if next == nil {
break
}
sender, _ := types.Sender(n.signer, next)
status, ok := statuses[sender]
if !ok {
status = &addrStatus{
nonce: state.GetNonce(sender),
}
statuses[sender] = status
}
// The tx pool may be out of sync with current state, so we iterate
// through the account transactions until we get to one that is
// executable.
switch {
case next.Nonce() < status.nonce:
stxs.Shift()
continue
case next.Nonce() > status.nonce, time.Since(next.FirstSeen()) < regossipFrequency.Duration,
status.txsAdded >= maxAcctTxs:
stxs.Pop()
continue
}
queued = append(queued, next)
status.nonce++
status.txsAdded++
stxs.Shift()
}
return queued
}
// queueRegossipTxs finds the best non-priority transactions in the mempool and adds up to
// [RegossipMaxTxs] of them to [txsToGossip].
func (n *pushGossiper) queueRegossipTxs() types.Transactions {
// Fetch all pending transactions
pending := n.txPool.Pending(true)
// Split the pending transactions into locals and remotes
localTxs := make(map[common.Address]types.Transactions)
remoteTxs := pending
for _, account := range n.txPool.Locals() {
if txs := remoteTxs[account]; len(txs) > 0 {
delete(remoteTxs, account)
localTxs[account] = txs
}
}
// Add best transactions to be gossiped (preferring local txs)
tip := n.blockchain.CurrentBlock()
state, err := n.blockchain.StateAt(tip.Root)
if err != nil || state == nil {
log.Debug(
"could not get state at tip",
"tip", tip.Hash(),
"err", err,
)
return nil
}
rgFrequency := n.config.RegossipFrequency
rgMaxTxs := n.config.RegossipMaxTxs
rgTxsPerAddr := n.config.RegossipTxsPerAddress
localQueued := n.queueExecutableTxs(state, tip.BaseFee, localTxs, rgFrequency, rgMaxTxs, rgTxsPerAddr)
localCount := len(localQueued)
n.stats.IncEthTxsRegossipQueuedLocal(localCount)
if localCount >= rgMaxTxs {
n.stats.IncEthTxsRegossipQueued()
return localQueued
}
remoteQueued := n.queueExecutableTxs(state, tip.BaseFee, remoteTxs, rgFrequency, rgMaxTxs-localCount, rgTxsPerAddr)
n.stats.IncEthTxsRegossipQueuedRemote(len(remoteQueued))
if localCount+len(remoteQueued) > 0 {
// only increment the regossip stat when there are any txs queued
n.stats.IncEthTxsRegossipQueued()
}
return append(localQueued, remoteQueued...)
}
// queuePriorityRegossipTxs finds the best priority transactions in the mempool and adds up to
// [PriorityRegossipMaxTxs] of them to [txsToGossip].
func (n *pushGossiper) queuePriorityRegossipTxs() types.Transactions {
// Fetch all pending transactions from the priority addresses
priorityTxs := n.txPool.PendingFrom(n.config.PriorityRegossipAddresses, true)
// Add best transactions to be gossiped
tip := n.blockchain.CurrentBlock()
state, err := n.blockchain.StateAt(tip.Root)
if err != nil || state == nil {
log.Debug(
"could not get state at tip",
"tip", tip.Hash(),
"err", err,
)
return nil
}
return n.queueExecutableTxs(
state, tip.BaseFee, priorityTxs,
n.config.PriorityRegossipFrequency,
n.config.PriorityRegossipMaxTxs,
n.config.PriorityRegossipTxsPerAddress,
)
}
// awaitEthTxGossip periodically gossips transactions that have been queued for
// gossip at least once every [txsGossipInterval].
func (n *pushGossiper) awaitEthTxGossip() {
n.shutdownWg.Add(1)
go n.ctx.Log.RecoverAndPanic(func() {
var (
gossipTicker = time.NewTicker(txsGossipInterval)
regossipTicker = time.NewTicker(n.config.RegossipFrequency.Duration)
priorityRegossipTicker = time.NewTicker(n.config.PriorityRegossipFrequency.Duration)
)
defer func() {
gossipTicker.Stop()
regossipTicker.Stop()
priorityRegossipTicker.Stop()
n.shutdownWg.Done()
}()
for {
select {
case <-gossipTicker.C:
if attempted, err := n.gossipTxs(false); err != nil {
log.Warn(
"failed to send eth transactions",
"len(txs)", attempted,
"err", err,
)
}
case <-regossipTicker.C:
for _, tx := range n.queueRegossipTxs() {
n.txsToGossip[tx.Hash()] = tx
}
if attempted, err := n.gossipTxs(true); err != nil {
log.Warn(
"failed to regossip eth transactions",
"len(txs)", attempted,
"err", err,
)
}
case <-priorityRegossipTicker.C:
for _, tx := range n.queuePriorityRegossipTxs() {
n.txsToGossip[tx.Hash()] = tx
}
if attempted, err := n.gossipTxs(true); err != nil {
log.Warn(
"failed to regossip priority eth transactions",
"len(txs)", attempted,
"err", err,
)
}
case txs := <-n.txsToGossipChan:
for _, tx := range txs {
n.txsToGossip[tx.Hash()] = tx
}
if attempted, err := n.gossipTxs(false); err != nil {
log.Warn(
"failed to send eth transactions",
"len(txs)", attempted,
"err", err,
)
}
case <-n.shutdownChan:
return
}
}
})
}
func (n *pushGossiper) sendTxs(txs []*types.Transaction) error {
if len(txs) == 0 {
return nil
}
txBytes, err := rlp.EncodeToBytes(txs)
if err != nil {
return err
}
msg := message.TxsGossip{
Txs: txBytes,
}
msgBytes, err := message.BuildGossipMessage(n.codec, msg)
if err != nil {
return err
}
log.Trace(
"gossiping eth txs",
"len(txs)", len(txs),
"size(txs)", len(msg.Txs),
)
n.stats.IncEthTxsGossipSent()
return n.client.Gossip(msgBytes)
}
func (n *pushGossiper) gossipTxs(force bool) (int, error) {
if (!force && time.Since(n.lastGossiped) < minGossipBatchInterval) || len(n.txsToGossip) == 0 {
return 0, nil
}
n.lastGossiped = time.Now()
txs := make([]*types.Transaction, 0, len(n.txsToGossip))
for txHash, tx := range n.txsToGossip {
txs = append(txs, tx)
delete(n.txsToGossip, txHash)
}
selectedTxs := make([]*types.Transaction, 0)
for _, tx := range txs {
txHash := tx.Hash()
txStatus := n.txPool.Status([]common.Hash{txHash})[0]
if txStatus != txpool.TxStatusPending {
continue
}
if n.config.RemoteGossipOnlyEnabled && n.txPool.HasLocal(txHash) {
continue
}
// We check [force] outside of the if statement to avoid an unnecessary
// cache lookup.
if !force {
if _, has := n.recentTxs.Get(txHash); has {
continue
}
}
n.recentTxs.Put(txHash, nil)
selectedTxs = append(selectedTxs, tx)
}
if len(selectedTxs) == 0 {
return 0, nil
}
// Attempt to gossip [selectedTxs]
msgTxs := make([]*types.Transaction, 0)
msgTxsSize := uint64(0)
for _, tx := range selectedTxs {
size := tx.Size()
if msgTxsSize+size > message.TxMsgSoftCapSize {
if err := n.sendTxs(msgTxs); err != nil {
return len(selectedTxs), err
}
msgTxs = msgTxs[:0]
msgTxsSize = 0
}
msgTxs = append(msgTxs, tx)
msgTxsSize += size
}
// Send any remaining [msgTxs]
return len(selectedTxs), n.sendTxs(msgTxs)
}
// GossipTxs enqueues the provided [txs] for gossiping. At some point, the
// [pushGossiper] will attempt to gossip the provided txs to other nodes
// (usually right away if not under load).
//
// NOTE: We never return a non-nil error from this function but retain the
// option to do so in case it becomes useful.
func (n *pushGossiper) GossipTxs(txs []*types.Transaction) error {
select {
case n.txsToGossipChan <- txs:
case <-n.shutdownChan:
}
return nil
}
// GossipHandler handles incoming gossip messages
type GossipHandler struct {
vm *VM
txPool *txpool.TxPool
stats GossipReceivedStats
}
func NewGossipHandler(vm *VM, stats GossipReceivedStats) *GossipHandler {
return &GossipHandler{
vm: vm,
txPool: vm.txPool,
stats: stats,
}
}
func (h *GossipHandler) HandleTxs(nodeID ids.NodeID, msg message.TxsGossip) error {
log.Trace(
"AppGossip called with TxsGossip",
"peerID", nodeID,
"size(txs)", len(msg.Txs),
)
if len(msg.Txs) == 0 {
log.Trace(
"AppGossip received empty TxsGossip Message",
"peerID", nodeID,
)
return nil
}
// The maximum size of this encoded object is enforced by the codec.
txs := make([]*types.Transaction, 0)
if err := rlp.DecodeBytes(msg.Txs, &txs); err != nil {
log.Trace(
"AppGossip provided invalid txs",
"peerID", nodeID,
"err", err,
)
return nil
}
h.stats.IncEthTxsGossipReceived()
errs := h.txPool.AddRemotes(txs)
for i, err := range errs {
if err != nil {
log.Trace(
"AppGossip failed to add to mempool",
"err", err,
"tx", txs[i].Hash(),
)
if err == txpool.ErrAlreadyKnown {
h.stats.IncEthTxsGossipReceivedKnown()
}
continue
}
h.stats.IncEthTxsGossipReceivedNew()
}
return nil
}