Skip to content

Commit

Permalink
fix stats
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Nov 30, 2024
1 parent fb55d78 commit 5256410
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 19 deletions.
25 changes: 25 additions & 0 deletions internal/indexer/db/client/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

indexerdbmodel "github.com/babylonlabs-io/staking-api-service/internal/indexer/db/model"
indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
"github.com/babylonlabs-io/staking-api-service/internal/shared/db"
dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -63,3 +64,27 @@ func (indexerdbclient *IndexerDatabase) GetDelegations(
indexerdbmodel.BuildDelegationPaginationToken,
)
}

func (indexerdbclient *IndexerDatabase) GetSlashedFpDelegations(ctx context.Context, fpBtcPkHex string) ([]*indexerdbmodel.IndexerDelegationDetails, error) {
collection := indexerdbclient.Client.
Database(indexerdbclient.DbName).
Collection(indexerdbmodel.BTCDelegationDetailsCollection)

filter := bson.M{
"finality_provider_btc_pk_hex": fpBtcPkHex,
"state": indexertypes.StateSlashed.String(),
}

cursor, err := collection.Find(ctx, filter)
if err != nil {
return nil, err
}
defer cursor.Close(ctx)

var delegations []*indexerdbmodel.IndexerDelegationDetails
if err := cursor.All(ctx, &delegations); err != nil {
return nil, err
}

return delegations, nil
}
2 changes: 2 additions & 0 deletions internal/indexer/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type IndexerDBClient interface {
// Finality Providers
GetFinalityProviders(ctx context.Context) ([]*indexerdbmodel.IndexerFinalityProviderDetails, error)
GetFinalityProviderByPk(ctx context.Context, fpPk string) (*indexerdbmodel.IndexerFinalityProviderDetails, error)
GetSlashedFpDelegations(ctx context.Context, fpBtcPkHex string) ([]*indexerdbmodel.IndexerDelegationDetails, error)

// Staker Delegations
GetDelegation(ctx context.Context, stakingTxHashHex string) (*indexerdbmodel.IndexerDelegationDetails, error)
GetDelegations(ctx context.Context, stakerPKHex string, paginationToken string) (*db.DbResultMap[indexerdbmodel.IndexerDelegationDetails], error)
Expand Down
4 changes: 4 additions & 0 deletions internal/indexer/types/delegation_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
StateSlashed DelegationState = "SLASHED"
)

func (s DelegationState) String() string {
return string(s)
}

type DelegationSubState string

const (
Expand Down
9 changes: 9 additions & 0 deletions internal/v2/queue/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,14 @@ func (h *V2QueueHandler) SlashedFpHandler(ctx context.Context, messageBody strin
return types.NewError(http.StatusBadRequest, types.BadRequest, err)
}

statsErr := h.Service.ProcessSlashedFpStats(
ctx,
slashedFpEvent.FinalityProviderBtcPkHex,
)
if statsErr != nil {
log.Ctx(ctx).Error().Err(statsErr).Msg("Failed to process slashed FP stats calculation")
return statsErr
}

return nil
}
16 changes: 0 additions & 16 deletions internal/v2/service/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,6 @@ func getUnbondingSignatures(covenantSignatures []indexerdbmodel.CovenantSignatur
return covenantSignaturesPublic
}

func (s *V2Service) IsDelegationPresent(ctx context.Context, txHashHex string) (bool, *types.Error) {
delegation, err := s.DbClients.IndexerDBClient.GetDelegation(ctx, txHashHex)
if err != nil {
if db.IsNotFoundError(err) {
return false, nil
}
log.Ctx(ctx).Error().Err(err).Msg("Failed to find delegation by tx hash hex")
return false, types.NewInternalServiceError(err)
}
if delegation != nil {
return true, nil
}

return false, nil
}

func (s *V2Service) SaveUnprocessableMessages(ctx context.Context, messageBody, receipt string) *types.Error {
err := s.DbClients.V2DBClient.SaveUnprocessableMessage(ctx, messageBody, receipt)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/v2/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type V2ServiceProvider interface {
[]*FinalityProviderStatsPublic, *types.Error,
)
GetNetworkInfo(ctx context.Context) (*NetworkInfoPublic, *types.Error)
IsDelegationPresent(ctx context.Context, txHashHex string) (bool, *types.Error)
GetDelegation(ctx context.Context, stakingTxHashHex string) (*StakerDelegationPublic, *types.Error)
GetDelegations(ctx context.Context, stakerPKHex string, paginationKey string) ([]*StakerDelegationPublic, string, *types.Error)
GetOverallStats(ctx context.Context) (*OverallStatsPublic, *types.Error)
Expand All @@ -20,4 +19,5 @@ type V2ServiceProvider interface {
SaveUnprocessableMessages(ctx context.Context, messageBody, receipt string) *types.Error
ProcessActiveDelegationStats(ctx context.Context, stakingTxHashHex, stakerPkHex string, fpBtcPkHexes []string, amount uint64) *types.Error
ProcessUnbondingDelegationStats(ctx context.Context, stakingTxHashHex, stakerPkHex string, fpBtcPkHexes []string, amount uint64) *types.Error
ProcessSlashedFpStats(ctx context.Context, fpBtcPkHex string) *types.Error
}
69 changes: 67 additions & 2 deletions internal/v2/service/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (s *V2Service) ProcessActiveDelegationStats(ctx context.Context, stakingTxH
return types.NewInternalServiceError(err)
}
}

// Add to the overall stats
// The overall stats should be the last to be updated as it has dependency
// on staker stats.
Expand All @@ -126,7 +125,7 @@ func (s *V2Service) ProcessActiveDelegationStats(ctx context.Context, stakingTxH
func (s *V2Service) ProcessUnbondingDelegationStats(ctx context.Context, stakingTxHashHex, stakerPkHex string, fpBtcPkHexes []string, amount uint64) *types.Error {
// Fetch existing or initialize the stats lock document if not exist
statsLockDocument, err := s.DbClients.V2DBClient.GetOrCreateStatsLock(
ctx, stakingTxHashHex, types.Active.ToString(),
ctx, stakingTxHashHex, types.Unbonding.ToString(),
)
if err != nil {
log.Ctx(ctx).Error().Err(err).Str("stakingTxHashHex", stakingTxHashHex).
Expand Down Expand Up @@ -181,3 +180,69 @@ func (s *V2Service) ProcessUnbondingDelegationStats(ctx context.Context, staking

return nil
}

func (s *V2Service) ProcessSlashedFpStats(ctx context.Context, fpBtcPkHex string) *types.Error {
slashedFpDelegations, err := s.DbClients.IndexerDBClient.GetSlashedFpDelegations(ctx, fpBtcPkHex)
if err != nil {
log.Ctx(ctx).Error().Err(err).Str("fpBtcPkHex", fpBtcPkHex).Msg("error while fetching slashed fp delegations")
return types.NewInternalServiceError(err)
}

for _, delegation := range slashedFpDelegations {
statsLockDocument, err := s.DbClients.V2DBClient.GetOrCreateStatsLock(
ctx, delegation.StakingTxHashHex, types.Unbonding.ToString(),
)
if err != nil {
log.Ctx(ctx).Error().Err(err).Str("stakingTxHashHex", delegation.StakingTxHashHex).
Msg("error while fetching stats lock document")
return types.NewInternalServiceError(err)
}

// Subtract from the finality stats
if !statsLockDocument.FinalityProviderStats {
err = s.DbClients.V2DBClient.SubtractFinalityProviderStats(
ctx, delegation.StakingTxHashHex, delegation.FinalityProviderBtcPksHex, delegation.StakingAmount,
)
if err != nil {
if db.IsNotFoundError(err) {
return nil
}
log.Ctx(ctx).Error().Err(err).Str("stakingTxHashHex", delegation.StakingTxHashHex).
Msg("error while subtracting finality stats")
return types.NewInternalServiceError(err)
}
}

if !statsLockDocument.StakerStats {
err = s.DbClients.V2DBClient.SubtractStakerStats(
ctx, delegation.StakingTxHashHex, delegation.StakerBtcPkHex, delegation.StakingAmount,
)
if err != nil {
if db.IsNotFoundError(err) {
return nil
}
log.Ctx(ctx).Error().Err(err).Str("stakingTxHashHex", delegation.StakingTxHashHex).
Msg("error while subtracting staker stats")
return types.NewInternalServiceError(err)
}
}
// Subtract from the overall stats.
// The overall stats should be the last to be updated as it has dependency
// on staker stats.
if !statsLockDocument.OverallStats {
err = s.DbClients.V2DBClient.SubtractOverallStats(
ctx, delegation.StakingTxHashHex, delegation.StakerBtcPkHex, delegation.StakingAmount,
)
if err != nil {
if db.IsNotFoundError(err) {
return nil
}
log.Ctx(ctx).Error().Err(err).Str("stakingTxHashHex", delegation.StakingTxHashHex).
Msg("error while subtracting overall stats")
return types.NewInternalServiceError(err)
}
}
}

return nil
}

0 comments on commit 5256410

Please sign in to comment.