Skip to content

Commit

Permalink
feat(stakingEventWatcher): implement ADR-26 pre-approval staking flow…
Browse files Browse the repository at this point in the history
… and inclusion proof reporting (#70)

### Summary
This PR implements the necessary changes to support the
[ADR-26](https://github.com/babylonlabs-io/pm/blob/main/adr/adr-026-pre-approval-staking-flow.md)
pre-approval staking flow.

### Key Changes
- **Renaming:** The `unbondingWatcher` has been renamed to
`stakingEventWatcher` to reflect its broader responsibilities.
- **Functionality Updates:**
  - Continues to report unbonding transactions as before.
  - Now reports inclusion proofs for staking transactions to Babylon.
- **E2E** Introduced a new e2e test that verifies a delegation created
without an inclusion proof will become active once the proof is
provided.

### Related Issue
Closes [#67](#67)
  • Loading branch information
Lazar955 authored Oct 3, 2024
1 parent b5b8608 commit 8568281
Show file tree
Hide file tree
Showing 15 changed files with 1,097 additions and 570 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mocks:
$(MOCKGEN_CMD) -source=monitor/expected_babylon_client.go -package monitor -destination monitor/mock_babylon_client.go
$(MOCKGEN_CMD) -source=btcstaking-tracker/btcslasher/expected_babylon_client.go -package btcslasher -destination btcstaking-tracker/btcslasher/mock_babylon_client.go
$(MOCKGEN_CMD) -source=btcstaking-tracker/atomicslasher/expected_babylon_client.go -package atomicslasher -destination btcstaking-tracker/atomicslasher/mock_babylon_client.go
$(MOCKGEN_CMD) -source=btcstaking-tracker/unbondingwatcher/expected_babylon_client.go -package unbondingwatcher -destination btcstaking-tracker/unbondingwatcher/mock_babylon_client.go
$(MOCKGEN_CMD) -source=btcstaking-tracker/stakingeventwatcher/expected_babylon_client.go -package stakingeventwatcher -destination btcstaking-tracker/stakingeventwatcher/mock_babylon_client.go

update-changelog:
@echo ./scripts/update_changelog.sh $(sinceTag) $(upcomingTag)
Expand Down
1 change: 1 addition & 0 deletions btcclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BTCClient interface {
SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error)
GetTransaction(txHash *chainhash.Hash) (*btcjson.GetTransactionResult, error)
GetRawTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error)
TxDetails(txHash *chainhash.Hash, pkScript []byte) (*notifier.TxConfirmation, TxStatus, error)
}

type BTCWallet interface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package unbondingwatcher
package stakingeventwatcher

import (
"context"
"fmt"
btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"

"cosmossdk.io/errors"
bbnclient "github.com/babylonlabs-io/babylon/client/client"
Expand All @@ -14,22 +15,21 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
)

var (
ErrInvalidBabylonMsgExecution = fmt.Errorf("invalid babylon msg execution")
)

type Delegation struct {
StakingTx *wire.MsgTx
StakingOutputIdx uint32
DelegationStartHeight uint64
UnbondingOutput *wire.TxOut
HasProof bool
}

type BabylonNodeAdapter interface {
ActiveBtcDelegations(offset uint64, limit uint64) ([]Delegation, error)
DelegationsByStatus(status btcstakingtypes.BTCDelegationStatus, offset uint64, limit uint64) ([]Delegation, error)
IsDelegationActive(stakingTxHash chainhash.Hash) (bool, error)
IsDelegationVerified(stakingTxHash chainhash.Hash) (bool, error)
ReportUnbonding(ctx context.Context, stakingTxHash chainhash.Hash, stakerUnbondingSig *schnorr.Signature) error
BtcClientTipHeight() (uint32, error)
ActivateDelegation(ctx context.Context, stakingTxHash chainhash.Hash, proof *btcctypes.BTCSpvProof) error
}

type BabylonClientAdapter struct {
Expand All @@ -44,10 +44,11 @@ func NewBabylonClientAdapter(babylonClient *bbnclient.Client) *BabylonClientAdap
}
}

// TODO: Consider doing quick retries for failed queries.
func (bca *BabylonClientAdapter) ActiveBtcDelegations(offset uint64, limit uint64) ([]Delegation, error) {
// DelegationsByStatus - returns btc delegations by status
func (bca *BabylonClientAdapter) DelegationsByStatus(
status btcstakingtypes.BTCDelegationStatus, offset uint64, limit uint64) ([]Delegation, error) {
resp, err := bca.babylonClient.BTCDelegations(
btcstakingtypes.BTCDelegationStatus_ACTIVE,
status,
&query.PageRequest{
Key: nil,
Offset: offset,
Expand Down Expand Up @@ -75,8 +76,8 @@ func (bca *BabylonClientAdapter) ActiveBtcDelegations(offset uint64, limit uint6
StakingTx: stakingTx,
StakingOutputIdx: delegation.StakingOutputIdx,
DelegationStartHeight: delegation.StartHeight,
// unbonding transaction always has only one output
UnbondingOutput: unbondingTx.TxOut[0],
UnbondingOutput: unbondingTx.TxOut[0],
HasProof: delegation.StartHeight > 0,
}
}

Expand All @@ -94,6 +95,17 @@ func (bca *BabylonClientAdapter) IsDelegationActive(stakingTxHash chainhash.Hash
return resp.BtcDelegation.Active, nil
}

// IsDelegationVerified method for BabylonClientAdapter checks if delegation is in status verified
func (bca *BabylonClientAdapter) IsDelegationVerified(stakingTxHash chainhash.Hash) (bool, error) {
resp, err := bca.babylonClient.BTCDelegation(stakingTxHash.String())

if err != nil {
return false, fmt.Errorf("failed to retrieve delegation from babyln: %w", err)
}

return resp.BtcDelegation.StatusDesc == btcstakingtypes.BTCDelegationStatus_VERIFIED.String(), nil
}

// ReportUnbonding method for BabylonClientAdapter
func (bca *BabylonClientAdapter) ReportUnbonding(
ctx context.Context,
Expand Down Expand Up @@ -130,3 +142,27 @@ func (bca *BabylonClientAdapter) BtcClientTipHeight() (uint32, error) {

return uint32(resp.Header.Height), nil
}

// ActivateDelegation provides inclusion proof to activate delegation
func (bca *BabylonClientAdapter) ActivateDelegation(
ctx context.Context, stakingTxHash chainhash.Hash, proof *btcctypes.BTCSpvProof) error {
signer := bca.babylonClient.MustGetAddr()

msg := btcstakingtypes.MsgAddBTCDelegationInclusionProof{
Signer: signer,
StakingTxHash: stakingTxHash.String(),
StakingTxInclusionProof: btcstakingtypes.NewInclusionProofFromSpvProof(proof),
}

resp, err := bca.babylonClient.ReliablySendMsg(ctx, &msg, []*errors.Error{}, []*errors.Error{})

if err != nil && resp != nil {
return fmt.Errorf("msg MsgAddBTCDelegationInclusionProof failed exeuction with code %d and error %w", resp.Code, err)
}

if err != nil {
return fmt.Errorf("failed to report unbonding: %w", err)
}

return nil
}

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

Loading

0 comments on commit 8568281

Please sign in to comment.