Skip to content

Commit

Permalink
set filter into txpool (#245)
Browse files Browse the repository at this point in the history
* set filter into txpool

* fix
  • Loading branch information
Lawliet-Chan authored Nov 19, 2024
1 parent a3f2de2 commit f1e0e94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/txpool/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type ItxPool interface {
WithBaseCheck(checkFn TxnChecker) ItxPool
WithTripodCheck(tripodName string, checker TxnChecker) ItxPool

SetPackFilter(fn func(txn *SignedTxn) bool)

BaseCheck(*SignedTxn) error
TripodsCheck(stxn *SignedTxn) error

Expand Down
13 changes: 10 additions & 3 deletions core/txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type TxPool struct {

baseChecks []TxnCheckFn
tripodChecks map[string]TxnCheckFn

filter func(txn *SignedTxn) bool
}

func NewTxPool(nodeType int, cfg *TxpoolConf) *TxPool {
Expand All @@ -32,6 +34,7 @@ func NewTxPool(nodeType int, cfg *TxpoolConf) *TxPool {
unpackedTxns: ordered,
baseChecks: make([]TxnCheckFn, 0),
tripodChecks: make(map[string]TxnCheckFn),
filter: func(*SignedTxn) bool { return true },
}
return tp
}
Expand All @@ -49,6 +52,10 @@ func (tp *TxPool) withDefaultBaseChecks() *TxPool {
return tp
}

func (tp *TxPool) SetPackFilter(fn func(txn *SignedTxn) bool) {
tp.filter = fn
}

func (tp *TxPool) Capacity() int {
return tp.capacity
}
Expand Down Expand Up @@ -115,9 +122,9 @@ func (tp *TxPool) GetAllTxns() ([]*SignedTxn, error) {
}

func (tp *TxPool) Pack(numLimit uint64) ([]*SignedTxn, error) {
return tp.PackFor(numLimit, func(*SignedTxn) bool {
return true
})
metrics.TxpoolSizeGauge.Set(float64(tp.unpackedTxns.Size()))
txns := tp.unpackedTxns.Gets(numLimit, tp.filter)
return txns, nil
}

func (tp *TxPool) PackFor(numLimit uint64, filter func(txn *SignedTxn) bool) ([]*SignedTxn, error) {
Expand Down

0 comments on commit f1e0e94

Please sign in to comment.