From 0824022ff1e16ec50e8bcaf1185518b90ab0bf32 Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Wed, 23 Aug 2023 16:12:45 +0200 Subject: [PATCH] chore: remove old signer and refactor tests (#2314) This PR consolidates a lot of our testing infrastructure using the new signer instead of the old --- pkg/square/builder_test.go | 59 ++++++++++++++++++----------- pkg/square/square_benchmark_test.go | 13 +++++-- pkg/square/square_fuzz_test.go | 5 ++- pkg/square/square_test.go | 34 +++++++++++------ 4 files changed, 72 insertions(+), 39 deletions(-) diff --git a/pkg/square/builder_test.go b/pkg/square/builder_test.go index 705a100..704ba00 100644 --- a/pkg/square/builder_test.go +++ b/pkg/square/builder_test.go @@ -7,18 +7,16 @@ import ( "github.com/stretchr/testify/assert" - apptypes "github.com/celestiaorg/celestia-app/x/blob/types" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/celestiaorg/celestia-app/app" "github.com/celestiaorg/celestia-app/app/encoding" "github.com/celestiaorg/celestia-app/pkg/appconsts" ns "github.com/celestiaorg/celestia-app/pkg/namespace" "github.com/celestiaorg/celestia-app/pkg/shares" "github.com/celestiaorg/celestia-app/pkg/square" + "github.com/celestiaorg/celestia-app/pkg/user" "github.com/celestiaorg/celestia-app/test/util/blobfactory" "github.com/celestiaorg/celestia-app/test/util/testfactory" + "github.com/celestiaorg/celestia-app/test/util/testnode" "github.com/stretchr/testify/require" tmrand "github.com/tendermint/tendermint/libs/rand" coretypes "github.com/tendermint/tendermint/types" @@ -49,7 +47,9 @@ func TestBuilderSquareSizeEstimation(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { rand := tmrand.NewRand() - txs := generateMixedTxs(rand, tt.normalTxs, tt.pfbCount, 1, tt.pfbSize) + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + txs := generateMixedTxs(signer, rand, tt.normalTxs, tt.pfbCount, 1, tt.pfbSize) square, _, err := square.Build(txs, appconsts.LatestVersion, appconsts.DefaultGovMaxSquareSize) require.NoError(t, err) require.EqualValues(t, tt.expectedSquareSize, square.Size()) @@ -57,14 +57,13 @@ func TestBuilderSquareSizeEstimation(t *testing.T) { } } -func generateMixedTxs(rand *tmrand.Rand, normalTxCount, pfbCount, blobsPerPfb, blobSize int) [][]byte { - return shuffle(rand, generateOrderedTxs(rand, normalTxCount, pfbCount, blobsPerPfb, blobSize)) +func generateMixedTxs(signer *user.Signer, rand *tmrand.Rand, normalTxCount, pfbCount, blobsPerPfb, blobSize int) [][]byte { + return shuffle(rand, generateOrderedTxs(signer, rand, normalTxCount, pfbCount, blobsPerPfb, blobSize)) } -func generateOrderedTxs(rand *tmrand.Rand, normalTxCount, pfbCount, blobsPerPfb, blobSize int) [][]byte { - encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) - pfbTxs := blobfactory.RandBlobTxs(encCfg.TxConfig.TxEncoder(), rand, pfbCount, blobsPerPfb, blobSize) - normieTxs := blobfactory.GenerateManyRawSendTxs(encCfg.TxConfig, normalTxCount) +func generateOrderedTxs(signer *user.Signer, rand *tmrand.Rand, normalTxCount, pfbCount, blobsPerPfb, blobSize int) [][]byte { + pfbTxs := blobfactory.RandBlobTxs(signer, rand, pfbCount, blobsPerPfb, blobSize) + normieTxs := blobfactory.GenerateManyRawSendTxs(signer, normalTxCount) txs := append(append( make([]coretypes.Tx, 0, len(pfbTxs)+len(normieTxs)), normieTxs...), @@ -74,10 +73,9 @@ func generateOrderedTxs(rand *tmrand.Rand, normalTxCount, pfbCount, blobsPerPfb, } // GenerateOrderedRandomTxs generates normalTxCount random Send transactions and pfbCount random MultiBlob transactions. -func GenerateOrderedRandomTxs(t *testing.T, txConfig client.TxConfig, rand *tmrand.Rand, normalTxCount, pfbCount int) [][]byte { - signer := apptypes.GenerateKeyringSigner(t) - noramlTxs := blobfactory.GenerateManyRandomRawSendTxsSameSigner(txConfig, rand, signer, normalTxCount) - pfbTxs := blobfactory.RandMultiBlobTxsSameSigner(t, txConfig.TxEncoder(), rand, signer, pfbCount) +func GenerateOrderedRandomTxs(t *testing.T, signer *user.Signer, rand *tmrand.Rand, normalTxCount, pfbCount int) [][]byte { + noramlTxs := blobfactory.GenerateManyRandomRawSendTxsSameSigner(rand, signer, normalTxCount) + pfbTxs := blobfactory.RandMultiBlobTxsSameSigner(t, rand, signer, pfbCount) txs := append(append( make([]coretypes.Tx, 0, len(pfbTxs)+len(noramlTxs)), noramlTxs...), @@ -92,19 +90,26 @@ func TestGenerateOrderedRandomTxs_Deterministic(t *testing.T) { noramlCount := 10 encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) + kr := testfactory.TestKeyring(encCfg.Codec) + signer, err := user.NewSigner(kr, nil, testnode.TestAddress(), encCfg.TxConfig, testfactory.ChainID, 1, 0) + require.NoError(t, err) + rand1 := tmrand.NewRand() rand1.Seed(1) - set1 := GenerateOrderedRandomTxs(t, encCfg.TxConfig, rand1, noramlCount, pfbCount) + set1 := GenerateOrderedRandomTxs(t, signer, rand1, noramlCount, pfbCount) + + signer, err = user.NewSigner(kr, nil, testnode.TestAddress(), encCfg.TxConfig, testfactory.ChainID, 1, 0) + require.NoError(t, err) rand2 := tmrand.NewRand() rand2.Seed(1) - set2 := GenerateOrderedRandomTxs(t, encCfg.TxConfig, rand2, noramlCount, pfbCount) + set2 := GenerateOrderedRandomTxs(t, signer, rand2, noramlCount, pfbCount) assert.Equal(t, set2, set1) } -func GenerateMixedRandomTxs(t *testing.T, txConfig client.TxConfig, rand *tmrand.Rand, normalTxCount, pfbCount int) [][]byte { - return shuffle(rand, GenerateOrderedRandomTxs(t, txConfig, rand, normalTxCount, pfbCount)) +func GenerateMixedRandomTxs(t *testing.T, signer *user.Signer, rand *tmrand.Rand, normalTxCount, pfbCount int) [][]byte { + return shuffle(rand, GenerateOrderedRandomTxs(t, signer, rand, normalTxCount, pfbCount)) } // TestGenerateMixedRandomTxs_Deterministic ensures that the same seed produces the same txs @@ -113,13 +118,20 @@ func TestGenerateMixedRandomTxs_Deterministic(t *testing.T) { noramlCount := 10 encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) + kr := testfactory.TestKeyring(encCfg.Codec) + signer, err := user.NewSigner(kr, nil, testnode.TestAddress(), encCfg.TxConfig, testfactory.ChainID, 1, 0) + require.NoError(t, err) + rand1 := tmrand.NewRand() rand1.Seed(1) - set1 := GenerateMixedRandomTxs(t, encCfg.TxConfig, rand1, noramlCount, pfbCount) + set1 := GenerateMixedRandomTxs(t, signer, rand1, noramlCount, pfbCount) + + signer, err = user.NewSigner(kr, nil, testnode.TestAddress(), encCfg.TxConfig, testfactory.ChainID, 1, 0) + require.NoError(t, err) rand2 := tmrand.NewRand() rand2.Seed(1) - set2 := GenerateMixedRandomTxs(t, encCfg.TxConfig, rand2, noramlCount, pfbCount) + set2 := GenerateMixedRandomTxs(t, signer, rand2, noramlCount, pfbCount) assert.Equal(t, set2, set1) } @@ -200,9 +212,10 @@ func newTx(len int) []byte { } func TestBuilderFindTxShareRange(t *testing.T) { + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) blockTxs := testfactory.GenerateRandomTxs(5, 900).ToSliceOfBytes() - encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) - blockTxs = append(blockTxs, blobfactory.RandBlobTxsRandomlySized(encCfg.TxConfig.TxEncoder(), tmrand.NewRand(), 5, 1000, 10).ToSliceOfBytes()...) + blockTxs = append(blockTxs, blobfactory.RandBlobTxsRandomlySized(signer, tmrand.NewRand(), 5, 1000, 10).ToSliceOfBytes()...) require.Len(t, blockTxs, 10) builder, err := square.NewBuilder(appconsts.DefaultSquareSizeUpperBound, appconsts.LatestVersion, blockTxs...) diff --git a/pkg/square/square_benchmark_test.go b/pkg/square/square_benchmark_test.go index 327b171..387dd96 100644 --- a/pkg/square/square_benchmark_test.go +++ b/pkg/square/square_benchmark_test.go @@ -8,13 +8,16 @@ import ( "github.com/celestiaorg/celestia-app/pkg/appconsts" "github.com/celestiaorg/celestia-app/pkg/square" + "github.com/celestiaorg/celestia-app/test/util/testnode" "github.com/stretchr/testify/require" ) func BenchmarkSquareConstruct(b *testing.B) { for _, txCount := range []int{10, 100, 1000} { b.Run(fmt.Sprintf("txCount=%d", txCount), func(b *testing.B) { - txs := generateOrderedTxs(tmrand.NewRand(), txCount/2, txCount/2, 1, 1024) + signer, err := testnode.NewOfflineSigner() + require.NoError(b, err) + txs := generateOrderedTxs(signer, tmrand.NewRand(), txCount/2, txCount/2, 1, 1024) b.ResetTimer() for i := 0; i < b.N; i++ { _, err := square.Construct(txs, appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) @@ -27,7 +30,9 @@ func BenchmarkSquareConstruct(b *testing.B) { func BenchmarkSquareBuild(b *testing.B) { for _, txCount := range []int{10, 100, 1000, 10000} { b.Run(fmt.Sprintf("txCount=%d", txCount), func(b *testing.B) { - txs := generateMixedTxs(tmrand.NewRand(), txCount/2, txCount/2, 1, 1024) + signer, err := testnode.NewOfflineSigner() + require.NoError(b, err) + txs := generateMixedTxs(signer, tmrand.NewRand(), txCount/2, txCount/2, 1, 1024) b.ResetTimer() for i := 0; i < b.N; i++ { _, _, err := square.Build(txs, appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) @@ -38,7 +43,9 @@ func BenchmarkSquareBuild(b *testing.B) { const txCount = 10 for _, blobSize := range []int{10, 100, 1000, 10000} { b.Run(fmt.Sprintf("blobSize=%d", blobSize), func(b *testing.B) { - txs := generateMixedTxs(tmrand.NewRand(), 0, txCount, 1, blobSize) + signer, err := testnode.NewOfflineSigner() + require.NoError(b, err) + txs := generateMixedTxs(signer, tmrand.NewRand(), 0, txCount, 1, blobSize) b.ResetTimer() for i := 0; i < b.N; i++ { _, _, err := square.Build(txs, appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) diff --git a/pkg/square/square_fuzz_test.go b/pkg/square/square_fuzz_test.go index 07056c8..4ff7fc4 100644 --- a/pkg/square/square_fuzz_test.go +++ b/pkg/square/square_fuzz_test.go @@ -10,6 +10,7 @@ import ( "github.com/celestiaorg/celestia-app/pkg/inclusion" "github.com/celestiaorg/celestia-app/pkg/shares" "github.com/celestiaorg/celestia-app/pkg/square" + "github.com/celestiaorg/celestia-app/test/util/testnode" blob "github.com/celestiaorg/celestia-app/x/blob/types" "github.com/celestiaorg/rsmt2d" "github.com/stretchr/testify/require" @@ -39,7 +40,9 @@ func FuzzSquare(f *testing.F) { encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) rand := tmrand.NewRand() rand.Seed(seed) - txs := GenerateMixedRandomTxs(t, encCfg.TxConfig, rand, normalTxCount, pfbCount) + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + txs := GenerateMixedRandomTxs(t, signer, rand, normalTxCount, pfbCount) s, orderedTxs, err := square.Build(txs, appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) require.NoError(t, err) diff --git a/pkg/square/square_test.go b/pkg/square/square_test.go index c5a7c70..5548c22 100644 --- a/pkg/square/square_test.go +++ b/pkg/square/square_test.go @@ -16,7 +16,7 @@ import ( "github.com/celestiaorg/celestia-app/pkg/shares" "github.com/celestiaorg/celestia-app/pkg/square" "github.com/celestiaorg/celestia-app/test/util/blobfactory" - "github.com/celestiaorg/celestia-app/test/util/testfactory" + "github.com/celestiaorg/celestia-app/test/util/testnode" blob "github.com/celestiaorg/celestia-app/x/blob/types" "github.com/celestiaorg/rsmt2d" "github.com/stretchr/testify/assert" @@ -26,9 +26,10 @@ import ( func TestSquareConstruction(t *testing.T) { rand := tmrand.NewRand() - encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) - sendTxs := blobfactory.GenerateManyRawSendTxs(encCfg.TxConfig, 250) - pfbTxs := blobfactory.RandBlobTxs(encCfg.TxConfig.TxEncoder(), rand, 10000, 1, 1024) + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + sendTxs := blobfactory.GenerateManyRawSendTxs(signer, 250) + pfbTxs := blobfactory.RandBlobTxs(signer, rand, 10000, 1, 1024) t.Run("normal transactions after PFB trasactions", func(t *testing.T) { txs := append(sendTxs[:5], append(pfbTxs, sendTxs[5:]...)...) _, err := square.Construct(coretypes.Txs(txs).ToSliceOfBytes(), appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) @@ -120,10 +121,10 @@ func TestSquareTxShareRange(t *testing.T) { func generateBlobTxsWithNamespaces(t *testing.T, namespaces []ns.Namespace, blobSizes [][]int) [][]byte { encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) const acc = "signer" - kr := testfactory.GenerateKeyring(acc) + kr, _ := testnode.NewKeyring(acc) return blobfactory.ManyMultiBlobTx( t, - encCfg.TxConfig.TxEncoder(), + encCfg.TxConfig, kr, "chainid", blobfactory.Repeat(acc, len(blobSizes)), @@ -134,9 +135,10 @@ func generateBlobTxsWithNamespaces(t *testing.T, namespaces []ns.Namespace, blob // The "_Flaky" suffix indicates that the test may fail non-deterministically especially when executed in CI. func TestSquareBlobShareRange_Flaky(t *testing.T) { - encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) rand := tmrand.NewRand() - txs := blobfactory.RandBlobTxsRandomlySized(encCfg.TxConfig.TxEncoder(), rand, 10, 1000, 10).ToSliceOfBytes() + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + txs := blobfactory.RandBlobTxsRandomlySized(signer, rand, 10, 1000, 10).ToSliceOfBytes() builder, err := square.NewBuilder(appconsts.DefaultSquareSizeUpperBound, appconsts.LatestVersion, txs...) require.NoError(t, err) @@ -178,7 +180,9 @@ func TestSquareDeconstruct(t *testing.T) { // 8192 -> square size 128 for _, numTxs := range []int{2, 128, 1024, 8192} { t.Run(fmt.Sprintf("%d", numTxs), func(t *testing.T) { - txs := generateOrderedTxs(rand, numTxs/2, numTxs/2, 1, 800) + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + txs := generateOrderedTxs(signer, rand, numTxs/2, numTxs/2, 1, 800) dataSquare, err := square.Construct(txs, appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) require.NoError(t, err) recomputedTxs, err := square.Deconstruct(dataSquare, encCfg.TxConfig.TxDecoder()) @@ -189,7 +193,9 @@ func TestSquareDeconstruct(t *testing.T) { }) t.Run("NoPFBs", func(t *testing.T) { const numTxs = 10 - txs := coretypes.Txs(blobfactory.GenerateManyRawSendTxs(encCfg.TxConfig, numTxs)).ToSliceOfBytes() + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + txs := coretypes.Txs(blobfactory.GenerateManyRawSendTxs(signer, numTxs)).ToSliceOfBytes() dataSquare, err := square.Construct(txs, appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) require.NoError(t, err) recomputedTxs, err := square.Deconstruct(dataSquare, encCfg.TxConfig.TxDecoder()) @@ -197,7 +203,9 @@ func TestSquareDeconstruct(t *testing.T) { require.Equal(t, txs, recomputedTxs.ToSliceOfBytes()) }) t.Run("PFBsOnly", func(t *testing.T) { - txs := blobfactory.RandBlobTxs(encCfg.TxConfig.TxEncoder(), rand, 100, 1, 1024).ToSliceOfBytes() + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + txs := blobfactory.RandBlobTxs(signer, rand, 100, 1, 1024).ToSliceOfBytes() dataSquare, err := square.Construct(txs, appconsts.LatestVersion, appconsts.DefaultSquareSizeUpperBound) require.NoError(t, err) recomputedTxs, err := square.Deconstruct(dataSquare, encCfg.TxConfig.TxDecoder()) @@ -214,7 +222,9 @@ func TestSquareDeconstruct(t *testing.T) { func TestSquareShareCommitments(t *testing.T) { const numTxs = 10 rand := tmrand.NewRand() - txs := generateOrderedTxs(rand, numTxs, numTxs, 3, 800) + signer, err := testnode.NewOfflineSigner() + require.NoError(t, err) + txs := generateOrderedTxs(signer, rand, numTxs, numTxs, 3, 800) builder, err := square.NewBuilder(appconsts.DefaultSquareSizeUpperBound, appconsts.LatestVersion, txs...) require.NoError(t, err)