Skip to content

Commit

Permalink
chore: remove old signer and refactor tests (#2314)
Browse files Browse the repository at this point in the history
This PR consolidates a lot of our testing infrastructure using the new signer instead of the old
  • Loading branch information
cmwaters authored Aug 23, 2023
1 parent c63bc2a commit 0824022
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 39 deletions.
59 changes: 36 additions & 23 deletions pkg/square/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -49,22 +47,23 @@ 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())
})
}
}

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...),
Expand All @@ -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...),
Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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...)
Expand Down
13 changes: 10 additions & 3 deletions pkg/square/square_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pkg/square/square_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 22 additions & 12 deletions pkg/square/square_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)),
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -189,15 +193,19 @@ 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())
require.NoError(t, err)
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())
Expand All @@ -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)

Expand Down

0 comments on commit 0824022

Please sign in to comment.