Skip to content

Commit

Permalink
rm extra code
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Jan 13, 2025
1 parent c409ebe commit a34049e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 246 deletions.
15 changes: 0 additions & 15 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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(
Expand Down Expand Up @@ -148,20 +147,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
Expand Down
137 changes: 5 additions & 132 deletions internal/observability/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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,
)
}

Expand Down Expand Up @@ -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()
}
57 changes: 0 additions & 57 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -433,62 +432,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, bbnBlockHeight int64,
) *types.Error {
Expand Down
37 changes: 2 additions & 35 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -96,14 +89,10 @@ func (s *Service) processEvent(
}

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
}

Expand Down Expand Up @@ -387,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(
Expand Down
8 changes: 1 addition & 7 deletions internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/babylonlabs-io/babylon-staking-indexer/internal/clients/bbnclient"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db"
"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"
Expand Down Expand Up @@ -176,7 +175,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).
Expand All @@ -186,7 +185,6 @@ func (s *Service) handleSpendingStakingTransaction(
return nil
}

metrics.IncrementFailedVerifyingUnbondingTxCounter()
return fmt.Errorf("failed to validate unbonding tx: %w", err)
}
if isUnbonding {
Expand All @@ -202,7 +200,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 {
Expand All @@ -216,7 +213,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 {
Expand Down Expand Up @@ -268,7 +264,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 {
Expand All @@ -283,7 +278,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 {
Expand Down

0 comments on commit a34049e

Please sign in to comment.