Skip to content

Commit ef6c9dd

Browse files
txpool metrics (#1619)
1 parent 980cd3c commit ef6c9dd

6 files changed

Lines changed: 410 additions & 128 deletions

File tree

txpool/metrics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
)
1111

1212
var (
13-
metricTxPoolGauge = metrics.LazyLoadGaugeVec("txpool_current_tx_count", []string{"source", "type"})
13+
metricTxPoolGauge = metrics.LazyLoadGaugeVec("txpool_current_tx_count", []string{"source", "type", "status"})
14+
metricTxPoolWashedCounter = metrics.LazyLoadCounterVec("txpool_washed_tx_count", []string{"source", "type"})
1415
metricBadTxGauge = metrics.LazyLoadGaugeVec("bad_tx_count", []string{"source"})
1516
metricTxPoolExecutablesGauge = metrics.LazyLoadGauge("txpool_executable_tx_count")
1617
metricTxPoolAllGauge = metrics.LazyLoadGauge("txpool_all_tx_count")

txpool/tx_object.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,22 @@ import (
2020
"github.com/vechain/thor/v2/tx"
2121
)
2222

23+
type txSource string
24+
25+
const (
26+
txSourceLocal txSource = "local"
27+
txSourceRemote txSource = "remote"
28+
txSourceFill txSource = "fill"
29+
)
30+
2331
type TxObject struct {
2432
*tx.Transaction
2533
resolved *runtime.ResolvedTransaction
2634

27-
timeAdded int64
28-
localSubmitted bool // tx is submitted locally on this node, or synced remotely from p2p.
29-
payer *thor.Address // payer of the tx, either origin, delegator, or on-chain delegation payer
30-
cost *big.Int // total tx cost the payer needs to pay before execution(gas price * gas)
35+
timeAdded int64
36+
source txSource // where the tx came from: local, remote or fill.
37+
payer *thor.Address // payer of the tx, either origin, delegator, or on-chain delegation payer
38+
cost *big.Int // total tx cost the payer needs to pay before execution(gas price * gas)
3139

3240
// basic unit of tip price for the validator, before GALACTICA it's the overallGasPrice(provedWork included) and validator
3341
// gets <reward-ratio>% of the tip, after GALACTICA it's the effective priority fee per gas and validator gets 100% of the tip
@@ -37,19 +45,31 @@ type TxObject struct {
3745
}
3846

3947
func ResolveTx(tx *tx.Transaction, localSubmitted bool) (*TxObject, error) {
48+
source := txSourceRemote
49+
if localSubmitted {
50+
source = txSourceLocal
51+
}
52+
return resolveTxWithSource(tx, source)
53+
}
54+
55+
func resolveTxWithSource(tx *tx.Transaction, source txSource) (*TxObject, error) {
4056
resolved, err := runtime.ResolveTransaction(tx)
4157
if err != nil {
4258
return nil, err
4359
}
4460

4561
return &TxObject{
46-
Transaction: tx,
47-
resolved: resolved,
48-
timeAdded: time.Now().UnixNano(),
49-
localSubmitted: localSubmitted,
62+
Transaction: tx,
63+
resolved: resolved,
64+
timeAdded: time.Now().UnixNano(),
65+
source: source,
5066
}, nil
5167
}
5268

69+
func (o *TxObject) localSubmitted() bool {
70+
return o.source == txSourceLocal
71+
}
72+
5373
func (o *TxObject) Origin() thor.Address {
5474
return o.resolved.Origin
5575
}

txpool/tx_object_map.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ func (m *txObjectMap) ToTxs() tx.Transactions {
180180
return txs
181181
}
182182

183-
func (m *txObjectMap) Fill(txObjs []*TxObject) {
183+
func (m *txObjectMap) Fill(txObjs []*TxObject) []*TxObject {
184184
m.lock.Lock()
185185
defer m.lock.Unlock()
186+
187+
inserted := make([]*TxObject, 0, len(txObjs))
186188
for _, txObj := range txObjs {
187189
if _, found := m.mapByHash[txObj.Hash()]; found {
188190
continue
@@ -194,8 +196,10 @@ func (m *txObjectMap) Fill(txObjs []*TxObject) {
194196
}
195197
m.mapByHash[txObj.Hash()] = txObj
196198
m.mapByID[txObj.ID()] = txObj
199+
inserted = append(inserted, txObj)
197200
// skip cost check and accumulation
198201
}
202+
return inserted
199203
}
200204

201205
func (m *txObjectMap) Len() int {

txpool/tx_object_map_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,19 @@ func TestPendingCost(t *testing.T) {
175175
state := stater.NewState(best.Root())
176176

177177
baseFee := galactica.CalcBaseFee(best.Header, forkConfig)
178-
txObj1.executable, err = txObj1.Executable(chain, state, best.Header, forkConfig, baseFee)
178+
exec1, err := txObj1.Executable(chain, state, best.Header, forkConfig, baseFee)
179179
assert.Nil(t, err)
180+
txObj1.executable = exec1
180181
assert.True(t, txObj1.executable)
181182

182-
txObj2.executable, err = txObj2.Executable(chain, state, best.Header, forkConfig, baseFee)
183+
exec2, err := txObj2.Executable(chain, state, best.Header, forkConfig, baseFee)
183184
assert.Nil(t, err)
185+
txObj2.executable = exec2
184186
assert.True(t, txObj2.executable)
185187

186-
txObj3.executable, err = txObj3.Executable(chain, state, best.Header, forkConfig, baseFee)
188+
exec3, err := txObj3.Executable(chain, state, best.Header, forkConfig, baseFee)
187189
assert.Nil(t, err)
190+
txObj3.executable = exec3
188191
assert.True(t, txObj3.executable)
189192

190193
// Creating a new txObjectMap

0 commit comments

Comments
 (0)