Skip to content

Commit e937057

Browse files
committedAug 9, 2024·
add inclusion delay
1 parent 8a5d797 commit e937057

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed
 

‎mempool/cat/pool.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ var (
2929
ErrTxRecentlyCommitted = errors.New("tx was recently committed")
3030
)
3131

32+
// InclusionDelay is the amount of time a transaction must be in the mempool
33+
// before it is included in the block.
34+
const InclusionDelay = time.Second
35+
3236
// TxPoolOption sets an optional parameter on the TxPool.
3337
type TxPoolOption func(*TxPool)
3438

@@ -452,9 +456,16 @@ func (txmp *TxPool) allEntriesSorted() []*wrappedTx {
452456
// constraints, the result will also be empty.
453457
func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
454458
var totalGas, totalBytes int64
459+
currentTime := time.Now()
455460

456461
var keep []types.Tx //nolint:prealloc
457462
for _, w := range txmp.allEntriesSorted() {
463+
// skip transactions that have been in the mempool for less than the inclusion delay
464+
// This gives time for the transaction to be broadcast to all peers
465+
if currentTime.Sub(w.timestamp) < InclusionDelay {
466+
break
467+
}
468+
458469
// N.B. When computing byte size, we need to include the overhead for
459470
// encoding as protobuf to send to the application. This actually overestimates it
460471
// as we add the proto overhead to each transaction
@@ -479,8 +490,11 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
479490
// does not have that many transactions available.
480491
func (txmp *TxPool) ReapMaxTxs(max int) types.Txs {
481492
var keep []types.Tx //nolint:prealloc
482-
493+
currentTime := time.Now()
483494
for _, w := range txmp.allEntriesSorted() {
495+
if currentTime.Sub(w.timestamp) < InclusionDelay {
496+
break
497+
}
484498
if max >= 0 && len(keep) >= max {
485499
break
486500
}

‎mempool/cat/reactor.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
340340
}
341341
if has && !memR.opts.ListenOnly {
342342
peerID := memR.ids.GetIDForPeer(e.Src.ID())
343-
memR.Logger.Info("sending a tx in response to a want msg", "peer", peerID)
343+
memR.Logger.Info("sending a tx in response to a want msg", "peer", peerID, "txKey", txKey)
344344
if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck
345345
ChannelID: mempool.MempoolChannel,
346346
Message: &protomem.Txs{Txs: [][]byte{tx}},
@@ -399,10 +399,11 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) {
399399
continue
400400
}
401401

402-
if peer.Send(MempoolStateChannel, bz) {
403-
memR.Logger.Info("sent seen tx to peer", "peerID", peer.ID(), "txKey", txKey)
402+
if !peer.Send(MempoolStateChannel, bz) {
403+
memR.Logger.Error("failed to send seen tx to peer", "peerID", peer.ID(), "txKey", txKey)
404404
}
405405
}
406+
memR.Logger.Info("broadcasted seen tx to all peers", "tx_key", txKey.String())
406407
}
407408

408409
// broadcastNewTx broadcast new transaction to all peers unless we are already sure they have seen the tx.

0 commit comments

Comments
 (0)