diff --git a/bin/local-startup.sh b/bin/local-startup.sh index 66ea2185..80523edc 100755 --- a/bin/local-startup.sh +++ b/bin/local-startup.sh @@ -10,6 +10,16 @@ else docker compose up -d mongodb fi +# Check if the MongoDB container is already running +MONGO_CONTAINER_NAME="indexer-mongodb" +if [ $(docker ps -q -f name=^/${MONGO_CONTAINER_NAME}$) ]; then + echo "Indexer mongoDB container already running. Skipping MongoDB startup." +else + echo "Starting indexer mongoDB" + # Start indexer mongoDB + docker compose up -d indexer-mongodb +fi + # Check if the RabbitMQ container is already running RABBITMQ_CONTAINER_NAME="rabbitmq" if [ $(docker ps -q -f name=^/${RABBITMQ_CONTAINER_NAME}$) ]; then diff --git a/cmd/staking-api-service/scripts/pubkey_address_backfill.go b/cmd/staking-api-service/scripts/pubkey_address_backfill.go index e95e1314..b68b9e75 100644 --- a/cmd/staking-api-service/scripts/pubkey_address_backfill.go +++ b/cmd/staking-api-service/scripts/pubkey_address_backfill.go @@ -12,11 +12,11 @@ import ( ) func BackfillPubkeyAddressesMappings(ctx context.Context, cfg *config.Config) error { - client, err := dbclient.NewMongoClient(ctx, cfg.Db) + client, err := dbclient.NewMongoClient(ctx, cfg.StakingDb) if err != nil { return fmt.Errorf("failed to create db client: %w", err) } - v1dbClient, err := v1dbclient.New(ctx, client, cfg.Db) + v1dbClient, err := v1dbclient.New(ctx, client, cfg.StakingDb) if err != nil { return fmt.Errorf("failed to create db client: %w", err) } diff --git a/config/config-docker.yml b/config/config-docker.yml index 948c7ac5..b71cd8d2 100644 --- a/config/config-docker.yml +++ b/config/config-docker.yml @@ -9,7 +9,7 @@ server: btc-net: "mainnet" max-content-length: 4096 health-check-interval: 300 # 5 minutes interval -db: +staking-db: username: root password: example address: "mongodb://mongodb:27017" @@ -17,6 +17,13 @@ db: max-pagination-limit: 10 db-batch-size-limit: 100 logical-shard-count: 10 +indexer-db: + username: root + password: example + address: "mongodb://indexer-mongodb:27019" + db-name: indexer-db + max-pagination-limit: 10 + db-batch-size-limit: 100 queue: queue_user: user # can be replaced by values in .env file queue_password: password diff --git a/config/config-local.yml b/config/config-local.yml index a9d03c97..26e0c646 100644 --- a/config/config-local.yml +++ b/config/config-local.yml @@ -9,13 +9,20 @@ server: btc-net: "signet" max-content-length: 4096 health-check-interval: 300 # 5 minutes interval -db: +staking-db: username: root password: example address: "mongodb://localhost:27017/?directConnection=true" db-name: staking-api-service max-pagination-limit: 10 db-batch-size-limit: 100 +indexer-db: + username: root + password: example + address: "mongodb://localhost:27019/?directConnection=true" + db-name: indexer-db + max-pagination-limit: 10 + db-batch-size-limit: 100 logical-shard-count: 2 queue: queue_user: user # can be replaced by values in .env file diff --git a/docker-compose.yml b/docker-compose.yml index afd70618..7e889e4f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,6 +23,18 @@ services: volumes: - ./bin/init-mongo.sh:/init-mongo.sh entrypoint: [ "/init-mongo.sh" ] + indexer-mongodb: + image: mongo:latest + container_name: indexer-mongodb + hostname: indexer-mongodb + ports: + - "27019:27017" + environment: + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: example + volumes: + - ./bin/init-mongo.sh:/init-mongo.sh + entrypoint: [ "/init-mongo.sh" ] rabbitmq: image: rabbitmq:3-management container_name: rabbitmq diff --git a/internal/shared/config/config.go b/internal/shared/config/config.go index b2e0f816..cfdef0d7 100644 --- a/internal/shared/config/config.go +++ b/internal/shared/config/config.go @@ -10,11 +10,12 @@ import ( ) type Config struct { - Server *ServerConfig `mapstructure:"server"` - Db *DbConfig `mapstructure:"db"` - Queue *queue.QueueConfig `mapstructure:"queue"` - Metrics *MetricsConfig `mapstructure:"metrics"` - Assets *AssetsConfig `mapstructure:"assets"` + Server *ServerConfig `mapstructure:"server"` + StakingDb *DbConfig `mapstructure:"staking-db"` + IndexerDb *DbConfig `mapstructure:"indexer-db"` + Queue *queue.QueueConfig `mapstructure:"queue"` + Metrics *MetricsConfig `mapstructure:"metrics"` + Assets *AssetsConfig `mapstructure:"assets"` } func (cfg *Config) Validate() error { @@ -22,7 +23,11 @@ func (cfg *Config) Validate() error { return err } - if err := cfg.Db.Validate(); err != nil { + if err := cfg.StakingDb.Validate(); err != nil { + return err + } + + if err := cfg.IndexerDb.Validate(); err != nil { return err } diff --git a/internal/shared/config/db.go b/internal/shared/config/db.go index e7ee645d..e39171ae 100644 --- a/internal/shared/config/db.go +++ b/internal/shared/config/db.go @@ -17,7 +17,7 @@ type DbConfig struct { Address string `mapstructure:"address"` MaxPaginationLimit int64 `mapstructure:"max-pagination-limit"` DbBatchSizeLimit int64 `mapstructure:"db-batch-size-limit"` - LogicalShardCount int64 `mapstructure:"logical-shard-count"` + LogicalShardCount *int64 `mapstructure:"logical-shard-count"` } func (cfg *DbConfig) Validate() error { @@ -72,14 +72,16 @@ func (cfg *DbConfig) Validate() error { return fmt.Errorf("db batch size limit must be greater than 0") } - if cfg.LogicalShardCount <= 1 { - return fmt.Errorf("logical shard count must be greater than 1") - } + if cfg.LogicalShardCount != nil { + if *cfg.LogicalShardCount <= 1 { + return fmt.Errorf("logical shard count must be greater than 1") + } - // Below is adding as a safety net to avoid performance issue. - // Changes to the logical shard count shall be discussed with the team - if cfg.LogicalShardCount > maxLogicalShardCount { - return fmt.Errorf("large logical shard count will have significant performance impact, please inform the team before changing this value") + // Below is adding as a safety net to avoid performance issue. + // Changes to the logical shard count shall be discussed with the team + if *cfg.LogicalShardCount > maxLogicalShardCount { + return fmt.Errorf("large logical shard count will have significant performance impact, please inform the team before changing this value") + } } return nil diff --git a/internal/shared/db/clients/db_clients.go b/internal/shared/db/clients/db_clients.go index bde64af5..0c0afc3e 100644 --- a/internal/shared/db/clients/db_clients.go +++ b/internal/shared/db/clients/db_clients.go @@ -5,6 +5,7 @@ import ( "github.com/babylonlabs-io/staking-api-service/internal/shared/config" dbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/client" + indexerdbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/indexer_db_client" v1dbclient "github.com/babylonlabs-io/staking-api-service/internal/v1/db/client" v2dbclient "github.com/babylonlabs-io/staking-api-service/internal/v2/db/client" "github.com/rs/zerolog/log" @@ -12,39 +13,54 @@ import ( ) type DbClients struct { - MongoClient *mongo.Client - SharedDBClient dbclient.DBClient - V1DBClient v1dbclient.V1DBClient - V2DBClient v2dbclient.V2DBClient + StakingMongoClient *mongo.Client + IndexerMongoClient *mongo.Client + SharedDBClient dbclient.DBClient + V1DBClient v1dbclient.V1DBClient + V2DBClient v2dbclient.V2DBClient + IndexerDBClient indexerdbclient.IndexerDBClient } func New(ctx context.Context, cfg *config.Config) (*DbClients, error) { - mongoClient, err := dbclient.NewMongoClient(ctx, cfg.Db) + stakingMongoClient, err := dbclient.NewMongoClient(ctx, cfg.StakingDb) if err != nil { return nil, err } - dbClient, err := dbclient.New(ctx, mongoClient, cfg.Db) + dbClient, err := dbclient.New(ctx, stakingMongoClient, cfg.StakingDb) if err != nil { return nil, err } - v1dbClient, err := v1dbclient.New(ctx, mongoClient, cfg.Db) + v1dbClient, err := v1dbclient.New(ctx, stakingMongoClient, cfg.StakingDb) if err != nil { log.Ctx(ctx).Fatal().Err(err).Msg("error while creating v1 db client") return nil, err } - v2dbClient, err := v2dbclient.New(ctx, mongoClient, cfg.Db) + v2dbClient, err := v2dbclient.New(ctx, stakingMongoClient, cfg.StakingDb) if err != nil { log.Ctx(ctx).Fatal().Err(err).Msg("error while creating v2 db client") return nil, err } + indexerMongoClient, err := dbclient.NewMongoClient(ctx, cfg.IndexerDb) + if err != nil { + return nil, err + } + + indexerDbClient, err := indexerdbclient.New(ctx, indexerMongoClient, cfg.IndexerDb) + if err != nil { + log.Ctx(ctx).Fatal().Err(err).Msg("error while creating indexer db client") + return nil, err + } + dbClients := DbClients{ - MongoClient: mongoClient, - SharedDBClient: dbClient, - V1DBClient: v1dbClient, - V2DBClient: v2dbClient, + StakingMongoClient: stakingMongoClient, + IndexerMongoClient: indexerMongoClient, + SharedDBClient: dbClient, + V1DBClient: v1dbClient, + V2DBClient: v2dbClient, + IndexerDBClient: indexerDbClient, } return &dbClients, nil diff --git a/internal/shared/db/indexer_db_client/db_client.go b/internal/shared/db/indexer_db_client/db_client.go new file mode 100644 index 00000000..058fe0ec --- /dev/null +++ b/internal/shared/db/indexer_db_client/db_client.go @@ -0,0 +1,23 @@ +package indexerdbclient + +import ( + "context" + + "github.com/babylonlabs-io/staking-api-service/internal/shared/config" + dbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/client" + "go.mongodb.org/mongo-driver/mongo" +) + +type IndexerDatabase struct { + *dbclient.Database +} + +func New(ctx context.Context, client *mongo.Client, cfg *config.DbConfig) (*IndexerDatabase, error) { + return &IndexerDatabase{ + Database: &dbclient.Database{ + DbName: cfg.DbName, + Client: client, + Cfg: cfg, + }, + }, nil +} diff --git a/internal/shared/db/indexer_db_client/interface.go b/internal/shared/db/indexer_db_client/interface.go new file mode 100644 index 00000000..fb7554fc --- /dev/null +++ b/internal/shared/db/indexer_db_client/interface.go @@ -0,0 +1,9 @@ +package indexerdbclient + +import ( + dbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/client" +) + +type IndexerDBClient interface { + dbclient.DBClient +} diff --git a/internal/shared/db/model/setup.go b/internal/shared/db/model/setup.go index 2a00d014..3bb1b5d8 100644 --- a/internal/shared/db/model/setup.go +++ b/internal/shared/db/model/setup.go @@ -58,10 +58,10 @@ var collections = map[string][]index{ func Setup(ctx context.Context, cfg *config.Config) error { credential := options.Credential{ - Username: cfg.Db.Username, - Password: cfg.Db.Password, + Username: cfg.StakingDb.Username, + Password: cfg.StakingDb.Password, } - clientOps := options.Client().ApplyURI(cfg.Db.Address).SetAuth(credential) + clientOps := options.Client().ApplyURI(cfg.StakingDb.Address).SetAuth(credential) client, err := mongo.Connect(ctx, clientOps) if err != nil { return err @@ -72,7 +72,7 @@ func Setup(ctx context.Context, cfg *config.Config) error { defer cancel() // Access a database and create collections. - database := client.Database(cfg.Db.DbName) + database := client.Database(cfg.StakingDb.DbName) // Create collections. for collection := range collections { diff --git a/internal/shared/services/service/service.go b/internal/shared/services/service/service.go index 30168751..6da1dd32 100644 --- a/internal/shared/services/service/service.go +++ b/internal/shared/services/service/service.go @@ -41,9 +41,10 @@ func New( // DoHealthCheck checks the health of the services by ping the database. func (s *Service) DoHealthCheck(ctx context.Context) error { - return s.DbClients.V1DBClient.Ping(ctx) - - // TODO: extend on this to ping indexer db + if err := s.DbClients.SharedDBClient.Ping(ctx); err != nil { + return err + } + return s.DbClients.IndexerDBClient.Ping(ctx) } func (s *Service) SaveUnprocessableMessages(ctx context.Context, messageBody, receipt string) *types.Error { diff --git a/internal/v1/db/client/stats.go b/internal/v1/db/client/stats.go index 79bd8f09..27f4914c 100644 --- a/internal/v1/db/client/stats.go +++ b/internal/v1/db/client/stats.go @@ -167,7 +167,7 @@ func (v1dbclient *V1Database) SubtractOverallStats( func (v1dbclient *V1Database) GetOverallStats(ctx context.Context) (*v1dbmodel.OverallStatsDocument, error) { // The collection is sharded by the _id field, so we need to query all the shards var shardsId []string - for i := 0; i < int(v1dbclient.Cfg.LogicalShardCount); i++ { + for i := 0; i < int(*v1dbclient.Cfg.LogicalShardCount); i++ { shardsId = append(shardsId, fmt.Sprintf("%d", i)) } @@ -201,7 +201,7 @@ func (v1dbclient *V1Database) GetOverallStats(ctx context.Context) (*v1dbmodel.O // It's a logical shard to avoid locking the same field during concurrent writes // The sharding number should never be reduced after roll out func (v1dbclient *V1Database) generateOverallStatsId() (string, error) { - max := big.NewInt(int64(v1dbclient.Cfg.LogicalShardCount)) + max := big.NewInt(*v1dbclient.Cfg.LogicalShardCount) // Generate a secure random number within the range [0, max) n, err := rand.Int(rand.Reader, max) if err != nil { diff --git a/tests/config/config-test.yml b/tests/config/config-test.yml index 1aa5c413..185dede9 100644 --- a/tests/config/config-test.yml +++ b/tests/config/config-test.yml @@ -9,7 +9,7 @@ server: btc-net: "signet" max-content-length: 40960 health-check-interval: 2 -db: +staking-db: username: root password: example address: "mongodb://localhost:27017" @@ -17,6 +17,13 @@ db: max-pagination-limit: 10 db-batch-size-limit: 100 logical-shard-count: 2 +indexer-db: + username: root + password: example + address: "mongodb://localhost:27019" + db-name: indexer-db + max-pagination-limit: 10 + db-batch-size-limit: 100 queue: queue_user: user queue_password: password diff --git a/tests/integration_test/finality_provider_test.go b/tests/integration_test/finality_provider_test.go index a42cfeab..32bffbc0 100644 --- a/tests/integration_test/finality_provider_test.go +++ b/tests/integration_test/finality_provider_test.go @@ -56,8 +56,8 @@ func TestGetFinalityProviderShouldNotFailInCaseOfDbFailure(t *testing.T) { mockV1DBClient.On("FindFinalityProviderStats", mock.Anything, mock.Anything).Return(nil, errors.New("just an error")) mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }}) shouldGetFinalityProvidersSuccessfully(t, testServer) } @@ -72,8 +72,8 @@ func TestGetFinalityProviderShouldReturnFallbackToGlobalParams(t *testing.T) { mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }}) shouldGetFinalityProvidersSuccessfully(t, testServer) } @@ -84,8 +84,8 @@ func TestGetFinalityProviderReturn4xxErrorIfPageTokenInvalid(t *testing.T) { mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }}) url := testServer.Server.URL + finalityProvidersPath defer testServer.Close() @@ -129,8 +129,8 @@ func FuzzGetFinalityProviderShouldReturnAllRegisteredFps(f *testing.F) { mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }, MockedFinalityProviders: fpParams}) url := testServer.Server.URL + finalityProvidersPath @@ -212,7 +212,7 @@ func FuzzTestGetFinalityProviderWithPaginationResponse(f *testing.F) { if err != nil { t.Fatalf("Failed to load test config: %v", err) } - cfg.Db.MaxPaginationLimit = 2 + cfg.StakingDb.MaxPaginationLimit = 2 testServer := setupTestServer(t, &TestServerDependency{ConfigOverrides: cfg}) defer testServer.Close() @@ -273,8 +273,8 @@ func FuzzGetFinalityProviderShouldNotReturnRegisteredFpWithoutStakingForPaginate mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }, MockedFinalityProviders: fpParams}) url := testServer.Server.URL + finalityProvidersPath @@ -337,8 +337,8 @@ func FuzzShouldNotReturnDefaultFpFromParamsWhenPageTokenIsPresent(f *testing.F) mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }, MockedFinalityProviders: fpParams}) url := testServer.Server.URL + finalityProvidersPath + "?pagination_key=abcd" @@ -370,8 +370,8 @@ func FuzzGetFinalityProvider(f *testing.F) { mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }, MockedFinalityProviders: fpParams}) url := testServer.Server.URL + finalityProvidersPath + "?fp_btc_pk=" + fpParams[0].BtcPk // Make a GET request to the finality providers endpoint @@ -395,8 +395,8 @@ func FuzzGetFinalityProvider(f *testing.F) { ).Return(fpStats, nil) testServer = setupTestServer(t, &TestServerDependency{ MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }, MockedFinalityProviders: fpParams, }) diff --git a/tests/integration_test/healthcheck_test.go b/tests/integration_test/healthcheck_test.go index 6bfb753c..ad23019e 100644 --- a/tests/integration_test/healthcheck_test.go +++ b/tests/integration_test/healthcheck_test.go @@ -45,13 +45,15 @@ func TestHealthCheck(t *testing.T) { // Test the db connection error case func TestHealthCheckDBError(t *testing.T) { - mockV1DBClient := new(testmock.V1DBClient) - mockV1DBClient.On("Ping", mock.Anything).Return(io.EOF) // Expect db error + mockDbClients := new(testmock.DBClient) + mockDbClients.On("Ping", mock.Anything).Return(io.EOF) // Expect db error mockMongoClient := &mongo.Client{} + mockIndexerDbClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + IndexerMongoClient: mockIndexerDbClient, + SharedDBClient: mockDbClients, }}) defer testServer.Close() diff --git a/tests/integration_test/replay_unprocessed_messages_test.go b/tests/integration_test/replay_unprocessed_messages_test.go index 1680d5a5..b7acc801 100644 --- a/tests/integration_test/replay_unprocessed_messages_test.go +++ b/tests/integration_test/replay_unprocessed_messages_test.go @@ -37,8 +37,8 @@ func TestReplayUnprocessableMessages(t *testing.T) { dbmodel.NewUnprocessableMessageDocument(doc, "receipt"), ) dbClients, _ := testutils.DirectDbConnection(testServer.Config) - defer dbClients.MongoClient.Disconnect(ctx) - dbclient, err := dbclient.New(ctx, dbClients.MongoClient, testServer.Config.Db) + defer dbClients.StakingMongoClient.Disconnect(ctx) + dbclient, err := dbclient.New(ctx, dbClients.StakingMongoClient, testServer.Config.StakingDb) assert.NoError(t, err, "creating db client should not return an error") scripts.ReplayUnprocessableMessages(ctx, testServer.Config, testServer.Queues, dbclient) diff --git a/tests/integration_test/setup.go b/tests/integration_test/setup.go index 82940c61..f0cf0096 100644 --- a/tests/integration_test/setup.go +++ b/tests/integration_test/setup.go @@ -119,7 +119,7 @@ func setupTestServer(t *testing.T, dep *TestServerDependency) *TestServer { // setup test db dbClients := testutils.SetupTestDB(*cfg) - if dep != nil && (dep.MockDbClients.V1DBClient != nil || dep.MockDbClients.V2DBClient != nil || dep.MockDbClients.MongoClient != nil) { + if dep != nil && (dep.MockDbClients.V1DBClient != nil || dep.MockDbClients.V2DBClient != nil || dep.MockDbClients.StakingMongoClient != nil) { dbClients = &dep.MockDbClients } @@ -155,7 +155,7 @@ func setupTestServer(t *testing.T, dep *TestServerDependency) *TestServer { Conn: conn, channel: ch, Config: cfg, - Db: dbClients.MongoClient, + Db: dbClients.StakingMongoClient, } } diff --git a/tests/integration_test/staker_test.go b/tests/integration_test/staker_test.go index 9717830a..5f6d1812 100644 --- a/tests/integration_test/staker_test.go +++ b/tests/integration_test/staker_test.go @@ -32,7 +32,7 @@ func FuzzTestStakerDelegationsWithPaginationResponse(f *testing.F) { r := rand.New(rand.NewSource(seed)) testServer := setupTestServer(t, nil) defer testServer.Close() - numOfStaker1Events := int(testServer.Config.Db.MaxPaginationLimit) + r.Intn(100) + numOfStaker1Events := int(testServer.Config.StakingDb.MaxPaginationLimit) + r.Intn(100) activeStakingEventsByStaker1 := testutils.GenerateRandomActiveStakingEvents( r, &testutils.TestActiveEventGeneratorOpts{ @@ -43,7 +43,7 @@ func FuzzTestStakerDelegationsWithPaginationResponse(f *testing.F) { activeStakingEventsByStaker2 := testutils.GenerateRandomActiveStakingEvents( r, &testutils.TestActiveEventGeneratorOpts{ - NumOfEvents: int(testServer.Config.Db.MaxPaginationLimit) + 1, + NumOfEvents: int(testServer.Config.StakingDb.MaxPaginationLimit) + 1, Stakers: testutils.GeneratePks(1), }, ) @@ -310,7 +310,7 @@ func FuzzStakerDelegationsFilterByState(f *testing.F) { r := rand.New(rand.NewSource(seed)) testServer := setupTestServer(t, nil) defer testServer.Close() - numOfDelegations := int(testServer.Config.Db.MaxPaginationLimit) + + numOfDelegations := int(testServer.Config.StakingDb.MaxPaginationLimit) + testutils.RandomPositiveInt(r, 10) activeStakingEventsByStaker := testutils.GenerateRandomActiveStakingEvents( r, diff --git a/tests/integration_test/stats_test.go b/tests/integration_test/stats_test.go index c54e6641..3e02e134 100644 --- a/tests/integration_test/stats_test.go +++ b/tests/integration_test/stats_test.go @@ -426,7 +426,7 @@ func FuzzTestTopStakersWithPaginationResponse(f *testing.F) { if err != nil { t.Fatalf("Failed to load test config: %v", err) } - cfg.Db.MaxPaginationLimit = int64(paginationSize) + cfg.StakingDb.MaxPaginationLimit = int64(paginationSize) testServer := setupTestServer(t, &TestServerDependency{ConfigOverrides: cfg}) defer testServer.Close() diff --git a/tests/integration_test/unbonding_test.go b/tests/integration_test/unbonding_test.go index 2732ed1c..4c6a411d 100644 --- a/tests/integration_test/unbonding_test.go +++ b/tests/integration_test/unbonding_test.go @@ -515,8 +515,8 @@ func TestUnbondingRequestValidation(t *testing.T) { ) mockMongoClient := &mongo.Client{} testServer := setupTestServer(t, &TestServerDependency{MockDbClients: dbclients.DbClients{ - MongoClient: mockMongoClient, - V1DBClient: mockV1DBClient, + StakingMongoClient: mockMongoClient, + V1DBClient: mockV1DBClient, }}) defer testServer.Close() unbondingUrl := testServer.Server.URL + unbondingPath diff --git a/tests/scripts_test/pubkey_address_backfil_test.go b/tests/scripts_test/pubkey_address_backfil_test.go index 22d61e18..79d90ecd 100644 --- a/tests/scripts_test/pubkey_address_backfil_test.go +++ b/tests/scripts_test/pubkey_address_backfil_test.go @@ -52,7 +52,7 @@ func TestBackfillAddressesBasedOnPubKeys(t *testing.T) { // Clean the database testutils.SetupTestDB(*cfg) // inject some data - docs := createNewDelegationDocuments(cfg, int(cfg.Db.MaxPaginationLimit)+1) + docs := createNewDelegationDocuments(cfg, int(cfg.StakingDb.MaxPaginationLimit)+1) for _, doc := range docs { testutils.InjectDbDocument( cfg, dbmodel.V1DelegationCollection, doc, diff --git a/tests/testutils/database.go b/tests/testutils/database.go index a9e5f5cf..628e3e95 100644 --- a/tests/testutils/database.go +++ b/tests/testutils/database.go @@ -7,6 +7,7 @@ import ( "github.com/babylonlabs-io/staking-api-service/internal/shared/config" dbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/client" dbclients "github.com/babylonlabs-io/staking-api-service/internal/shared/db/clients" + indexerdbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/indexer_db_client" dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model" v1dbclient "github.com/babylonlabs-io/staking-api-service/internal/v1/db/client" v2dbclient "github.com/babylonlabs-io/staking-api-service/internal/v2/db/client" @@ -18,30 +19,45 @@ import ( var setUpDbIndex = false func DirectDbConnection(cfg *config.Config) (*dbclients.DbClients, string) { - mongoClient, err := mongo.Connect( - context.TODO(), options.Client().ApplyURI(cfg.Db.Address), + stakingMongoClient, err := mongo.Connect( + context.TODO(), options.Client().ApplyURI(cfg.StakingDb.Address), ) if err != nil { log.Fatal(err) } - dbClient, err := dbclient.New(context.TODO(), mongoClient, cfg.Db) + dbClient, err := dbclient.New(context.TODO(), stakingMongoClient, cfg.StakingDb) if err != nil { log.Fatal(err) } - v1dbClient, err := v1dbclient.New(context.TODO(), mongoClient, cfg.Db) + v1dbClient, err := v1dbclient.New(context.TODO(), stakingMongoClient, cfg.StakingDb) if err != nil { log.Fatal(err) } - v2dbClient, err := v2dbclient.New(context.TODO(), mongoClient, cfg.Db) + v2dbClient, err := v2dbclient.New(context.TODO(), stakingMongoClient, cfg.StakingDb) + if err != nil { + log.Fatal(err) + } + + // IndexerDBClient + indexerMongoClient, err := mongo.Connect( + context.TODO(), options.Client().ApplyURI(cfg.IndexerDb.Address), + ) + if err != nil { + log.Fatal(err) + } + + indexerdbClient, err := indexerdbclient.New(context.TODO(), indexerMongoClient, cfg.IndexerDb) if err != nil { log.Fatal(err) } return &dbclients.DbClients{ - MongoClient: mongoClient, - SharedDBClient: dbClient, - V1DBClient: v1dbClient, - V2DBClient: v2dbClient, - }, cfg.Db.DbName + StakingMongoClient: stakingMongoClient, + IndexerMongoClient: indexerMongoClient, + SharedDBClient: dbClient, + V1DBClient: v1dbClient, + V2DBClient: v2dbClient, + IndexerDBClient: indexerdbClient, + }, cfg.StakingDb.DbName } // SetupTestDB connects to MongoDB and purges all collections. @@ -57,7 +73,7 @@ func SetupTestDB(cfg config.Config) *dbclients.DbClients { } setUpDbIndex = true } - if err := PurgeAllCollections(context.TODO(), dbClients.MongoClient, dbName); err != nil { + if err := PurgeAllCollections(context.TODO(), dbClients.StakingMongoClient, dbName); err != nil { log.Fatal("Failed to purge database:", err) } @@ -69,8 +85,8 @@ func InjectDbDocument[T any]( cfg *config.Config, collectionName string, doc T, ) { connection, dbName := DirectDbConnection(cfg) - defer connection.MongoClient.Disconnect(context.Background()) - collection := connection.MongoClient.Database(dbName). + defer connection.StakingMongoClient.Disconnect(context.Background()) + collection := connection.StakingMongoClient.Database(dbName). Collection(collectionName) _, err := collection.InsertOne(context.Background(), doc) @@ -84,7 +100,7 @@ func InspectDbDocuments[T any]( cfg *config.Config, collectionName string, ) ([]T, error) { connection, dbName := DirectDbConnection(cfg) - collection := connection.MongoClient.Database(dbName). + collection := connection.StakingMongoClient.Database(dbName). Collection(collectionName) cursor, err := collection.Find(context.Background(), bson.D{}) @@ -92,7 +108,7 @@ func InspectDbDocuments[T any]( return nil, err } defer cursor.Close(context.Background()) - defer connection.MongoClient.Disconnect(context.Background()) + defer connection.StakingMongoClient.Disconnect(context.Background()) var results []T for cursor.Next(context.Background()) { @@ -113,7 +129,7 @@ func UpdateDbDocument( connection *mongo.Client, cfg *config.Config, collectionName string, filter bson.M, update bson.M, ) error { - collection := connection.Database(cfg.Db.DbName). + collection := connection.Database(cfg.StakingDb.DbName). Collection(collectionName) // Perform the update operation