Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Nov 30, 2024
1 parent 4929932 commit 8429c7f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/v2/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ type V2DBClient interface {
IncrementFinalityProviderStats(
ctx context.Context, stakingTxHashHex string, fpPkHexes []string, amount uint64,
) error
SubtractFinalityProviderStats(
ctx context.Context, stakingTxHashHex string, fpPkHexes []string, amount uint64,
) error
}
30 changes: 30 additions & 0 deletions internal/v2/db/client/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,36 @@ func (v2dbclient *V2Database) IncrementFinalityProviderStats(
)
}

func (v2dbclient *V2Database) SubtractFinalityProviderStats(
ctx context.Context,
stakingTxHashHex string,
fpPkHexes []string,
amount uint64,
) error {

// Create bulk write operations for each FP
var operations []mongo.WriteModel
for _, fpPkHex := range fpPkHexes {
operation := mongo.NewUpdateOneModel().
SetFilter(bson.M{"_id": fpPkHex}).
SetUpdate(bson.M{
"$inc": bson.M{
"active_tvl": -int64(amount),
"active_delegations": -1,
},
}).
SetUpsert(true)
operations = append(operations, operation)
}

return v2dbclient.updateFinalityProviderStats(
ctx,
types.Unbonded.ToString(),
stakingTxHashHex,
operations,
)
}

func (v2dbclient *V2Database) updateFinalityProviderStats(
ctx context.Context,
state string,
Expand Down
15 changes: 14 additions & 1 deletion internal/v2/service/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,20 @@ func (s *V2Service) ProcessStakingStatsCalculation(
}
}
case types.Unbonded:
// TODO: Add finality provider stats calculation
// Subtract from the finality stats
if !statsLockDocument.FinalityProviderStats {
err = s.DbClients.V2DBClient.SubtractFinalityProviderStats(
ctx, stakingTxHashHex, finalityProviderBtcPksHex, amount,
)
if err != nil {
if db.IsNotFoundError(err) {
return nil
}
log.Ctx(ctx).Error().Err(err).Str("stakingTxHashHex", stakingTxHashHex).
Msg("error while subtracting finality stats")
return types.NewInternalServiceError(err)
}
}

if !statsLockDocument.StakerStats {
err = s.DbClients.V2DBClient.SubtractStakerStats(
Expand Down
36 changes: 36 additions & 0 deletions tests/mocks/mock_v2_db_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8429c7f

Please sign in to comment.