forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossiper.go
523 lines (467 loc) · 13.7 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// (c) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"container/heap"
"math/big"
"sync"
"time"
"github.com/MetalBlockchain/metalgo/codec"
"github.com/MetalBlockchain/coreth/peer"
"github.com/MetalBlockchain/metalgo/cache"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow"
"github.com/MetalBlockchain/metalgo/utils/wrappers"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/MetalBlockchain/coreth/core"
"github.com/MetalBlockchain/coreth/core/state"
"github.com/MetalBlockchain/coreth/core/txpool"
"github.com/MetalBlockchain/coreth/core/types"
"github.com/MetalBlockchain/coreth/plugin/evm/message"
)
const (
// We allow [recentCacheSize] to be fairly large because we only store hashes
// in the cache, not entire transactions.
recentCacheSize = 512
// [ethTxsGossipInterval] is how often we attempt to gossip newly seen
// transactions to other nodes.
ethTxsGossipInterval = 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 {
// GossipAtomicTxs sends AppGossip message containing the given [txs]
GossipAtomicTxs(txs []*Tx) error
// GossipEthTxs sends AppGossip message containing the given [txs]
GossipEthTxs(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
atomicMempool *Mempool
// We attempt to batch transactions we need to gossip to avoid runaway
// amplification of mempol chatter.
ethTxsToGossipChan chan []*types.Transaction
ethTxsToGossip map[common.Hash]*types.Transaction
lastGossiped time.Time
shutdownChan chan struct{}
shutdownWg *sync.WaitGroup
// [recentAtomicTxs] and [recentEthTxs] prevent us from over-gossiping the
// same transaction in a short period of time.
recentAtomicTxs *cache.LRU[ids.ID, interface{}]
recentEthTxs *cache.LRU[common.Hash, interface{}]
codec codec.Manager
stats GossipSentStats
}
// createGossiper constructs and returns a pushGossiper or noopGossiper
// based on whether vm.chainConfig.ApricotPhase4BlockTimestamp 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,
atomicMempool: vm.mempool,
ethTxsToGossipChan: make(chan []*types.Transaction),
ethTxsToGossip: make(map[common.Hash]*types.Transaction),
shutdownChan: vm.shutdownChan,
shutdownWg: &vm.shutdownWg,
recentAtomicTxs: &cache.LRU[ids.ID, interface{}]{Size: recentCacheSize},
recentEthTxs: &cache.LRU[common.Hash, interface{}]{Size: recentCacheSize},
codec: vm.networkCodec,
stats: stats,
}
net.awaitEthTxGossip()
return net
}
// queueExecutableTxs attempts to select up to [maxTxs] from the tx pool for
// regossiping.
//
// 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, maxTxs int) types.Transactions {
// Setup heap for transactions
heads := make(types.TxByPriceAndTime, 0, len(txs))
for addr, accountTxs := range txs {
// Short-circuit here to avoid performing an unnecessary state lookup
if len(accountTxs) == 0 {
continue
}
// Ensure any transactions regossiped are immediately executable
var (
currentNonce = state.GetNonce(addr)
tx *types.Transaction
)
for _, accountTx := range accountTxs {
// 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.
if accountTx.Nonce() == currentNonce {
tx = accountTx
break
}
// There may be gaps in the tx pool and we could jump past the nonce we'd
// like to execute.
if accountTx.Nonce() > currentNonce {
break
}
}
if tx == nil {
continue
}
// Don't try to regossip a transaction too frequently
if time.Since(tx.FirstSeen()) < n.config.RegossipFrequency.Duration {
continue
}
// Ensure the fee the transaction pays is valid at tip
wrapped, err := types.NewTxWithMinerFee(tx, baseFee)
if err != nil {
log.Debug(
"not queuing tx for regossip",
"tx", tx.Hash(),
"err", err,
)
continue
}
heads = append(heads, wrapped)
}
heap.Init(&heads)
// Add up to [maxTxs] transactions to be gossiped
queued := make([]*types.Transaction, 0, maxTxs)
for len(heads) > 0 && len(queued) < maxTxs {
tx := heads[0].Tx
queued = append(queued, tx)
heap.Pop(&heads)
}
return queued
}
// queueRegossipTxs finds the best transactions in the mempool and adds up to
// [TxRegossipMaxSize] of them to [ethTxsToGossip].
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
}
localQueued := n.queueExecutableTxs(state, tip.BaseFee, localTxs, n.config.RegossipMaxTxs)
localCount := len(localQueued)
n.stats.IncEthTxsRegossipQueuedLocal(localCount)
if localCount >= n.config.RegossipMaxTxs {
n.stats.IncEthTxsRegossipQueued()
return localQueued
}
remoteQueued := n.queueExecutableTxs(state, tip.BaseFee, remoteTxs, n.config.RegossipMaxTxs-localCount)
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...)
}
// awaitEthTxGossip periodically gossips transactions that have been queued for
// gossip at least once every [ethTxsGossipInterval].
func (n *pushGossiper) awaitEthTxGossip() {
n.shutdownWg.Add(1)
go n.ctx.Log.RecoverAndPanic(func() {
var (
gossipTicker = time.NewTicker(ethTxsGossipInterval)
regossipTicker = time.NewTicker(n.config.RegossipFrequency.Duration)
)
defer func() {
gossipTicker.Stop()
regossipTicker.Stop()
n.shutdownWg.Done()
}()
for {
select {
case <-gossipTicker.C:
if attempted, err := n.gossipEthTxs(false); err != nil {
log.Warn(
"failed to send eth transactions",
"len(txs)", attempted,
"err", err,
)
}
case <-regossipTicker.C:
for _, tx := range n.queueRegossipTxs() {
n.ethTxsToGossip[tx.Hash()] = tx
}
if attempted, err := n.gossipEthTxs(true); err != nil {
log.Warn(
"failed to send eth transactions",
"len(txs)", attempted,
"err", err,
)
}
case txs := <-n.ethTxsToGossipChan:
for _, tx := range txs {
n.ethTxsToGossip[tx.Hash()] = tx
}
if attempted, err := n.gossipEthTxs(false); err != nil {
log.Warn(
"failed to send eth transactions",
"len(txs)", attempted,
"err", err,
)
}
case <-n.shutdownChan:
return
}
}
})
}
func (n *pushGossiper) GossipAtomicTxs(txs []*Tx) error {
errs := wrappers.Errs{}
for _, tx := range txs {
errs.Add(n.gossipAtomicTx(tx))
}
return errs.Err
}
func (n *pushGossiper) gossipAtomicTx(tx *Tx) error {
txID := tx.ID()
// Don't gossip transaction if it has been recently gossiped.
if _, has := n.recentAtomicTxs.Get(txID); has {
return nil
}
// If the transaction is not pending according to the mempool
// then there is no need to gossip it further.
if _, pending := n.atomicMempool.GetPendingTx(txID); !pending {
return nil
}
n.recentAtomicTxs.Put(txID, nil)
msg := message.AtomicTxGossip{
Tx: tx.SignedBytes(),
}
msgBytes, err := message.BuildGossipMessage(n.codec, msg)
if err != nil {
return err
}
log.Trace(
"gossiping atomic tx",
"txID", txID,
)
n.stats.IncAtomicGossipSent()
return n.client.Gossip(msgBytes)
}
func (n *pushGossiper) sendEthTxs(txs []*types.Transaction) error {
if len(txs) == 0 {
return nil
}
txBytes, err := rlp.EncodeToBytes(txs)
if err != nil {
return err
}
msg := message.EthTxsGossip{
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) gossipEthTxs(force bool) (int, error) {
if (!force && time.Since(n.lastGossiped) < minGossipBatchInterval) || len(n.ethTxsToGossip) == 0 {
return 0, nil
}
n.lastGossiped = time.Now()
txs := make([]*types.Transaction, 0, len(n.ethTxsToGossip))
for _, tx := range n.ethTxsToGossip {
txs = append(txs, tx)
delete(n.ethTxsToGossip, tx.Hash())
}
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.recentEthTxs.Get(txHash); has {
continue
}
}
n.recentEthTxs.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.EthMsgSoftCapSize {
if err := n.sendEthTxs(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.sendEthTxs(msgTxs)
}
// GossipEthTxs 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) GossipEthTxs(txs []*types.Transaction) error {
select {
case n.ethTxsToGossipChan <- txs:
case <-n.shutdownChan:
}
return nil
}
// GossipHandler handles incoming gossip messages
type GossipHandler struct {
vm *VM
atomicMempool *Mempool
txPool *txpool.TxPool
stats GossipReceivedStats
}
func NewGossipHandler(vm *VM, stats GossipReceivedStats) *GossipHandler {
return &GossipHandler{
vm: vm,
atomicMempool: vm.mempool,
txPool: vm.txPool,
stats: stats,
}
}
func (h *GossipHandler) HandleAtomicTx(nodeID ids.NodeID, msg message.AtomicTxGossip) error {
log.Trace(
"AppGossip called with AtomicTxGossip",
"peerID", nodeID,
)
if len(msg.Tx) == 0 {
log.Trace(
"AppGossip received empty AtomicTxGossip Message",
"peerID", nodeID,
)
return nil
}
// In the case that the gossip message contains a transaction,
// attempt to parse it and add it as a remote.
tx := Tx{}
if _, err := Codec.Unmarshal(msg.Tx, &tx); err != nil {
log.Trace(
"AppGossip provided invalid tx",
"err", err,
)
return nil
}
unsignedBytes, err := Codec.Marshal(codecVersion, &tx.UnsignedAtomicTx)
if err != nil {
log.Trace(
"AppGossip failed to marshal unsigned tx",
"err", err,
)
return nil
}
tx.Initialize(unsignedBytes, msg.Tx)
txID := tx.ID()
h.stats.IncAtomicGossipReceived()
if _, dropped, found := h.atomicMempool.GetTx(txID); found {
h.stats.IncAtomicGossipReceivedKnown()
return nil
} else if dropped {
h.stats.IncAtomicGossipReceivedDropped()
return nil
}
h.stats.IncAtomicGossipReceivedNew()
if err := h.vm.issueTx(&tx, false /*=local*/); err != nil {
log.Trace(
"AppGossip provided invalid transaction",
"peerID", nodeID,
"err", err,
)
}
return nil
}
func (h *GossipHandler) HandleEthTxs(nodeID ids.NodeID, msg message.EthTxsGossip) error {
log.Trace(
"AppGossip called with EthTxsGossip",
"peerID", nodeID,
"size(txs)", len(msg.Txs),
)
if len(msg.Txs) == 0 {
log.Trace(
"AppGossip received empty EthTxsGossip 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()
} else {
h.stats.IncAtomicGossipReceivedError()
}
continue
}
h.stats.IncEthTxsGossipReceivedNew()
}
return nil
}