From 39410035b504a9842acac76206248a99ecb8411f Mon Sep 17 00:00:00 2001 From: Vidhan Date: Wed, 30 Nov 2022 00:52:18 +0530 Subject: [PATCH] refactor: consolidate duplicate test methods (#1074) ## Overview Closes #1073 Affected packages 1. `pkg` -> `shares` & `prove` - Remove duplicated `generateRandomTransaction`, `generateRandomlySizedTransactions`, `generateRandomBlob`, and `generateRandomlySizedBlobs` 2. `testutil` - Add `factory` package with generator method for the random transaction(s) and random blob(s) - Deprecate blobtestutil - Remove duplicated `GenerateKeyringSigner` and `generateKeyring` methods 3. `x/blob/types` - Refactor keyring generator method in separate file `test_util.go` - Reuse keyring generators from `x/blob/types` in `app` package - Use constants instead of hardcoded strings for the test account and chain id 4. `app`/`app_test` - Refactor all helper generate methods in `app` package's `test_util.go` - Reuse keyring generators from `blobtypes` - Simplify some of the helper generateTxs methods ## Checklist - [X] ~~New and updated code has appropriate documentation~~ - [X] New and updated code has new and/or updated testing - [x] Required CI checks are passing - [X] ~~Visual proof for any user facing features like CLI or documentation updates~~ - [X] Linked issues closed with keywords --- app/estimate_square_size_test.go | 26 +++-- app/test/fuzz_abci_test.go | 8 +- app/test/prepare_proposal_test.go | 57 +--------- app/test/process_proposal_test.go | 7 +- app/test_util.go | 164 ++++++++++------------------ pkg/prove/proof_test.go | 72 ++---------- pkg/shares/compact_shares_test.go | 13 ++- pkg/shares/shares_test.go | 64 +---------- pkg/shares/sparse_shares_test.go | 9 +- pkg/shares/utils_test.go | 9 +- testutil/blob/testutil.go | 132 ---------------------- testutil/test_app.go | 39 +------ testutil/testfactory/blob.go | 36 ++++++ testutil/testfactory/txs.go | 32 ++++++ x/blob/types/builder_test.go | 50 ++------- x/blob/types/payforblob_test.go | 9 +- x/blob/types/test_util.go | 40 +++++++ x/blob/types/wirepayfordata_test.go | 5 +- 18 files changed, 238 insertions(+), 534 deletions(-) delete mode 100644 testutil/blob/testutil.go create mode 100644 testutil/testfactory/blob.go create mode 100644 testutil/testfactory/txs.go create mode 100644 x/blob/types/test_util.go diff --git a/app/estimate_square_size_test.go b/app/estimate_square_size_test.go index 1a4455e58f..8d26787514 100644 --- a/app/estimate_square_size_test.go +++ b/app/estimate_square_size_test.go @@ -16,6 +16,10 @@ import ( coretypes "github.com/tendermint/tendermint/types" ) +const ( + testEstimateKey = "estimate-key" +) + func Test_estimateSquareSize(t *testing.T) { type test struct { name string @@ -36,11 +40,11 @@ func Test_estimateSquareSize(t *testing.T) { {"one over the perfect estimation edge case", 10, 1, appconsts.SparseShareContentSize * 10, 8}, } encConf := encoding.MakeConfig(ModuleEncodingRegisters...) - signer := generateKeyringSigner(t, "estimate-key") + signer := types.GenerateKeyringSigner(t, testEstimateKey) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - txs := generateManyRawWirePFB(t, encConf.TxConfig, signer, tt.wPFBCount, tt.messgeSize) - txs = append(txs, generateManyRawSendTxs(t, encConf.TxConfig, signer, tt.normalTxs)...) + txs := GenerateManyRawWirePFB(t, encConf.TxConfig, signer, tt.wPFBCount, tt.messgeSize) + txs = append(txs, GenerateManyRawSendTxs(t, encConf.TxConfig, signer, tt.normalTxs)...) parsedTxs := parseTxs(encConf.TxConfig, txs) squareSize, totalSharesUsed := estimateSquareSize(parsedTxs, core.EvidenceList{}) assert.Equal(t, tt.expectedSize, squareSize) @@ -70,9 +74,9 @@ func Test_estimateSquareSize(t *testing.T) { func Test_pruning(t *testing.T) { encConf := encoding.MakeConfig(ModuleEncodingRegisters...) - signer := generateKeyringSigner(t, "estimate-key") - txs := generateManyRawSendTxs(t, encConf.TxConfig, signer, 10) - txs = append(txs, generateManyRawWirePFB(t, encConf.TxConfig, signer, 10, 1000)...) + signer := types.GenerateKeyringSigner(t, testEstimateKey) + txs := GenerateManyRawSendTxs(t, encConf.TxConfig, signer, 10) + txs = append(txs, GenerateManyRawWirePFB(t, encConf.TxConfig, signer, 10, 1000)...) parsedTxs := parseTxs(encConf.TxConfig, txs) ss, total := estimateSquareSize(parsedTxs, core.EvidenceList{}) nextLowestSS := ss / 2 @@ -125,9 +129,9 @@ func Test_overEstimateMalleatedTxSize(t *testing.T) { } encConf := encoding.MakeConfig(ModuleEncodingRegisters...) - signer := generateKeyringSigner(t, "estimate-key") + signer := types.GenerateKeyringSigner(t, testEstimateKey) for _, tt := range tests { - wpfbTx := generateRawWirePFBTx( + wpfbTx := generateRawWirePFB( t, encConf.TxConfig, namespace.RandomBlobNamespace(), @@ -163,11 +167,11 @@ func Test_calculateCompactShareCount(t *testing.T) { {"one over the perfect estimation edge case", 10, 1, totalBlobSize(appconsts.SparseShareContentSize + 1)}, } encConf := encoding.MakeConfig(ModuleEncodingRegisters...) - signer := generateKeyringSigner(t, "estimate-key") + signer := types.GenerateKeyringSigner(t, testEstimateKey) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - txs := generateManyRawWirePFB(t, encConf.TxConfig, signer, tt.wPFBCount, tt.messgeSize) - txs = append(txs, generateManyRawSendTxs(t, encConf.TxConfig, signer, tt.normalTxs)...) + txs := GenerateManyRawWirePFB(t, encConf.TxConfig, signer, tt.wPFBCount, tt.messgeSize) + txs = append(txs, GenerateManyRawSendTxs(t, encConf.TxConfig, signer, tt.normalTxs)...) parsedTxs := parseTxs(encConf.TxConfig, txs) squareSize, totalSharesUsed := estimateSquareSize(parsedTxs, core.EvidenceList{}) diff --git a/app/test/fuzz_abci_test.go b/app/test/fuzz_abci_test.go index d22b8e48ad..c07bd4e979 100644 --- a/app/test/fuzz_abci_test.go +++ b/app/test/fuzz_abci_test.go @@ -7,7 +7,7 @@ import ( "github.com/celestiaorg/celestia-app/app" "github.com/celestiaorg/celestia-app/app/encoding" "github.com/celestiaorg/celestia-app/testutil" - paytestutil "github.com/celestiaorg/celestia-app/testutil/blob" + "github.com/celestiaorg/celestia-app/x/blob/types" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" tmrand "github.com/tendermint/tendermint/libs/rand" @@ -23,7 +23,7 @@ import ( // when fuzzing. func TestFuzzPrepareProcessProposal(t *testing.T) { encConf := encoding.MakeConfig(app.ModuleEncodingRegisters...) - signer := testutil.GenerateKeyringSigner(t, testAccName) + signer := types.GenerateKeyringSigner(t, types.TestAccName) testApp := testutil.SetupTestAppWithGenesisValSet(t) timer := time.After(time.Second * 30) for { @@ -32,8 +32,8 @@ func TestFuzzPrepareProcessProposal(t *testing.T) { return default: t.Run("randomized inputs to Prepare and Process Proposal", func(t *testing.T) { - pfbTxs := paytestutil.GenerateManyRawWirePFB(t, encConf.TxConfig, signer, tmrand.Intn(100), -1) - txs := paytestutil.GenerateManyRawSendTxs(t, encConf.TxConfig, signer, tmrand.Intn(20)) + pfbTxs := app.GenerateManyRawWirePFB(t, encConf.TxConfig, signer, tmrand.Intn(100), -1) + txs := app.GenerateManyRawSendTxs(t, encConf.TxConfig, signer, tmrand.Intn(20)) txs = append(txs, pfbTxs...) resp := testApp.PrepareProposal(abci.RequestPrepareProposal{ BlockData: &core.Data{ diff --git a/app/test/prepare_proposal_test.go b/app/test/prepare_proposal_test.go index 111108a8a5..71618f2125 100644 --- a/app/test/prepare_proposal_test.go +++ b/app/test/prepare_proposal_test.go @@ -5,8 +5,6 @@ import ( "testing" "github.com/celestiaorg/nmt/namespace" - "github.com/cosmos/cosmos-sdk/client" - sdk "github.com/cosmos/cosmos-sdk/types" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -21,7 +19,7 @@ import ( ) func TestPrepareProposal(t *testing.T) { - signer := testutil.GenerateKeyringSigner(t, testAccName) + signer := types.GenerateKeyringSigner(t, types.TestAccName) encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) @@ -35,15 +33,15 @@ func TestPrepareProposal(t *testing.T) { firstNamespace := []byte{2, 2, 2, 2, 2, 2, 2, 2} firstBlob := bytes.Repeat([]byte{4}, 512) - firstRawTx := generateRawTx(t, encCfg.TxConfig, firstNamespace, firstBlob, signer) + firstRawTx := app.GenerateRawWirePFB(t, encCfg.TxConfig, firstNamespace, firstBlob, signer) secondNamespace := []byte{1, 1, 1, 1, 1, 1, 1, 1} secondBlob := []byte{2} - secondRawTx := generateRawTx(t, encCfg.TxConfig, secondNamespace, secondBlob, signer) + secondRawTx := app.GenerateRawWirePFB(t, encCfg.TxConfig, secondNamespace, secondBlob, signer) thirdNamespace := []byte{3, 3, 3, 3, 3, 3, 3, 3} thirdBlob := []byte{1} - thirdRawTx := generateRawTx(t, encCfg.TxConfig, thirdNamespace, thirdBlob, signer) + thirdRawTx := app.GenerateRawWirePFB(t, encCfg.TxConfig, thirdNamespace, thirdBlob, signer) tests := []test{ { @@ -109,7 +107,7 @@ func TestPrepareProposalWithReservedNamespaces(t *testing.T) { testApp := testutil.SetupTestAppWithGenesisValSet(t) encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) - signer := testutil.GenerateKeyringSigner(t, testAccName) + signer := types.GenerateKeyringSigner(t, types.TestAccName) type test struct { name string @@ -128,7 +126,7 @@ func TestPrepareProposalWithReservedNamespaces(t *testing.T) { for _, tt := range tests { blob := []byte{1} - tx := generateRawTx(t, encCfg.TxConfig, tt.namespace, blob, signer) + tx := app.GenerateRawWirePFB(t, encCfg.TxConfig, tt.namespace, blob, signer) input := abci.RequestPrepareProposal{ BlockData: &core.Data{ Txs: [][]byte{tx}, @@ -138,46 +136,3 @@ func TestPrepareProposalWithReservedNamespaces(t *testing.T) { assert.Equal(t, tt.expectedBlobs, len(res.BlockData.Blobs)) } } - -func generateRawTx(t *testing.T, txConfig client.TxConfig, ns, blob []byte, signer *types.KeyringSigner) (rawTx []byte) { - coin := sdk.Coin{ - Denom: app.BondDenom, - Amount: sdk.NewInt(10), - } - - opts := []types.TxBuilderOption{ - types.SetFeeAmount(sdk.NewCoins(coin)), - types.SetGasLimit(10000000), - } - - msg := generateSignedWirePayForBlob(t, ns, blob, appconsts.ShareVersionZero, signer, opts) - - builder := signer.NewTxBuilder(opts...) - - tx, err := signer.BuildSignedTx(builder, msg) - require.NoError(t, err) - - // encode the tx - rawTx, err = txConfig.TxEncoder()(tx) - require.NoError(t, err) - - return rawTx -} - -func generateSignedWirePayForBlob(t *testing.T, ns []byte, blob []byte, shareVersion uint8, signer *types.KeyringSigner, options []types.TxBuilderOption) *types.MsgWirePayForBlob { - msg, err := types.NewWirePayForBlob(ns, blob, shareVersion) - if err != nil { - t.Error(err) - } - - err = msg.SignShareCommitment(signer, options...) - if err != nil { - t.Error(err) - } - - return msg -} - -const ( - testAccName = "test-account" -) diff --git a/app/test/process_proposal_test.go b/app/test/process_proposal_test.go index 733214b3d1..f8aab7c078 100644 --- a/app/test/process_proposal_test.go +++ b/app/test/process_proposal_test.go @@ -15,21 +15,20 @@ import ( "github.com/celestiaorg/celestia-app/app/encoding" "github.com/celestiaorg/celestia-app/pkg/appconsts" "github.com/celestiaorg/celestia-app/testutil" - paytestutil "github.com/celestiaorg/celestia-app/testutil/blob" "github.com/celestiaorg/celestia-app/x/blob/types" "github.com/celestiaorg/nmt/namespace" sdk "github.com/cosmos/cosmos-sdk/types" ) func TestBlobInclusionCheck(t *testing.T) { - signer := testutil.GenerateKeyringSigner(t, testAccName) + signer := types.GenerateKeyringSigner(t, types.TestAccName) testApp := testutil.SetupTestAppWithGenesisValSet(t) encConf := encoding.MakeConfig(app.ModuleEncodingRegisters...) // block with all blobs included validData := func() *core.Data { return &core.Data{ - Txs: paytestutil.GenerateManyRawWirePFB(t, encConf.TxConfig, signer, 4, 1000), + Txs: app.GenerateManyRawWirePFB(t, encConf.TxConfig, signer, 4, 1000), } } @@ -122,7 +121,7 @@ func TestProcessProposalWithParityShareNamespace(t *testing.T) { testApp := testutil.SetupTestAppWithGenesisValSet(t) encConf := encoding.MakeConfig(app.ModuleEncodingRegisters...) - signer := testutil.GenerateKeyringSigner(t, testAccName) + signer := types.GenerateKeyringSigner(t, types.TestAccName) pfb, blobData := genRandMsgPayForBlobForNamespace(t, signer, appconsts.ParitySharesNamespaceID) input := abci.RequestProcessProposal{ diff --git a/app/test_util.go b/app/test_util.go index 03134258bd..453ec93fd3 100644 --- a/app/test_util.go +++ b/app/test_util.go @@ -3,54 +3,25 @@ package app import ( "testing" - "github.com/celestiaorg/celestia-app/app/encoding" "github.com/celestiaorg/celestia-app/pkg/appconsts" "github.com/celestiaorg/celestia-app/testutil/namespace" "github.com/celestiaorg/celestia-app/x/blob/types" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/stretchr/testify/require" tmrand "github.com/tendermint/tendermint/libs/rand" - core "github.com/tendermint/tendermint/proto/tendermint/types" - coretypes "github.com/tendermint/tendermint/types" ) -func GenerateValidBlockData( - t *testing.T, - txConfig client.TxConfig, - signer *types.KeyringSigner, - pfbCount, - normalTxCount, - size int, -) (coretypes.Data, error) { - rawTxs := generateManyRawWirePFB(t, txConfig, signer, pfbCount, size) - rawTxs = append(rawTxs, generateManyRawSendTxs(t, txConfig, signer, normalTxCount)...) - parsedTxs := parseTxs(txConfig, rawTxs) - - squareSize, totalSharesUsed := estimateSquareSize(parsedTxs, core.EvidenceList{}) - - if totalSharesUsed > int(squareSize*squareSize) { - parsedTxs = prune(txConfig, parsedTxs, totalSharesUsed, int(squareSize)) +// GenerateManyRawWirePFB creates many raw WirePayForBlob transactions. Using +// negative numbers for count and size will randomize those values. count is +// capped at 5000 and size is capped at 3MB. Going over these caps will result +// in randomized values. +func GenerateManyRawWirePFB(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count, size int) [][]byte { + // hardcode a maximum of 5000 transactions so that we can use this for fuzzing + if count > 5000 || count < 0 { + count = tmrand.Intn(5000) } - - processedTxs, blobs, err := malleateTxs(txConfig, squareSize, parsedTxs, core.EvidenceList{}) - require.NoError(t, err) - - blockData := core.Data{ - Txs: processedTxs, - Evidence: core.EvidenceList{}, - Blobs: blobs, - SquareSize: squareSize, - } - - return coretypes.DataFromProto(&blockData) -} - -func generateManyRawWirePFB(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count, size int) [][]byte { txs := make([][]byte, count) coin := sdk.Coin{ @@ -64,7 +35,10 @@ func generateManyRawWirePFB(t *testing.T, txConfig client.TxConfig, signer *type } for i := 0; i < count; i++ { - wpfbTx := generateRawWirePFBTx( + if size < 0 || size > 3000000 { + size = tmrand.Intn(1000000) + } + wpfbTx := generateRawWirePFB( t, txConfig, namespace.RandomBlobNamespace(), @@ -78,7 +52,43 @@ func generateManyRawWirePFB(t *testing.T, txConfig client.TxConfig, signer *type return txs } -func generateManyRawSendTxs(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count int) [][]byte { +func GenerateRawWirePFB(t *testing.T, txConfig client.TxConfig, ns, blob []byte, signer *types.KeyringSigner) (rawTx []byte) { + coin := sdk.Coin{ + Denom: BondDenom, + Amount: sdk.NewInt(10), + } + + opts := []types.TxBuilderOption{ + types.SetFeeAmount(sdk.NewCoins(coin)), + types.SetGasLimit(10000000), + } + + return generateRawWirePFB( + t, + txConfig, + ns, + blob, + appconsts.ShareVersionZero, + signer, + opts..., + ) +} + +func GenerateSignedWirePayForBlob(t *testing.T, ns []byte, blob []byte, shareVersion uint8, signer *types.KeyringSigner, options []types.TxBuilderOption) *types.MsgWirePayForBlob { + msg, err := types.NewWirePayForBlob(ns, blob, shareVersion) + if err != nil { + t.Error(err) + } + + err = msg.SignShareCommitment(signer, options...) + if err != nil { + t.Error(err) + } + + return msg +} + +func GenerateManyRawSendTxs(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count int) [][]byte { txs := make([][]byte, count) for i := 0; i < count; i++ { txs[i] = generateRawSendTx(t, txConfig, signer, 100) @@ -86,7 +96,7 @@ func generateManyRawSendTxs(t *testing.T, txConfig client.TxConfig, signer *type return txs } -// this creates send transactions meant to help test encoding/prepare/process +// generateRawSendTx creates send transactions meant to help test encoding/prepare/process // proposal, they are not meant to actually be executed by the state machine. If // we want that, we have to update nonce, and send funds to someone other than // the same account signing the transaction. @@ -109,84 +119,26 @@ func generateRawSendTx(t *testing.T, txConfig client.TxConfig, signer *types.Key addr, err := signer.GetSignerInfo().GetAddress() require.NoError(t, err) - builder := signer.NewTxBuilder(opts...) - msg := banktypes.NewMsgSend(addr, addr, sdk.NewCoins(amountCoin)) - tx, err := signer.BuildSignedTx(builder, msg) - require.NoError(t, err) - - rawTx, err = txConfig.TxEncoder()(tx) - require.NoError(t, err) - - return rawTx + return genrateRawTx(t, txConfig, msg, signer, opts...) } -// generateRawWirePFBTx creates a tx with a single MsgWirePayForBlob using +// generateRawWirePFB creates a tx with a single MsgWirePayForBlob using // the provided namespace, blob, and shareVersion -func generateRawWirePFBTx(t *testing.T, txConfig client.TxConfig, ns []byte, blob []byte, shareVersion uint8, signer *types.KeyringSigner, opts ...types.TxBuilderOption) (rawTx []byte) { - // create a msg - msg := generateSignedWirePayForBlob(t, ns, blob, shareVersion, signer, opts) +func generateRawWirePFB(t *testing.T, txConfig client.TxConfig, ns []byte, blob []byte, shareVersion uint8, signer *types.KeyringSigner, opts ...types.TxBuilderOption) (rawTx []byte) { + msg := GenerateSignedWirePayForBlob(t, ns, blob, shareVersion, signer, opts) + return genrateRawTx(t, txConfig, msg, signer, opts...) +} +func genrateRawTx(t *testing.T, txConfig client.TxConfig, msg sdk.Msg, signer *types.KeyringSigner, opts ...types.TxBuilderOption) []byte { builder := signer.NewTxBuilder(opts...) tx, err := signer.BuildSignedTx(builder, msg) require.NoError(t, err) // encode the tx - rawTx, err = txConfig.TxEncoder()(tx) + rawTx, err := txConfig.TxEncoder()(tx) require.NoError(t, err) return rawTx } - -func generateSignedWirePayForBlob(t *testing.T, ns []byte, blob []byte, shareVersion uint8, signer *types.KeyringSigner, options []types.TxBuilderOption) *types.MsgWirePayForBlob { - msg, err := types.NewWirePayForBlob(ns, blob, shareVersion) - if err != nil { - t.Error(err) - } - - err = msg.SignShareCommitment(signer, options...) - if err != nil { - t.Error(err) - } - - return msg -} - -const ( - TestAccountName = "test-account" -) - -func generateKeyring(t *testing.T, cdc codec.Codec, accts ...string) keyring.Keyring { - t.Helper() - kb := keyring.NewInMemory(cdc) - - for _, acc := range accts { - _, _, err := kb.NewMnemonic(acc, keyring.English, "", "", hd.Secp256k1) - if err != nil { - t.Error(err) - } - } - - _, err := kb.NewAccount(testAccName, testMnemo, "1234", "", hd.Secp256k1) - if err != nil { - panic(err) - } - - return kb -} - -// generateKeyringSigner creates a types.KeyringSigner with keys generated for -// the provided accounts -func generateKeyringSigner(t *testing.T, acct string) *types.KeyringSigner { - encCfg := encoding.MakeConfig(ModuleEncodingRegisters...) - kr := generateKeyring(t, encCfg.Codec, acct) - return types.NewKeyringSigner(kr, acct, testChainID) -} - -const ( - // nolint:lll - testMnemo = `ramp soldier connect gadget domain mutual staff unusual first midnight iron good deputy wage vehicle mutual spike unlock rocket delay hundred script tumble choose` - testAccName = "test-account" - testChainID = "test-chain-1" -) diff --git a/pkg/prove/proof_test.go b/pkg/prove/proof_test.go index e02c764c53..f70407e792 100644 --- a/pkg/prove/proof_test.go +++ b/pkg/prove/proof_test.go @@ -1,13 +1,11 @@ package prove import ( - "math/rand" - "sort" "testing" "github.com/celestiaorg/celestia-app/pkg/appconsts" "github.com/celestiaorg/celestia-app/pkg/shares" - "github.com/celestiaorg/celestia-app/testutil/namespace" + "github.com/celestiaorg/celestia-app/testutil/testfactory" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" tmrand "github.com/tendermint/tendermint/libs/rand" @@ -16,12 +14,12 @@ import ( func TestTxInclusion(t *testing.T) { typicalBlockData := types.Data{ - Txs: generateRandomlySizedTxs(100, 500), - Blobs: generateRandomlySizedBlobs(40, 16000), + Txs: testfactory.GenerateRandomlySizedTxs(100, 500), + Blobs: testfactory.GenerateRandomlySizedBlobs(40, 16000), SquareSize: 64, } lotsOfTxsNoBlobs := types.Data{ - Txs: generateRandomlySizedTxs(1000, 500), + Txs: testfactory.GenerateRandomlySizedTxs(1000, 500), SquareSize: 64, } overlappingSquareSize := 16 @@ -41,7 +39,7 @@ func TestTxInclusion(t *testing.T) { tmrand.Bytes(10000), }, ), - Blobs: generateRandomlySizedBlobs(8, 400), + Blobs: testfactory.GenerateRandomlySizedBlobs(8, 400), SquareSize: uint64(overlappingSquareSize), } @@ -81,14 +79,14 @@ func TestTxSharePosition(t *testing.T) { tests := []test{ { name: "typical", - txs: generateRandomlySizedTxs(44, 200), + txs: testfactory.GenerateRandomlySizedTxs(44, 200), }, { name: "many small tx", - txs: generateRandomlySizedTxs(444, 100), + txs: testfactory.GenerateRandomlySizedTxs(444, 100), }, { - // this is a concrete output from generateRandomlySizedTxs(444, 100) + // this is a concrete output from testfactory.GenerateRandomlySizedTxs(444, 100) // that surfaced a bug in txSharePositions so it is included here to // prevent regressions name: "many small tx (without randomness)", @@ -96,15 +94,15 @@ func TestTxSharePosition(t *testing.T) { }, { name: "one small tx", - txs: generateRandomlySizedTxs(1, 200), + txs: testfactory.GenerateRandomlySizedTxs(1, 200), }, { name: "one large tx", - txs: generateRandomlySizedTxs(1, 2000), + txs: testfactory.GenerateRandomlySizedTxs(1, 2000), }, { name: "many large txs", - txs: generateRandomlySizedTxs(100, 2000), + txs: testfactory.GenerateRandomlySizedTxs(100, 2000), }, } @@ -185,51 +183,3 @@ func stripCompactShares(compactShares []shares.Share, start uint64, end uint64) } return result } - -func generateRandomlySizedTxs(count, max int) types.Txs { - txs := make(types.Txs, count) - for i := 0; i < count; i++ { - size := rand.Intn(max) - if size == 0 { - size = 1 - } - txs[i] = generateRandomTxs(1, size)[0] - } - return txs -} - -func generateRandomTxs(count, size int) types.Txs { - txs := make(types.Txs, count) - for i := 0; i < count; i++ { - tx := make([]byte, size) - _, err := rand.Read(tx) - if err != nil { - panic(err) - } - txs[i] = tx - } - return txs -} - -func generateRandomlySizedBlobs(count, maxBlobSize int) []types.Blob { - blobs := make([]types.Blob, count) - for i := 0; i < count; i++ { - blobs[i] = generateRandomBlob(rand.Intn(maxBlobSize)) - } - - // this is just to let us use assert.Equal - if count == 0 { - blobs = nil - } - - sort.Sort(types.BlobsByNamespace(blobs)) - return blobs -} - -func generateRandomBlob(size int) types.Blob { - blob := types.Blob{ - NamespaceID: namespace.RandomBlobNamespace(), - Data: tmrand.Bytes(size), - } - return blob -} diff --git a/pkg/shares/compact_shares_test.go b/pkg/shares/compact_shares_test.go index 31a5b33367..eea4a455b9 100644 --- a/pkg/shares/compact_shares_test.go +++ b/pkg/shares/compact_shares_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/testutil/testfactory" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" coretypes "github.com/tendermint/tendermint/types" @@ -16,7 +17,7 @@ func TestCompactShareWriter(t *testing.T) { // note that this test is mainly for debugging purposes, the main round trip // tests occur in TestMerge and Test_processCompactShares w := NewCompactShareSplitter(appconsts.TxNamespaceID, appconsts.ShareVersionZero) - txs := generateRandomTransaction(33, 200) + txs := testfactory.GenerateRandomTxs(33, 200) for _, tx := range txs { rawTx, _ := MarshalDelimitedTx(tx) w.WriteBytes(rawTx) @@ -74,7 +75,7 @@ func Test_processCompactShares(t *testing.T) { // run the tests with identically sized txs t.Run(fmt.Sprintf("%s idendically sized", tc.name), func(t *testing.T) { - txs := generateRandomTransaction(tc.txCount, tc.txSize) + txs := testfactory.GenerateRandomTxs(tc.txCount, tc.txSize) shares := SplitTxs(txs) rawShares := ToBytes(shares) @@ -92,7 +93,7 @@ func Test_processCompactShares(t *testing.T) { // run the same tests using randomly sized txs with caps of tc.txSize t.Run(fmt.Sprintf("%s randomly sized", tc.name), func(t *testing.T) { - txs := generateRandomlySizedTransactions(tc.txCount, tc.txSize) + txs := testfactory.GenerateRandomlySizedTxs(tc.txCount, tc.txSize) shares := SplitTxs(txs) rawShares := ToBytes(shares) @@ -112,7 +113,7 @@ func Test_processCompactShares(t *testing.T) { func TestCompactShareContainsInfoByte(t *testing.T) { css := NewCompactShareSplitter(appconsts.TxNamespaceID, appconsts.ShareVersionZero) - txs := generateRandomTransaction(1, appconsts.ContinuationCompactShareContentSize/4) + txs := testfactory.GenerateRandomTxs(1, appconsts.ContinuationCompactShareContentSize/4) for _, tx := range txs { css.WriteTx(tx) @@ -132,7 +133,7 @@ func TestCompactShareContainsInfoByte(t *testing.T) { func TestContiguousCompactShareContainsInfoByte(t *testing.T) { css := NewCompactShareSplitter(appconsts.TxNamespaceID, appconsts.ShareVersionZero) - txs := generateRandomTransaction(1, appconsts.ContinuationCompactShareContentSize*4) + txs := testfactory.GenerateRandomTxs(1, appconsts.ContinuationCompactShareContentSize*4) for _, tx := range txs { css.WriteTx(tx) @@ -156,7 +157,7 @@ func Test_parseCompactSharesErrors(t *testing.T) { rawShares [][]byte } - txs := generateRandomTransaction(2, appconsts.ContinuationCompactShareContentSize*4) + txs := testfactory.GenerateRandomTxs(2, appconsts.ContinuationCompactShareContentSize*4) shares := SplitTxs(txs) rawShares := ToBytes(shares) diff --git a/pkg/shares/shares_test.go b/pkg/shares/shares_test.go index 8089026ad0..7b6803d7af 100644 --- a/pkg/shares/shares_test.go +++ b/pkg/shares/shares_test.go @@ -3,17 +3,14 @@ package shares import ( "context" "math" - "math/rand" - "sort" "testing" "time" "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/testutil/testfactory" "github.com/celestiaorg/rsmt2d" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - tmrand "github.com/tendermint/tendermint/libs/rand" - "github.com/tendermint/tendermint/types" coretypes "github.com/tendermint/tendermint/types" ) @@ -99,38 +96,13 @@ func TestFuzz_Merge(t *testing.T) { // generateRandomBlockData returns randomly generated block data for testing purposes func generateRandomBlockData(txCount, evdCount, blobCount, maxSize int) (data coretypes.Data) { - data.Txs = generateRandomlySizedTransactions(txCount, maxSize) + data.Txs = testfactory.GenerateRandomlySizedTxs(txCount, maxSize) data.Evidence = generateIdenticalEvidence(evdCount) - data.Blobs = generateRandomlySizedBlobs(blobCount, maxSize) + data.Blobs = testfactory.GenerateRandomlySizedBlobs(blobCount, maxSize) data.SquareSize = appconsts.MaxSquareSize return data } -func generateRandomlySizedTransactions(count, maxSize int) coretypes.Txs { - txs := make(coretypes.Txs, count) - for i := 0; i < count; i++ { - size := rand.Intn(maxSize) - if size == 0 { - size = 1 - } - txs[i] = generateRandomTransaction(1, size)[0] - } - return txs -} - -func generateRandomTransaction(count, size int) coretypes.Txs { - txs := make(coretypes.Txs, count) - for i := 0; i < count; i++ { - tx := make([]byte, size) - _, err := rand.Read(tx) - if err != nil { - panic(err) - } - txs[i] = tx - } - return txs -} - func generateIdenticalEvidence(count int) coretypes.EvidenceData { evidence := make([]coretypes.Evidence, count) for i := 0; i < count; i++ { @@ -140,39 +112,11 @@ func generateIdenticalEvidence(count int) coretypes.EvidenceData { return coretypes.EvidenceData{Evidence: evidence} } -func generateRandomlySizedBlobs(count, maxBlobSize int) []coretypes.Blob { - blobs := make([]coretypes.Blob, count) - for i := 0; i < count; i++ { - blobs[i] = generateRandomBlob(rand.Intn(maxBlobSize)) - if len(blobs[i].Data) == 0 { - i-- - } - } - - // this is just to let us use assert.Equal - if count == 0 { - blobs = nil - } - - sort.Sort(types.BlobsByNamespace(blobs)) - return blobs -} - -// generateRandomBlob returns a random blob of the given size (in bytes) -func generateRandomBlob(size int) coretypes.Blob { - blob := coretypes.Blob{ - NamespaceID: tmrand.Bytes(appconsts.NamespaceSize), - Data: tmrand.Bytes(size), - ShareVersion: appconsts.ShareVersionZero, - } - return blob -} - // generateRandomBlobOfShareCount returns a blob that spans the given // number of shares func generateRandomBlobOfShareCount(count int) coretypes.Blob { size := rawBlobSize(appconsts.SparseShareContentSize * count) - return generateRandomBlob(size) + return testfactory.GenerateRandomBlob(size) } // rawBlobSize returns the raw blob size that can be used to construct a diff --git a/pkg/shares/sparse_shares_test.go b/pkg/shares/sparse_shares_test.go index dc3febfa42..8416e7f509 100644 --- a/pkg/shares/sparse_shares_test.go +++ b/pkg/shares/sparse_shares_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/testutil/testfactory" "github.com/celestiaorg/nmt/namespace" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -43,7 +44,7 @@ func Test_parseSparseShares(t *testing.T) { t.Run(fmt.Sprintf("%s identically sized ", tc.name), func(t *testing.T) { blobs := make([]coretypes.Blob, tc.blobCount) for i := 0; i < tc.blobCount; i++ { - blobs[i] = generateRandomBlob(tc.blobSize) + blobs[i] = testfactory.GenerateRandomBlob(tc.blobSize) } sort.Sort(coretypes.BlobsByNamespace(blobs)) @@ -65,7 +66,7 @@ func Test_parseSparseShares(t *testing.T) { // run the same tests using randomly sized blobs with caps of tc.blobSize t.Run(fmt.Sprintf("%s randomly sized", tc.name), func(t *testing.T) { - blobs := generateRandomlySizedBlobs(tc.blobCount, tc.blobSize) + blobs := testfactory.GenerateRandomlySizedBlobs(tc.blobCount, tc.blobSize) shares, _ := SplitBlobs(0, nil, blobs, false) rawShares := make([][]byte, len(shares)) for i, share := range shares { @@ -117,8 +118,8 @@ func Test_parseSparseSharesErrors(t *testing.T) { func TestParsePaddedBlob(t *testing.T) { sss := NewSparseShareSplitter() - randomSmallBlob := generateRandomBlob(appconsts.SparseShareContentSize / 2) - randomLargeBlob := generateRandomBlob(appconsts.SparseShareContentSize * 4) + randomSmallBlob := testfactory.GenerateRandomBlob(appconsts.SparseShareContentSize / 2) + randomLargeBlob := testfactory.GenerateRandomBlob(appconsts.SparseShareContentSize * 4) blobs := []coretypes.Blob{ randomSmallBlob, randomLargeBlob, diff --git a/pkg/shares/utils_test.go b/pkg/shares/utils_test.go index 3815658330..fb08afdb24 100644 --- a/pkg/shares/utils_test.go +++ b/pkg/shares/utils_test.go @@ -4,19 +4,20 @@ import ( "reflect" "testing" + "github.com/celestiaorg/celestia-app/testutil/testfactory" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/types" ) func FuzzBlobSharesUsed(f *testing.F) { - f.Add(int(1)) + f.Add(1) f.Fuzz(func(t *testing.T, a int) { if a < 1 { t.Skip() } - ml := BlobSharesUsed(int(a)) - blob := generateRandomBlob(int(a)) + ml := BlobSharesUsed(a) + blob := testfactory.GenerateRandomBlob(a) rawShares, err := SplitBlobs(0, nil, []types.Blob{blob}, false) require.NoError(t, err) require.Equal(t, len(rawShares), ml) @@ -54,7 +55,7 @@ func Test_zeroPadIfNecessary(t *testing.T) { func TestParseDelimiter(t *testing.T) { for i := uint64(0); i < 100; i++ { - tx := generateRandomTransaction(1, int(i))[0] + tx := testfactory.GenerateRandomTxs(1, int(i))[0] input, err := MarshalDelimitedTx(tx) if err != nil { panic(err) diff --git a/testutil/blob/testutil.go b/testutil/blob/testutil.go deleted file mode 100644 index 31219276d5..0000000000 --- a/testutil/blob/testutil.go +++ /dev/null @@ -1,132 +0,0 @@ -package blobtestutil - -import ( - "testing" - - "github.com/celestiaorg/celestia-app/app" - "github.com/celestiaorg/celestia-app/pkg/appconsts" - "github.com/celestiaorg/celestia-app/testutil/namespace" - "github.com/celestiaorg/celestia-app/x/blob/types" - "github.com/cosmos/cosmos-sdk/client" - sdk "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/stretchr/testify/require" - tmrand "github.com/tendermint/tendermint/libs/rand" -) - -// GenerateManyRawWirePFB creates many raw WirePayForBlob transactions. Using -// negative numbers for count and size will randomize those values. count is -// capped at 5000 and size is capped at 3MB. Going over these caps will result -// in randomized values. -func GenerateManyRawWirePFB(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count, size int) [][]byte { - // hardcode a maximum of 5000 transactions so that we can use this for fuzzing - if count > 5000 || count < 0 { - count = tmrand.Intn(5000) - } - txs := make([][]byte, count) - - coin := sdk.Coin{ - Denom: app.BondDenom, - Amount: sdk.NewInt(10), - } - - opts := []types.TxBuilderOption{ - types.SetFeeAmount(sdk.NewCoins(coin)), - types.SetGasLimit(10000000), - } - - for i := 0; i < count; i++ { - if size < 0 || size > 3000000 { - size = tmrand.Intn(1000000) - } - wpfbTx := generateRawWirePFBTx( - t, - txConfig, - namespace.RandomBlobNamespace(), - tmrand.Bytes(size), - appconsts.ShareVersionZero, - signer, - opts..., - ) - txs[i] = wpfbTx - } - return txs -} - -func GenerateManyRawSendTxs(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count int) [][]byte { - txs := make([][]byte, count) - for i := 0; i < count; i++ { - txs[i] = generateRawSendTx(t, txConfig, signer, 100) - } - return txs -} - -// this creates send transactions meant to help test encoding/prepare/process -// proposal, they are not meant to actually be executed by the state machine. If -// we want that, we have to update nonce, and send funds to someone other than -// the same account signing the transaction. -func generateRawSendTx(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, amount int64) (rawTx []byte) { - feeCoin := sdk.Coin{ - Denom: app.BondDenom, - Amount: sdk.NewInt(1), - } - - opts := []types.TxBuilderOption{ - types.SetFeeAmount(sdk.NewCoins(feeCoin)), - types.SetGasLimit(1000000000), - } - - amountCoin := sdk.Coin{ - Denom: app.BondDenom, - Amount: sdk.NewInt(amount), - } - - addr, err := signer.GetSignerInfo().GetAddress() - require.NoError(t, err) - - builder := signer.NewTxBuilder(opts...) - - msg := banktypes.NewMsgSend(addr, addr, sdk.NewCoins(amountCoin)) - - tx, err := signer.BuildSignedTx(builder, msg) - require.NoError(t, err) - - rawTx, err = txConfig.TxEncoder()(tx) - require.NoError(t, err) - - return rawTx -} - -// generateRawWirePFBTx creates a tx with a single MsgWirePayForBlob using the provided namespace, blob, and shareVersion -func generateRawWirePFBTx(t *testing.T, txConfig client.TxConfig, ns []byte, blob []byte, shareVersion uint8, signer *types.KeyringSigner, opts ...types.TxBuilderOption) (rawTx []byte) { - // create a msg - msg := generateSignedWirePayForBlob(t, ns, blob, shareVersion, signer, opts) - - builder := signer.NewTxBuilder(opts...) - tx, err := signer.BuildSignedTx(builder, msg) - require.NoError(t, err) - - // encode the tx - rawTx, err = txConfig.TxEncoder()(tx) - require.NoError(t, err) - - return rawTx -} - -func generateSignedWirePayForBlob(t *testing.T, ns []byte, blob []byte, shareVersion uint8, signer *types.KeyringSigner, options []types.TxBuilderOption) *types.MsgWirePayForBlob { - msg, err := types.NewWirePayForBlob(ns, blob, shareVersion) - if err != nil { - t.Error(err) - } - - err = msg.SignShareCommitment(signer, options...) - if err != nil { - t.Error(err) - } - - return msg -} - -const ( - TestAccountName = "test-account" -) diff --git a/testutil/test_app.go b/testutil/test_app.go index 70b408ed78..683277091a 100644 --- a/testutil/test_app.go +++ b/testutil/test_app.go @@ -8,13 +8,10 @@ import ( "github.com/celestiaorg/celestia-app/app" "github.com/celestiaorg/celestia-app/app/encoding" - "github.com/celestiaorg/celestia-app/x/blob/types" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/simapp" @@ -177,26 +174,7 @@ func AddGenesisAccount(addr sdk.AccAddress, appState app.GenesisState, cdc codec return appState, nil } -func generateKeyring(t *testing.T, cdc codec.Codec, accts ...string) keyring.Keyring { - t.Helper() - kb := keyring.NewInMemory(cdc) - - for _, acc := range accts { - _, _, err := kb.NewMnemonic(acc, keyring.English, "", "", hd.Secp256k1) - if err != nil { - t.Error(err) - } - } - - _, err := kb.NewAccount(TestAccName, testMnemo, "1234", "", hd.Secp256k1) - if err != nil { - panic(err) - } - - return kb -} - -// SetupWithGenesisValSet initializes GenesisState with a single validator and genesis accounts +// GenesisStateWithSingleValidator initializes GenesisState with a single validator and genesis accounts // that also act as delegators. func GenesisStateWithSingleValidator(t *testing.T, testApp *app.App) (app.GenesisState, *tmtypes.ValidatorSet) { t.Helper() @@ -289,21 +267,6 @@ func genesisStateWithValSet(t *testing.T, return genesisState } -// GenerateKeyringSigner creates a types.KeyringSigner with keys generated for -// the provided accounts -func GenerateKeyringSigner(t *testing.T, acct string) *types.KeyringSigner { - encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) - kr := generateKeyring(t, encCfg.Codec, acct) - return types.NewKeyringSigner(kr, acct, testChainID) -} - -const ( - // nolint:lll - testMnemo = `ramp soldier connect gadget domain mutual staff unusual first midnight iron good deputy wage vehicle mutual spike unlock rocket delay hundred script tumble choose` - TestAccName = "test-account" - testChainID = "test-chain-1" -) - // NewDefaultGenesisState generates the default state for the application. func NewDefaultGenesisState(cdc codec.JSONCodec) app.GenesisState { return app.ModuleBasics.DefaultGenesis(cdc) diff --git a/testutil/testfactory/blob.go b/testutil/testfactory/blob.go new file mode 100644 index 0000000000..6da4673efa --- /dev/null +++ b/testutil/testfactory/blob.go @@ -0,0 +1,36 @@ +package testfactory + +import ( + "sort" + + "github.com/celestiaorg/celestia-app/pkg/appconsts" + tmrand "github.com/tendermint/tendermint/libs/rand" + "github.com/tendermint/tendermint/types" +) + +func GenerateRandomlySizedBlobs(count, maxBlobSize int) []types.Blob { + blobs := make([]types.Blob, count) + for i := 0; i < count; i++ { + blobs[i] = GenerateRandomBlob(tmrand.Intn(maxBlobSize)) + if len(blobs[i].Data) == 0 { + i-- + } + } + + // this is just to let us use assert.Equal + if count == 0 { + blobs = nil + } + + sort.Sort(types.BlobsByNamespace(blobs)) + return blobs +} + +func GenerateRandomBlob(size int) types.Blob { + blob := types.Blob{ + NamespaceID: tmrand.Bytes(appconsts.NamespaceSize), + Data: tmrand.Bytes(size), + ShareVersion: appconsts.ShareVersionZero, + } + return blob +} diff --git a/testutil/testfactory/txs.go b/testutil/testfactory/txs.go new file mode 100644 index 0000000000..50da0937c2 --- /dev/null +++ b/testutil/testfactory/txs.go @@ -0,0 +1,32 @@ +package testfactory + +import ( + mrand "math/rand" + + "github.com/tendermint/tendermint/types" +) + +func GenerateRandomlySizedTxs(count, maxSize int) types.Txs { + txs := make(types.Txs, count) + for i := 0; i < count; i++ { + size := mrand.Intn(maxSize) + if size == 0 { + size = 1 + } + txs[i] = GenerateRandomTxs(1, size)[0] + } + return txs +} + +func GenerateRandomTxs(count, size int) types.Txs { + txs := make(types.Txs, count) + for i := 0; i < count; i++ { + tx := make([]byte, size) + _, err := mrand.Read(tx) + if err != nil { + panic(err) + } + txs[i] = tx + } + return txs +} diff --git a/x/blob/types/builder_test.go b/x/blob/types/builder_test.go index 4949bfc0ce..970c42568f 100644 --- a/x/blob/types/builder_test.go +++ b/x/blob/types/builder_test.go @@ -5,8 +5,6 @@ import ( "testing" "github.com/celestiaorg/celestia-app/pkg/appconsts" - "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keyring" sdktypes "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" @@ -17,12 +15,12 @@ import ( ) func TestBuildWirePayForBlob(t *testing.T) { - testRing := generateKeyring(t) + testRing := GenerateKeyring(t) - info, err := testRing.Key(testAccName) + info, err := testRing.Key(TestAccName) require.NoError(t, err) - k := NewKeyringSigner(testRing, testAccName, "chain-id") + k := NewKeyringSigner(testRing, TestAccName, testChainID) require.NoError(t, err) namespace := []byte{1, 1, 1, 1, 1, 1, 1, 1} @@ -57,14 +55,14 @@ func TestBuildWirePayForBlob(t *testing.T) { } func TestBroadcastPayForBlob(t *testing.T) { - testRing := generateKeyring(t) - info, err := testRing.Key(testAccName) + testRing := GenerateKeyring(t) + info, err := testRing.Key(TestAccName) require.NoError(t, err) addr, err := info.GetAddress() require.NoError(t, err) t.Skipf("no local connection to app and no funds in wallet %s", addr) - k := NewKeyringSigner(testRing, testAccName, "test") + k := NewKeyringSigner(testRing, TestAccName, testChainID) RPCAddress := "127.0.0.1:9090" @@ -103,9 +101,7 @@ func TestBroadcastPayForBlob(t *testing.T) { func TestQueryAccountNumber(t *testing.T) { t.Skip("no local connection to app and no funds in wallet") - testRing := generateKeyring(t) - - k := NewKeyringSigner(testRing, testAccName, "test") + k := GenerateKeyringSigner(t, TestAccName) RPCAddress := "127.0.0.1:9090" @@ -114,35 +110,3 @@ func TestQueryAccountNumber(t *testing.T) { err = k.QueryAccountNumber(context.TODO(), rpcClient) require.NoError(t, err) } - -func generateKeyring(t *testing.T, accts ...string) keyring.Keyring { - t.Helper() - encCfg := makeBlobEncodingConfig() - kb := keyring.NewInMemory(encCfg.Codec) - - for _, acc := range accts { - _, _, err := kb.NewMnemonic(acc, keyring.English, "", "", hd.Secp256k1) - if err != nil { - t.Error(err) - } - } - - _, err := kb.NewAccount(testAccName, testMnemo, "1234", "", hd.Secp256k1) - if err != nil { - panic(err) - } - - return kb -} - -func generateKeyringSigner(t *testing.T, accts ...string) *KeyringSigner { - kr := generateKeyring(t, accts...) - return NewKeyringSigner(kr, testAccName, testChainID) -} - -const ( - // nolint:lll - testMnemo = `ramp soldier connect gadget domain mutual staff unusual first midnight iron good deputy wage vehicle mutual spike unlock rocket delay hundred script tumble choose` - testAccName = "test-account" - testChainID = "test-chain-1" -) diff --git a/x/blob/types/payforblob_test.go b/x/blob/types/payforblob_test.go index 7f5869a7d3..0e588e02ba 100644 --- a/x/blob/types/payforblob_test.go +++ b/x/blob/types/payforblob_test.go @@ -119,9 +119,7 @@ func TestSignMalleatedTxs(t *testing.T) { options []TxBuilderOption } - kb := generateKeyring(t, "test") - - signer := NewKeyringSigner(kb, "test", "test-chain-id") + signer := GenerateKeyringSigner(t, TestAccName) tests := []test{ { @@ -285,7 +283,7 @@ func validWirePayForBlob(t *testing.T) *MsgWirePayForBlob { panic(err) } - signer := generateKeyringSigner(t) + signer := GenerateKeyringSigner(t) err = msgWPFB.SignShareCommitment(signer) if err != nil { @@ -295,8 +293,7 @@ func validWirePayForBlob(t *testing.T) *MsgWirePayForBlob { } func validMsgPayForBlob(t *testing.T) *MsgPayForBlob { - kb := generateKeyring(t, "test") - signer := NewKeyringSigner(kb, "test", "chain-id") + signer := GenerateKeyringSigner(t, TestAccName) ns := []byte{1, 1, 1, 1, 1, 1, 1, 2} blob := bytes.Repeat([]byte{2}, totalBlobSize(appconsts.SparseShareContentSize*12)) diff --git a/x/blob/types/test_util.go b/x/blob/types/test_util.go new file mode 100644 index 0000000000..90d40a72f7 --- /dev/null +++ b/x/blob/types/test_util.go @@ -0,0 +1,40 @@ +package types + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" +) + +const ( + // nolint:lll + TestAccName = "test-account" + testMnemo = `ramp soldier connect gadget domain mutual staff unusual first midnight iron good deputy wage vehicle mutual spike unlock rocket delay hundred script tumble choose` + testChainID = "test-chain-1" +) + +func GenerateKeyring(t *testing.T, accts ...string) keyring.Keyring { + t.Helper() + encCfg := makeBlobEncodingConfig() + kb := keyring.NewInMemory(encCfg.Codec) + + for _, acc := range accts { + _, _, err := kb.NewMnemonic(acc, keyring.English, "", "", hd.Secp256k1) + if err != nil { + t.Error(err) + } + } + + _, err := kb.NewAccount(TestAccName, testMnemo, "1234", "", hd.Secp256k1) + if err != nil { + panic(err) + } + + return kb +} + +func GenerateKeyringSigner(t *testing.T, accts ...string) *KeyringSigner { + kr := GenerateKeyring(t, accts...) + return NewKeyringSigner(kr, TestAccName, testChainID) +} diff --git a/x/blob/types/wirepayfordata_test.go b/x/blob/types/wirepayfordata_test.go index a33c80f031..65aa6d4337 100644 --- a/x/blob/types/wirepayfordata_test.go +++ b/x/blob/types/wirepayfordata_test.go @@ -165,10 +165,7 @@ func TestProcessWirePayForBlob(t *testing.T) { return in } - kb := generateKeyring(t, "test") - - signer := NewKeyringSigner(kb, "test", "chain-id") - + signer := GenerateKeyringSigner(t, TestAccName) tests := []test{ { name: "single share square size 2",