Skip to content

Commit

Permalink
resolve comments 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Oct 16, 2024
1 parent 0bc195b commit cb76398
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 45 deletions.
2 changes: 1 addition & 1 deletion config/config-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bbn:
rpc-addr: https://rpc.devnet.babylonchain.io:443
timeout: 30s
poller:
param-polling-interval: 30s
param-polling-interval: 10s
queue:
queue_user: user # can be replaced by values in .env file
queue_password: password
Expand Down
20 changes: 15 additions & 5 deletions internal/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/clients/bbnclient"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
)

Expand Down Expand Up @@ -54,13 +55,22 @@ type DbInterface interface {
ctx context.Context, btcPk string,
) (*model.FinalityProviderDetails, error)
/**
* SaveGlobalParams saves the global parameters to the database.
* If the document already exists by type and version, it will be skipped.
* SaveStakingParams saves the staking parameters to the database.
* @param ctx The context
* @param param The global parameters document
* @param version The version of the staking parameters
* @param params The staking parameters
* @return An error if the operation failed
*/
SaveGlobalParams(
ctx context.Context, param *model.GolablParamDocument,
SaveStakingParams(
ctx context.Context, version uint32, params *bbnclient.StakingParams,
) error
/**
* SaveCheckpointParams saves the checkpoint parameters to the database.
* @param ctx The context
* @param params The checkpoint parameters
* @return An error if the operation failed
*/
SaveCheckpointParams(
ctx context.Context, params *bbnclient.CheckpointParams,
) error
}
2 changes: 1 addition & 1 deletion internal/db/model/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model

type GolablParamDocument struct {
type GlobalParamsDocument struct {
Type string `bson:"type"`
Version uint32 `bson:"version"`
Params interface{} `bson:"params"`
Expand Down
53 changes: 47 additions & 6 deletions internal/db/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,70 @@ import (
"context"
"fmt"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/clients/bbnclient"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)

func (db *Database) SaveGlobalParams(
ctx context.Context, param *model.GolablParamDocument,
const (
// CHECKPOINT_PARAMS_VERSION is the version of the checkpoint params
// the value is hardcoded to 0 as the checkpoint params are not expected to change
// However, we keep the versioning in place for future compatibility and
// maintain the same pattern as other global params
CHECKPOINT_PARAMS_VERSION = 0
CHECKPOINT_PARAMS_TYPE = "CHECKPOINT"
STAKING_PARAMS_TYPE = "STAKING"
)

func (db *Database) SaveStakingParams(
ctx context.Context, version uint32, params *bbnclient.StakingParams,
) error {
collection := db.client.Database(db.dbName).
Collection(model.GlobalParamsCollection)

filter := bson.M{
"type": STAKING_PARAMS_TYPE,
"version": version,
}

update := bson.M{
"$setOnInsert": &model.GlobalParamsDocument{
Type: STAKING_PARAMS_TYPE,
Version: version,
Params: params,
},
}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
if err != nil {
return fmt.Errorf("failed to save staking params: %w", err)
}
return nil
}

func (db *Database) SaveCheckpointParams(
ctx context.Context, params *bbnclient.CheckpointParams,
) error {
collection := db.client.Database(db.dbName).
Collection(model.GlobalParamsCollection)

filter := bson.M{
"type": param.Type,
"version": param.Version,
"type": CHECKPOINT_PARAMS_TYPE,
"version": CHECKPOINT_PARAMS_VERSION,
}

update := bson.M{
"$setOnInsert": param, // Only insert if the document doesn't exist
"$setOnInsert": &model.GlobalParamsDocument{
Type: CHECKPOINT_PARAMS_TYPE,
Version: CHECKPOINT_PARAMS_VERSION,
Params: params,
},
}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
if err != nil {
return fmt.Errorf("error while upserting global params document: %w with type %s and version %d", err, param.Type, param.Version)
return fmt.Errorf("failed to save checkpoint params: %w", err)
}
return nil
}
23 changes: 2 additions & 21 deletions internal/services/global-params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@ import (
"context"
"fmt"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils/poller"
)

// CHECKPOINT_PARAMS_VERSION is the version of the checkpoint params
// the value is hardcoded to 0 as the checkpoint params are not expected to change
// However, we keep the versioning in place for future compatibility and
// maintain the same pattern as other global params
const (
CHECKPOINT_PARAMS_VERSION = 0
CHECKPOINT_PARAMS_TYPE = "CHECKPOINT"
STAKING_PARAMS_TYPE = "STAKING"
)

func (s *Service) SyncGlobalParams(ctx context.Context) {
paramsPoller := poller.NewPoller(
s.cfg.Poller.ParamPollingInterval,
Expand All @@ -36,11 +25,7 @@ func (s *Service) fetchAndSaveParams(ctx context.Context) *types.Error {
fmt.Errorf("failed to get checkpoint params: %w", err),
)
}
if err := s.db.SaveGlobalParams(ctx, &model.GolablParamDocument{
Type: CHECKPOINT_PARAMS_TYPE,
Version: CHECKPOINT_PARAMS_VERSION,
Params: checkpointParams,
}); err != nil {
if err := s.db.SaveCheckpointParams(ctx, checkpointParams); err != nil {
return types.NewInternalServiceError(
fmt.Errorf("failed to save checkpoint params: %w", err),
)
Expand All @@ -58,11 +43,7 @@ func (s *Service) fetchAndSaveParams(ctx context.Context) *types.Error {
fmt.Errorf("nil staking params for version %d", version),
)
}
if err := s.db.SaveGlobalParams(ctx, &model.GolablParamDocument{
Type: STAKING_PARAMS_TYPE,
Version: version,
Params: params,
}); err != nil {
if err := s.db.SaveStakingParams(ctx, version, params); err != nil {
return types.NewInternalServiceError(
fmt.Errorf("failed to save staking params: %w", err),
)
Expand Down
10 changes: 5 additions & 5 deletions tests/mocks/mock_bbn_client.go

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

34 changes: 28 additions & 6 deletions tests/mocks/mock_db_client.go

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

0 comments on commit cb76398

Please sign in to comment.