Skip to content

Commit

Permalink
fix: stats and queues (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 authored Dec 12, 2024
1 parent f0c51a2 commit bd3f529
Show file tree
Hide file tree
Showing 12 changed files with 10 additions and 99 deletions.
15 changes: 0 additions & 15 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,17 +1297,8 @@ const docTemplate = `{
"active_tvl": {
"type": "integer"
},
"total_delegations": {
"type": "integer"
},
"total_finality_providers": {
"type": "integer"
},
"total_stakers": {
"type": "integer"
},
"total_tvl": {
"type": "integer"
}
}
},
Expand Down Expand Up @@ -1339,12 +1330,6 @@ const docTemplate = `{
},
"staker_pk_hex": {
"type": "string"
},
"total_delegations": {
"type": "integer"
},
"total_tvl": {
"type": "integer"
}
}
},
Expand Down
15 changes: 0 additions & 15 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1289,17 +1289,8 @@
"active_tvl": {
"type": "integer"
},
"total_delegations": {
"type": "integer"
},
"total_finality_providers": {
"type": "integer"
},
"total_stakers": {
"type": "integer"
},
"total_tvl": {
"type": "integer"
}
}
},
Expand Down Expand Up @@ -1331,12 +1322,6 @@
},
"staker_pk_hex": {
"type": "string"
},
"total_delegations": {
"type": "integer"
},
"total_tvl": {
"type": "integer"
}
}
},
Expand Down
10 changes: 0 additions & 10 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,8 @@ definitions:
type: integer
active_tvl:
type: integer
total_delegations:
type: integer
total_finality_providers:
type: integer
total_stakers:
type: integer
total_tvl:
type: integer
type: object
v2service.ParamsPublic:
properties:
Expand All @@ -462,10 +456,6 @@ definitions:
type: integer
staker_pk_hex:
type: string
total_delegations:
type: integer
total_tvl:
type: integer
type: object
v2service.StakingStatusPublic:
properties:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.3
require (
github.com/babylonlabs-io/babylon v0.18.0
github.com/babylonlabs-io/networks/parameters v0.2.2
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241203052145-e50972fc19c9
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241212112557-9ac7de686075
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.6
Expand Down Expand Up @@ -174,7 +174,7 @@ require (
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/stretchr/testify v1.9.0
github.com/subosito/gotenv v1.6.0 // indirect
github.com/swaggo/http-swagger v1.3.4
go.mongodb.org/mongo-driver v1.14.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ github.com/babylonlabs-io/babylon v0.18.0 h1:phMY/GiR9N5MpD3XCmvyPpZkc1I3kTM9yX+
github.com/babylonlabs-io/babylon v0.18.0/go.mod h1:sT+KG2U+M0tDMNZZ2L5CwlXX0OpagGEs56BiWXqaZFw=
github.com/babylonlabs-io/networks/parameters v0.2.2 h1:TCu39fZvjX5f6ZZrjhYe54M6wWxglNewuKu56yE+zrc=
github.com/babylonlabs-io/networks/parameters v0.2.2/go.mod h1:iEJVOzaLsE33vpP7J4u+CRGfkSIfErUAwRmgCFCBpyI=
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241203052145-e50972fc19c9 h1:4UGpRPm5oxj3P8QNrGrkiQtlQYTAgsWzXJFi8oD7zpY=
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241203052145-e50972fc19c9/go.mod h1:n3fr3c+9LNiJlyETmcrVk94Zn76rAADhGZKxX+rVf+Q=
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241212104643-06828f6e25d5 h1:b89cYr2MuW8bzKDVIHT8kPKD1dQygCERYpTtR8eGmcY=
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241212104643-06828f6e25d5/go.mod h1:n3fr3c+9LNiJlyETmcrVk94Zn76rAADhGZKxX+rVf+Q=
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241212112557-9ac7de686075 h1:gB+jslBkK5/ror4sn9NHldKjLu4nE88jgD43d2L3osc=
github.com/babylonlabs-io/staking-queue-client v0.4.7-0.20241212112557-9ac7de686075/go.mod h1:n3fr3c+9LNiJlyETmcrVk94Zn76rAADhGZKxX+rVf+Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
22 changes: 0 additions & 22 deletions internal/v2/db/client/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (v2dbclient *V2Database) IncrementOverallStats(
ctx context.Context, stakingTxHashHex, stakerPkHex string, amount uint64,
) error {
overallStatsClient := v2dbclient.Client.Database(v2dbclient.DbName).Collection(dbmodel.V2OverallStatsCollection)
stakerStatsClient := v2dbclient.Client.Database(v2dbclient.DbName).Collection(dbmodel.V2StakerStatsCollection)

// Start a session
session, sessionErr := v2dbclient.Client.StartSession()
Expand All @@ -63,9 +62,7 @@ func (v2dbclient *V2Database) IncrementOverallStats(
upsertUpdate := bson.M{
"$inc": bson.M{
"active_tvl": int64(amount),
"total_tvl": int64(amount),
"active_delegations": 1,
"total_delegations": 1,
},
}
// Define the work to be done in the transaction
Expand All @@ -75,18 +72,6 @@ func (v2dbclient *V2Database) IncrementOverallStats(
return nil, err
}

// The order of the overall stats and staker stats update is important.
// The staker stats colleciton will need to be processed first to determine if the staker is new
// If the staker stats is the first delegation for the staker, we need to increment the total stakers
var stakerStats v2dbmodel.V2StakerStatsDocument
stakerStatsFilter := bson.M{"_id": stakerPkHex}
stakerErr := stakerStatsClient.FindOne(ctx, stakerStatsFilter).Decode(&stakerStats)
if stakerErr != nil {
return nil, stakerErr
}
if stakerStats.TotalDelegations == 1 {
upsertUpdate["$inc"].(bson.M)["total_stakers"] = 1
}
shardId, err := v2dbclient.generateOverallStatsId()
if err != nil {
return nil, err
Expand Down Expand Up @@ -184,10 +169,7 @@ func (v2dbclient *V2Database) GetOverallStats(ctx context.Context) (*v2dbmodel.V
var result v2dbmodel.V2OverallStatsDocument
for _, stats := range overallStats {
result.ActiveTvl += stats.ActiveTvl
result.TotalTvl += stats.TotalTvl
result.ActiveDelegations += stats.ActiveDelegations
result.TotalDelegations += stats.TotalDelegations
result.TotalStakers += stats.TotalStakers
}

return &result, nil
Expand Down Expand Up @@ -236,9 +218,7 @@ func (v2dbclient *V2Database) IncrementStakerStats(
upsertUpdate := bson.M{
"$inc": bson.M{
"active_tvl": int64(amount),
"total_tvl": int64(amount),
"active_delegations": 1,
"total_delegations": 1,
},
}
return v2dbclient.updateStakerStats(ctx, types.Active.ToString(), stakingTxHashHex, stakerPkHex, upsertUpdate)
Expand Down Expand Up @@ -341,9 +321,7 @@ func (v2dbclient *V2Database) IncrementFinalityProviderStats(
SetUpdate(bson.M{
"$inc": bson.M{
"active_tvl": int64(amount),
"total_tvl": int64(amount),
"active_delegations": 1,
"total_delegations": 1,
},
}).
SetUpsert(true)
Expand Down
19 changes: 0 additions & 19 deletions internal/v2/db/model/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,17 @@ func NewV2StatsLockDocument(
type V2OverallStatsDocument struct {
Id string `bson:"_id"`
ActiveTvl int64 `bson:"active_tvl"`
TotalTvl int64 `bson:"total_tvl"`
ActiveDelegations int64 `bson:"active_delegations"`
TotalDelegations int64 `bson:"total_delegations"`
TotalStakers uint64 `bson:"total_stakers"`
}

type V2StakerStatsDocument struct {
StakerPkHex string `bson:"_id"`
ActiveTvl int64 `bson:"active_tvl"`
TotalTvl int64 `bson:"total_tvl"`
ActiveDelegations int64 `bson:"active_delegations"`
TotalDelegations int64 `bson:"total_delegations"`
}

type V2FinalityProviderStatsDocument struct {
FinalityProviderPkHex string `bson:"_id"`
ActiveTvl int64 `bson:"active_tvl"`
TotalTvl int64 `bson:"total_tvl"`
ActiveDelegations int64 `bson:"active_delegations"`
TotalDelegations int64 `bson:"total_delegations"`
}

type V2FpSlashingLockDocument struct {
FinalityProviderPkHex string `bson:"_id"` // <fp_btc_pk_hex>
IsProcessed bool `bson:"is_processed"` // true when processing is complete
}

func NewV2FpSlashingLockDocument(fpBtcPkHex string) *V2FpSlashingLockDocument {
return &V2FpSlashingLockDocument{
FinalityProviderPkHex: fpBtcPkHex,
IsProcessed: false,
}
}
10 changes: 0 additions & 10 deletions internal/v2/service/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ import (

type OverallStatsPublic struct {
ActiveTvl int64 `json:"active_tvl"`
TotalTvl int64 `json:"total_tvl"`
ActiveDelegations int64 `json:"active_delegations"`
TotalDelegations int64 `json:"total_delegations"`
ActiveStakers uint64 `json:"active_stakers"`
TotalStakers uint64 `json:"total_stakers"`
ActiveFinalityProviders uint64 `json:"active_finality_providers"`
TotalFinalityProviders uint64 `json:"total_finality_providers"`
}

type StakerStatsPublic struct {
StakerPkHex string `json:"staker_pk_hex"`
ActiveTvl int64 `json:"active_tvl"`
TotalTvl int64 `json:"total_tvl"`
ActiveDelegations int64 `json:"active_delegations"`
TotalDelegations int64 `json:"total_delegations"`
}

func (s *V2Service) GetOverallStats(ctx context.Context) (*OverallStatsPublic, *types.Error) {
Expand Down Expand Up @@ -58,11 +53,8 @@ func (s *V2Service) GetOverallStats(ctx context.Context) (*OverallStatsPublic, *

return &OverallStatsPublic{
ActiveTvl: overallStats.ActiveTvl,
TotalTvl: overallStats.TotalTvl,
ActiveDelegations: overallStats.ActiveDelegations,
TotalDelegations: overallStats.TotalDelegations,
ActiveStakers: uint64(activeStakersCount),
TotalStakers: overallStats.TotalStakers,
ActiveFinalityProviders: uint64(activeFinalityProvidersCount),
TotalFinalityProviders: uint64(len(finalityProviders)),
}, nil
Expand All @@ -78,9 +70,7 @@ func (s *V2Service) GetStakerStats(ctx context.Context, stakerPKHex string) (*St
return &StakerStatsPublic{
StakerPkHex: stakerStats.StakerPkHex,
ActiveTvl: stakerStats.ActiveTvl,
TotalTvl: stakerStats.TotalTvl,
ActiveDelegations: stakerStats.ActiveDelegations,
TotalDelegations: stakerStats.TotalDelegations,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion tests/mocks/mock_db_client.go

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

2 changes: 1 addition & 1 deletion tests/mocks/mock_ordinal_client.go

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

2 changes: 1 addition & 1 deletion tests/mocks/mock_v1_db_client.go

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

2 changes: 1 addition & 1 deletion 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 bd3f529

Please sign in to comment.