Skip to content

Commit

Permalink
Refactor to export testutils (#207)
Browse files Browse the repository at this point in the history
* refactor to export testutils

* linter
  • Loading branch information
evan-forbes authored Feb 13, 2022
1 parent 63519ec commit ad9e077
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 122 deletions.
117 changes: 117 additions & 0 deletions app/test/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package app_test

import (
"bytes"
"testing"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/testutil"
"github.com/celestiaorg/celestia-app/x/payment/types"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/spm/cosmoscmd"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/pkg/consts"
core "github.com/tendermint/tendermint/proto/tendermint/types"
)

func TestPreprocessTxs(t *testing.T) {
signer := testutil.GenerateKeyringSigner(t, testAccName)
info := signer.GetSignerInfo()

encCfg := cosmoscmd.MakeEncodingConfig(app.ModuleBasics)

testApp := testutil.SetupTestApp(t, info.GetAddress())

type test struct {
input abci.RequestPreprocessTxs
expectedMessages []*core.Message
expectedTxs int
}

firstNS := []byte{2, 2, 2, 2, 2, 2, 2, 2}
firstMessage := bytes.Repeat([]byte{2}, 512)
firstRawTx := generateRawTx(t, encCfg.TxConfig, firstNS, firstMessage, signer)

secondNS := []byte{1, 1, 1, 1, 1, 1, 1, 1}
secondMessage := []byte{2}
secondRawTx := generateRawTx(t, encCfg.TxConfig, secondNS, secondMessage, signer)

thirdNS := []byte{3, 3, 3, 3, 3, 3, 3, 3}
thirdMessage := []byte{}
thirdRawTx := generateRawTx(t, encCfg.TxConfig, thirdNS, thirdMessage, signer)

tests := []test{
{
input: abci.RequestPreprocessTxs{
Txs: [][]byte{firstRawTx, secondRawTx, thirdRawTx},
},
expectedMessages: []*core.Message{
{
NamespaceId: secondNS, // the second message should be first
Data: append([]byte{2}, bytes.Repeat([]byte{0}, 255)...), // check that the message is padded
},
{
NamespaceId: firstNS,
Data: firstMessage,
},
{
NamespaceId: thirdNS,
Data: nil,
},
},
expectedTxs: 3,
},
}

for _, tt := range tests {
res := testApp.PreprocessTxs(tt.input)
assert.Equal(t, tt.expectedMessages, res.Messages.MessagesList)
assert.Equal(t, tt.expectedTxs, len(res.Txs))
}
}

func generateRawTx(t *testing.T, txConfig client.TxConfig, ns, message []byte, signer *types.KeyringSigner) (rawTx []byte) {
// create a msg
msg := generateSignedWirePayForMessage(t, consts.MaxSquareSize, ns, message, signer)

builder := signer.NewTxBuilder()

coin := sdk.Coin{
Denom: "token",
Amount: sdk.NewInt(1000),
}

builder.SetFeeAmount(sdk.NewCoins(coin))
builder.SetGasLimit(10000)
builder.SetTimeoutHeight(99)

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 generateSignedWirePayForMessage(t *testing.T, k uint64, ns, message []byte, signer *types.KeyringSigner) *types.MsgWirePayForMessage {
msg, err := types.NewWirePayForMessage(ns, message, k)
if err != nil {
t.Error(err)
}

err = msg.SignShareCommitments(signer)
if err != nil {
t.Error(err)
}

return msg
}

const (
testAccName = "test-account"
)
141 changes: 19 additions & 122 deletions app/abci_test.go → testutil/test_app.go
Original file line number Diff line number Diff line change
@@ -1,101 +1,38 @@
package app
package testutil

import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/x/payment/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/spm/cosmoscmd"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/pkg/consts"
core "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
)

const testingKeyAcc = "test"

// Get flags every time the simulator is run
func init() {
simapp.GetSimulatorFlags()
}

func TestPreprocessTxs(t *testing.T) {
kb := keyring.NewInMemory()
info, _, err := kb.NewMnemonic(testingKeyAcc, keyring.English, "", "", hd.Secp256k1)
if err != nil {
t.Error(err)
}

testApp := setupApp(t, info.GetPubKey())

type test struct {
input abci.RequestPreprocessTxs
expectedMessages []*core.Message
expectedTxs int
}

firstNS := []byte{2, 2, 2, 2, 2, 2, 2, 2}
firstMessage := bytes.Repeat([]byte{2}, 512)
firstRawTx := generateRawTx(t, testApp.txConfig, firstNS, firstMessage, kb)

secondNS := []byte{1, 1, 1, 1, 1, 1, 1, 1}
secondMessage := []byte{2}
secondRawTx := generateRawTx(t, testApp.txConfig, secondNS, secondMessage, kb)

thirdNS := []byte{3, 3, 3, 3, 3, 3, 3, 3}
thirdMessage := []byte{}
thirdRawTx := generateRawTx(t, testApp.txConfig, thirdNS, thirdMessage, kb)

tests := []test{
{
input: abci.RequestPreprocessTxs{
Txs: [][]byte{firstRawTx, secondRawTx, thirdRawTx},
},
expectedMessages: []*core.Message{
{
NamespaceId: secondNS, // the second message should be first
Data: append([]byte{2}, bytes.Repeat([]byte{0}, 255)...), // check that the message is padded
},
{
NamespaceId: firstNS,
Data: firstMessage,
},
{
NamespaceId: thirdNS,
Data: nil,
},
},
expectedTxs: 3,
},
}

for _, tt := range tests {
res := testApp.PreprocessTxs(tt.input)
assert.Equal(t, tt.expectedMessages, res.Messages.MessagesList)
assert.Equal(t, tt.expectedTxs, len(res.Txs))
}
}

func setupApp(t *testing.T, pub cryptotypes.PubKey) *App {
// SetupTestApp initializes a celestia-app application with a funded account
func SetupTestApp(t *testing.T, addr sdk.AccAddress) *app.App {
// var cache sdk.MultiStorePersistentCache
// EmptyAppOptions is a stub implementing AppOptions
emptyOpts := emptyAppOptions{}
Expand All @@ -105,9 +42,9 @@ func setupApp(t *testing.T, pub cryptotypes.PubKey) *App {

skipUpgradeHeights := make(map[int64]bool)

encCfg := cosmoscmd.MakeEncodingConfig(ModuleBasics)
encCfg := cosmoscmd.MakeEncodingConfig(app.ModuleBasics)

testApp := New(
testApp := app.New(
logger, db, nil, true, skipUpgradeHeights,
cast.ToString(emptyOpts.Get(flags.FlagHome)),
cast.ToUint(emptyOpts.Get(server.FlagInvCheckPeriod)),
Expand All @@ -116,9 +53,9 @@ func setupApp(t *testing.T, pub cryptotypes.PubKey) *App {
anteOpt,
)

genesisState := newDefaultGenesisState(encCfg.Marshaler)
genesisState := NewDefaultGenesisState(encCfg.Marshaler)

genesisState, err := addGenesisAccount(sdk.AccAddress(pub.Address().Bytes()), genesisState, encCfg.Marshaler)
genesisState, err := AddGenesisAccount(addr, genesisState, encCfg.Marshaler)
if err != nil {
t.Error(err)
}
Expand All @@ -144,16 +81,16 @@ func (ao emptyAppOptions) Get(o string) interface{} {
return nil
}

// addGenesisAccount mimics the cli addGenesisAccount command, providing an
// AddGenesisAccount mimics the cli addGenesisAccount command, providing an
// account with an allocation of to "token" and "celes" tokens in the genesis
// state
func addGenesisAccount(addr sdk.AccAddress, appState map[string]json.RawMessage, cdc codec.Codec) (map[string]json.RawMessage, error) {
func AddGenesisAccount(addr sdk.AccAddress, appState map[string]json.RawMessage, cdc codec.Codec) (map[string]json.RawMessage, error) {
// create concrete account type based on input parameters
var genAccount authtypes.GenesisAccount

coins := sdk.Coins{
sdk.NewCoin("token", sdk.NewInt(1000000)),
sdk.NewCoin(BondDenom, sdk.NewInt(1000000)),
sdk.NewCoin(app.BondDenom, sdk.NewInt(1000000)),
}

balances := banktypes.Balance{Address: addr.String(), Coins: coins.Sort()}
Expand Down Expand Up @@ -207,48 +144,6 @@ func addGenesisAccount(addr sdk.AccAddress, appState map[string]json.RawMessage,
return appState, nil
}

func generateRawTx(t *testing.T, txConfig client.TxConfig, ns, message []byte, ring keyring.Keyring) (rawTx []byte) {
// create a msg
msg := generateSignedWirePayForMessage(t, consts.MaxSquareSize, ns, message, ring)

krs := generateKeyringSigner(t, "test")
builder := krs.NewTxBuilder()

coin := sdk.Coin{
Denom: "token",
Amount: sdk.NewInt(1000),
}

builder.SetFeeAmount(sdk.NewCoins(coin))
builder.SetGasLimit(10000)
builder.SetTimeoutHeight(99)

tx, err := krs.BuildSignedTx(builder, msg)
require.NoError(t, err)

// encode the tx
rawTx, err = txConfig.TxEncoder()(tx)
require.NoError(t, err)

return rawTx
}

func generateSignedWirePayForMessage(t *testing.T, k uint64, ns, message []byte, ring keyring.Keyring) *types.MsgWirePayForMessage {
signer := generateKeyringSigner(t, "test")

msg, err := types.NewWirePayForMessage(ns, message, k)
if err != nil {
t.Error(err)
}

err = msg.SignShareCommitments(signer)
if err != nil {
t.Error(err)
}

return msg
}

func generateKeyring(t *testing.T, accts ...string) keyring.Keyring {
t.Helper()
kb := keyring.NewInMemory()
Expand All @@ -268,9 +163,11 @@ func generateKeyring(t *testing.T, accts ...string) keyring.Keyring {
return kb
}

func generateKeyringSigner(t *testing.T, accts ...string) *types.KeyringSigner {
kr := generateKeyring(t, accts...)
return types.NewKeyringSigner(kr, testAccName, testChainID)
// GenerateKeyringSigner creates a types.KeyringSigner with keys generated for
// the provided accounts
func GenerateKeyringSigner(t *testing.T, acct string) *types.KeyringSigner {
kr := generateKeyring(t)
return types.NewKeyringSigner(kr, acct, testChainID)
}

const (
Expand All @@ -280,7 +177,7 @@ const (
testChainID = "test-chain-1"
)

// newDefaultGenesisState generates the default state for the application.
func newDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
return ModuleBasics.DefaultGenesis(cdc)
// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState(cdc codec.JSONCodec) app.GenesisState {
return app.ModuleBasics.DefaultGenesis(cdc)
}

0 comments on commit ad9e077

Please sign in to comment.