From 8ea6196530bbf5f61a7932602020dd1a14d67bf3 Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Mon, 23 Dec 2024 09:55:48 +0100 Subject: [PATCH] fix param version translation --- clientcontroller/babylon.go | 1 + clientcontroller/babylon_test.go | 68 ++++++++++++++++++++++++++++++++ covenant/covenant_test.go | 2 + 3 files changed, 71 insertions(+) create mode 100644 clientcontroller/babylon_test.go diff --git a/clientcontroller/babylon.go b/clientcontroller/babylon.go index 19adb0d..2b7efaa 100644 --- a/clientcontroller/babylon.go +++ b/clientcontroller/babylon.go @@ -290,6 +290,7 @@ func DelegationRespToDelegation(del *btcstakingtypes.BTCDelegationResponse) (*ty CovenantSigs: covenantSigs, UnbondingTime: uint16(del.UnbondingTime), BtcUndelegation: undelegation, + ParamsVersion: del.ParamsVersion, }, nil } diff --git a/clientcontroller/babylon_test.go b/clientcontroller/babylon_test.go new file mode 100644 index 0000000..6a3cf8e --- /dev/null +++ b/clientcontroller/babylon_test.go @@ -0,0 +1,68 @@ +package clientcontroller_test + +import ( + "math/rand" + "testing" + + "github.com/babylonlabs-io/babylon/testutil/datagen" + bbntypes "github.com/babylonlabs-io/babylon/types" + "github.com/babylonlabs-io/babylon/x/btcstaking/types" + "github.com/babylonlabs-io/covenant-emulator/clientcontroller" + "github.com/babylonlabs-io/covenant-emulator/testutil" + "github.com/stretchr/testify/require" +) + +func TestBabylonController(t *testing.T) { + + // datagen.GenRandomBTCDelegation() + + require.Equal(t, 1, 1) +} + +func FuzzAddCovenantSig(f *testing.F) { + testutil.AddRandomSeedsToFuzzer(f, 10) + f.Fuzz(func(t *testing.T, seed int64) { + r := rand.New(rand.NewSource(seed)) + + delPub, err := datagen.GenRandomBIP340PubKey(r) + require.NoError(t, err) + + fpPub, err := datagen.GenRandomBIP340PubKey(r) + require.NoError(t, err) + + randSat := datagen.RandomInRange(r, 10000, 10000000) + randVersion := datagen.RandomInRange(r, 1, 10) + stakingTx := datagen.GenRandomHexStr(r, 100) + slashingTx := datagen.GenRandomHexStr(r, 100) + stakingTime := datagen.RandomInRange(r, 100, 1000000) + startHeight := datagen.RandomInRange(r, 100, 1000000) + endHeight := datagen.RandomInRange(r, 100, 1000000) + stakingOutputIdx := datagen.RandomInRange(r, 100, 1000000) + + response := types.BTCDelegationResponse{ + BtcPk: delPub, + FpBtcPkList: []bbntypes.BIP340PubKey{*fpPub}, + TotalSat: uint64(randSat), + ParamsVersion: uint32(randVersion), + StakingTxHex: stakingTx, + SlashingTxHex: slashingTx, + StakingTime: uint32(stakingTime), + StartHeight: uint32(startHeight), + EndHeight: uint32(endHeight), + StakingOutputIdx: uint32(stakingOutputIdx), + } + + del, err := clientcontroller.DelegationRespToDelegation(&response) + require.NoError(t, err) + require.NotNil(t, del) + require.Equal(t, response.ParamsVersion, del.ParamsVersion) + require.Equal(t, response.StakingTime, del.StakingTime) + require.Equal(t, response.StartHeight, del.StartHeight) + require.Equal(t, response.EndHeight, del.EndHeight) + require.Equal(t, response.StakingOutputIdx, del.StakingOutputIdx) + require.Equal(t, response.StakingTxHex, del.StakingTxHex) + require.Equal(t, response.SlashingTxHex, del.SlashingTxHex) + require.Equal(t, response.BtcPk, bbntypes.NewBIP340PubKeyFromBTCPK(del.BtcPk)) + require.Equal(t, response.FpBtcPkList, bbntypes.NewBIP340PKsFromBTCPKs(del.FpBtcPks)) + }) +} diff --git a/covenant/covenant_test.go b/covenant/covenant_test.go index 62ad46d..6785288 100644 --- a/covenant/covenant_test.go +++ b/covenant/covenant_test.go @@ -91,6 +91,7 @@ func FuzzAddCovenantSig(f *testing.F) { startHeight := uint32(datagen.RandomInt(r, 1000) + 100) stakingOutputIdx, err := bbntypes.GetOutputIdxInBTCTx(testInfo.StakingTx, testInfo.StakingInfo.StakingOutput) require.NoError(t, err) + randParamsVersion := datagen.RandomInRange(r, 1, 10) btcDel := &types.Delegation{ BtcPk: delPK, FpBtcPks: fpPks, @@ -102,6 +103,7 @@ func FuzzAddCovenantSig(f *testing.F) { StakingTxHex: hex.EncodeToString(stakingTxBytes), StakingOutputIdx: stakingOutputIdx, SlashingTxHex: testInfo.SlashingTx.ToHexStr(), + ParamsVersion: uint32(randParamsVersion), } btcDels = append(btcDels, btcDel) // generate covenant staking sigs