Skip to content

Commit

Permalink
refactor: consolidate duplicate test methods (#1074)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
vidhanarya committed Nov 29, 2022
1 parent 2f89956 commit 3941003
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 534 deletions.
26 changes: 15 additions & 11 deletions app/estimate_square_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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{})
Expand Down
8 changes: 4 additions & 4 deletions app/test/fuzz_abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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{
Expand Down
57 changes: 6 additions & 51 deletions app/test/prepare_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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...)

Expand All @@ -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{
{
Expand Down Expand Up @@ -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
Expand All @@ -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},
Expand All @@ -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"
)
7 changes: 3 additions & 4 deletions app/test/process_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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{
Expand Down
Loading

0 comments on commit 3941003

Please sign in to comment.