From a0ac6c7a9742376fd763573aa39add28dfb90ebb Mon Sep 17 00:00:00 2001 From: Gurjot Date: Sat, 30 Nov 2024 12:32:11 +0530 Subject: [PATCH] fix fp stats --- internal/v2/db/client/interface.go | 2 +- internal/v2/db/client/stats.go | 65 ++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/internal/v2/db/client/interface.go b/internal/v2/db/client/interface.go index e89fbdac..0a515a39 100644 --- a/internal/v2/db/client/interface.go +++ b/internal/v2/db/client/interface.go @@ -27,6 +27,6 @@ type V2DBClient interface { ctx context.Context, stakingTxHashHex, stakerPkHex string, amount uint64, ) error IncrementFinalityProviderStats( - ctx context.Context, stakingTxHashHex, fpPkHex string, amount uint64, + ctx context.Context, stakingTxHashHex string, fpPkHexes []string, amount uint64, ) error } diff --git a/internal/v2/db/client/stats.go b/internal/v2/db/client/stats.go index 60843a68..ee8810af 100644 --- a/internal/v2/db/client/stats.go +++ b/internal/v2/db/client/stats.go @@ -309,23 +309,44 @@ func (v2dbclient *V2Database) GetStakerStats( } func (v2dbclient *V2Database) IncrementFinalityProviderStats( - ctx context.Context, stakingTxHashHex, fpPkHex string, amount uint64, + ctx context.Context, + stakingTxHashHex string, + fpPkHexes []string, + amount uint64, ) error { - upsertUpdate := bson.M{ - "$inc": bson.M{ - "active_tvl": int64(amount), - "total_tvl": int64(amount), - "active_delegations": 1, - "total_delegations": 1, - }, + // 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), + "total_tvl": int64(amount), + "active_delegations": 1, + "total_delegations": 1, + }, + }). + SetUpsert(true) + operations = append(operations, operation) } - return v2dbclient.updateFinalityProviderStats(ctx, types.Active.ToString(), stakingTxHashHex, fpPkHex, upsertUpdate) + + return v2dbclient.updateFinalityProviderStats( + ctx, + types.Active.ToString(), + stakingTxHashHex, + operations, + ) } -func (v2dbclient *V2Database) updateFinalityProviderStats(ctx context.Context, state, stakingTxHashHex, fpPkHex string, upsertUpdate primitive.M) error { +func (v2dbclient *V2Database) updateFinalityProviderStats( + ctx context.Context, + state string, + stakingTxHashHex string, + operations []mongo.WriteModel, +) error { client := v2dbclient.Client.Database(v2dbclient.DbName).Collection(dbmodel.V2FinalityProviderStatsCollection) - // Start a session session, sessionErr := v2dbclient.Client.StartSession() if sessionErr != nil { return sessionErr @@ -333,25 +354,27 @@ func (v2dbclient *V2Database) updateFinalityProviderStats(ctx context.Context, s defer session.EndSession(ctx) transactionWork := func(sessCtx mongo.SessionContext) (interface{}, error) { - err := v2dbclient.updateStatsLockByFieldName(sessCtx, stakingTxHashHex, state, "finality_provider_stats") + // Single lock for the entire operation + err := v2dbclient.updateStatsLockByFieldName( + sessCtx, + stakingTxHashHex, + state, + "finality_provider_stats", + ) if err != nil { return nil, err } - upsertFilter := bson.M{"_id": fpPkHex} - - _, err = client.UpdateOne(sessCtx, upsertFilter, upsertUpdate, options.Update().SetUpsert(true)) + // Execute all updates in a single bulk write + opts := options.BulkWrite().SetOrdered(true) + _, err = client.BulkWrite(sessCtx, operations, opts) if err != nil { return nil, err } + return nil, nil } - // Execute the transaction _, txErr := session.WithTransaction(ctx, transactionWork) - if txErr != nil { - return txErr - } - - return nil + return txErr }