Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/txm/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var (
Name: "txm_time_until_tx_confirmed",
Help: "The amount of time elapsed from a transaction being broadcast to being included in a block.",
}, []string{"chainID"})
promRPCNonce = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "txm_rpc_nonce",
Help: "The latest confirmed nonce reported by the RPC node for a given address.",
}, []string{"chainID", "address"})
)

type txmMetrics struct {
Expand All @@ -50,6 +54,7 @@ type txmMetrics struct {
numNonceGaps metric.Int64Counter
reachedMaxAttempts metric.Int64Gauge
timeUntilTxConfirmed metric.Float64Histogram
rpcNonce metric.Int64Gauge
}

func NewTxmMetrics(chainID *big.Int) (*txmMetrics, error) {
Expand Down Expand Up @@ -78,6 +83,11 @@ func NewTxmMetrics(chainID *big.Int) (*txmMetrics, error) {
return nil, fmt.Errorf("failed to register max attempts indicator: %w", err)
}

rpcNonce, err := beholder.GetMeter().Int64Gauge("txm_rpc_nonce")
if err != nil {
return nil, fmt.Errorf("failed to register rpc nonce gauge: %w", err)
}

return &txmMetrics{
chainID: chainID,
Labeler: metrics.NewLabeler().With("chainID", chainID.String()),
Expand All @@ -86,6 +96,7 @@ func NewTxmMetrics(chainID *big.Int) (*txmMetrics, error) {
numNonceGaps: numNonceGaps,
reachedMaxAttempts: reachedMaxAttempts,
timeUntilTxConfirmed: timeUntilTxConfirmed,
rpcNonce: rpcNonce,
}, nil
}

Expand Down Expand Up @@ -118,6 +129,11 @@ func (m *txmMetrics) RecordTimeUntilTxConfirmed(ctx context.Context, duration fl
m.timeUntilTxConfirmed.Record(ctx, duration)
}

func (m *txmMetrics) SetRPCNonce(ctx context.Context, address common.Address, nonce uint64) {
promRPCNonce.WithLabelValues(m.chainID.String(), address.String()).Set(float64(nonce))
m.rpcNonce.Record(ctx, int64(nonce))
}

func (m *txmMetrics) EmitTxMessage(ctx context.Context, txHash common.Hash, fromAddress common.Address, tx *types.Transaction) error {
meta, err := tx.GetMeta()
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/txm/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,20 @@ func TestReachedMaxAttempts(t *testing.T) {
value = testutil.ToFloat64(promReachedMaxAttempts.WithLabelValues(testutils.FixtureChainID.String()))
require.InDelta(t, float64(0), value, 0.00001)
}

func TestSetRPCNonce(t *testing.T) {
ctx := t.Context()
chainID := testutils.FixtureChainID
address := testutils.NewAddress()

m, err := NewTxmMetrics(chainID)
require.NoError(t, err)

m.SetRPCNonce(ctx, address, 10)
value := testutil.ToFloat64(promRPCNonce.WithLabelValues(chainID.String(), address.String()))
assert.InDelta(t, float64(10), value, 0.00001)

m.SetRPCNonce(ctx, address, 25)
value = testutil.ToFloat64(promRPCNonce.WithLabelValues(chainID.String(), address.String()))
assert.InDelta(t, float64(25), value, 0.00001)
}
3 changes: 3 additions & 0 deletions pkg/txm/txm.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, I don't find much value in tracking the local nonce. It is initialized once and gets incremented sequentially. You can also infer that information by looking at the transactions of the node. Similar case for the pending nonce, which is used once for initialization and then there are clear log errors if you're getting throttled. I would keep the latest nonce value to understand which transactions are being confirmed and which are not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're saying, in theory the latest nonce is going to be much more valuable. Yeah, I'll remove it then.

Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ func (t *Txm) BackfillTransactions(ctx context.Context, address common.Address)
if err != nil {
return err
}
if t.Metrics != nil {
t.Metrics.SetRPCNonce(ctx, address, latestNonce)
}

confirmedTransactions, unconfirmedTransactionIDs, err := t.txStore.MarkConfirmedAndReorgedTransactions(ctx, latestNonce, address)
if err != nil {
Expand Down
Loading