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(sew): ADR-029 generalized unbonding and babylon v0.13.0 #87

Merged
merged 10 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#84](https://github.com/babylonlabs-io/vigilante/pull/84) fix spawning more go routines than needed when activating
delegations, add more logging

### Improvements
* [#87](https://github.com/babylonlabs-io/vigilante/pull/87) adr 029 for generalized unbonding

## v0.13.0

Expand Down
24 changes: 19 additions & 5 deletions btcstaking-tracker/btcslasher/bootstrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package btcslasher

import (
"fmt"
"github.com/babylonlabs-io/babylon/types"

ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -22,16 +23,29 @@ func (bs *BTCSlasher) Bootstrap(startHeight uint64) error {

// handle all evidences since the given start height, i.e., for each evidence,
// extract its SK and try to slash all BTC delegations under it
err := bs.handleAllEvidences(startHeight, func(evidences []*ftypes.Evidence) error {
err := bs.handleAllEvidences(startHeight, func(evidences []*ftypes.EvidenceResponse) error {
var accumulatedErrs error // we use this variable to accumulate errors

for _, evidence := range evidences {
fpBTCPK := evidence.FpBtcPk
fpBTCPKHex := fpBTCPK.MarshalHex()
fpBTCPKHex := evidence.FpBtcPkHex
bs.logger.Infof("found evidence for finality provider %s at height %d after start height %d", fpBTCPKHex, evidence.BlockHeight, startHeight)

btcPK, err := types.NewBIP340PubKeyFromHex(fpBTCPKHex)
if err != nil {
return fmt.Errorf("err parsing fp btc %w", err)
}

e := ftypes.Evidence{
FpBtcPk: btcPK,
BlockHeight: evidence.BlockHeight,
PubRand: evidence.PubRand,
CanonicalAppHash: evidence.CanonicalAppHash,
ForkAppHash: evidence.ForkAppHash,
CanonicalFinalitySig: evidence.CanonicalFinalitySig,
ForkFinalitySig: evidence.ForkFinalitySig,
}
// extract the SK of the slashed finality provider
fpBTCSK, err := evidence.ExtractBTCSK()
fpBTCSK, err := e.ExtractBTCSK()
if err != nil {
bs.logger.Errorf("failed to extract BTC SK of the slashed finality provider %s: %v", fpBTCPKHex, err)
accumulatedErrs = multierror.Append(accumulatedErrs, err)
Expand All @@ -57,7 +71,7 @@ func (bs *BTCSlasher) Bootstrap(startHeight uint64) error {
return nil
}

func (bs *BTCSlasher) handleAllEvidences(startHeight uint64, handleFunc func(evidences []*ftypes.Evidence) error) error {
func (bs *BTCSlasher) handleAllEvidences(startHeight uint64, handleFunc func(evidences []*ftypes.EvidenceResponse) error) error {
pagination := query.PageRequest{Limit: defaultPaginationLimit}
for {
resp, err := bs.BBNQuerier.ListEvidences(startHeight, &pagination)
Expand Down
11 changes: 10 additions & 1 deletion btcstaking-tracker/btcslasher/bootstrapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,17 @@ func FuzzSlasher_Bootstrapping(f *testing.F) {
// mock an evidence with this finality provider
evidence, err := datagen.GenRandomEvidence(r, fpSK, 100)
require.NoError(t, err)
er := &ftypes.EvidenceResponse{
FpBtcPkHex: evidence.FpBtcPk.MarshalHex(),
BlockHeight: evidence.BlockHeight,
PubRand: evidence.PubRand,
CanonicalAppHash: evidence.CanonicalAppHash,
ForkAppHash: evidence.ForkAppHash,
CanonicalFinalitySig: evidence.CanonicalFinalitySig,
ForkFinalitySig: evidence.ForkFinalitySig,
}
mockBabylonQuerier.EXPECT().ListEvidences(gomock.Any(), gomock.Any()).Return(&ftypes.QueryListEvidencesResponse{
Evidences: []*ftypes.Evidence{evidence},
Evidences: []*ftypes.EvidenceResponse{er},
Pagination: &query.PageResponse{NextKey: nil},
}, nil).Times(1)

Expand Down
8 changes: 4 additions & 4 deletions btcstaking-tracker/btcslasher/slasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ func FuzzSlasher(f *testing.F) {
err = unbondingSlashingInfo.UnbondingTx.Serialize(&unbondingTxBuffer)
require.NoError(t, err)
unbondingBTCDel.BtcUndelegation = &bstypes.BTCUndelegation{
UnbondingTx: unbondingTxBuffer.Bytes(),
SlashingTx: unbondingSlashingInfo.SlashingTx,
DelegatorSlashingSig: delSlashingSig,
DelegatorUnbondingSig: delSlashingSig,
UnbondingTx: unbondingTxBuffer.Bytes(),
SlashingTx: unbondingSlashingInfo.SlashingTx,
DelegatorSlashingSig: delSlashingSig,
DelegatorUnbondingInfo: &bstypes.DelegatorUnbondingInfo{SpendStakeTx: unbondingTxBuffer.Bytes()},
// TODO: currently requires only one sig, in reality requires all of them
CovenantSlashingSigs: covenantSlashingSigs,
CovenantUnbondingSigList: covenantUnbondingSigs,
Expand Down
3 changes: 1 addition & 2 deletions btcstaking-tracker/btcslasher/slasher_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,7 @@ func (bs *BTCSlasher) getAllActiveAndUnbondedBTCDelegations(
activeDels = append(activeDels, dels.Dels[i])
}
if strings.EqualFold(del.StatusDesc, bstypes.BTCDelegationStatus_UNBONDED.String()) &&
len(del.UndelegationResponse.CovenantSlashingSigs) >= int(bsParams.CovenantQuorum) &&
len(del.UndelegationResponse.DelegatorUnbondingSigHex) > 0 {
len(del.UndelegationResponse.CovenantSlashingSigs) >= int(bsParams.CovenantQuorum) {
// NOTE: Babylon considers a BTC delegation to be unbonded once it
// receives staker signature for unbonding transaction, no matter
// whether the unbonding tx's timelock has expired. In monitor's view we need to try to slash every
Expand Down
22 changes: 14 additions & 8 deletions btcstaking-tracker/stakingeventwatcher/expected_babylon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
bbnclient "github.com/babylonlabs-io/babylon/client/client"
bbn "github.com/babylonlabs-io/babylon/types"
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -28,7 +27,8 @@ type BabylonNodeAdapter interface {
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
ReportUnbonding(ctx context.Context, stakingTxHash chainhash.Hash, stakeSpendingTx *wire.MsgTx,
inclusionProof *btcstakingtypes.InclusionProof) error
BtcClientTipHeight() (uint32, error)
ActivateDelegation(ctx context.Context, stakingTxHash chainhash.Hash, proof *btcctypes.BTCSpvProof) error
}
Expand Down Expand Up @@ -111,17 +111,23 @@ func (bca *BabylonClientAdapter) IsDelegationVerified(stakingTxHash chainhash.Ha
func (bca *BabylonClientAdapter) ReportUnbonding(
ctx context.Context,
stakingTxHash chainhash.Hash,
stakerUnbondingSig *schnorr.Signature) error {
stakeSpendingTx *wire.MsgTx,
inclusionProof *btcstakingtypes.InclusionProof) error {
signer := bca.babylonClient.MustGetAddr()

stakeSpendingBytes, err := bbn.SerializeBTCTx(stakeSpendingTx)
if err != nil {
return err
}

msg := btcstakingtypes.MsgBTCUndelegate{
Signer: signer,
StakingTxHash: stakingTxHash.String(),
UnbondingTxSig: bbn.NewBIP340SignatureFromBTCSig(stakerUnbondingSig),
Signer: signer,
StakingTxHash: stakingTxHash.String(),
StakeSpendingTx: stakeSpendingBytes,
StakeSpendingTxInclusionProof: inclusionProof,
}

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

if err != nil && resp != nil {
return fmt.Errorf("msg MsgBTCUndelegate failed exeuction with code %d and error %w", resp.Code, err)
}
Expand All @@ -141,7 +147,7 @@ func (bca *BabylonClientAdapter) BtcClientTipHeight() (uint32, error) {
return 0, fmt.Errorf("failed to retrieve btc light client tip from babyln: %w", err)
}

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

// ActivateDelegation provides inclusion proof to activate delegation
Expand Down
10 changes: 5 additions & 5 deletions btcstaking-tracker/stakingeventwatcher/mock_babylon_client.go

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

79 changes: 75 additions & 4 deletions btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ func (sew *StakingEventWatcher) waitForDelegationToStopBeingActive(
func (sew *StakingEventWatcher) reportUnbondingToBabylon(
ctx context.Context,
stakingTxHash chainhash.Hash,
unbondingSignature *schnorr.Signature,
stakeSpendingTx *wire.MsgTx,
proof *btcstakingtypes.InclusionProof,
) {
_ = retry.Do(func() error {
active, err := sew.babylonNodeAdapter.IsDelegationActive(stakingTxHash)
Expand All @@ -394,7 +395,7 @@ func (sew *StakingEventWatcher) reportUnbondingToBabylon(
return nil
}

if err = sew.babylonNodeAdapter.ReportUnbonding(ctx, stakingTxHash, unbondingSignature); err != nil {
if err = sew.babylonNodeAdapter.ReportUnbonding(ctx, stakingTxHash, stakeSpendingTx, proof); err != nil {
sew.metrics.FailedReportedUnbondingTransactions.Inc()
return fmt.Errorf("error reporting unbonding tx %s to babylon: %v", stakingTxHash, err)
}
Expand Down Expand Up @@ -426,7 +427,7 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t
return
}

schnorrSignature, err := tryParseStakerSignatureFromSpentTx(spendingTx, td)
_, err := tryParseStakerSignatureFromSpentTx(spendingTx, td)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function actually checks whether stake spending transaction is unbonding transaction registered to Babylon.

So imo we can leave it but we should send inclusion proof through sew.reportUnbondingToBabylon(quitCtx, delegationId, spendingTx, proof) even if it returns error (as now we have possibility to report even invalid unbondings to Babylon)

So the only difference between error case and non err case is metrics which we are incrementing.

delegationId := td.StakingTx.TxHash()
spendingTxHash := spendingTx.TxHash()

Expand All @@ -437,13 +438,24 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t
// As we only care about unbonding transactions, we do not need to take additional actions.
// We start polling babylon for delegation to stop being active, and then delete it from unbondingTracker.
sew.logger.Debugf("Spending tx %s for staking tx %s is not unbonding tx. Info: %v", spendingTxHash, delegationId, err)
proof, err := sew.waitForStakeSpendInclusionProof(quitCtx, spendingTx)
if err != nil {
sew.logger.Errorf("unbonding tx %s for staking tx %s proof not built", spendingTxHash, delegationId)
return
}
sew.reportUnbondingToBabylon(quitCtx, delegationId, spendingTx, proof)
sew.waitForDelegationToStopBeingActive(quitCtx, delegationId)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably do not need it here any more, as after succesful unbonding report delegation won't be active (and reportUnbondingToBabylon has acitvity check inside)

} else {
sew.metrics.DetectedUnbondingTransactionsCounter.Inc()
// We found valid unbonding tx. We need to try to report it to babylon.
// We stop reporting if delegation is no longer active or we succeed.
proof, err := sew.waitForStakeSpendInclusionProof(quitCtx, spendingTx)
if err != nil {
sew.logger.Errorf("unbonding tx %s for staking tx %s proof not built", spendingTxHash, delegationId)
return
}
sew.logger.Debugf("found unbonding tx %s for staking tx %s", spendingTxHash, delegationId)
sew.reportUnbondingToBabylon(quitCtx, delegationId, schnorrSignature)
sew.reportUnbondingToBabylon(quitCtx, delegationId, spendingTx, proof)
sew.logger.Debugf("unbonding tx %s for staking tx %s reported to babylon", spendingTxHash, delegationId)
}

Expand All @@ -454,6 +466,65 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t
)
}

func (sew *StakingEventWatcher) buildSpendingTxProof(spendingTx *wire.MsgTx) (*btcstakingtypes.InclusionProof, error) {
txHash := spendingTx.TxHash()
if len(spendingTx.TxOut) == 0 {
panic(fmt.Errorf("stake spending tx has no outputs %s", spendingTx.TxHash().String())) // this is a software error
}
details, status, err := sew.btcClient.TxDetails(&txHash, spendingTx.TxOut[0].PkScript)
if err != nil {
return nil, err
}

if status != btcclient.TxInChain {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we need this check i.e whether <-spendEvent.Spend: return when tx is in the mempool or in chain. (eitherway better to have it just in case)

return nil, nil
}

btcTxs := types.GetWrappedTxs(details.Block)
ib := types.NewIndexedBlock(int32(details.BlockHeight), &details.Block.Header, btcTxs)

proof, err := ib.GenSPVProof(int(details.TxIndex))
if err != nil {
return nil, err
}

return btcstakingtypes.NewInclusionProofFromSpvProof(proof), nil
}

// waitForStakeSpendInclusionProof polls btc until stake spend tx has inclusion proof built
func (sew *StakingEventWatcher) waitForStakeSpendInclusionProof(
ctx context.Context,
spendingTx *wire.MsgTx,
) (*btcstakingtypes.InclusionProof, error) {
var (
proof *btcstakingtypes.InclusionProof
err error
)
_ = retry.Do(func() error {
proof, err = sew.buildSpendingTxProof(spendingTx)
if err != nil {
return err
}

if proof == nil {
return fmt.Errorf("proof not yet built")
}

return nil
},
retry.Context(ctx),
retryForever,
fixedDelyTypeWithJitter,
retry.MaxDelay(sew.cfg.CheckDelegationActiveInterval),
retry.MaxJitter(sew.cfg.RetryJitter),
retry.OnRetry(func(n uint, err error) {
sew.logger.Debugf("retrying checking if stake spending tx is in chain %s. Attempt: %d. Err: %v", spendingTx.TxHash(), n, err)
}),
)

return proof, nil
}

func (sew *StakingEventWatcher) handleUnbondedDelegations() {
defer sew.wg.Done()
for {
Expand Down
2 changes: 1 addition & 1 deletion e2etest/atomicslasher_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestAtomicSlasher_Unbonding(t *testing.T) {
/*
the victim BTC delegation unbonds
*/
tm.Undelegate(t, stakingSlashingInfo, unbondingSlashingInfo, btcDelSK)
tm.Undelegate(t, stakingSlashingInfo, unbondingSlashingInfo, btcDelSK, func() { tm.CatchUpBTCLightClient(t) })

/*
finality provider builds unbonding slashing tx witness and sends it to Bitcoin
Expand Down
4 changes: 2 additions & 2 deletions e2etest/container/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const (

// NewImageConfig returns ImageConfig needed for running e2e test.
func NewImageConfig(t *testing.T) ImageConfig {
babylondVersion, err := testutil.GetBabylonVersion()
babylonVersion, err := testutil.GetBabylonVersion()
require.NoError(t, err)

return ImageConfig{
BitcoindRepository: dockerBitcoindRepository,
BitcoindVersion: dockerBitcoindVersionTag,
BabylonRepository: dockerBabylondRepository,
BabylonVersion: babylondVersion,
BabylonVersion: babylonVersion,
}
}
2 changes: 1 addition & 1 deletion e2etest/slasher_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestSlasher_SlashingUnbonding(t *testing.T) {
stakingSlashingInfo1, unbondingSlashingInfo1, stakerPrivKey1 := tm.CreateBTCDelegation(t, fpSK)

// undelegate
unbondingSlashingInfo, _ := tm.Undelegate(t, stakingSlashingInfo1, unbondingSlashingInfo1, stakerPrivKey1)
unbondingSlashingInfo, _ := tm.Undelegate(t, stakingSlashingInfo1, unbondingSlashingInfo1, stakerPrivKey1, func() { tm.CatchUpBTCLightClient(t) })

// commit public randomness, vote and equivocate
tm.VoteAndEquivocate(t, fpSK)
Expand Down
28 changes: 20 additions & 8 deletions e2etest/test_manager_btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ func (tm *TestManager) Undelegate(
t *testing.T,
stakingSlashingInfo *datagen.TestStakingSlashingInfo,
unbondingSlashingInfo *datagen.TestUnbondingSlashingInfo,
delSK *btcec.PrivateKey) (*datagen.TestUnbondingSlashingInfo, *schnorr.Signature) {
delSK *btcec.PrivateKey,
catchUpLightClientFunc func()) (*datagen.TestUnbondingSlashingInfo, *schnorr.Signature) {
signerAddr := tm.BabylonClient.MustGetAddr()

// TODO: This generates unbonding tx signature, move it to undelegate
Expand All @@ -491,14 +492,9 @@ func (tm *TestManager) Undelegate(
)
require.NoError(t, err)

msgUndel := &bstypes.MsgBTCUndelegate{
Signer: signerAddr,
StakingTxHash: stakingSlashingInfo.StakingTx.TxHash().String(),
UnbondingTxSig: bbn.NewBIP340SignatureFromBTCSig(unbondingTxSchnorrSig),
}
_, err = tm.BabylonClient.ReliablySendMsg(context.Background(), msgUndel, nil, nil)
var unbondingTxBuf bytes.Buffer
err = unbondingSlashingInfo.UnbondingTx.Serialize(&unbondingTxBuf)
require.NoError(t, err)
t.Logf("submitted MsgBTCUndelegate")

resp, err := tm.BabylonClient.BTCDelegation(stakingSlashingInfo.StakingTx.TxHash().String())
require.NoError(t, err)
Expand Down Expand Up @@ -527,6 +523,22 @@ func (tm *TestManager) Undelegate(
mBlock := tm.mineBlock(t)
require.Equal(t, 2, len(mBlock.Transactions))

catchUpLightClientFunc()

unbondingTxInfo := getTxInfo(t, mBlock)
msgUndel := &bstypes.MsgBTCUndelegate{
Signer: signerAddr,
StakingTxHash: stakingSlashingInfo.StakingTx.TxHash().String(),
StakeSpendingTx: unbondingTxBuf.Bytes(),
StakeSpendingTxInclusionProof: &bstypes.InclusionProof{
Key: unbondingTxInfo.Key,
Proof: unbondingTxInfo.Proof,
},
}
_, err = tm.BabylonClient.ReliablySendMsg(context.Background(), msgUndel, nil, nil)
require.NoError(t, err)
t.Logf("submitted MsgBTCUndelegate")

// wait until unbonding tx is on Bitcoin
require.Eventually(t, func() bool {
resp, err := tm.BTCClient.GetRawTransactionVerbose(unbondingTxHash)
Expand Down
Loading
Loading