From 2586ba2d2aee39991de3775bc22f44bf56ef4854 Mon Sep 17 00:00:00 2001 From: Cirrus Gai Date: Tue, 9 Jan 2024 09:12:17 +0800 Subject: [PATCH] chore: Bump babylon with checking of slashing time lock (#6) --- clientcontroller/babylon.go | 2 +- covenant/covenant.go | 8 ++++++-- covenant/covenant_test.go | 11 +++++------ go.mod | 4 ++-- go.sum | 8 ++++---- itest/test_manager.go | 30 +++++++++++++++++------------- testutil/datagen.go | 1 + tools/go.mod | 4 ++-- tools/go.sum | 4 ++-- types/delegation.go | 8 +++++--- 10 files changed, 45 insertions(+), 35 deletions(-) diff --git a/clientcontroller/babylon.go b/clientcontroller/babylon.go index 4a9294e..eee056f 100644 --- a/clientcontroller/babylon.go +++ b/clientcontroller/babylon.go @@ -324,6 +324,7 @@ func ConvertDelegationType(del *btcstakingtypes.BTCDelegation) *types.Delegation StakingTxHex: stakingTxHex, SlashingTxHex: slashingTxHex, CovenantSigs: covenantSigs, + UnbondingTime: del.UnbondingTime, BtcUndelegation: undelegation, } } @@ -373,7 +374,6 @@ func ConvertUndelegationType(undel *btcstakingtypes.BTCUndelegation) *types.Unde SlashingTxHex: slashingTxHex, CovenantSlashingSigs: covenantSlashingSigs, CovenantUnbondingSigs: covenantUnbondingSigs, - UnbondingTime: undel.UnbondingTime, DelegatorUnbondingSig: undel.DelegatorUnbondingSig, } } diff --git a/covenant/covenant.go b/covenant/covenant.go index 8f8ac9d..149854f 100644 --- a/covenant/covenant.go +++ b/covenant/covenant.go @@ -128,7 +128,7 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty // which is larger value from: // - MinUnbondingTime // - CheckpointFinalizationTimeout - unbondingTime := btcDel.BtcUndelegation.UnbondingTime + unbondingTime := btcDel.UnbondingTime minUnbondingTime := ce.params.MinUnbondingTime if unbondingTime <= minUnbondingTime { return nil, fmt.Errorf("unbonding time %d must be larger than %d", @@ -158,6 +158,8 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty int64(ce.params.MinSlashingTxFeeSat), ce.params.SlashingRate, ce.params.SlashingAddress, + btcDel.BtcPk, + uint16(unbondingTime), &ce.config.BTCNetParams, ); err != nil { return nil, fmt.Errorf("invalid txs in the delegation: %w", err) @@ -179,7 +181,7 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty btcDel.FpBtcPks, ce.params.CovenantPks, ce.params.CovenantQuorum, - uint16(btcDel.BtcUndelegation.UnbondingTime), + uint16(unbondingTime), btcutil.Amount(unbondingMsgTx.TxOut[0].Value), &ce.config.BTCNetParams, ) @@ -194,6 +196,8 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty int64(ce.params.MinSlashingTxFeeSat), ce.params.SlashingRate, ce.params.SlashingAddress, + btcDel.BtcPk, + uint16(unbondingTime), &ce.config.BTCNetParams, ) if err != nil { diff --git a/covenant/covenant_test.go b/covenant/covenant_test.go index 3713102..f1a58f6 100644 --- a/covenant/covenant_test.go +++ b/covenant/covenant_test.go @@ -55,12 +55,11 @@ func FuzzAddCovenantSig(f *testing.F) { require.NoError(t, err) // generate BTC delegation - changeAddr, err := datagen.GenRandomBTCAddress(r, &chaincfg.SimNetParams) - require.NoError(t, err) delSK, delPK, err := datagen.GenRandomBTCKeyPair(r) require.NoError(t, err) stakingTimeBlocks := uint16(5) stakingValue := int64(2 * 10e8) + unbondingTime := uint16(params.MinimumUnbondingTime()) + 1 fpNum := datagen.RandomInt(r, 5) + 1 fpPks := testutil.GenBtcPublicKeys(r, t, int(fpNum)) testInfo := datagen.GenBTCStakingSlashingInfo( @@ -74,8 +73,8 @@ func FuzzAddCovenantSig(f *testing.F) { stakingTimeBlocks, stakingValue, params.SlashingAddress.String(), - changeAddr.String(), params.SlashingRate, + unbondingTime, ) stakingTxBytes, err := bbntypes.SerializeBTCTx(testInfo.StakingTx) require.NoError(t, err) @@ -86,6 +85,7 @@ func FuzzAddCovenantSig(f *testing.F) { StartHeight: startHeight, // not relevant here EndHeight: startHeight + uint64(stakingTimeBlocks), TotalSat: uint64(stakingValue), + UnbondingTime: uint32(unbondingTime), StakingTxHex: hex.EncodeToString(stakingTxBytes), StakingOutputIdx: 0, SlashingTxHex: testInfo.SlashingTx.ToHexStr(), @@ -108,7 +108,6 @@ func FuzzAddCovenantSig(f *testing.F) { } // generate undelegation - unbondingTime := uint16(params.FinalizationTimeoutBlocks) + 1 unbondingValue := int64(btcDel.TotalSat) - 1000 stakingTxHash := testInfo.StakingTx.TxHash() @@ -123,8 +122,9 @@ func FuzzAddCovenantSig(f *testing.F) { wire.NewOutPoint(&stakingTxHash, 0), unbondingTime, unbondingValue, - params.SlashingAddress.String(), changeAddr.String(), + params.SlashingAddress.String(), params.SlashingRate, + unbondingTime, ) require.NoError(t, err) // random signer @@ -137,7 +137,6 @@ func FuzzAddCovenantSig(f *testing.F) { require.NoError(t, err) undel := &types.Undelegation{ UnbondingTxHex: hex.EncodeToString(serializedUnbondingTx), - UnbondingTime: uint32(unbondingTime), SlashingTxHex: testUnbondingInfo.SlashingTx.ToHexStr(), } btcDel.BtcUndelegation = undel diff --git a/go.mod b/go.mod index f8ec3d5..f1147b5 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( cosmossdk.io/math v1.2.0 github.com/avast/retry-go/v4 v4.5.1 github.com/babylonchain/babylon v0.7.2 - github.com/babylonchain/finality-provider v0.0.0-20231222112306-5891f21cd080 + github.com/babylonchain/finality-provider v0.0.0-20240105110631-3971eeaefe64 github.com/babylonchain/rpc-client v0.7.0 github.com/btcsuite/btcd v0.23.5-0.20230711222809-7faa9b266231 github.com/btcsuite/btcd/btcec/v2 v2.3.2 @@ -239,7 +239,7 @@ require ( ) replace ( - github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866 + github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598 github.com/babylonchain/rpc-client => github.com/babylonchain/rpc-client-private v0.7.0-rc0.0.20231214053715-9de58555773a github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 diff --git a/go.sum b/go.sum index a2a09c1..0668194 100644 --- a/go.sum +++ b/go.sum @@ -279,10 +279,10 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX github.com/aws/aws-sdk-go v1.44.312 h1:llrElfzeqG/YOLFFKjg1xNpZCFJ2xraIi3PqSuP+95k= github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= -github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866 h1:PhcQKkrFX2F+NJHgzQUquLAB3hjvZ5ojEnilf/lZycI= -github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss= -github.com/babylonchain/finality-provider v0.0.0-20231222112306-5891f21cd080 h1:p04I5UnuiaUALu+xioFxIR+VwBH1olAPSfS9aKY9yI4= -github.com/babylonchain/finality-provider v0.0.0-20231222112306-5891f21cd080/go.mod h1:81SxlAFLZWfCfmi+vqISIgSnRy5ehWyPBw8StWbrTB4= +github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598 h1:ysEA08WGHlMm2pIoXgTt1O9YAW4QjBQXg3TM0CSn21g= +github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss= +github.com/babylonchain/finality-provider v0.0.0-20240105110631-3971eeaefe64 h1:5KXqgroUxfP7VPOPEhFGb3kra3mWXwX+cG4fYvjesMg= +github.com/babylonchain/finality-provider v0.0.0-20240105110631-3971eeaefe64/go.mod h1:FupoqbNfunaQUBgknLIyKvlM1+VUXBgkAwDveIlr7lM= github.com/babylonchain/rpc-client-private v0.7.0-rc0.0.20231214053715-9de58555773a h1:IwntCUOlcsczQfl4P72ckqGf09coMdM42LvAG1aM+mg= github.com/babylonchain/rpc-client-private v0.7.0-rc0.0.20231214053715-9de58555773a/go.mod h1:dnUVCa5yHq2giiSpBpXx7W3CbJuuMabvQi3hG81AAtM= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= diff --git a/itest/test_manager.go b/itest/test_manager.go index 7dc60c6..88a6514 100644 --- a/itest/test_manager.go +++ b/itest/test_manager.go @@ -64,6 +64,7 @@ type TestManager struct { EOTSClient *client.EOTSManagerGRpcClient FPBBNClient *fpcc.BabylonController CovBBNClient *covcc.BabylonController + StakingParams *types.StakingParams baseDir string } @@ -79,7 +80,6 @@ type TestDelegationData struct { FpPks []*btcec.PublicKey SlashingAddr string - ChangeAddr string StakingTime uint16 StakingAmount int64 } @@ -151,9 +151,12 @@ func StartManager(t *testing.T) *TestManager { func (tm *TestManager) WaitForServicesStart(t *testing.T) { // wait for Babylon node starts require.Eventually(t, func() bool { - _, err := tm.FPBBNClient.QueryStakingParams() - - return err == nil + params, err := tm.CovBBNClient.QueryStakingParams() + if err != nil { + return false + } + tm.StakingParams = params + return true }, eventuallyWaitTimeOut, eventuallyPollTime) t.Logf("Babylon node is started") @@ -244,7 +247,11 @@ func (tm *TestManager) WaitForFpRegistered(t *testing.T, bbnPk *secp256k1.PubKey func (tm *TestManager) WaitForFpPubRandCommitted(t *testing.T, fpIns *service.FinalityProviderInstance) { require.Eventually(t, func() bool { - return fpIns.GetLastCommittedHeight() > 0 + lastCommittedHeight, err := fpIns.GetLastCommittedHeight() + if err != nil { + return false + } + return lastCommittedHeight > 0 }, eventuallyWaitTimeOut, eventuallyPollTime) t.Logf("public randomness is successfully committed") @@ -274,7 +281,7 @@ func (tm *TestManager) WaitForFpNActiveDels(t *testing.T, btcPk *bbntypes.BIP340 var dels []*types.Delegation currentBtcTip, err := tm.FPBBNClient.QueryBtcLightClientTip() require.NoError(t, err) - params, err := tm.FPBBNClient.QueryStakingParams() + params, err := tm.CovBBNClient.QueryStakingParams() require.NoError(t, err) require.Eventually(t, func() bool { dels, err = tm.CovBBNClient.QueryFinalityProviderDelegations(btcPk, 1000) @@ -368,9 +375,7 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe delBtcPrivKey, delBtcPubKey, err := datagen.GenRandomBTCKeyPair(r) require.NoError(t, err) - changeAddress, err := datagen.GenRandomBTCAddress(r, btcNetworkParams) - require.NoError(t, err) - + unbondingTime := uint16(tm.StakingParams.MinimumUnbondingTime()) + 1 testStakingInfo := datagen.GenBTCStakingSlashingInfo( r, t, @@ -381,8 +386,9 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe params.CovenantQuorum, stakingTime, stakingAmount, - params.SlashingAddress.String(), changeAddress.String(), + params.SlashingAddress.String(), params.SlashingRate, + unbondingTime, ) // delegator Babylon key pairs @@ -431,7 +437,6 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe ) require.NoError(t, err) - unbondingTime := uint16(params.FinalizationTimeoutBlocks) + 1 unbondingValue := stakingAmount - 1000 stakingTxHash := testStakingInfo.StakingTx.TxHash() @@ -447,8 +452,8 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe unbondingTime, unbondingValue, params.SlashingAddress.String(), - changeAddress.String(), params.SlashingRate, + unbondingTime, ) unbondingTxMsg := testUnbondingInfo.UnbondingTx @@ -498,7 +503,6 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe StakingTxInfo: txInfo, DelegatorSig: delegatorSig, SlashingAddr: params.SlashingAddress.String(), - ChangeAddr: changeAddress.String(), StakingTime: stakingTime, StakingAmount: stakingAmount, } diff --git a/testutil/datagen.go b/testutil/datagen.go index c2f599a..a02be2b 100644 --- a/testutil/datagen.go +++ b/testutil/datagen.go @@ -48,6 +48,7 @@ func GenRandomParams(r *rand.Rand, t *testing.T) *types.StakingParams { return &types.StakingParams{ ComfirmationTimeBlocks: 10, FinalizationTimeoutBlocks: 100, + MinUnbondingTime: 100, MinSlashingTxFeeSat: 1, CovenantPks: covenantPks, SlashingAddress: slashingAddr, diff --git a/tools/go.mod b/tools/go.mod index c726060..e6b0df4 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -1,4 +1,4 @@ -module github.com/babylonchain/finality-provider/tools +module github.com/babylonchain/covenant-emulator/tools go 1.21 @@ -212,7 +212,7 @@ require ( ) replace ( - github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866 + github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598 // Downgraded to stable version see: https://github.com/cosmos/cosmos-sdk/pull/14952 github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 diff --git a/tools/go.sum b/tools/go.sum index 389fcbb..c6135e4 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -272,8 +272,8 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX github.com/aws/aws-sdk-go v1.44.224 h1:09CiaaF35nRmxrzWZ2uRq5v6Ghg/d2RiPjZnSgtt+RQ= github.com/aws/aws-sdk-go v1.44.224/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= -github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866 h1:PhcQKkrFX2F+NJHgzQUquLAB3hjvZ5ojEnilf/lZycI= -github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss= +github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598 h1:ysEA08WGHlMm2pIoXgTt1O9YAW4QjBQXg3TM0CSn21g= +github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= diff --git a/types/delegation.go b/types/delegation.go index c326c1d..bef557d 100644 --- a/types/delegation.go +++ b/types/delegation.go @@ -1,9 +1,10 @@ package types import ( - bbn "github.com/babylonchain/babylon/types" "math" + bbn "github.com/babylonchain/babylon/types" + "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2/schnorr" ) @@ -29,6 +30,9 @@ type Delegation struct { StakingOutputIdx uint32 // The hex string of the slashing tx SlashingTxHex string + // UnbondingTime describes how long the funds will be locked either in unbonding output + // or slashing change output + UnbondingTime uint32 // The signatures on the slashing tx // by the covenants (i.e., SKs corresponding to covenant_pks in params) // It will be a part of the witness for the staking tx output. @@ -57,8 +61,6 @@ func (d *Delegation) GetStakingTime() uint16 { // Undelegation signalizes that the delegation is being undelegated type Undelegation struct { - // How long the funds will be locked in the unbonding output - UnbondingTime uint32 // The hex string of the transaction which will transfer the funds from staking // output to unbonding output. Unbonding output will usually have lower timelock // than staking output.