From b82ba45bffcfa11e315339c4c030778270204739 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Mon, 14 Oct 2024 20:38:30 +1100 Subject: [PATCH 1/3] init --- x/btcstaking/types/btc_delegation.go | 2 ++ x/btcstaking/types/btc_delegation_test.go | 3 +-- x/btcstaking/types/btc_slashing_tx.go | 6 ++++++ x/btcstaking/types/btc_slashing_tx_test.go | 7 +++++-- x/btcstaking/types/btc_undelegation_test.go | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/x/btcstaking/types/btc_delegation.go b/x/btcstaking/types/btc_delegation.go index 521cdeb77..3f2a393db 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 06ec13301..5018a4fde 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..ce429fcb7 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,17 @@ 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 + } } /* 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 2388ea12e..5013392d4 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) From b55faa48fca363adca49c96f085b94a5239d7220 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Mon, 14 Oct 2024 20:40:22 +1100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d53271761..560382838 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### 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 ### Improvements From 343eb846a0b21d789bac8ef460fd7da0835767e3 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Mon, 14 Oct 2024 22:11:09 +1100 Subject: [PATCH 3/3] fix --- x/btcstaking/types/btc_slashing_tx.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/btcstaking/types/btc_slashing_tx.go b/x/btcstaking/types/btc_slashing_tx.go index ce429fcb7..7258fdea6 100644 --- a/x/btcstaking/types/btc_slashing_tx.go +++ b/x/btcstaking/types/btc_slashing_tx.go @@ -301,6 +301,10 @@ func (tx *BTCSlashingTx) BuildSlashingTxWithWitness( 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") + } /* construct finality providers' part of witness, i.e.,