2019-05-16 08:25:32 -07:00
|
|
|
package cli
|
2018-10-10 15:45:41 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-07 08:40:46 -07:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-10-10 15:45:41 -07:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-02-08 12:45:23 -08:00
|
|
|
"github.com/spf13/viper"
|
2018-10-10 15:45:41 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-07 08:40:46 -07:00
|
|
|
abci_server "github.com/tendermint/tendermint/abci/server"
|
|
|
|
tmcfg "github.com/tendermint/tendermint/config"
|
2019-02-08 12:45:23 -08:00
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
2018-10-10 15:45:41 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2019-04-04 07:36:39 -07:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-16 18:34:54 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-06-04 03:38:24 -07:00
|
|
|
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
2019-04-04 07:36:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
"github.com/cosmos/cosmos-sdk/server/mock"
|
2019-05-16 18:34:54 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/tests"
|
2019-05-16 08:25:32 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
2018-10-10 15:45:41 -07:00
|
|
|
)
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
var testMbm = module.NewBasicManager(genutil.AppModuleBasic{})
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
func createDefaultTendermintConfig(rootDir string) (*tmcfg.Config, error) {
|
|
|
|
conf := tmcfg.DefaultConfig()
|
|
|
|
conf.SetRoot(rootDir)
|
|
|
|
tmcfg.EnsureRoot(rootDir)
|
|
|
|
|
|
|
|
if err := conf.ValidateBasic(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error in config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
func TestInitCmd(t *testing.T) {
|
2019-05-16 18:34:54 -07:00
|
|
|
home, cleanup := tests.NewTestCaseDir(t)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(cleanup)
|
2018-10-10 15:45:41 -07:00
|
|
|
|
|
|
|
logger := log.NewNopLogger()
|
2020-07-07 08:40:46 -07:00
|
|
|
cfg, err := createDefaultTendermintConfig(home)
|
|
|
|
require.NoError(t, err)
|
2018-11-12 05:45:05 -08:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
serverCtx := server.NewContext(viper.New(), cfg, logger)
|
2020-07-07 10:20:09 -07:00
|
|
|
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec()).WithHomeDir(home)
|
2018-11-12 05:45:05 -08:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
|
|
|
|
|
|
|
|
cmd := InitCmd(testMbm, home)
|
|
|
|
cmd.SetArgs([]string{"appnode-test"})
|
|
|
|
|
|
|
|
require.NoError(t, cmd.ExecuteContext(ctx))
|
2018-10-10 15:45:41 -07:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:00:27 -07:00
|
|
|
func setupClientHome(t *testing.T) func() {
|
2019-05-16 18:34:54 -07:00
|
|
|
clientDir, cleanup := tests.NewTestCaseDir(t)
|
2020-07-08 00:02:04 -07:00
|
|
|
viper.Set(cli.HomeFlag, clientDir)
|
2019-05-16 18:34:54 -07:00
|
|
|
return cleanup
|
2018-10-19 11:00:27 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
func TestEmptyState(t *testing.T) {
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(setupClientHome(t))
|
2018-11-12 05:45:05 -08:00
|
|
|
|
2019-05-16 18:34:54 -07:00
|
|
|
home, cleanup := tests.NewTestCaseDir(t)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(cleanup)
|
2019-05-16 18:34:54 -07:00
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
logger := log.NewNopLogger()
|
2020-07-07 08:40:46 -07:00
|
|
|
cfg, err := createDefaultTendermintConfig(home)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serverCtx := server.NewContext(viper.New(), cfg, logger)
|
2020-07-07 10:20:09 -07:00
|
|
|
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec()).WithHomeDir(home)
|
2018-11-12 05:45:05 -08:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
|
2018-11-12 05:45:05 -08:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
cmd := InitCmd(testMbm, home)
|
|
|
|
cmd.SetArgs([]string{"appnode-test", fmt.Sprintf("--%s=%s", cli.HomeFlag, home)})
|
|
|
|
|
|
|
|
require.NoError(t, cmd.ExecuteContext(ctx))
|
2018-10-10 15:45:41 -07:00
|
|
|
|
|
|
|
old := os.Stdout
|
|
|
|
r, w, _ := os.Pipe()
|
|
|
|
os.Stdout = w
|
2018-11-12 05:45:05 -08:00
|
|
|
|
2020-07-07 08:40:46 -07:00
|
|
|
cmd = server.ExportCmd(nil)
|
|
|
|
cmd.SetArgs([]string{fmt.Sprintf("--%s=%s", cli.HomeFlag, home)})
|
|
|
|
require.NoError(t, cmd.ExecuteContext(ctx))
|
2018-10-10 15:45:41 -07:00
|
|
|
|
|
|
|
outC := make(chan string)
|
|
|
|
go func() {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, r)
|
|
|
|
outC <- buf.String()
|
|
|
|
}()
|
|
|
|
|
|
|
|
w.Close()
|
|
|
|
os.Stdout = old
|
|
|
|
out := <-outC
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
require.Contains(t, out, "genesis_time")
|
|
|
|
require.Contains(t, out, "chain_id")
|
|
|
|
require.Contains(t, out, "consensus_params")
|
|
|
|
require.Contains(t, out, "app_hash")
|
2019-05-16 08:25:32 -07:00
|
|
|
require.Contains(t, out, "app_state")
|
2018-10-10 15:45:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartStandAlone(t *testing.T) {
|
2019-05-16 18:34:54 -07:00
|
|
|
home, cleanup := tests.NewTestCaseDir(t)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(cleanup)
|
|
|
|
t.Cleanup(setupClientHome(t))
|
2018-10-10 15:45:41 -07:00
|
|
|
|
|
|
|
logger := log.NewNopLogger()
|
2020-07-07 08:40:46 -07:00
|
|
|
cfg, err := createDefaultTendermintConfig(home)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serverCtx := server.NewContext(viper.New(), cfg, logger)
|
2020-07-07 10:20:09 -07:00
|
|
|
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec()).WithHomeDir(home)
|
2020-07-07 08:40:46 -07:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
|
|
|
|
|
|
|
|
cmd := InitCmd(testMbm, home)
|
2020-07-07 10:20:09 -07:00
|
|
|
cmd.SetArgs([]string{"appnode-test"})
|
2020-07-07 08:40:46 -07:00
|
|
|
|
|
|
|
require.NoError(t, cmd.ExecuteContext(ctx))
|
2018-10-10 15:45:41 -07:00
|
|
|
|
|
|
|
app, err := mock.NewApp(home, logger)
|
2020-07-07 08:40:46 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
svrAddr, _, err := server.FreeTCPAddr()
|
2020-07-07 08:40:46 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
svr, err := abci_server.NewServer(svrAddr, "socket", app)
|
|
|
|
require.NoError(t, err, "error creating listener")
|
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
svr.SetLogger(logger.With("module", "abci-server"))
|
2020-03-20 12:24:18 -07:00
|
|
|
err = svr.Start()
|
|
|
|
require.NoError(t, err)
|
2018-10-10 15:45:41 -07:00
|
|
|
|
|
|
|
timer := time.NewTimer(time.Duration(2) * time.Second)
|
2019-09-16 10:11:17 -07:00
|
|
|
for range timer.C {
|
2020-03-20 12:24:18 -07:00
|
|
|
err = svr.Stop()
|
|
|
|
require.NoError(t, err)
|
2019-09-16 10:11:17 -07:00
|
|
|
break
|
2018-10-10 15:45:41 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 11:00:27 -07:00
|
|
|
|
|
|
|
func TestInitNodeValidatorFiles(t *testing.T) {
|
2019-05-16 18:34:54 -07:00
|
|
|
home, cleanup := tests.NewTestCaseDir(t)
|
2020-07-07 11:19:32 -07:00
|
|
|
cfg, err := createDefaultTendermintConfig(home)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(cleanup)
|
2019-05-16 08:25:32 -07:00
|
|
|
nodeID, valPubKey, err := genutil.InitializeNodeValidatorFiles(cfg)
|
2018-10-19 11:00:27 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEqual(t, "", nodeID)
|
|
|
|
require.NotEqual(t, 0, len(valPubKey.Bytes()))
|
|
|
|
}
|
2019-05-16 18:34:54 -07:00
|
|
|
|
|
|
|
// custom tx codec
|
|
|
|
func makeCodec() *codec.Codec {
|
|
|
|
var cdc = codec.New()
|
|
|
|
sdk.RegisterCodec(cdc)
|
2020-06-04 03:38:24 -07:00
|
|
|
cryptocodec.RegisterCrypto(cdc)
|
2019-05-16 18:34:54 -07:00
|
|
|
return cdc
|
|
|
|
}
|