Skip to content

Commit

Permalink
fix: multiple expiry documents for same staking tx can exist (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 authored Dec 20, 2024
1 parent d406118 commit 8eefb80
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 31 deletions.
2 changes: 1 addition & 1 deletion internal/db/model/timelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package model
import "github.com/babylonlabs-io/babylon-staking-indexer/internal/types"

type TimeLockDocument struct {
StakingTxHashHex string `bson:"_id"` // Primary key
StakingTxHashHex string `bson:"staking_tx_hash_hex"`
ExpireHeight uint32 `bson:"expire_height"`
DelegationSubState types.DelegationSubState `bson:"delegation_sub_state"`
}
Expand Down
18 changes: 1 addition & 17 deletions internal/db/timelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package db

import (
"context"
"errors"
"fmt"

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

Expand All @@ -22,21 +20,7 @@ func (db *Database) SaveNewTimeLockExpire(
_, err := db.client.Database(db.dbName).
Collection(model.TimeLockCollection).
InsertOne(ctx, tlDoc)
if err != nil {
var writeErr mongo.WriteException
if errors.As(err, &writeErr) {
for _, e := range writeErr.WriteErrors {
if mongo.IsDuplicateKeyError(e) {
return &DuplicateKeyError{
Key: tlDoc.StakingTxHashHex,
Message: "timelock already exists",
}
}
}
}
return err
}
return nil
return err
}

func (db *Database) FindExpiredDelegations(ctx context.Context, btcTipHeight, limit uint64) ([]model.TimeLockDocument, error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/services/expiry_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
Expand Down Expand Up @@ -47,6 +48,8 @@ func (s *Service) checkExpiry(ctx context.Context) *types.Error {
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Str("current_state", delegation.State.String()).
Str("new_sub_state", tlDoc.DelegationSubState.String()).
Str("expire_height", strconv.FormatUint(uint64(tlDoc.ExpireHeight), 10)).
Msg("checking if delegation is expired")

// Check if the delegation is in a qualified state to transition to Withdrawable
Expand Down
2 changes: 1 addition & 1 deletion tests/mocks/mock_bbn_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_btc_client.go

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

22 changes: 11 additions & 11 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 8eefb80

Please sign in to comment.