Skip to content

Commit

Permalink
fix: slashing state transition and add spendingHeight (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab authored Dec 20, 2024
1 parent d0cb9e9 commit d406118
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
16 changes: 12 additions & 4 deletions internal/db/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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).
Expand Down
16 changes: 14 additions & 2 deletions internal/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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(
Expand Down
13 changes: 11 additions & 2 deletions internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d406118

Please sign in to comment.