From 26a9dcbbcb57583d3013453ef198c83d473a0025 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Mon, 14 Oct 2024 22:50:10 +1100 Subject: [PATCH] fix: fixing witness construction of slashing tx (#193) --- CHANGELOG.md | 24 +++++++++++++++++++++ x/btcstaking/types/btc_delegation.go | 2 ++ x/btcstaking/types/btc_delegation_test.go | 3 +-- x/btcstaking/types/btc_slashing_tx.go | 10 +++++++++ x/btcstaking/types/btc_slashing_tx_test.go | 7 ++++-- x/btcstaking/types/btc_undelegation_test.go | 2 +- 6 files changed, 43 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31d5725a5..2a4ad8e27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,30 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## Unreleased +### State Machine Breaking + +* [#181](https://github.com/babylonlabs-io/babylon/pull/181) Modify BTC heights + and depths to be of uint32 type instead of uint64. + +### Bug fixes + +* [#193](https://github.com/babylonlabs-io/babylon/pull/193) Fix witness construction of slashing tx +* [#154](https://github.com/babylonlabs-io/babylon/pull/154) Fix "edit-finality-provider" cmd argument index +* [#186](https://github.com/babylonlabs-io/babylon/pull/186) Do not panic on `nil` +Proof when handling finality votes + +### Improvements + +* [#148](https://github.com/babylonlabs-io/babylon/pull/148) Add block results query + +### Misc Improvements + +* [#170](https://github.com/babylonlabs-io/babylon/pull/170) Go releaser setup +* [#168](https://github.com/babylonlabs-io/babylon/pull/168) Remove devdoc from + Makefile and remove unnecessary gin replace. +* [#184](https://github.com/babylonlabs-io/babylon/pull/184) Remove localnet + setup as it provides no additional testing value. + ## v0.12.1 ### Bug fixes diff --git a/x/btcstaking/types/btc_delegation.go b/x/btcstaking/types/btc_delegation.go index e5726090d..0b17ef416 100644 --- a/x/btcstaking/types/btc_delegation.go +++ b/x/btcstaking/types/btc_delegation.go @@ -382,6 +382,7 @@ func (d *BTCDelegation) BuildSlashingTxWithWitness(bsParams *Params, btcNet *cha d.StakingOutputIdx, d.DelegatorSig, covAdaptorSigs, + bsParams.CovenantQuorum, slashingSpendInfo, ) if err != nil { @@ -433,6 +434,7 @@ func (d *BTCDelegation) BuildUnbondingSlashingTxWithWitness(bsParams *Params, bt 0, d.BtcUndelegation.DelegatorSlashingSig, covAdaptorSigs, + bsParams.CovenantQuorum, slashingSpendInfo, ) if err != nil { diff --git a/x/btcstaking/types/btc_delegation_test.go b/x/btcstaking/types/btc_delegation_test.go index aaaf9e9f7..82b531875 100644 --- a/x/btcstaking/types/btc_delegation_test.go +++ b/x/btcstaking/types/btc_delegation_test.go @@ -109,8 +109,7 @@ func FuzzBTCDelegation_SlashingTx(f *testing.F) { slashingChangeLockTime := unbondingTime // only the quorum of signers provided the signatures - covenantSigners := covenantSKs[:covenantQuorum] - + covenantSigners := covenantSKs // construct the BTC delegation with everything btcDel, err := datagen.GenRandomBTCDelegation( r, diff --git a/x/btcstaking/types/btc_slashing_tx.go b/x/btcstaking/types/btc_slashing_tx.go index 613caec3d..7258fdea6 100644 --- a/x/btcstaking/types/btc_slashing_tx.go +++ b/x/btcstaking/types/btc_slashing_tx.go @@ -274,6 +274,7 @@ func (tx *BTCSlashingTx) BuildSlashingTxWithWitness( outputIdx uint32, delegatorSig *bbn.BIP340Signature, covenantSigs []*asig.AdaptorSignature, + covenantQuorum uint32, slashingPathSpendInfo *btcstaking.SpendInfo, ) (*wire.MsgTx, error) { /* @@ -288,12 +289,21 @@ func (tx *BTCSlashingTx) BuildSlashingTxWithWitness( } // decrypt each covenant adaptor signature to Schnorr signature covSigs := make([]*schnorr.Signature, len(covenantSigs)) + numSigs := uint32(0) for i, covenantSig := range covenantSigs { if covenantSig != nil { covSigs[i] = covenantSig.Decrypt(decKey) + numSigs++ } else { covSigs[i] = nil } + if numSigs == covenantQuorum { + break + } + } + // ensure the number of covenant signatures is at least the quorum number + if numSigs < covenantQuorum { + return nil, fmt.Errorf("not enough covenant signatures to reach quorum") } /* diff --git a/x/btcstaking/types/btc_slashing_tx_test.go b/x/btcstaking/types/btc_slashing_tx_test.go index 10a77da0e..9cd253555 100644 --- a/x/btcstaking/types/btc_slashing_tx_test.go +++ b/x/btcstaking/types/btc_slashing_tx_test.go @@ -194,7 +194,10 @@ func FuzzSlashingTxWithWitness(f *testing.F) { delSig, err := slashingTx.Sign(stakingMsgTx, 0, slashingPkScriptPath, delSK) require.NoError(t, err) - covenantSigners := covenantSKs[:covenantQuorum] + // ensure that event if all covenant members provide covenant signatures, + // BuildSlashingTxWithWitness will only take a quorum number of signatures + // to construct the witness + covenantSigners := covenantSKs // get covenant Schnorr signatures covenantSigs, err := datagen.GenCovenantAdaptorSigs( covenantSigners, @@ -235,7 +238,7 @@ func FuzzSlashingTxWithWitness(f *testing.F) { } // create slashing tx with witness - slashingMsgTxWithWitness, err := slashingTx.BuildSlashingTxWithWitness(fpSK, fpBTCPKs, stakingMsgTx, 0, delSig, covSigsForFP, slashingSpendInfo) + slashingMsgTxWithWitness, err := slashingTx.BuildSlashingTxWithWitness(fpSK, fpBTCPKs, stakingMsgTx, 0, delSig, covSigsForFP, covenantQuorum, slashingSpendInfo) require.NoError(t, err) // verify slashing tx with witness diff --git a/x/btcstaking/types/btc_undelegation_test.go b/x/btcstaking/types/btc_undelegation_test.go index 59694c426..94fbbe688 100644 --- a/x/btcstaking/types/btc_undelegation_test.go +++ b/x/btcstaking/types/btc_undelegation_test.go @@ -47,7 +47,7 @@ func FuzzBTCUndelegation_SlashingTx(f *testing.F) { CovenantPks: bbn.NewBIP340PKsFromBTCPKs(covenantPKs), CovenantQuorum: covenantQuorum, } - covenantSigners := covenantSKs[:covenantQuorum] + covenantSigners := covenantSKs stakingTimeBlocks := uint16(5) stakingValue := int64(2 * 10e8)