Skip to content

Commit 3910df6

Browse files
andyzhang2023andyzhang2023bnoieh
authored
fix: txpool metrics and unit test (bnb-chain#59)
Co-authored-by: andyzhang2023 <andyzhang2023@gmail.com> Co-authored-by: bnoieh <135800952+bnoieh@users.noreply.github.com>
1 parent 2a7bb9c commit 3910df6

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

core/txpool/invalid.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
)
66

77
const (
8-
AlreadyKnown = "AlreadyKnown"
98
TypeNotSupportDeposit = "TypeNotSupportDeposit"
109
TypeNotSupport1559 = "TypeNotSupport1559"
1110
TypeNotSupport2718 = "TypeNotSupport2718"
@@ -23,22 +22,17 @@ const (
2322
InsufficientFunds = "InsufficientFunds"
2423
Overdraft = "Overdraft"
2524
IntrinsicGas = "IntrinsicGas"
26-
Throttle = "Throttle"
27-
Overflow = "Overflow"
2825
FutureReplacePending = "FutureReplacePending"
29-
ReplaceUnderpriced = "ReplaceUnderpriced"
30-
QueuedDiscard = "QueueDiscard"
3126
GasUnitOverflow = "GasUnitOverflow"
3227
)
3328

34-
func meter(err string) metrics.Meter {
29+
func Meter(err string) metrics.Meter {
3530
return metrics.GetOrRegisterMeter("txpool/invalid/"+err, nil)
3631
}
3732

3833
func init() {
3934
// init the metrics
4035
for _, err := range []string{
41-
AlreadyKnown,
4236
TypeNotSupportDeposit,
4337
TypeNotSupport1559,
4438
TypeNotSupport2718,
@@ -56,13 +50,9 @@ func init() {
5650
InsufficientFunds,
5751
Overdraft,
5852
IntrinsicGas,
59-
Throttle,
60-
Overflow,
6153
FutureReplacePending,
62-
ReplaceUnderpriced,
63-
QueuedDiscard,
6454
GasUnitOverflow,
6555
} {
66-
meter(err).Mark(0)
56+
Meter(err).Mark(0)
6757
}
6858
}

core/txpool/legacypool/legacypool.go

+2
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ func (pool *LegacyPool) add(tx *types.Transaction, local bool) (replaced bool, e
829829
pool.priced.Put(dropTx, false)
830830
}
831831
log.Trace("Discarding future transaction replacing pending tx", "hash", hash)
832+
txpool.Meter(txpool.FutureReplacePending).Mark(1)
832833
return false, txpool.ErrFutureReplacePending
833834
}
834835
}
@@ -940,6 +941,7 @@ func (pool *LegacyPool) enqueueTx(hash common.Hash, tx *types.Transaction, local
940941
// If the transaction isn't in lookup set but it's expected to be there,
941942
// show the error log.
942943
if pool.all.Get(hash) == nil && !addAll {
944+
txpool.Meter(txpool.MissingTransaction).Mark(1)
943945
log.Error("Missing transaction in lookup set, please report the issue", "hash", hash)
944946
}
945947
if addAll {

core/txpool/validation.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
6767
// This is for spam protection, not consensus,
6868
// as the external engine-API user authenticates deposits.
6969
if tx.Type() == types.DepositTxType {
70-
meter(TypeNotSupportDeposit).Mark(1)
70+
Meter(TypeNotSupportDeposit).Mark(1)
7171
return core.ErrTxTypeNotSupported
7272
}
7373
// Ensure transactions not implemented by the calling pool are rejected
@@ -81,25 +81,25 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
8181
}
8282
// Ensure only transactions that have been enabled are accepted
8383
if !opts.Config.IsBerlin(head.Number) && tx.Type() != types.LegacyTxType {
84-
meter(TypeNotSupport2718).Mark(1)
84+
Meter(TypeNotSupport2718).Mark(1)
8585
return fmt.Errorf("%w: type %d rejected, pool not yet in Berlin", core.ErrTxTypeNotSupported, tx.Type())
8686
}
8787
if !opts.Config.IsLondon(head.Number) && tx.Type() == types.DynamicFeeTxType {
88-
meter(TypeNotSupport1559).Mark(1)
88+
Meter(TypeNotSupport1559).Mark(1)
8989
return fmt.Errorf("%w: type %d rejected, pool not yet in London", core.ErrTxTypeNotSupported, tx.Type())
9090
}
9191
if !opts.Config.IsCancun(head.Number, head.Time) && tx.Type() == types.BlobTxType {
9292
return fmt.Errorf("%w: type %d rejected, pool not yet in Cancun", core.ErrTxTypeNotSupported, tx.Type())
9393
}
9494
// Check whether the init code size has been exceeded
9595
if opts.Config.IsShanghai(head.Number, head.Time) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize {
96-
meter(MaxInitCodeSizeExceeded).Mark(1)
96+
Meter(MaxInitCodeSizeExceeded).Mark(1)
9797
return fmt.Errorf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize)
9898
}
9999
// Transactions can't be negative. This may never happen using RLP decoded
100100
// transactions but may occur for transactions created using the RPC.
101101
if tx.Value().Sign() < 0 {
102-
meter(NegativeValue).Mark(1)
102+
Meter(NegativeValue).Mark(1)
103103
return ErrNegativeValue
104104
}
105105
// Ensure the transaction doesn't exceed the current block limit gas
@@ -108,36 +108,38 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
108108
}
109109
// Sanity check for extremely large numbers (supported by RLP or RPC)
110110
if tx.GasFeeCap().BitLen() > 256 {
111-
meter(FeeCapVeryHigh).Mark(1)
111+
Meter(FeeCapVeryHigh).Mark(1)
112112
return core.ErrFeeCapVeryHigh
113113
}
114114
if tx.GasTipCap().BitLen() > 256 {
115-
meter(TipVeryHigh).Mark(1)
115+
Meter(TipVeryHigh).Mark(1)
116116
return core.ErrTipVeryHigh
117117
}
118118
// Ensure gasFeeCap is greater than or equal to gasTipCap
119119
if tx.GasFeeCapIntCmp(tx.GasTipCap()) < 0 {
120-
meter(TipAboveFeeCap).Mark(1)
120+
Meter(TipAboveFeeCap).Mark(1)
121121
return core.ErrTipAboveFeeCap
122122
}
123123
// Make sure the transaction is signed properly
124124
if _, err := types.Sender(signer, tx); err != nil {
125-
meter(InvalidSender).Mark(1)
125+
Meter(InvalidSender).Mark(1)
126126
return ErrInvalidSender
127127
}
128128
// Ensure the transaction has more gas than the bare minimum needed to cover
129129
// the transaction metadata
130130
intrGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, opts.Config.IsIstanbul(head.Number), opts.Config.IsShanghai(head.Number, head.Time))
131131
if err != nil {
132+
Meter(GasUnitOverflow).Mark(1)
132133
return err
133134
}
134135
if tx.Gas() < intrGas {
136+
Meter(IntrinsicGas).Mark(1)
135137
return fmt.Errorf("%w: needed %v, allowed %v", core.ErrIntrinsicGas, intrGas, tx.Gas())
136138
}
137139
// Ensure the gasprice is high enough to cover the requirement of the calling
138140
// pool and/or block producer
139141
if tx.GasTipCapIntCmp(opts.MinTip) < 0 {
140-
meter(Underpriced).Mark(1)
142+
Meter(Underpriced).Mark(1)
141143
return fmt.Errorf("%w: tip needed %v, tip permitted %v", ErrUnderpriced, opts.MinTip, tx.GasTipCap())
142144
}
143145
// Ensure blob transactions have valid commitments
@@ -240,7 +242,7 @@ func ValidateTransactionWithState(tx *types.Transaction, signer types.Signer, op
240242
}
241243
next := opts.State.GetNonce(from)
242244
if next > tx.Nonce() {
243-
meter(NonceTooLow).Mark(1)
245+
Meter(NonceTooLow).Mark(1)
244246
return fmt.Errorf("%w: next nonce %v, tx nonce %v", core.ErrNonceTooLow, next, tx.Nonce())
245247
}
246248
// Ensure the transaction doesn't produce a nonce gap in pools that do not
@@ -261,7 +263,7 @@ func ValidateTransactionWithState(tx *types.Transaction, signer types.Signer, op
261263
}
262264
}
263265
if balance.Cmp(cost) < 0 {
264-
meter(InsufficientFunds).Mark(1)
266+
Meter(InsufficientFunds).Mark(1)
265267
return fmt.Errorf("%w: balance %v, tx cost %v, overshot %v", core.ErrInsufficientFunds, balance, cost, new(big.Int).Sub(cost, balance))
266268
}
267269
// Ensure the transactor has enough funds to cover for replacements or nonce
@@ -271,11 +273,13 @@ func ValidateTransactionWithState(tx *types.Transaction, signer types.Signer, op
271273
bump := new(big.Int).Sub(cost, prev)
272274
need := new(big.Int).Add(spent, bump)
273275
if balance.Cmp(need) < 0 {
276+
Meter(Overdraft).Mark(1)
274277
return fmt.Errorf("%w: balance %v, queued cost %v, tx bumped %v, overshot %v", core.ErrInsufficientFunds, balance, spent, bump, new(big.Int).Sub(need, balance))
275278
}
276279
} else {
277280
need := new(big.Int).Add(spent, cost)
278281
if balance.Cmp(need) < 0 {
282+
Meter(Overdraft).Mark(1)
279283
return fmt.Errorf("%w: balance %v, queued cost %v, tx cost %v, overshot %v", core.ErrInsufficientFunds, balance, spent, cost, new(big.Int).Sub(need, balance))
280284
}
281285
// Transaction takes a new nonce value out of the pool. Ensure it doesn't

eth/handler_eth_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,14 @@ func TestTransactionPendingReannounce(t *testing.T) {
461461

462462
sink := newTestHandler()
463463
defer sink.close()
464-
sink.handler.acceptTxs = 1 // mark synced to accept transactions
464+
sink.handler.synced.Store(true) // mark synced to accept transactions
465465

466466
sourcePipe, sinkPipe := p2p.MsgPipe()
467467
defer sourcePipe.Close()
468468
defer sinkPipe.Close()
469469

470-
sourcePeer := eth.NewPeer(eth.ETH66, p2p.NewPeer(enode.ID{0}, "", nil), sourcePipe, source.txpool)
471-
sinkPeer := eth.NewPeer(eth.ETH66, p2p.NewPeer(enode.ID{0}, "", nil), sinkPipe, sink.txpool)
470+
sourcePeer := eth.NewPeer(eth.ETH68, p2p.NewPeer(enode.ID{0}, "", nil), sourcePipe, source.txpool)
471+
sinkPeer := eth.NewPeer(eth.ETH68, p2p.NewPeer(enode.ID{0}, "", nil), sinkPipe, sink.txpool)
472472
defer sourcePeer.Close()
473473
defer sinkPeer.Close()
474474

@@ -481,7 +481,7 @@ func TestTransactionPendingReannounce(t *testing.T) {
481481

482482
// Subscribe transaction pools
483483
txCh := make(chan core.NewTxsEvent, 1024)
484-
sub := sink.txpool.SubscribeNewTxsEvent(txCh)
484+
sub := sink.txpool.SubscribeTransactions(txCh, false)
485485
defer sub.Unsubscribe()
486486

487487
txs := make([]*types.Transaction, 64)

0 commit comments

Comments
 (0)