From d406118cdda52f03bb326ccf4e06c5f62268cf8e Mon Sep 17 00:00:00 2001 From: Crypto Minion <154598612+jrwbabylonlab@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:06:33 +1100 Subject: [PATCH] fix: slashing state transition and add spendingHeight (#95) --- internal/db/delegation.go | 16 ++++++++++++---- internal/db/interface.go | 16 ++++++++++++++-- internal/db/model/delegation.go | 9 +++++++-- internal/services/watch_btc_events.go | 13 +++++++++++-- internal/types/state.go | 2 +- 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/internal/db/delegation.go b/internal/db/delegation.go index 231373e..d80164b 100644 --- a/internal/db/delegation.go +++ b/internal/db/delegation.go @@ -238,12 +238,16 @@ func (db *Database) GetDelegationsByFinalityProvider( } func (db *Database) SaveBTCDelegationSlashingTxHex( - ctx context.Context, stakingTxHash string, slashingTxHex string, + ctx context.Context, + stakingTxHash string, + slashingTxHex string, + spendingHeight uint32, ) error { filter := bson.M{"_id": stakingTxHash} update := bson.M{ "$set": bson.M{ - "slashing_tx_hex": slashingTxHex, + "slashing_tx.slashing_tx_hex": slashingTxHex, + "slashing_tx.spending_height": spendingHeight, }, } result, err := db.client.Database(db.dbName). @@ -264,12 +268,16 @@ func (db *Database) SaveBTCDelegationSlashingTxHex( } func (db *Database) SaveBTCDelegationUnbondingSlashingTxHex( - ctx context.Context, stakingTxHash string, unbondingSlashingTxHex string, + ctx context.Context, + stakingTxHash string, + unbondingSlashingTxHex string, + spendingHeight uint32, ) error { filter := bson.M{"_id": stakingTxHash} update := bson.M{ "$set": bson.M{ - "unbonding_slashing_tx_hex": unbondingSlashingTxHex, + "slashing_tx.unbonding_slashing_tx_hex": unbondingSlashingTxHex, + "slashing_tx.spending_height": spendingHeight, }, } result, err := db.client.Database(db.dbName). diff --git a/internal/db/interface.go b/internal/db/interface.go index d63064d..3d24efe 100644 --- a/internal/db/interface.go +++ b/internal/db/interface.go @@ -208,17 +208,29 @@ type DbInterface interface { * @param ctx The context * @param stakingTxHashHex The staking tx hash hex * @param slashingTxHex The slashing tx hex + * @param spendingHeight The spending height * @return An error if the operation failed */ - SaveBTCDelegationSlashingTxHex(ctx context.Context, stakingTxHashHex string, slashingTxHex string) error + SaveBTCDelegationSlashingTxHex( + ctx context.Context, + stakingTxHashHex string, + slashingTxHex string, + spendingHeight uint32, + ) error /** * SaveBTCDelegationUnbondingSlashingTxHex saves the BTC delegation unbonding slashing tx hex. * @param ctx The context * @param stakingTxHashHex The staking tx hash hex * @param unbondingSlashingTxHex The unbonding slashing tx hex + * @param spendingHeight The spending height * @return An error if the operation failed */ - SaveBTCDelegationUnbondingSlashingTxHex(ctx context.Context, stakingTxHashHex string, unbondingSlashingTxHex string) error + SaveBTCDelegationUnbondingSlashingTxHex( + ctx context.Context, + stakingTxHashHex string, + unbondingSlashingTxHex string, + spendingHeight uint32, + ) error /** * GetBTCDelegationsByStates retrieves the BTC delegations by the states. * @param ctx The context diff --git a/internal/db/model/delegation.go b/internal/db/model/delegation.go index 81f1ed4..9cd2c96 100644 --- a/internal/db/model/delegation.go +++ b/internal/db/model/delegation.go @@ -21,6 +21,12 @@ type BTCDelegationCreatedBbnBlock struct { Timestamp int64 `bson:"timestamp"` // epoch time in seconds } +type SlashingTx struct { + SlashingTxHex string `bson:"slashing_tx_hex"` + UnbondingSlashingTxHex string `bson:"unbonding_slashing_tx_hex"` + SpendingHeight uint32 `bson:"spending_height"` +} + type BTCDelegationDetails struct { StakingTxHashHex string `bson:"_id"` // Primary key StakingTxHex string `bson:"staking_tx_hex"` @@ -38,8 +44,7 @@ type BTCDelegationDetails struct { UnbondingTx string `bson:"unbonding_tx"` CovenantUnbondingSignatures []CovenantSignature `bson:"covenant_unbonding_signatures"` BTCDelegationCreatedBlock BTCDelegationCreatedBbnBlock `bson:"btc_delegation_created_bbn_block"` - SlashingTxHex string `bson:"slashing_tx_hex"` // Will be "" if not slashed - UnbondingSlashingTxHex string `bson:"unbonding_slashing_tx_hex"` // Will be "" if not slashed + SlashingTx SlashingTx `bson:"slashing_tx"` } func FromEventBTCDelegationCreated( diff --git a/internal/services/watch_btc_events.go b/internal/services/watch_btc_events.go index 0e1e2e5..7760b5b 100644 --- a/internal/services/watch_btc_events.go +++ b/internal/services/watch_btc_events.go @@ -216,7 +216,12 @@ func (s *Service) handleSpendingStakingTransaction( return fmt.Errorf("failed to convert slashing tx to bytes: %w", err) } slashingTxHex := slashingTx.ToHexStr() - if err := s.db.SaveBTCDelegationSlashingTxHex(ctx, delegation.StakingTxHashHex, slashingTxHex); err != nil { + if err := s.db.SaveBTCDelegationSlashingTxHex( + ctx, + delegation.StakingTxHashHex, + slashingTxHex, + spendingHeight, + ); err != nil { return fmt.Errorf("failed to save slashing tx hex: %w", err) } @@ -273,7 +278,11 @@ func (s *Service) handleSpendingUnbondingTransaction( return fmt.Errorf("failed to convert unbonding slashing tx to bytes: %w", err) } unbondingSlashingTxHex := unbondingSlashingTx.ToHexStr() - if err := s.db.SaveBTCDelegationUnbondingSlashingTxHex(ctx, delegation.StakingTxHashHex, unbondingSlashingTxHex); err != nil { + if err := s.db.SaveBTCDelegationUnbondingSlashingTxHex( + ctx, delegation.StakingTxHashHex, + unbondingSlashingTxHex, + spendingHeight, + ); err != nil { return fmt.Errorf("failed to save unbonding slashing tx hex: %w", err) } diff --git a/internal/types/state.go b/internal/types/state.go index cc00155..91b1631 100644 --- a/internal/types/state.go +++ b/internal/types/state.go @@ -61,7 +61,7 @@ func QualifiedStatesForWithdrawn() []DelegationState { // QualifiedStatesForWithdrawable returns the qualified current states for Withdrawable event func QualifiedStatesForWithdrawable() []DelegationState { - return []DelegationState{StateUnbonding} + return []DelegationState{StateUnbonding, StateSlashed} } type DelegationSubState string