From daf6de574a326a61c5bc27649242bddf723e6ff6 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Thu, 19 Dec 2024 18:54:18 +0530 Subject: [PATCH] pr comment: remove unused code --- Makefile | 3 +- e2etest/bitcoind_node_setup.go | 6 - e2etest/e2e_test.go | 2 +- e2etest/test_manager_btcstaking.go | 322 ----------------------------- 4 files changed, 3 insertions(+), 330 deletions(-) diff --git a/Makefile b/Makefile index d2b8f15..9a9a3e0 100644 --- a/Makefile +++ b/Makefile @@ -53,4 +53,5 @@ test: test-e2e: ./bin/local-startup.sh; - go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e + go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e; + docker compose down diff --git a/e2etest/bitcoind_node_setup.go b/e2etest/bitcoind_node_setup.go index b6b6380..b2cb88f 100644 --- a/e2etest/bitcoind_node_setup.go +++ b/e2etest/bitcoind_node_setup.go @@ -105,12 +105,6 @@ func (h *BitcoindTestHandler) CreateWallet(walletName string, passphrase string) return &response } -// InvalidateBlock invalidates blocks starting from specified block hash -func (h *BitcoindTestHandler) InvalidateBlock(blockHash string) { - _, _, err := h.m.ExecBitcoindCliCmd(h.t, []string{"invalidateblock", blockHash}) - require.NoError(h.t, err) -} - // ImportDescriptors imports a given Bitcoin address descriptor into the Bitcoind func (h *BitcoindTestHandler) ImportDescriptors(descriptor string) { _, _, err := h.m.ExecBitcoindCliCmd(h.t, []string{"importdescriptors", descriptor}) diff --git a/e2etest/e2e_test.go b/e2etest/e2e_test.go index 7bacba1..e541f87 100644 --- a/e2etest/e2e_test.go +++ b/e2etest/e2e_test.go @@ -36,7 +36,7 @@ func TestQueueConsumer(t *testing.T) { require.NoError(t, err) }() - n := 1 + n := rand.Intn(10) + 1 r := rand.New(rand.NewSource(time.Now().UnixNano())) stakingEventList := make([]*queuecli.StakingEvent, 0) diff --git a/e2etest/test_manager_btcstaking.go b/e2etest/test_manager_btcstaking.go index 98d8224..cb24039 100644 --- a/e2etest/test_manager_btcstaking.go +++ b/e2etest/test_manager_btcstaking.go @@ -3,36 +3,26 @@ package e2etest import ( "bytes" "context" - "encoding/hex" "fmt" "math/rand" "testing" "time" sdkmath "cosmossdk.io/math" - "github.com/avast/retry-go/v4" "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" "github.com/babylonlabs-io/babylon/btcstaking" - txformat "github.com/babylonlabs-io/babylon/btctxformatter" - "github.com/babylonlabs-io/babylon/crypto/eots" asig "github.com/babylonlabs-io/babylon/crypto/schnorr-adaptor-signature" "github.com/babylonlabs-io/babylon/testutil/datagen" bbn "github.com/babylonlabs-io/babylon/types" btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types" - btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types" bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" - ckpttypes "github.com/babylonlabs-io/babylon/x/checkpointing/types" - ftypes "github.com/babylonlabs-io/babylon/x/finality/types" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" sdk "github.com/cosmos/cosmos-sdk/types" - sdkquery "github.com/cosmos/cosmos-sdk/types/query" - sdkquerytypes "github.com/cosmos/cosmos-sdk/types/query" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/cosmos/relayer/v2/relayer/provider" "github.com/stretchr/testify/require" ) @@ -538,92 +528,6 @@ func (tm *TestManager) Undelegate( return unbondingSlashingInfo, unbondingTxSchnorrSig } -func (tm *TestManager) VoteAndEquivocate(t *testing.T, fpSK *btcec.PrivateKey) { - signerAddr := tm.BabylonClient.MustGetAddr() - - // get the finality provider - fpBTCPK := bbn.NewBIP340PubKeyFromBTCPK(fpSK.PubKey()) - fpResp, err := tm.BabylonClient.FinalityProvider(fpBTCPK.MarshalHex()) - require.NoError(t, err) - btcFp := fpResp.FinalityProvider - - _, err = tm.BabylonClient.ActivatedHeight() - require.Error(t, err) - - activatedHeight := uint64(1) - commitStartHeight := activatedHeight - - /* - commit a number of public randomness since activatedHeight - */ - srList, msgCommitPubRandList, err := datagen.GenRandomMsgCommitPubRandList(r, fpSK, activatedHeight, 100) - require.NoError(t, err) - msgCommitPubRandList.Signer = signerAddr - _, err = tm.BabylonClient.ReliablySendMsg(context.Background(), msgCommitPubRandList, nil, nil) - require.NoError(t, err) - t.Logf("committed public randomness") - - tm.mineBlock(t) - - tm.waitForFpPubRandTimestamped(t, fpSK.PubKey()) - - require.Eventually(t, func() bool { - acr, err := tm.BabylonClient.ActivatedHeight() - if err != nil { - return false - } - activatedHeight = acr.Height - return activatedHeight > 0 - }, eventuallyWaitTimeOut, eventuallyPollTime) - - /* - submit finality signature - */ - // get block to vote - blockToVote, err := tm.BabylonClient.GetBlock(int64(activatedHeight)) - require.NoError(t, err) - msgToSign := append(sdk.Uint64ToBigEndian(activatedHeight), blockToVote.Block.AppHash...) - // generate EOTS signature - idx := activatedHeight - commitStartHeight - sig, err := eots.Sign(fpSK, srList.SRList[idx], msgToSign) - require.NoError(t, err) - eotsSig := bbn.NewSchnorrEOTSSigFromModNScalar(sig) - // submit finality signature - msgAddFinalitySig := &ftypes.MsgAddFinalitySig{ - Signer: signerAddr, - FpBtcPk: btcFp.BtcPk, - BlockHeight: activatedHeight, - PubRand: &srList.PRList[idx], - Proof: srList.ProofList[idx].ToProto(), - BlockAppHash: blockToVote.Block.AppHash, - FinalitySig: eotsSig, - } - _, err = tm.BabylonClient.ReliablySendMsg(context.Background(), msgAddFinalitySig, nil, nil) - require.NoError(t, err) - t.Logf("submitted finality signature") - - /* - equivocate - */ - invalidAppHash := datagen.GenRandomByteArray(r, 32) - invalidMsgToSign := append(sdk.Uint64ToBigEndian(activatedHeight), invalidAppHash...) - invalidSig, err := eots.Sign(fpSK, srList.SRList[idx], invalidMsgToSign) - require.NoError(t, err) - invalidEotsSig := bbn.NewSchnorrEOTSSigFromModNScalar(invalidSig) - invalidMsgAddFinalitySig := &ftypes.MsgAddFinalitySig{ - Signer: signerAddr, - FpBtcPk: btcFp.BtcPk, - BlockHeight: activatedHeight, - PubRand: &srList.PRList[idx], - Proof: srList.ProofList[idx].ToProto(), - BlockAppHash: invalidAppHash, - FinalitySig: invalidEotsSig, - } - _, err = tm.BabylonClient.ReliablySendMsg(context.Background(), invalidMsgAddFinalitySig, nil, nil) - require.NoError(t, err) - t.Logf("submitted equivocating finality signature") -} - func getTxInfo(t *testing.T, block *wire.MsgBlock) *btcctypes.TransactionInfo { mHeaderBytes := bbn.NewBTCHeaderBytesFromBlockHeader(&block.Header) var txBytes [][]byte @@ -659,232 +563,6 @@ func outIdx(tx *wire.MsgTx, candOut *wire.TxOut) (uint32, error) { return 0, fmt.Errorf("couldn't find output") } -func (tm *TestManager) waitForFpPubRandTimestamped(t *testing.T, fpPk *btcec.PublicKey) { - var lastCommittedHeight uint64 - var err error - - require.Eventually(t, func() bool { - lastCommittedHeight, err = tm.getLastCommittedHeight(fpPk) - if err != nil { - return false - } - return lastCommittedHeight > 0 - }, eventuallyWaitTimeOut, eventuallyPollTime) - - t.Logf("public randomness is successfully committed, last committed height: %d", lastCommittedHeight) - - // wait until the last registered epoch is finalized - currentEpoch, err := tm.BabylonClient.CurrentEpoch() - require.NoError(t, err) - - tm.finalizeUntilEpoch(t, currentEpoch.CurrentEpoch) - - res, err := tm.BabylonClient.LatestEpochFromStatus(ckpttypes.Finalized) - require.NoError(t, err) - t.Logf("last finalized epoch: %d", res.RawCheckpoint.EpochNum) - - t.Logf("public randomness is successfully timestamped, last finalized epoch: %v", currentEpoch) -} - -// queryLastCommittedPublicRand returns the last public randomness commitments -func (tm *TestManager) queryLastCommittedPublicRand(fpPk *btcec.PublicKey, count uint64) (map[uint64]*ftypes.PubRandCommitResponse, error) { - fpBtcPk := bbn.NewBIP340PubKeyFromBTCPK(fpPk) - - pagination := &sdkquery.PageRequest{ - Limit: count, - Reverse: true, - } - - res, err := tm.BabylonClient.QueryClient.ListPubRandCommit(fpBtcPk.MarshalHex(), pagination) - if err != nil { - return nil, fmt.Errorf("failed to query committed public randomness: %w", err) - } - - return res.PubRandCommitMap, nil -} - -func (tm *TestManager) lastCommittedPublicRandWithRetry(btcPk *btcec.PublicKey, count uint64) (map[uint64]*ftypes.PubRandCommitResponse, error) { - var response map[uint64]*ftypes.PubRandCommitResponse - - if err := retry.Do(func() error { - resp, err := tm.queryLastCommittedPublicRand(btcPk, count) - if err != nil { - return err - } - response = resp - return nil - }, - retry.Attempts(5), - retry.Delay(1*time.Second), - retry.LastErrorOnly(true)); err != nil { - return nil, err - } - - return response, nil -} - -func (tm *TestManager) getLastCommittedHeight(btcPk *btcec.PublicKey) (uint64, error) { - pubRandCommitMap, err := tm.lastCommittedPublicRandWithRetry(btcPk, 1) - if err != nil { - return 0, err - } - - // no committed randomness yet - if len(pubRandCommitMap) == 0 { - return 0, nil - } - - if len(pubRandCommitMap) > 1 { - return 0, fmt.Errorf("got more than one last committed public randomness") - } - var lastCommittedHeight uint64 - for startHeight, resp := range pubRandCommitMap { - lastCommittedHeight = startHeight + resp.NumPubRand - 1 - } - - return lastCommittedHeight, nil -} - -func (tm *TestManager) finalizeUntilEpoch(t *testing.T, epoch uint64) { - bbnClient := tm.BabylonClient - - // wait until the checkpoint of this epoch is sealed - require.Eventually(t, func() bool { - lastSealedCkpt, err := bbnClient.LatestEpochFromStatus(ckpttypes.Sealed) - if err != nil { - return false - } - return epoch <= lastSealedCkpt.RawCheckpoint.EpochNum - }, eventuallyWaitTimeOut, 1*time.Second) - - t.Logf("start finalizing epochs until %d", epoch) - // Random source for the generation of BTC data - r := rand.New(rand.NewSource(time.Now().Unix())) - - // get all checkpoints of these epochs - pagination := &sdkquerytypes.PageRequest{ - Key: ckpttypes.CkptsObjectKey(0), - Limit: epoch, - } - resp, err := bbnClient.RawCheckpoints(pagination) - require.NoError(t, err) - require.Equal(t, int(epoch), len(resp.RawCheckpoints)) - - submitterAddr, err := sdk.AccAddressFromBech32(tm.BabylonClient.MustGetAddr()) - require.NoError(t, err) - - for _, checkpoint := range resp.RawCheckpoints { - currentBtcTipResp, err := tm.BabylonClient.QueryClient.BTCHeaderChainTip() - require.NoError(t, err) - tipHeader, err := bbn.NewBTCHeaderBytesFromHex(currentBtcTipResp.Header.HeaderHex) - require.NoError(t, err) - - rawCheckpoint, err := checkpoint.Ckpt.ToRawCheckpoint() - require.NoError(t, err) - - btcCheckpoint, err := ckpttypes.FromRawCkptToBTCCkpt(rawCheckpoint, submitterAddr) - require.NoError(t, err) - - babylonTagBytes, err := hex.DecodeString("01020304") - require.NoError(t, err) - - p1, p2, err := txformat.EncodeCheckpointData( - babylonTagBytes, - txformat.CurrentVersion, - btcCheckpoint, - ) - require.NoError(t, err) - - tx1 := datagen.CreatOpReturnTransaction(r, p1) - - opReturn1 := datagen.CreateBlockWithTransaction(r, tipHeader.ToBlockHeader(), tx1) - tx2 := datagen.CreatOpReturnTransaction(r, p2) - opReturn2 := datagen.CreateBlockWithTransaction(r, opReturn1.HeaderBytes.ToBlockHeader(), tx2) - - // insert headers and proofs - _, err = tm.insertBtcBlockHeaders([]bbn.BTCHeaderBytes{ - opReturn1.HeaderBytes, - opReturn2.HeaderBytes, - }) - require.NoError(t, err) - - _, err = tm.insertSpvProofs(submitterAddr.String(), []*btcctypes.BTCSpvProof{ - opReturn1.SpvProof, - opReturn2.SpvProof, - }) - require.NoError(t, err) - - // wait until this checkpoint is submitted - require.Eventually(t, func() bool { - ckpt, err := bbnClient.RawCheckpoint(checkpoint.Ckpt.EpochNum) - if err != nil { - return false - } - return ckpt.RawCheckpoint.Status == ckpttypes.Submitted - }, eventuallyWaitTimeOut, eventuallyPollTime) - } - - // insert w BTC headers - tm.insertWBTCHeaders(t, r) - - // wait until the checkpoint of this epoch is finalised - require.Eventually(t, func() bool { - lastFinalizedCkpt, err := bbnClient.LatestEpochFromStatus(ckpttypes.Finalized) - if err != nil { - t.Logf("failed to get last finalized epoch: %v", err) - return false - } - return epoch <= lastFinalizedCkpt.RawCheckpoint.EpochNum - }, eventuallyWaitTimeOut, 1*time.Second) - - t.Logf("epoch %d is finalised", epoch) -} - -func (tm *TestManager) insertBtcBlockHeaders(headers []bbn.BTCHeaderBytes) (*provider.RelayerTxResponse, error) { - msg := &btclctypes.MsgInsertHeaders{ - Signer: tm.MustGetBabylonSigner(), - Headers: headers, - } - - res, err := tm.BabylonClient.ReliablySendMsg(context.Background(), msg, nil, nil) - if err != nil { - return nil, err - } - - return res, nil -} - -func (tm *TestManager) insertSpvProofs(submitter string, proofs []*btcctypes.BTCSpvProof) (*provider.RelayerTxResponse, error) { - msg := &btcctypes.MsgInsertBTCSpvProof{ - Submitter: submitter, - Proofs: proofs, - } - - res, err := tm.BabylonClient.ReliablySendMsg(context.Background(), msg, nil, nil) - if err != nil { - return nil, err - } - - return res, nil -} - -func (tm *TestManager) insertWBTCHeaders(t *testing.T, r *rand.Rand) { - ckptParamRes, err := tm.BabylonClient.QueryClient.BTCCheckpointParams() - require.NoError(t, err) - btcTipResp, err := tm.BabylonClient.QueryClient.BTCHeaderChainTip() - require.NoError(t, err) - tipHeader, err := bbn.NewBTCHeaderBytesFromHex(btcTipResp.Header.HeaderHex) - require.NoError(t, err) - kHeaders := datagen.NewBTCHeaderChainFromParentInfo(r, &btclctypes.BTCHeaderInfo{ - Header: &tipHeader, - Hash: tipHeader.Hash(), - Height: btcTipResp.Header.Height, - Work: &btcTipResp.Header.Work, - }, uint32(ckptParamRes.Params.CheckpointFinalizationTimeout)) - _, err = tm.insertBtcBlockHeaders(kHeaders.ChainToBytes()) - require.NoError(t, err) -} - func (tm *TestManager) getHighUTXOAndSum() (*btcjson.ListUnspentResult, float64, error) { utxos, err := tm.WalletClient.ListUnspent() if err != nil {