diff --git a/internal/db/model/delegation.go b/internal/db/model/delegation.go index 4968a88..9cd2c96 100644 --- a/internal/db/model/delegation.go +++ b/internal/db/model/delegation.go @@ -27,12 +27,6 @@ type SlashingTx struct { SpendingHeight uint32 `bson:"spending_height"` } -type UnexpectedUnbondingDetails struct { - SpendStakeTxHash string `bson:"spend_stake_tx_hash"` - SpendStakeTxHeaderHash string `bson:"spend_stake_tx_header_hash"` - SpendStakeTxBlockIndex uint32 `bson:"spend_stake_tx_block_index"` -} - type BTCDelegationDetails struct { StakingTxHashHex string `bson:"_id"` // Primary key StakingTxHex string `bson:"staking_tx_hex"` @@ -51,7 +45,6 @@ type BTCDelegationDetails struct { CovenantUnbondingSignatures []CovenantSignature `bson:"covenant_unbonding_signatures"` BTCDelegationCreatedBlock BTCDelegationCreatedBbnBlock `bson:"btc_delegation_created_bbn_block"` SlashingTx SlashingTx `bson:"slashing_tx"` - UnexpectedUnbondingDetails *UnexpectedUnbondingDetails `bson:"unexpected_unbonding_details,omitempty"` } func FromEventBTCDelegationCreated( @@ -140,20 +133,6 @@ func FromEventBTCDelegationInclusionProofReceived( } } -func FromEventUnexpectedUnbondingTx( - event *bbntypes.EventUnexpectedUnbondingTx, -) *BTCDelegationDetails { - return &BTCDelegationDetails{ - State: types.StateUnbonding, - SubState: types.SubStateEarlyUnbonding, - UnexpectedUnbondingDetails: &UnexpectedUnbondingDetails{ - SpendStakeTxHash: event.SpendStakeTxHash, - SpendStakeTxHeaderHash: event.SpendStakeTxHeaderHash, - SpendStakeTxBlockIndex: uint32(event.SpendStakeTxBlockIndex), - }, - } -} - func (d *BTCDelegationDetails) HasInclusionProof() bool { // Ref: https://github.com/babylonlabs-io/babylon/blob/b1a4b483f60458fcf506adf1d80aaa6c8c10f8a4/x/btcstaking/types/btc_delegation.go#L47 return d.StartHeight > 0 && d.EndHeight > 0 diff --git a/internal/observability/metrics/metrics.go b/internal/observability/metrics/metrics.go index 06fdd4d..f6cdae7 100644 --- a/internal/observability/metrics/metrics.go +++ b/internal/observability/metrics/metrics.go @@ -6,7 +6,6 @@ import ( "sync" "time" - "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" "github.com/go-chi/chi/v5" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -29,19 +28,11 @@ func (O Outcome) String() string { } var ( - once sync.Once - metricsRouter *chi.Mux - btcClientDurationHistogram *prometheus.HistogramVec - queueSendErrorCounter prometheus.Counter - clientRequestDurationHistogram *prometheus.HistogramVec - invalidTransactionsCounter *prometheus.CounterVec - failedVerifyingUnbondingTxsCounter prometheus.Counter - failedVerifyingStakingWithdrawalTxsCounter prometheus.Counter - failedVerifyingUnbondingWithdrawalTxsCounter prometheus.Counter - failedVerifyingStakingSlashingTxsCounter prometheus.Counter - failedVerifyingUnbondingSlashingTxsCounter prometheus.Counter - eventProcessingTotal *prometheus.CounterVec - eventProcessingDuration *prometheus.HistogramVec + once sync.Once + metricsRouter *chi.Mux + btcClientDurationHistogram *prometheus.HistogramVec + queueSendErrorCounter prometheus.Counter + clientRequestDurationHistogram *prometheus.HistogramVec ) // Init initializes the metrics package. @@ -108,80 +99,10 @@ func registerMetrics() { }, ) - invalidTransactionsCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "invalid_txs_counter", - Help: "Total number of invalid transactions", - }, - []string{ - "tx_type", - }, - ) - - failedVerifyingUnbondingTxsCounter = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "failed_verifying_unbonding_txs_counter", - Help: "Total number of failed verifying unbonding txs", - }, - ) - - failedVerifyingStakingWithdrawalTxsCounter = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "failed_verifying_staking_withdrawal_txs_counter", - Help: "Total number of failed verifying staking withdrawal txs", - }, - ) - - failedVerifyingUnbondingWithdrawalTxsCounter = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "failed_verifying_unbonding_withdrawal_txs_counter", - Help: "Total number of failed verifying unbonding withdrawal txs", - }, - ) - - failedVerifyingStakingSlashingTxsCounter = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "failed_verifying_staking_slashing_txs_counter", - Help: "Total number of failed verifying staking slashing txs", - }, - ) - - failedVerifyingUnbondingSlashingTxsCounter = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "failed_verifying_unbonding_slashing_txs_counter", - Help: "Total number of failed verifying unbonding slashing txs", - }, - ) - - eventProcessingTotal = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "indexer_event_processing_total", - Help: "Total number of events processed by type", - }, - []string{"event_type", "status"}, // status can be "success" or "failure" - ) - - eventProcessingDuration = prometheus.NewHistogramVec( - prometheus.HistogramOpts{ - Name: "indexer_event_processing_duration_seconds", - Help: "Duration of event processing by type", - Buckets: prometheus.DefBuckets, - }, - []string{"event_type", "status"}, - ) - prometheus.MustRegister( btcClientDurationHistogram, queueSendErrorCounter, clientRequestDurationHistogram, - invalidTransactionsCounter, - failedVerifyingUnbondingTxsCounter, - failedVerifyingStakingWithdrawalTxsCounter, - failedVerifyingUnbondingWithdrawalTxsCounter, - failedVerifyingStakingSlashingTxsCounter, - failedVerifyingUnbondingSlashingTxsCounter, - eventProcessingTotal, - eventProcessingDuration, ) } @@ -222,54 +143,6 @@ func StartClientRequestDurationTimer(baseUrl, method, path string) func(statusCo } } -// StartEventProcessingTimer starts a timer to measure event processing duration -func StartEventProcessingTimer(eventType string) func(err *types.Error) { - startTime := time.Now() - return func(err *types.Error) { - duration := time.Since(startTime).Seconds() - status := "success" - if err != nil { - status = "failure" - } - eventProcessingDuration.WithLabelValues( - eventType, - status, - ).Observe(duration) - } -} - func RecordQueueSendError() { queueSendErrorCounter.Inc() } - -func IncrementInvalidUnbondingTxCounter() { - invalidTransactionsCounter.WithLabelValues("unbonding_transactions").Inc() -} - -func IncrementFailedVerifyingUnbondingTxCounter() { - failedVerifyingUnbondingTxsCounter.Inc() -} - -func IncrementFailedVerifyingStakingWithdrawalTxCounter() { - failedVerifyingStakingWithdrawalTxsCounter.Inc() -} - -func IncrementFailedVerifyingUnbondingWithdrawalTxCounter() { - failedVerifyingUnbondingWithdrawalTxsCounter.Inc() -} - -func IncrementFailedVerifyingStakingSlashingTxCounter() { - failedVerifyingStakingSlashingTxsCounter.Inc() -} - -func IncrementFailedVerifyingUnbondingSlashingTxCounter() { - failedVerifyingUnbondingSlashingTxsCounter.Inc() -} - -func IncrementEventProcessingFailureCounter(eventType string) { - eventProcessingTotal.WithLabelValues(eventType, "failure").Inc() -} - -func IncrementEventProcessingSuccessCounter(eventType string) { - eventProcessingTotal.WithLabelValues(eventType, "success").Inc() -} diff --git a/internal/services/delegation.go b/internal/services/delegation.go index a951d6e..05f52f5 100644 --- a/internal/services/delegation.go +++ b/internal/services/delegation.go @@ -22,7 +22,6 @@ const ( EventBTCDelegationInclusionProofReceived EventTypes = "babylon.btcstaking.v1.EventBTCDelegationInclusionProofReceived" EventBTCDelgationUnbondedEarly EventTypes = "babylon.btcstaking.v1.EventBTCDelgationUnbondedEarly" EventBTCDelegationExpired EventTypes = "babylon.btcstaking.v1.EventBTCDelegationExpired" - EventUnexpectedUnbondingTx EventTypes = "babylon.btcstaking.v1.EventUnexpectedUnbondingTx" EventSlashedFinalityProvider EventTypes = "babylon.finality.v1.EventSlashedFinalityProvider" ) @@ -430,62 +429,6 @@ func (s *Service) processBTCDelegationExpiredEvent( return nil } -func (s *Service) processUnexpectedUnbondingTxEvent( - ctx context.Context, event abcitypes.Event, -) *types.Error { - unexpectedUnbondingTxEvent, err := parseEvent[*bbntypes.EventUnexpectedUnbondingTx]( - EventUnexpectedUnbondingTx, - event, - ) - if err != nil { - return err - } - - shouldProcess, err := s.validateUnexpectedUnbondingTxEvent(ctx, unexpectedUnbondingTxEvent) - if err != nil { - return err - } - if !shouldProcess { - // Event is valid but should be skipped - return nil - } - - delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, unexpectedUnbondingTxEvent.StakingTxHash) - if dbErr != nil { - return types.NewError( - http.StatusInternalServerError, - types.InternalServiceError, - fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr), - ) - } - - // Emit consumer event - if err := s.emitUnbondingDelegationEvent(ctx, delegation); err != nil { - return err - } - - log.Debug(). - Str("staking_tx", unexpectedUnbondingTxEvent.StakingTxHash). - Str("spend_stake_tx", unexpectedUnbondingTxEvent.SpendStakeTxHash). - Str("event_type", EventUnexpectedUnbondingTx.String()). - Msg("handling unexpected unbonding tx event") - - unexpectedUnbondingDetails := model.FromEventUnexpectedUnbondingTx(unexpectedUnbondingTxEvent) - if err := s.db.UpdateBTCDelegationDetails( - ctx, - delegation.StakingTxHashHex, - unexpectedUnbondingDetails, - ); err != nil { - return types.NewError( - http.StatusInternalServerError, - types.InternalServiceError, - fmt.Errorf("failed to update BTC delegation details: %w", err), - ) - } - - return nil -} - func (s *Service) processSlashedFinalityProviderEvent( ctx context.Context, event abcitypes.Event, ) *types.Error { diff --git a/internal/services/events.go b/internal/services/events.go index eaf0383..031834f 100644 --- a/internal/services/events.go +++ b/internal/services/events.go @@ -7,7 +7,6 @@ import ( "strings" "time" - "github.com/babylonlabs-io/babylon-staking-indexer/internal/observability/metrics" "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" "github.com/babylonlabs-io/babylon-staking-indexer/internal/utils" bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" @@ -50,19 +49,13 @@ func (s *Service) processEvent( event BbnEvent, blockHeight int64, ) *types.Error { - var err *types.Error - // Note: We no longer need to check for the event category here. We can directly // process the event based on its type. bbnEvent := event.Event - eventType := EventTypes(bbnEvent.Type) - finishTimer := metrics.StartEventProcessingTimer(eventType.String()) - defer func() { - finishTimer(err) - }() + var err *types.Error - switch eventType { + switch EventTypes(bbnEvent.Type) { case EventFinalityProviderCreatedType: log.Debug().Msg("Processing new finality provider event") err = s.processNewFinalityProviderEvent(ctx, bbnEvent) @@ -90,23 +83,16 @@ func (s *Service) processEvent( case EventBTCDelegationExpired: log.Debug().Msg("Processing BTC delegation expired event") err = s.processBTCDelegationExpiredEvent(ctx, bbnEvent) - case EventUnexpectedUnbondingTx: - log.Debug().Msg("Processing unexpected unbonding tx event") - err = s.processUnexpectedUnbondingTxEvent(ctx, bbnEvent) case EventSlashedFinalityProvider: log.Debug().Msg("Processing slashed finality provider event") err = s.processSlashedFinalityProviderEvent(ctx, bbnEvent) } if err != nil { - // Increment failure counter - metrics.IncrementEventProcessingFailureCounter(eventType.String()) log.Error().Err(err).Msg("Failed to process event") return err } - // Increment success counter - metrics.IncrementEventProcessingSuccessCounter(eventType.String()) return nil } @@ -390,28 +376,6 @@ func (s *Service) validateBTCDelegationExpiredEvent(ctx context.Context, event * return true, nil } -func (s *Service) validateUnexpectedUnbondingTxEvent(ctx context.Context, event *bstypes.EventUnexpectedUnbondingTx) (bool, *types.Error) { - // Check if the staking tx hash is present - if event.StakingTxHash == "" { - return false, types.NewErrorWithMsg( - http.StatusInternalServerError, - types.InternalServiceError, - "unexpected unbonding tx event missing staking tx hash", - ) - } - - // Check if the spend stake tx hash is present - if event.SpendStakeTxHash == "" { - return false, types.NewErrorWithMsg( - http.StatusInternalServerError, - types.InternalServiceError, - "unexpected unbonding tx event missing spend stake tx hash", - ) - } - - return true, nil -} - func (s *Service) validateSlashedFinalityProviderEvent(ctx context.Context, event *ftypes.EventSlashedFinalityProvider) (bool, *types.Error) { if event.Evidence == nil { return false, types.NewErrorWithMsg( diff --git a/internal/services/watch_btc_events.go b/internal/services/watch_btc_events.go index eba9c8e..00465cb 100644 --- a/internal/services/watch_btc_events.go +++ b/internal/services/watch_btc_events.go @@ -8,7 +8,6 @@ import ( "github.com/babylonlabs-io/babylon-staking-indexer/internal/clients/bbnclient" "github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model" - "github.com/babylonlabs-io/babylon-staking-indexer/internal/observability/metrics" "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" "github.com/babylonlabs-io/babylon-staking-indexer/internal/utils" "github.com/babylonlabs-io/babylon/btcstaking" @@ -175,7 +174,7 @@ func (s *Service) handleSpendingStakingTransaction( isUnbonding, err := s.IsValidUnbondingTx(spendingTx, delegation, params) if err != nil { if errors.Is(err, types.ErrInvalidUnbondingTx) { - metrics.IncrementInvalidUnbondingTxCounter() + // TODO: Add metrics log.Error(). Err(err). Str("staking_tx", delegation.StakingTxHashHex). @@ -185,7 +184,6 @@ func (s *Service) handleSpendingStakingTransaction( return nil } - metrics.IncrementFailedVerifyingUnbondingTxCounter() return fmt.Errorf("failed to validate unbonding tx: %w", err) } if isUnbonding { @@ -201,7 +199,6 @@ func (s *Service) handleSpendingStakingTransaction( // Try to validate as withdrawal transaction isWithdrawal, err := s.validateWithdrawalTxFromStaking(spendingTx, spendingInputIdx, delegation, params) if err != nil { - metrics.IncrementFailedVerifyingStakingWithdrawalTxCounter() return fmt.Errorf("failed to validate withdrawal tx: %w", err) } if isWithdrawal { @@ -215,7 +212,6 @@ func (s *Service) handleSpendingStakingTransaction( // Try to validate as slashing transaction isSlashing, err := s.validateSlashingTxFromStaking(spendingTx, spendingInputIdx, delegation, params) if err != nil { - metrics.IncrementFailedVerifyingStakingSlashingTxCounter() return fmt.Errorf("failed to validate slashing tx: %w", err) } if isSlashing { @@ -267,7 +263,6 @@ func (s *Service) handleSpendingUnbondingTransaction( // First try to validate as withdrawal transaction isWithdrawal, err := s.validateWithdrawalTxFromUnbonding(spendingTx, delegation, spendingInputIdx, params) if err != nil { - metrics.IncrementFailedVerifyingUnbondingWithdrawalTxCounter() return fmt.Errorf("failed to validate withdrawal tx: %w", err) } if isWithdrawal { @@ -282,7 +277,6 @@ func (s *Service) handleSpendingUnbondingTransaction( // Try to validate as slashing transaction isSlashing, err := s.validateSlashingTxFromUnbonding(spendingTx, delegation, spendingInputIdx, params) if err != nil { - metrics.IncrementFailedVerifyingUnbondingSlashingTxCounter() return fmt.Errorf("failed to validate slashing tx: %w", err) } if isSlashing {