From 8eefb802f669bbf4dd70c050d95b51af1163e336 Mon Sep 17 00:00:00 2001 From: Gurjot Singh <111540954+gusin13@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:34:35 +0530 Subject: [PATCH] fix: multiple expiry documents for same staking tx can exist (#97) --- internal/db/model/timelock.go | 2 +- internal/db/timelock.go | 18 +----------------- internal/services/expiry_checker.go | 3 +++ tests/mocks/mock_bbn_client.go | 2 +- tests/mocks/mock_btc_client.go | 2 +- tests/mocks/mock_db_client.go | 22 +++++++++++----------- 6 files changed, 18 insertions(+), 31 deletions(-) diff --git a/internal/db/model/timelock.go b/internal/db/model/timelock.go index 2b4ece3..3ffa5f6 100644 --- a/internal/db/model/timelock.go +++ b/internal/db/model/timelock.go @@ -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"` } diff --git a/internal/db/timelock.go b/internal/db/timelock.go index c94e128..5d7c2ac 100644 --- a/internal/db/timelock.go +++ b/internal/db/timelock.go @@ -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" ) @@ -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) { diff --git a/internal/services/expiry_checker.go b/internal/services/expiry_checker.go index 0df880d..954fac8 100644 --- a/internal/services/expiry_checker.go +++ b/internal/services/expiry_checker.go @@ -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" @@ -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 diff --git a/tests/mocks/mock_bbn_client.go b/tests/mocks/mock_bbn_client.go index 1e50651..8789fe3 100644 --- a/tests/mocks/mock_bbn_client.go +++ b/tests/mocks/mock_bbn_client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.42.1. DO NOT EDIT. +// Code generated by mockery v2.44.1. DO NOT EDIT. package mocks diff --git a/tests/mocks/mock_btc_client.go b/tests/mocks/mock_btc_client.go index a97c702..ff1c791 100644 --- a/tests/mocks/mock_btc_client.go +++ b/tests/mocks/mock_btc_client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.42.1. DO NOT EDIT. +// Code generated by mockery v2.44.1. DO NOT EDIT. package mocks diff --git a/tests/mocks/mock_db_client.go b/tests/mocks/mock_db_client.go index a1bc70e..40a3532 100644 --- a/tests/mocks/mock_db_client.go +++ b/tests/mocks/mock_db_client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.42.1. DO NOT EDIT. +// Code generated by mockery v2.44.1. DO NOT EDIT. package mocks @@ -293,17 +293,17 @@ func (_m *DbInterface) Ping(ctx context.Context) error { return r0 } -// SaveBTCDelegationSlashingTxHex provides a mock function with given fields: ctx, stakingTxHashHex, slashingTxHex -func (_m *DbInterface) SaveBTCDelegationSlashingTxHex(ctx context.Context, stakingTxHashHex string, slashingTxHex string) error { - ret := _m.Called(ctx, stakingTxHashHex, slashingTxHex) +// SaveBTCDelegationSlashingTxHex provides a mock function with given fields: ctx, stakingTxHashHex, slashingTxHex, spendingHeight +func (_m *DbInterface) SaveBTCDelegationSlashingTxHex(ctx context.Context, stakingTxHashHex string, slashingTxHex string, spendingHeight uint32) error { + ret := _m.Called(ctx, stakingTxHashHex, slashingTxHex, spendingHeight) if len(ret) == 0 { panic("no return value specified for SaveBTCDelegationSlashingTxHex") } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, string) error); ok { - r0 = rf(ctx, stakingTxHashHex, slashingTxHex) + if rf, ok := ret.Get(0).(func(context.Context, string, string, uint32) error); ok { + r0 = rf(ctx, stakingTxHashHex, slashingTxHex, spendingHeight) } else { r0 = ret.Error(0) } @@ -329,17 +329,17 @@ func (_m *DbInterface) SaveBTCDelegationUnbondingCovenantSignature(ctx context.C return r0 } -// SaveBTCDelegationUnbondingSlashingTxHex provides a mock function with given fields: ctx, stakingTxHashHex, unbondingSlashingTxHex -func (_m *DbInterface) SaveBTCDelegationUnbondingSlashingTxHex(ctx context.Context, stakingTxHashHex string, unbondingSlashingTxHex string) error { - ret := _m.Called(ctx, stakingTxHashHex, unbondingSlashingTxHex) +// SaveBTCDelegationUnbondingSlashingTxHex provides a mock function with given fields: ctx, stakingTxHashHex, unbondingSlashingTxHex, spendingHeight +func (_m *DbInterface) SaveBTCDelegationUnbondingSlashingTxHex(ctx context.Context, stakingTxHashHex string, unbondingSlashingTxHex string, spendingHeight uint32) error { + ret := _m.Called(ctx, stakingTxHashHex, unbondingSlashingTxHex, spendingHeight) if len(ret) == 0 { panic("no return value specified for SaveBTCDelegationUnbondingSlashingTxHex") } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, string) error); ok { - r0 = rf(ctx, stakingTxHashHex, unbondingSlashingTxHex) + if rf, ok := ret.Get(0).(func(context.Context, string, string, uint32) error); ok { + r0 = rf(ctx, stakingTxHashHex, unbondingSlashingTxHex, spendingHeight) } else { r0 = ret.Error(0) }