Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add slashing/unbonding slashing tx in db #74

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions internal/db/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,54 @@ func (db *Database) UpdateDelegationsStateByFinalityProvider(
)
return nil
}

func (db *Database) SaveBTCDelegationSlashingTxHex(
ctx context.Context, stakingTxHash string, slashingTxHex string,
) error {
filter := bson.M{"_id": stakingTxHash}
update := bson.M{
"$set": bson.M{
"slashing_tx_hex": slashingTxHex,
},
}
result, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
UpdateOne(ctx, filter, update)
if err != nil {
return err
}

if result.MatchedCount == 0 {
return &NotFoundError{
Key: stakingTxHash,
Message: "BTC delegation not found when updating slashing tx hex",
}
}

return nil
}
func (db *Database) SaveBTCDelegationUnbondingSlashingTxHex(
ctx context.Context, stakingTxHash string, unbondingSlashingTxHex string,
) error {
filter := bson.M{"_id": stakingTxHash}
update := bson.M{
"$set": bson.M{
"unbonding_slashing_tx_hex": unbondingSlashingTxHex,
},
}
result, err := db.client.Database(db.dbName).
Collection(model.BTCDelegationDetailsCollection).
UpdateOne(ctx, filter, update)
if err != nil {
return err
}

if result.MatchedCount == 0 {
return &NotFoundError{
Key: stakingTxHash,
Message: "BTC delegation not found when updating unbonding slashing tx hex",
}
}

return nil
}
16 changes: 16 additions & 0 deletions internal/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,20 @@ type DbInterface interface {
* @return An error if the operation failed
*/
UpdateLastProcessedBbnHeight(ctx context.Context, height uint64) error
/**
* SaveBTCDelegationSlashingTxHex saves the BTC delegation slashing tx hex.
* @param ctx The context
* @param stakingTxHashHex The staking tx hash hex
* @param slashingTxHex The slashing tx hex
* @return An error if the operation failed
*/
SaveBTCDelegationSlashingTxHex(ctx context.Context, stakingTxHashHex string, slashingTxHex string) 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
* @return An error if the operation failed
*/
SaveBTCDelegationUnbondingSlashingTxHex(ctx context.Context, stakingTxHashHex string, unbondingSlashingTxHex string) error
}
2 changes: 2 additions & 0 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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
}

func FromEventBTCDelegationCreated(
Expand Down
21 changes: 21 additions & 0 deletions internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
"github.com/babylonlabs-io/babylon/btcstaking"
bbn "github.com/babylonlabs-io/babylon/types"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -203,6 +204,16 @@ func (s *Service) handleSpendingStakingTransaction(
return fmt.Errorf("failed to validate slashing tx: %w", err)
}

// Save slashing tx hex
slashingTx, err := bstypes.NewBTCSlashingTxFromMsgTx(spendingTx)
if err != nil {
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 {
return fmt.Errorf("failed to save slashing tx hex: %w", err)
}

// It's a valid slashing tx, watch for spending change output
return s.startWatchingSlashingChange(
ctx,
Expand Down Expand Up @@ -250,6 +261,16 @@ func (s *Service) handleSpendingUnbondingTransaction(
return fmt.Errorf("failed to validate slashing tx: %w", err)
}

// Save unbonding slashing tx hex
unbondingSlashingTx, err := bstypes.NewBTCSlashingTxFromMsgTx(spendingTx)
if err != nil {
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 {
return fmt.Errorf("failed to save unbonding slashing tx hex: %w", err)
}

// It's a valid slashing tx, watch for spending change output
return s.startWatchingSlashingChange(
ctx,
Expand Down
36 changes: 36 additions & 0 deletions tests/mocks/mock_db_client.go

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

Loading