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

chore: Add inclusion height to early unbonding event #228

Merged
merged 3 commits into from
Oct 25, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### API Breaking

* [#228](https://github.com/babylonlabs-io/babylon/pull/228) Add inclusion height to early unbonding event

## v0.14.0

### State Machine Breaking
Expand Down
3 changes: 0 additions & 3 deletions proto/babylon/btcstaking/v1/btcstaking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ message BTCDelegation {
// DelegatorUnbondingInfo contains the information about transaction which spent
// the staking output. It contains:
// - spend_stake_tx: the transaction which spent the staking output
// - spend_stake_tx_inclusion_block_hash: the block hash of the block in which
// spend_stake_tx was included
// - spend_stake_tx_sig_inclusion_index: the index of spend_stake_tx in the block
message DelegatorUnbondingInfo {
// spend_stake_tx is the transaction which spent the staking output. It is
// filled only if spend_stake_tx is different than unbonding_tx registered
Expand Down
4 changes: 3 additions & 1 deletion proto/babylon/btcstaking/v1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ message EventBTCDelgationUnbondedEarly {
// staking_tx_hash is the hash of the staking tx.
// It uniquely identifies a BTC delegation
string staking_tx_hash = 1 [(amino.dont_omitempty) = true];
// start_height is the start BTC height of the early unbonding
string start_height = 2 [(amino.dont_omitempty) = true];
// new_state of the BTC delegation
string new_state = 2 [(amino.dont_omitempty) = true];
string new_state = 3 [(amino.dont_omitempty) = true];
}

// EventBTCDelegationExpired is the event emitted when a BTC delegation
Expand Down
18 changes: 8 additions & 10 deletions x/btcstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ func (ms msgServer) BTCUndelegate(goCtx context.Context, req *types.MsgBTCUndele
// we do not need to save it in the database
SpendStakeTx: []byte{},
}

types.EmitEarlyUnbondedEvent(ctx, btcDel.MustGetStakingTxHash().String(), stakerSpendigTxHeader.Height)
} else {
// stakeSpendingTx is not unbonding tx, first we need to verify whether it
// acutally spends staking output
Expand All @@ -599,16 +601,12 @@ func (ms msgServer) BTCUndelegate(goCtx context.Context, req *types.MsgBTCUndele
SpendStakeTx: req.StakeSpendingTx,
}

ev := &types.EventUnexpectedUnbondingTx{
StakingTxHash: btcDel.MustGetStakingTxHash().String(),
SpendStakeTxHash: spendStakeTxHash.String(),
SpendStakeTxHeaderHash: req.StakeSpendingTxInclusionProof.Key.Hash.MarshalHex(),
SpendStakeTxBlockIndex: req.StakeSpendingTxInclusionProof.Key.Index,
}

if err := ctx.EventManager().EmitTypedEvent(ev); err != nil {
panic(fmt.Errorf("failed to emit EventUnexpectedUnbondingTx event: %w", err))
}
types.EmitUnexpectedUnbondingTxEvent(ctx,
btcDel.MustGetStakingTxHash().String(),
spendStakeTxHash.String(),
req.StakeSpendingTxInclusionProof.Key.Hash.MarshalHex(),
req.StakeSpendingTxInclusionProof.Key.Index,
)
}

// all good, add the signature to BTC delegation's undelegation
Expand Down
6 changes: 4 additions & 2 deletions x/btcstaking/keeper/power_dist_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
activeBTCDels[fpBTCPKHex] = append(activeBTCDels[fpBTCPKHex], btcDel)
}
} else if delEvent.NewState == types.BTCDelegationStatus_UNBONDED {
// emit event about this unbonded BTC delegation
types.EmitUnbondedBTCDelEvent(sdkCtx, delEvent.StakingTxHash, btcDel.IsUnbondedEarly())
// emit expired event if it is not early unbonding
if !btcDel.IsUnbondedEarly() {
types.EmitExpiredDelegationEvent(sdkCtx, delEvent.StakingTxHash)
}
// add the unbonded BTC delegation to the map
unbondedBTCDels[delEvent.StakingTxHash] = struct{}{}
}
Expand Down
3 changes: 0 additions & 3 deletions x/btcstaking/types/btcstaking.pb.go

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

55 changes: 41 additions & 14 deletions x/btcstaking/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"
"strconv"

bbn "github.com/babylonlabs-io/babylon/types"
sdk "github.com/cosmos/cosmos-sdk/types"

bbn "github.com/babylonlabs-io/babylon/types"
)

func NewEventPowerDistUpdateWithBTCDel(ev *EventBTCDelegationStateUpdate) *EventPowerDistUpdate {
Expand Down Expand Up @@ -127,9 +128,11 @@ func NewCovenantQuorumReachedEvent(

func NewDelegationUnbondedEarlyEvent(
stakingTxHash string,
startHeight uint32,
) *EventBTCDelgationUnbondedEarly {
return &EventBTCDelgationUnbondedEarly{
StakingTxHash: stakingTxHash,
StartHeight: strconv.FormatUint(uint64(startHeight), 10),
NewState: BTCDelegationStatus_UNBONDED.String(),
}
}
Expand All @@ -153,19 +156,43 @@ func NewFinalityProviderStatusChangeEvent(
}
}

// EmitUnbondedBTCDelEvent emits events for an unbonded BTC delegations
func EmitUnbondedBTCDelEvent(sdkCtx sdk.Context, stakingTxHash string, unbondedEarly bool) {
// delegation expired and become unbonded emit block event about this information
if unbondedEarly {
unbondedEarlyEvent := NewDelegationUnbondedEarlyEvent(stakingTxHash)
if err := sdkCtx.EventManager().EmitTypedEvent(unbondedEarlyEvent); err != nil {
panic(fmt.Errorf("failed to emit event the new unbonded BTC delegation: %w", err))
}
} else {
expiredEvent := NewExpiredDelegationEvent(stakingTxHash)
if err := sdkCtx.EventManager().EmitTypedEvent(expiredEvent); err != nil {
panic(fmt.Errorf("failed to emit event for the new expired BTC delegation: %w", err))
}
func NewUnexpectedUnbondingTxEvent(
stakingTxHash, spendStakeTxHash, spendStakeTxHeaderHash string,
spendStakeTxBlockIndex uint32,
) *EventUnexpectedUnbondingTx {
return &EventUnexpectedUnbondingTx{
StakingTxHash: stakingTxHash,
SpendStakeTxHash: spendStakeTxHash,
SpendStakeTxHeaderHash: spendStakeTxHeaderHash,
SpendStakeTxBlockIndex: spendStakeTxBlockIndex,
}
}

// EmitUnexpectedUnbondingTxEvent emits events for an unexpected unbonding tx
func EmitUnexpectedUnbondingTxEvent(
sdkCtx sdk.Context,
stakingTxHash, spendStakeTxHash, spendStakeTxHeaderHash string,
spendStakeTxBlockIndex uint32,
) {
ev := NewUnexpectedUnbondingTxEvent(stakingTxHash, spendStakeTxHash, spendStakeTxHeaderHash, spendStakeTxBlockIndex)
if err := sdkCtx.EventManager().EmitTypedEvent(ev); err != nil {
panic(fmt.Errorf("failed to emit event the unexpected unbonding tx event: %w", err))
}
}

// EmitEarlyUnbondedEvent emits events for an early unbonded BTC delegation
func EmitEarlyUnbondedEvent(sdkCtx sdk.Context, stakingTxHash string, inclusionHeight uint32) {
ev := NewDelegationUnbondedEarlyEvent(stakingTxHash, inclusionHeight)
if err := sdkCtx.EventManager().EmitTypedEvent(ev); err != nil {
panic(fmt.Errorf("failed to emit event the early unbonded BTC delegation: %w", err))
}
}

// EmitExpiredDelegationEvent emits events for an expired delegation
func EmitExpiredDelegationEvent(sdkCtx sdk.Context, stakingTxHash string) {
ev := NewExpiredDelegationEvent(stakingTxHash)
if err := sdkCtx.EventManager().EmitTypedEvent(ev); err != nil {
panic(fmt.Errorf("failed to emit event the expired BTC delegation: %w", err))
}
}

Expand Down
Loading
Loading