diff --git a/config/config-local.yml b/config/config-local.yml index c3e4843..aa1647a 100644 --- a/config/config-local.yml +++ b/config/config-local.yml @@ -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 diff --git a/internal/db/interface.go b/internal/db/interface.go index 3975273..d96cc62 100644 --- a/internal/db/interface.go +++ b/internal/db/interface.go @@ -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" ) @@ -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 } diff --git a/internal/db/model/params.go b/internal/db/model/params.go index 982c3f2..69c9ec3 100644 --- a/internal/db/model/params.go +++ b/internal/db/model/params.go @@ -1,6 +1,6 @@ package model -type GolablParamDocument struct { +type GlobalParamsDocument struct { Type string `bson:"type"` Version uint32 `bson:"version"` Params interface{} `bson:"params"` diff --git a/internal/db/params.go b/internal/db/params.go index 4b58a69..c089c5f 100644 --- a/internal/db/params.go +++ b/internal/db/params.go @@ -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 } diff --git a/internal/services/global-params.go b/internal/services/global-params.go index ee5fb52..22bc0c3 100644 --- a/internal/services/global-params.go +++ b/internal/services/global-params.go @@ -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, @@ -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), ) @@ -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), ) diff --git a/tests/mocks/mock_bbn_client.go b/tests/mocks/mock_bbn_client.go index 5bc0b55..92fdd8b 100644 --- a/tests/mocks/mock_bbn_client.go +++ b/tests/mocks/mock_bbn_client.go @@ -21,23 +21,23 @@ type BbnInterface struct { } // GetAllStakingParams provides a mock function with given fields: ctx -func (_m *BbnInterface) GetAllStakingParams(ctx context.Context) (map[uint32]bbnclient.StakingParams, *types.Error) { +func (_m *BbnInterface) GetAllStakingParams(ctx context.Context) (map[uint32]*bbnclient.StakingParams, *types.Error) { ret := _m.Called(ctx) if len(ret) == 0 { panic("no return value specified for GetAllStakingParams") } - var r0 map[uint32]bbnclient.StakingParams + var r0 map[uint32]*bbnclient.StakingParams var r1 *types.Error - if rf, ok := ret.Get(0).(func(context.Context) (map[uint32]bbnclient.StakingParams, *types.Error)); ok { + if rf, ok := ret.Get(0).(func(context.Context) (map[uint32]*bbnclient.StakingParams, *types.Error)); ok { return rf(ctx) } - if rf, ok := ret.Get(0).(func(context.Context) map[uint32]bbnclient.StakingParams); ok { + if rf, ok := ret.Get(0).(func(context.Context) map[uint32]*bbnclient.StakingParams); ok { r0 = rf(ctx) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(map[uint32]bbnclient.StakingParams) + r0 = ret.Get(0).(map[uint32]*bbnclient.StakingParams) } } diff --git a/tests/mocks/mock_db_client.go b/tests/mocks/mock_db_client.go index 2f88c4e..1efd07f 100644 --- a/tests/mocks/mock_db_client.go +++ b/tests/mocks/mock_db_client.go @@ -5,9 +5,13 @@ package mocks import ( context "context" + bbnclient "github.com/babylonlabs-io/babylon-staking-indexer/internal/clients/bbnclient" + mock "github.com/stretchr/testify/mock" model "github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model" + + types "github.com/babylonlabs-io/babylon/x/btccheckpoint/types" ) // DbInterface is an autogenerated mock type for the DbInterface type @@ -63,17 +67,17 @@ func (_m *DbInterface) Ping(ctx context.Context) error { return r0 } -// SaveGlobalParams provides a mock function with given fields: ctx, param -func (_m *DbInterface) SaveGlobalParams(ctx context.Context, param *model.GolablParamDocument) error { - ret := _m.Called(ctx, param) +// SaveCheckpointParams provides a mock function with given fields: ctx, params +func (_m *DbInterface) SaveCheckpointParams(ctx context.Context, params *types.Params) error { + ret := _m.Called(ctx, params) if len(ret) == 0 { - panic("no return value specified for SaveGlobalParams") + panic("no return value specified for SaveCheckpointParams") } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, *model.GolablParamDocument) error); ok { - r0 = rf(ctx, param) + if rf, ok := ret.Get(0).(func(context.Context, *types.Params) error); ok { + r0 = rf(ctx, params) } else { r0 = ret.Error(0) } @@ -99,6 +103,24 @@ func (_m *DbInterface) SaveNewFinalityProvider(ctx context.Context, fpDoc *model return r0 } +// SaveStakingParams provides a mock function with given fields: ctx, version, params +func (_m *DbInterface) SaveStakingParams(ctx context.Context, version uint32, params *bbnclient.StakingParams) error { + ret := _m.Called(ctx, version, params) + + if len(ret) == 0 { + panic("no return value specified for SaveStakingParams") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, uint32, *bbnclient.StakingParams) error); ok { + r0 = rf(ctx, version, params) + } else { + r0 = ret.Error(0) + } + + return r0 +} + // UpdateFinalityProviderDetailsFromEvent provides a mock function with given fields: ctx, detailsToUpdate func (_m *DbInterface) UpdateFinalityProviderDetailsFromEvent(ctx context.Context, detailsToUpdate *model.FinalityProviderDetails) error { ret := _m.Called(ctx, detailsToUpdate)