diff --git a/app/test/abci_test.go b/app/test/abci_test.go new file mode 100644 index 0000000000..c535682daf --- /dev/null +++ b/app/test/abci_test.go @@ -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" +) diff --git a/app/abci_test.go b/testutil/test_app.go similarity index 54% rename from app/abci_test.go rename to testutil/test_app.go index 2a844a0102..40b4bac83a 100644 --- a/app/abci_test.go +++ b/testutil/test_app.go @@ -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{} @@ -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)), @@ -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) } @@ -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()} @@ -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() @@ -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 ( @@ -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) }