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 7 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
20 changes: 14 additions & 6 deletions btcstaking-tracker/stakingeventwatcher/expected_babylon_client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stakingeventwatcher

import (
"bytes"
"context"
"fmt"

Expand All @@ -10,7 +11,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 +28,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,13 +112,20 @@ 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()

var stakeSpendingTxBuf bytes.Buffer
if err := stakeSpendingTx.Serialize(&stakeSpendingTxBuf); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

on bbn (bbn "github.com/babylonlabs-io/babylon/types")we have this helper - bbn.SerializeBTCTx(

return err
}

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

resp, err := bca.babylonClient.ReliablySendMsg(ctx, &msg, []*errors.Error{}, []*errors.Error{})
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.

74 changes: 70 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 @@ -442,8 +443,14 @@ func (sew *StakingEventWatcher) watchForSpend(spendEvent *notifier.SpendEvent, t
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 {
// todo(lazar): log and report a metric, push to removalChan
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 +461,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 {
return nil, fmt.Errorf("stake spending tx has no outputs")
Copy link
Collaborator

Choose a reason for hiding this comment

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

tbh this should not happen as we are handling tx after we detected it was included in mempool/chain. Maybe panic here or have some error which we do not retry ? (imo this means our assumptions are broken so we should do something drastic)

}
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
2 changes: 2 additions & 0 deletions e2etest/unbondingwatcher_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func TestUnbondingWatcher(t *testing.T) {
minedBlock := tm.mineBlock(t)
require.Equal(t, 2, len(minedBlock.Transactions))

tm.CatchUpBTCLightClient(t)

require.Eventually(t, func() bool {
resp, err := tm.BabylonClient.BTCDelegation(stakingSlashingInfo.StakingTx.TxHash().String())
require.NoError(t, err)
Expand Down
Loading
Loading