From 797b13430105a7f54f05bc508d34725400a6ae29 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Wed, 11 Dec 2024 23:58:17 +0530 Subject: [PATCH] fix --- internal/db/interface.go | 1 - internal/db/model/params.go | 32 --------------- internal/db/params.go | 29 -------------- internal/services/watch_btc_events.go | 58 +++++++++++++-------------- tests/mocks/mock_db_client.go | 30 -------------- 5 files changed, 28 insertions(+), 122 deletions(-) delete mode 100644 internal/db/model/params.go delete mode 100644 internal/db/params.go diff --git a/internal/db/interface.go b/internal/db/interface.go index 1ba0a3d..3972116 100644 --- a/internal/db/interface.go +++ b/internal/db/interface.go @@ -36,7 +36,6 @@ type DbInterface interface { GetBTCDelegationByStakingTxHash( ctx context.Context, stakingTxHash string, ) (*model.DelegationDocument, error) - GetStakingParams(ctx context.Context, version uint32) (*model.StakingParams, error) GetBTCDelegationsByStates(ctx context.Context, states []types.DelegationState) ([]*model.DelegationDocument, error) GetBTCDelegationState(ctx context.Context, stakingTxHash string) (*types.DelegationState, error) } diff --git a/internal/db/model/params.go b/internal/db/model/params.go deleted file mode 100644 index 52c872c..0000000 --- a/internal/db/model/params.go +++ /dev/null @@ -1,32 +0,0 @@ -package model - -// StakingParams represents the staking parameters of the BBN chain -// Reference: https://github.com/babylonlabs-io/babylon/blob/main/proto/babylon/btcstaking/v1/params.proto -type StakingParams struct { - CovenantPks []string `bson:"covenant_pks"` - CovenantQuorum uint32 `bson:"covenant_quorum"` - MinStakingValueSat int64 `bson:"min_staking_value_sat"` - MaxStakingValueSat int64 `bson:"max_staking_value_sat"` - MinStakingTimeBlocks uint32 `bson:"min_staking_time_blocks"` - MaxStakingTimeBlocks uint32 `bson:"max_staking_time_blocks"` - SlashingPkScript string `bson:"slashing_pk_script"` - MinSlashingTxFeeSat int64 `bson:"min_slashing_tx_fee_sat"` - SlashingRate string `bson:"slashing_rate"` - UnbondingTimeBlocks uint32 `bson:"unbonding_time_blocks"` - UnbondingFeeSat int64 `bson:"unbonding_fee_sat"` - MinCommissionRate string `bson:"min_commission_rate"` - DelegationCreationBaseGasFee uint64 `bson:"delegation_creation_base_gas_fee"` - AllowListExpirationHeight uint64 `bson:"allow_list_expiration_height"` - BtcActivationHeight uint32 `bson:"btc_activation_height"` -} - -type BaseParamsDocument struct { - Type string `bson:"type"` - Version uint32 `bson:"version"` -} - -// Specific document for staking params -type StakingParamsDocument struct { - BaseParamsDocument `bson:",inline"` - Params *StakingParams `bson:"params"` -} diff --git a/internal/db/params.go b/internal/db/params.go deleted file mode 100644 index e522a8e..0000000 --- a/internal/db/params.go +++ /dev/null @@ -1,29 +0,0 @@ -package db - -import ( - "context" - "fmt" - - "github.com/babylonlabs-io/staking-expiry-checker/internal/db/model" - "go.mongodb.org/mongo-driver/bson" -) - -const STAKING_PARAMS_TYPE = "staking_params" - -func (db *Database) GetStakingParams(ctx context.Context, version uint32) (*model.StakingParams, error) { - collection := db.client.Database(db.dbName). - Collection(model.GlobalParamsCollection) - - filter := bson.M{ - "type": STAKING_PARAMS_TYPE, - "version": version, - } - - var params model.StakingParamsDocument - err := collection.FindOne(ctx, filter).Decode(¶ms) - if err != nil { - return nil, fmt.Errorf("failed to get staking params: %w", err) - } - - return params.Params, nil -} diff --git a/internal/services/watch_btc_events.go b/internal/services/watch_btc_events.go index 80ceeaf..00a4afe 100644 --- a/internal/services/watch_btc_events.go +++ b/internal/services/watch_btc_events.go @@ -150,7 +150,7 @@ func (s *Service) handleSpendingStakingTransaction( } // Try to validate as withdrawal transaction - withdrawalErr := s.validateWithdrawalTxFromStaking(spendingTx, spendingInputIdx, delegation, params) + withdrawalErr := s.validateWithdrawalTxFromStaking(spendingTx, spendingInputIdx, delegation, paramsVersion) if withdrawalErr != nil { if errors.Is(err, types.ErrInvalidWithdrawalTx) { metrics.IncrementInvalidStakingWithdrawalTxCounter() @@ -178,13 +178,17 @@ func (s *Service) handleSpendingUnbondingTransaction( spendingInputIdx uint32, delegation *model.DelegationDocument, ) error { - params, err := s.db.GetStakingParams(ctx, delegation.ParamsVersion) - if err != nil { - return fmt.Errorf("failed to get staking params: %w", err) + paramsVersion := s.GetVersionedGlobalParamsByHeight(delegation.StakingTx.StartHeight) + if paramsVersion == nil { + log.Ctx(ctx).Error().Msg("failed to get global params") + return types.NewErrorWithMsg( + http.StatusInternalServerError, types.InternalServiceError, + "failed to get global params based on the staking tx height", + ) } // First try to validate as withdrawal transaction - withdrawalErr := s.validateWithdrawalTxFromUnbonding(spendingTx, delegation, spendingInputIdx, params) + withdrawalErr := s.validateWithdrawalTxFromUnbonding(spendingTx, delegation, spendingInputIdx, paramsVersion) if withdrawalErr != nil { if errors.Is(withdrawalErr, types.ErrInvalidWithdrawalTx) { metrics.IncrementInvalidUnbondingWithdrawalTxCounter() @@ -299,15 +303,15 @@ func (s *Service) IsValidUnbondingTx( // 5. check whether the script of an unbonding tx output is expected // by re-building unbonding output from params - unbondingFee := btcutil.Amount(params.UnbondingFeeSat) + unbondingFee := btcutil.Amount(params.UnbondingFee) expectedUnbondingOutputValue := stakingValue - unbondingFee if expectedUnbondingOutputValue <= 0 { return false, fmt.Errorf("%w: staking output value is too low, got %v, unbonding fee: %v", - types.ErrInvalidUnbondingTx, stakingValue, params.UnbondingFeeSat) + types.ErrInvalidUnbondingTx, stakingValue, params.UnbondingFee) } unbondingInfo, err := btcstaking.BuildUnbondingInfo( stakerPk.MustToBTCPK(), - finalityProviderPks, + []*btcec.PublicKey{fpPK}, covPks, uint32(params.CovenantQuorum), uint16(delegation.UnbondingTx.TimeLock), @@ -332,21 +336,18 @@ func (s *Service) validateWithdrawalTxFromStaking( tx *wire.MsgTx, spendingInputIdx uint32, delegation *model.DelegationDocument, - params *model.StakingParams, + params *types.VersionedGlobalParams, ) error { stakerPk, err := bbn.NewBIP340PubKeyFromHex(delegation.StakerPkHex) if err != nil { return fmt.Errorf("failed to convert staker btc pkh to a public key: %w", err) } - finalityProviderPks := make([]*btcec.PublicKey, len(delegation.FinalityProviderPkHex)) - for i, hex := range delegation.FinalityProviderPkHex { - fpPk, err := bbn.NewBIP340PubKeyFromHex(hex) - if err != nil { - return fmt.Errorf("failed to convert finality provider pk hex to a public key: %w", err) - } - finalityProviderPks[i] = fpPk.MustToBTCPK() + fpPKBIP340, err := bbn.NewBIP340PubKeyFromHex(delegation.FinalityProviderPkHex) + if err != nil { + return fmt.Errorf("failed to convert finality provider pk hex to a public key: %w", err) } + fpPK := fpPKBIP340.MustToBTCPK() covPks := make([]*btcec.PublicKey, len(params.CovenantPks)) for i, hex := range params.CovenantPks { @@ -370,9 +371,9 @@ func (s *Service) validateWithdrawalTxFromStaking( // the witness matches stakingInfo, err := btcstaking.BuildStakingInfo( stakerPk.MustToBTCPK(), - finalityProviderPks, + []*btcec.PublicKey{fpPK}, covPks, - params.CovenantQuorum, + uint32(params.CovenantQuorum), uint16(delegation.StakingTx.TimeLock), stakingValue, btcParams, @@ -404,21 +405,18 @@ func (s *Service) validateWithdrawalTxFromUnbonding( tx *wire.MsgTx, delegation *model.DelegationDocument, spendingInputIdx uint32, - params *model.StakingParams, + params *types.VersionedGlobalParams, ) error { stakerPk, err := bbn.NewBIP340PubKeyFromHex(delegation.StakerPkHex) if err != nil { return fmt.Errorf("failed to convert staker btc pkh to a public key: %w", err) } - finalityProviderPks := make([]*btcec.PublicKey, len(delegation.FinalityProviderPkHex)) - for i, hex := range delegation.FinalityProviderPkHex { - fpPk, err := bbn.NewBIP340PubKeyFromHex(hex) - if err != nil { - return fmt.Errorf("failed to convert finality provider pk hex to a public key: %w", err) - } - finalityProviderPks[i] = fpPk.MustToBTCPK() + fpPKBIP340, err := bbn.NewBIP340PubKeyFromHex(delegation.FinalityProviderPkHex) + if err != nil { + return fmt.Errorf("failed to convert finality provider pk hex to a public key: %w", err) } + fpPK := fpPKBIP340.MustToBTCPK() covPks := make([]*btcec.PublicKey, len(params.CovenantPks)) for i, hex := range params.CovenantPks { @@ -439,14 +437,14 @@ func (s *Service) validateWithdrawalTxFromUnbonding( // re-build the time-lock path script and check whether the script from // the witness matches stakingValue := btcutil.Amount(stakingTx.TxOut[delegation.StakingTx.OutputIndex].Value) - unbondingFee := btcutil.Amount(params.UnbondingFeeSat) + unbondingFee := btcutil.Amount(params.UnbondingFee) expectedUnbondingOutputValue := stakingValue - unbondingFee unbondingInfo, err := btcstaking.BuildUnbondingInfo( stakerPk.MustToBTCPK(), - finalityProviderPks, + []*btcec.PublicKey{fpPK}, covPks, - params.CovenantQuorum, - uint16(delegation.UnbondingTx.TimeLock), + uint32(params.CovenantQuorum), + uint16(params.UnbondingTime), expectedUnbondingOutputValue, btcParams, ) diff --git a/tests/mocks/mock_db_client.go b/tests/mocks/mock_db_client.go index 911da4b..4b0499d 100644 --- a/tests/mocks/mock_db_client.go +++ b/tests/mocks/mock_db_client.go @@ -157,36 +157,6 @@ func (_m *DbInterface) GetBTCDelegationsByStates(ctx context.Context, states []t return r0, r1 } -// GetStakingParams provides a mock function with given fields: ctx, version -func (_m *DbInterface) GetStakingParams(ctx context.Context, version uint32) (*model.StakingParams, error) { - ret := _m.Called(ctx, version) - - if len(ret) == 0 { - panic("no return value specified for GetStakingParams") - } - - var r0 *model.StakingParams - var r1 error - if rf, ok := ret.Get(0).(func(context.Context, uint32) (*model.StakingParams, error)); ok { - return rf(ctx, version) - } - if rf, ok := ret.Get(0).(func(context.Context, uint32) *model.StakingParams); ok { - r0 = rf(ctx, version) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.StakingParams) - } - } - - if rf, ok := ret.Get(1).(func(context.Context, uint32) error); ok { - r1 = rf(ctx, version) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // Ping provides a mock function with given fields: ctx func (_m *DbInterface) Ping(ctx context.Context) error { ret := _m.Called(ctx)