mirror of
				https://github.com/0glabs/0g-chain.git
				synced 2025-11-04 03:27:26 +00:00 
			
		
		
		
	add mempool tx replace callback
This commit is contained in:
		
							parent
							
								
									c2fdb3109e
								
							
						
					
					
						commit
						384d899eff
					
				@ -38,6 +38,8 @@ type PriorityNonceMempool struct {
 | 
				
			|||||||
	maxTx           int
 | 
						maxTx           int
 | 
				
			||||||
	counterBySender map[string]int
 | 
						counterBySender map[string]int
 | 
				
			||||||
	txRecord        map[txMeta]struct{}
 | 
						txRecord        map[txMeta]struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						txReplacedCallback func(ctx context.Context, oldTx, newTx sdk.Tx)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type PriorityNonceIterator struct {
 | 
					type PriorityNonceIterator struct {
 | 
				
			||||||
@ -124,6 +126,12 @@ func PriorityNonceWithMaxTx(maxTx int) PriorityNonceMempoolOption {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func PriorityNonceWithTxReplacedCallback(cb func(ctx context.Context, oldTx, newTx sdk.Tx)) PriorityNonceMempoolOption {
 | 
				
			||||||
 | 
						return func(mp *PriorityNonceMempool) {
 | 
				
			||||||
 | 
							mp.txReplacedCallback = cb
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DefaultPriorityMempool returns a priorityNonceMempool with no options.
 | 
					// DefaultPriorityMempool returns a priorityNonceMempool with no options.
 | 
				
			||||||
func DefaultPriorityMempool() mempool.Mempool {
 | 
					func DefaultPriorityMempool() mempool.Mempool {
 | 
				
			||||||
	return NewPriorityMempool()
 | 
						return NewPriorityMempool()
 | 
				
			||||||
@ -184,6 +192,7 @@ func (mp *PriorityNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error {
 | 
				
			|||||||
	sdkContext := sdk.UnwrapSDKContext(ctx)
 | 
						sdkContext := sdk.UnwrapSDKContext(ctx)
 | 
				
			||||||
	priority := sdkContext.Priority()
 | 
						priority := sdkContext.Priority()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var replacedTx sdk.Tx
 | 
				
			||||||
	var sender string
 | 
						var sender string
 | 
				
			||||||
	var nonce uint64
 | 
						var nonce uint64
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -244,6 +253,7 @@ func (mp *PriorityNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error {
 | 
				
			|||||||
	//
 | 
						//
 | 
				
			||||||
	// This O(log n) remove operation is rare and only happens when a tx's priority
 | 
						// This O(log n) remove operation is rare and only happens when a tx's priority
 | 
				
			||||||
	// changes.
 | 
						// changes.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sk := txMeta{nonce: nonce, sender: sender}
 | 
						sk := txMeta{nonce: nonce, sender: sender}
 | 
				
			||||||
	if oldScore, txExists := mp.scores[sk]; txExists {
 | 
						if oldScore, txExists := mp.scores[sk]; txExists {
 | 
				
			||||||
		if mp.txReplacement != nil && !mp.txReplacement(oldScore.priority, priority, senderIndex.Get(key).Value.(sdk.Tx), tx) {
 | 
							if mp.txReplacement != nil && !mp.txReplacement(oldScore.priority, priority, senderIndex.Get(key).Value.(sdk.Tx), tx) {
 | 
				
			||||||
@ -256,12 +266,13 @@ func (mp *PriorityNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error {
 | 
				
			|||||||
			)
 | 
								)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		mp.priorityIndex.Remove(txMeta{
 | 
							e := mp.priorityIndex.Remove(txMeta{
 | 
				
			||||||
			nonce:    nonce,
 | 
								nonce:    nonce,
 | 
				
			||||||
			sender:   sender,
 | 
								sender:   sender,
 | 
				
			||||||
			priority: oldScore.priority,
 | 
								priority: oldScore.priority,
 | 
				
			||||||
			weight:   oldScore.weight,
 | 
								weight:   oldScore.weight,
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
							replacedTx = e.Value.(sdk.Tx)
 | 
				
			||||||
		mp.priorityCounts[oldScore.priority]--
 | 
							mp.priorityCounts[oldScore.priority]--
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -274,6 +285,10 @@ func (mp *PriorityNonceMempool) Insert(ctx context.Context, tx sdk.Tx) error {
 | 
				
			|||||||
	mp.scores[sk] = txMeta{priority: priority}
 | 
						mp.scores[sk] = txMeta{priority: priority}
 | 
				
			||||||
	mp.priorityIndex.Set(key, tx)
 | 
						mp.priorityIndex.Set(key, tx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if mp.txReplacedCallback != nil && replacedTx != nil {
 | 
				
			||||||
 | 
							mp.txReplacedCallback(ctx, replacedTx, tx)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
@ -129,6 +130,9 @@ func (ac appCreator) newApp(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	mempool := app.NewPriorityMempool(
 | 
						mempool := app.NewPriorityMempool(
 | 
				
			||||||
		app.PriorityNonceWithMaxTx(cast.ToInt(appOpts.Get(server.FlagMempoolMaxTxs))),
 | 
							app.PriorityNonceWithMaxTx(cast.ToInt(appOpts.Get(server.FlagMempoolMaxTxs))),
 | 
				
			||||||
 | 
							app.PriorityNonceWithTxReplacedCallback(func(ctx context.Context, oldTx, newTx sdk.Tx) {
 | 
				
			||||||
 | 
								bApp.RegisterMempoolTxReplacedEvent(ctx, oldTx, newTx)
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	bApp.SetMempool(mempool)
 | 
						bApp.SetMempool(mempool)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user