Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper .collection(...) method to database #127

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/db/dbclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ func (db *Database) Ping(ctx context.Context) error {
}
return nil
}

func (db *Database) collection(name string) *mongo.Collection {
return db.client.Database(db.dbName).Collection(name)
}
30 changes: 10 additions & 20 deletions internal/db/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func WithBtcHeight(height int64) UpdateOption {
func (db *Database) SaveNewBTCDelegation(
ctx context.Context, delegationDoc *model.BTCDelegationDetails,
) error {
_, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
_, err := db.collection(model.BTCDelegationDetailsCollection).
InsertOne(ctx, delegationDoc)
if err != nil {
var writeErr mongo.WriteException
Expand Down Expand Up @@ -120,8 +119,7 @@ func (db *Database) UpdateBTCDelegationState(
},
}

res := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
res := db.collection(model.BTCDelegationDetailsCollection).
FindOneAndUpdate(ctx, filter, update)

if res.Err() != nil {
Expand Down Expand Up @@ -181,8 +179,7 @@ func (db *Database) UpdateBTCDelegationDetails(
update["$push"] = bson.M{"state_history": stateRecord}
}

res, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
res, err := db.collection(model.BTCDelegationDetailsCollection).
UpdateOne(ctx, filter, update)

if err != nil {
Expand Down Expand Up @@ -211,8 +208,7 @@ func (db *Database) SaveBTCDelegationUnbondingCovenantSignature(
},
},
}
_, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
_, err := db.collection(model.BTCDelegationDetailsCollection).
UpdateOne(ctx, filter, update)

return err
Expand All @@ -223,8 +219,7 @@ func (db *Database) GetBTCDelegationByStakingTxHash(
) (*model.BTCDelegationDetails, error) {
filter := bson.M{"_id": stakingTxHash}

res := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
res := db.collection(model.BTCDelegationDetailsCollection).
FindOne(ctx, filter)

var delegationDoc model.BTCDelegationDetails
Expand Down Expand Up @@ -266,8 +261,7 @@ func (db *Database) UpdateDelegationsStateByFinalityProvider(
},
}

result, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
result, err := db.collection(model.BTCDelegationDetailsCollection).
UpdateMany(ctx, filter, update)
if err != nil {
return fmt.Errorf("failed to update delegations: %w", err)
Expand All @@ -289,8 +283,7 @@ func (db *Database) GetDelegationsByFinalityProvider(
"finality_provider_btc_pks_hex": fpBTCPKHex,
}

cursor, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
cursor, err := db.collection(model.BTCDelegationDetailsCollection).
Find(ctx, filter)
if err != nil {
return nil, fmt.Errorf("failed to find delegations: %w", err)
Expand Down Expand Up @@ -322,8 +315,7 @@ func (db *Database) SaveBTCDelegationSlashingTxHex(
"slashing_tx.spending_height": spendingHeight,
},
}
result, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
result, err := db.collection(model.BTCDelegationDetailsCollection).
UpdateOne(ctx, filter, update)
if err != nil {
return err
Expand Down Expand Up @@ -352,8 +344,7 @@ func (db *Database) SaveBTCDelegationUnbondingSlashingTxHex(
"slashing_tx.spending_height": spendingHeight,
},
}
result, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
result, err := db.collection(model.BTCDelegationDetailsCollection).
UpdateOne(ctx, filter, update)
if err != nil {
return err
Expand Down Expand Up @@ -381,8 +372,7 @@ func (db *Database) GetBTCDelegationsByStates(

filter := bson.M{"state": bson.M{"$in": stateStrings}}

cursor, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
cursor, err := db.collection(model.BTCDelegationDetailsCollection).
Find(ctx, filter)
if err != nil {
return nil, err
Expand Down
11 changes: 4 additions & 7 deletions internal/db/finality_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
func (db *Database) SaveNewFinalityProvider(
ctx context.Context, fpDoc *model.FinalityProviderDetails,
) error {
_, err := db.client.Database(db.dbName).
Collection(model.FinalityProviderDetailsCollection).
_, err := db.collection(model.FinalityProviderDetailsCollection).
InsertOne(ctx, fpDoc)
if err != nil {
var writeErr mongo.WriteException
Expand Down Expand Up @@ -59,8 +58,7 @@ func (db *Database) UpdateFinalityProviderDetailsFromEvent(

// Perform the update only if there are fields to update
if len(updateFields) > 0 {
res, err := db.client.Database(db.dbName).
Collection(model.FinalityProviderDetailsCollection).
res, err := db.collection(model.FinalityProviderDetailsCollection).
UpdateOne(
ctx, bson.M{"_id": detailsToUpdate.BtcPk}, bson.M{"$set": updateFields},
)
Expand All @@ -87,7 +85,7 @@ func (db *Database) UpdateFinalityProviderState(
update := map[string]interface{}{"$set": map[string]string{"state": newState}}

// Perform the find and update
res := db.client.Database(db.dbName).Collection(model.FinalityProviderDetailsCollection).
res := db.collection(model.FinalityProviderDetailsCollection).
FindOneAndUpdate(ctx, filter, update)

// Check if the document was found
Expand All @@ -108,8 +106,7 @@ func (db *Database) GetFinalityProviderByBtcPk(
ctx context.Context, btcPk string,
) (*model.FinalityProviderDetails, error) {
filter := map[string]interface{}{"_id": btcPk}
res := db.client.Database(db.dbName).
Collection(model.FinalityProviderDetailsCollection).
res := db.collection(model.FinalityProviderDetailsCollection).
FindOne(ctx, filter)

var fpDoc model.FinalityProviderDetails
Expand Down
7 changes: 2 additions & 5 deletions internal/db/last_processed_height.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (

func (db *Database) GetLastProcessedBbnHeight(ctx context.Context) (uint64, error) {
var result model.LastProcessedHeight
err := db.client.Database(db.dbName).
Collection(model.LastProcessedHeightCollection).
err := db.collection(model.LastProcessedHeightCollection).
FindOne(ctx, bson.M{}).Decode(&result)
if err == mongo.ErrNoDocuments {
// If no document exists, return 0
Expand All @@ -27,8 +26,6 @@ func (db *Database) GetLastProcessedBbnHeight(ctx context.Context) (uint64, erro
func (db *Database) UpdateLastProcessedBbnHeight(ctx context.Context, height uint64) error {
update := bson.M{"$set": bson.M{"height": height}}
opts := options.Update().SetUpsert(true)
_, err := db.client.Database(db.dbName).
Collection(model.LastProcessedHeightCollection).
UpdateOne(ctx, bson.M{}, update, opts)
_, err := db.collection(model.LastProcessedHeightCollection).UpdateOne(ctx, bson.M{}, update, opts)
return err
}
7 changes: 3 additions & 4 deletions internal/db/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
func (db *Database) SaveStakingParams(
ctx context.Context, version uint32, params *bbnclient.StakingParams,
) error {
collection := db.client.Database(db.dbName).Collection(model.GlobalParamsCollection)
collection := db.collection(model.GlobalParamsCollection)

doc := &model.StakingParamsDocument{
BaseParamsDocument: model.BaseParamsDocument{
Expand All @@ -46,7 +46,7 @@ func (db *Database) SaveStakingParams(
func (db *Database) SaveCheckpointParams(
ctx context.Context, params *bbnclient.CheckpointParams,
) error {
collection := db.client.Database(db.dbName).Collection(model.GlobalParamsCollection)
collection := db.collection(model.GlobalParamsCollection)

doc := &model.CheckpointParamsDocument{
BaseParamsDocument: model.BaseParamsDocument{
Expand All @@ -67,8 +67,7 @@ func (db *Database) SaveCheckpointParams(
}

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

filter := bson.M{
"type": STAKING_PARAMS_TYPE,
Expand Down
8 changes: 3 additions & 5 deletions internal/db/timelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ func (db *Database) SaveNewTimeLockExpire(
subState types.DelegationSubState,
) error {
tlDoc := model.NewTimeLockDocument(stakingTxHashHex, expireHeight, subState)
_, err := db.client.Database(db.dbName).
Collection(model.TimeLockCollection).
InsertOne(ctx, tlDoc)
_, err := db.collection(model.TimeLockCollection).InsertOne(ctx, tlDoc)
return err
}

func (db *Database) FindExpiredDelegations(ctx context.Context, btcTipHeight, limit uint64) ([]model.TimeLockDocument, error) {
client := db.client.Database(db.dbName).Collection(model.TimeLockCollection)
client := db.collection(model.TimeLockCollection)
filter := bson.M{"expire_height": bson.M{"$lte": btcTipHeight}}

opts := options.Find().SetLimit(int64(limit))
Expand All @@ -43,7 +41,7 @@ func (db *Database) FindExpiredDelegations(ctx context.Context, btcTipHeight, li
}

func (db *Database) DeleteExpiredDelegation(ctx context.Context, stakingTxHashHex string) error {
client := db.client.Database(db.dbName).Collection(model.TimeLockCollection)
client := db.collection(model.TimeLockCollection)
filter := bson.M{"staking_tx_hash_hex": stakingTxHashHex}

result, err := client.DeleteOne(ctx, filter)
Expand Down
Loading