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(stakingEventWatcher): implement ADR-26 pre-approval staking flow and inclusion proof reporting #70

Merged
merged 18 commits into from
Oct 3, 2024
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,29 @@ func NewBabylonClientAdapter(babylonClient *bbnclient.Client) *BabylonClientAdap
}
}

// TODO: Consider doing quick retries for failed queries.
func (bca *BabylonClientAdapter) ActiveBtcDelegations(offset uint64, limit uint64) ([]Delegation, error) {
// BtcDelegations - returns btc delegations TODO: Consider doing quick retries for failed queries.
func (bca *BabylonClientAdapter) BtcDelegations(offset uint64, limit uint64) ([]Delegation, error) {
activeDelegations, err := bca.DelegationsByStatus(btcstakingtypes.BTCDelegationStatus_ACTIVE, offset, limit)
Lazar955 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("failed to fetch active delegations: %w", err)
}

verifiedDelegations, err := bca.DelegationsByStatus(btcstakingtypes.BTCDelegationStatus_VERIFIED, offset, limit)
if err != nil {
Lazar955 marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("failed to fetch verified delegations: %w", err)
}

// Combine results
allDelegations := append(activeDelegations, verifiedDelegations...)

return allDelegations, nil
}

// 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 +94,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 +113,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 +160,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
Loading