Skip to content

Commit

Permalink
Server/simd: Viper Removal (#6599)
Browse files Browse the repository at this point in the history
* init commit

* remove viper from tm cmds

* updates

* Undo x/bank/client/cli/tx.go

* Fix unit tests

* lint++

* rename var

* Fix genutil test

* fix test

* prefer cmd.Flags() over direct viper usage

* update

* fix ABCI error tests

* fix integration tests

* Add viper to context

* fix build

* fix unit test

* Implement and use AppOptions

* Revert Redact godoc

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
  • Loading branch information
alexanderbez and Alessio Treglia committed Jul 5, 2020
1 parent b832e10 commit 9ebda4e
Show file tree
Hide file tree
Showing 24 changed files with 319 additions and 357 deletions.
8 changes: 4 additions & 4 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {

tx, err := app.txDecoder(req.Tx)
if err != nil {
return sdkerrors.ResponseCheckTx(err, 0, 0)
return sdkerrors.ResponseCheckTx(err, 0, 0, app.trace)
}

var mode runTxMode
Expand All @@ -190,7 +190,7 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {

gInfo, result, err := app.runTx(mode, req.Tx, tx)
if err != nil {
return sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed)
return sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace)
}

return abci.ResponseCheckTx{
Expand All @@ -212,7 +212,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx

tx, err := app.txDecoder(req.Tx)
if err != nil {
return sdkerrors.ResponseDeliverTx(err, 0, 0)
return sdkerrors.ResponseDeliverTx(err, 0, 0, app.trace)
}

gInfo := sdk.GasInfo{}
Expand All @@ -228,7 +228,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx
gInfo, result, err := app.runTx(runTxModeDeliver, req.Tx, tx)
if err != nil {
resultStr = "failed"
return sdkerrors.ResponseDeliverTx(err, gInfo.GasWanted, gInfo.GasUsed)
return sdkerrors.ResponseDeliverTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace)
}

return abci.ResponseDeliverTx{
Expand Down
7 changes: 7 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ type BaseApp struct { // nolint: maligned

// recovery handler for app.runTx method
runTxRecoveryMiddleware recoveryMiddleware

// trace set will return full stack traces for errors in ABCI Log field
trace bool
}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand Down Expand Up @@ -276,6 +279,10 @@ func (app *BaseApp) setInterBlockCache(cache sdk.MultiStorePersistentCache) {
app.interBlockCache = cache
}

func (app *BaseApp) setTrace(trace bool) {
app.trace = trace
}

// Router returns the router of the BaseApp.
func (app *BaseApp) Router() sdk.Router {
if app.sealed {
Expand Down
5 changes: 5 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func SetHaltTime(haltTime uint64) func(*BaseApp) {
return func(bap *BaseApp) { bap.setHaltTime(haltTime) }
}

// SetTrace will turn on or off trace flag
func SetTrace(trace bool) func(*BaseApp) {
return func(app *BaseApp) { app.setTrace(trace) }
}

// SetInterBlockCache provides a BaseApp option function that sets the
// inter-block cache.
func SetInterBlockCache(cache sdk.MultiStorePersistentCache) func(*BaseApp) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/rakyll/statik v0.1.7
github.com/regen-network/cosmos-proto v0.3.0
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.0
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
Expand Down
46 changes: 23 additions & 23 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func DefaultConfig() *Config {
}

// GetConfig returns a fully parsed Config object.
func GetConfig() Config {
globalLabelsRaw := viper.Get("telemetry.global-labels").([]interface{})
func GetConfig(v *viper.Viper) Config {
globalLabelsRaw := v.Get("telemetry.global-labels").([]interface{})
globalLabels := make([][]string, 0, len(globalLabelsRaw))
for _, glr := range globalLabelsRaw {
labelsRaw := glr.([]interface{})
Expand All @@ -150,32 +150,32 @@ func GetConfig() Config {

return Config{
BaseConfig: BaseConfig{
MinGasPrices: viper.GetString("minimum-gas-prices"),
InterBlockCache: viper.GetBool("inter-block-cache"),
Pruning: viper.GetString("pruning"),
PruningKeepRecent: viper.GetString("pruning-keep-recent"),
PruningKeepEvery: viper.GetString("pruning-keep-every"),
PruningInterval: viper.GetString("pruning-interval"),
HaltHeight: viper.GetUint64("halt-height"),
HaltTime: viper.GetUint64("halt-time"),
MinGasPrices: v.GetString("minimum-gas-prices"),
InterBlockCache: v.GetBool("inter-block-cache"),
Pruning: v.GetString("pruning"),
PruningKeepRecent: v.GetString("pruning-keep-recent"),
PruningKeepEvery: v.GetString("pruning-keep-every"),
PruningInterval: v.GetString("pruning-interval"),
HaltHeight: v.GetUint64("halt-height"),
HaltTime: v.GetUint64("halt-time"),
},
Telemetry: telemetry.Config{
ServiceName: viper.GetString("telemetry.service-name"),
Enabled: viper.GetBool("telemetry.enabled"),
EnableHostname: viper.GetBool("telemetry.enable-hostname"),
EnableHostnameLabel: viper.GetBool("telemetry.enable-hostname-label"),
EnableServiceLabel: viper.GetBool("telemetry.enable-service-label"),
PrometheusRetentionTime: viper.GetInt64("telemetry.prometheus-retention-time"),
ServiceName: v.GetString("telemetry.service-name"),
Enabled: v.GetBool("telemetry.enabled"),
EnableHostname: v.GetBool("telemetry.enable-hostname"),
EnableHostnameLabel: v.GetBool("telemetry.enable-hostname-label"),
EnableServiceLabel: v.GetBool("telemetry.enable-service-label"),
PrometheusRetentionTime: v.GetInt64("telemetry.prometheus-retention-time"),
GlobalLabels: globalLabels,
},
API: APIConfig{
Enable: viper.GetBool("api.enable"),
Address: viper.GetString("api.address"),
MaxOpenConnections: viper.GetUint("api.max-open-connections"),
RPCReadTimeout: viper.GetUint("api.rpc-read-timeout"),
RPCWriteTimeout: viper.GetUint("api.rpc-write-timeout"),
RPCMaxBodyBytes: viper.GetUint("api.rpc-max-body-bytes"),
EnableUnsafeCORS: viper.GetBool("api.enabled-unsafe-cors"),
Enable: v.GetBool("api.enable"),
Address: v.GetString("api.address"),
MaxOpenConnections: v.GetUint("api.max-open-connections"),
RPCReadTimeout: v.GetUint("api.rpc-read-timeout"),
RPCWriteTimeout: v.GetUint("api.rpc-write-timeout"),
RPCMaxBodyBytes: v.GetUint("api.rpc-max-body-bytes"),
EnableUnsafeCORS: v.GetBool("api.enabled-unsafe-cors"),
},
}
}
4 changes: 2 additions & 2 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func init() {

// ParseConfig retrieves the default environment configuration for the
// application.
func ParseConfig() (*Config, error) {
func ParseConfig(v *viper.Viper) (*Config, error) {
conf := DefaultConfig()
err := viper.Unmarshal(conf)
err := v.Unmarshal(conf)

return conf, err
}
Expand Down
13 changes: 12 additions & 1 deletion server/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import (
)

type (
// AppOptions defines an interface that is passed into an application
// constructor, typically used to set BaseApp options that are either supplied
// via config file or through CLI arguments/flags. The underlying implementation
// is defined by the server package and is typically implemented via a Viper
// literal defined on the server Context. Note, casting Get calls may not yield
// the expected types and could result in type assertion errors. It is recommend
// to either use the cast package or perform manual conversion for safety.
AppOptions interface {
Get(string) interface{}
}

// Application defines an application interface that wraps abci.Application.
// The interface defines the necessary contracts to be implemented in order
// to fully bootstrap and start an application.
Expand All @@ -27,7 +38,7 @@ type (

// AppCreator is a function that allows us to lazily initialize an
// application using various configurations.
AppCreator func(log.Logger, dbm.DB, io.Writer) Application
AppCreator func(log.Logger, dbm.DB, io.Writer, AppOptions) Application

// AppExporter is a function that dumps all app state to
// JSON-serializable structure and returns the current validator set.
Expand Down
15 changes: 7 additions & 8 deletions server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -29,9 +28,9 @@ func ExportCmd(ctx *Context, cdc codec.JSONMarshaler, appExporter AppExporter) *
Short: "Export state to JSON",
RunE: func(cmd *cobra.Command, args []string) error {
config := ctx.Config
config.SetRoot(viper.GetString(flags.FlagHome))

traceWriterFile := viper.GetString(flagTraceStore)
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
config.SetRoot(homeDir)

db, err := openDB(config.RootDir)
if err != nil {
Expand All @@ -52,14 +51,15 @@ func ExportCmd(ctx *Context, cdc codec.JSONMarshaler, appExporter AppExporter) *
return nil
}

traceWriterFile, _ := cmd.Flags().GetString(flagTraceStore)
traceWriter, err := openTraceWriter(traceWriterFile)
if err != nil {
return err
}

height := viper.GetInt64(flagHeight)
forZeroHeight := viper.GetBool(flagForZeroHeight)
jailWhiteList := viper.GetStringSlice(flagJailWhitelist)
height, _ := cmd.Flags().GetInt64(flagHeight)
forZeroHeight, _ := cmd.Flags().GetBool(flagForZeroHeight)
jailWhiteList, _ := cmd.Flags().GetStringSlice(flagJailWhitelist)

appState, validators, cp, err := appExporter(ctx.Logger, db, traceWriter, height, forZeroHeight, jailWhiteList)
if err != nil {
Expand Down Expand Up @@ -98,11 +98,10 @@ func ExportCmd(ctx *Context, cdc codec.JSONMarshaler, appExporter AppExporter) *
},
}

cmd.Flags().String(flags.FlagHome, "", "The application home directory")
cmd.Flags().Int64(flagHeight, -1, "Export state from a particular height (-1 means latest height)")
cmd.Flags().Bool(flagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
cmd.Flags().StringSlice(flagJailWhitelist, []string{}, "List of validators to not jail state export")
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.OutOrStderr())

return cmd
}
11 changes: 3 additions & 8 deletions server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package server
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path"
"testing"

"github.com/stretchr/testify/require"

"github.com/spf13/viper"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
Expand Down Expand Up @@ -62,12 +61,8 @@ func TestExportCmd_ConsensusParams(t *testing.T) {

output := &bytes.Buffer{}
cmd.SetOut(output)

viper.Set(flags.FlagHome, tempDir)
err = cmd.RunE(cmd, []string{})
if err != nil {
t.Errorf("error: %s", err)
}
cmd.SetArgs([]string{fmt.Sprintf("--%s=%s", flags.FlagHome, tempDir)})
require.NoError(t, cmd.Execute())

var exportedGenDoc tmtypes.GenesisDoc
err = app.Codec().UnmarshalJSON(output.Bytes(), &exportedGenDoc)
Expand Down
11 changes: 6 additions & 5 deletions server/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/spf13/viper"
"github.com/spf13/cast"

"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -13,17 +13,18 @@ import (
// GetPruningOptionsFromFlags parses command flags and returns the correct
// PruningOptions. If a pruning strategy is provided, that will be parsed and
// returned, otherwise, it is assumed custom pruning options are provided.
func GetPruningOptionsFromFlags() (types.PruningOptions, error) {
strategy := strings.ToLower(viper.GetString(FlagPruning))
func GetPruningOptionsFromFlags(appOpts AppOptions) (types.PruningOptions, error) {
strategy := strings.ToLower(cast.ToString(appOpts.Get(FlagPruning)))

switch strategy {
case types.PruningOptionDefault, types.PruningOptionNothing, types.PruningOptionEverything:
return types.NewPruningOptionsFromString(strategy), nil

case types.PruningOptionCustom:
opts := types.NewPruningOptions(
viper.GetUint64(FlagPruningKeepRecent),
viper.GetUint64(FlagPruningKeepEvery), viper.GetUint64(FlagPruningInterval),
cast.ToUint64(appOpts.Get(FlagPruningKeepRecent)),
cast.ToUint64(appOpts.Get(FlagPruningKeepEvery)),
cast.ToUint64(appOpts.Get(FlagPruningInterval)),
)

if err := opts.Validate(); err != nil {
Expand Down
33 changes: 21 additions & 12 deletions server/pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,29 @@ import (
func TestGetPruningOptionsFromFlags(t *testing.T) {
tests := []struct {
name string
initParams func()
initParams func() *viper.Viper
expectedOptions types.PruningOptions
wantErr bool
}{
{
name: FlagPruning,
initParams: func() {
viper.Set(FlagPruning, types.PruningOptionNothing)
initParams: func() *viper.Viper {
v := viper.New()
v.Set(FlagPruning, types.PruningOptionNothing)
return v
},
expectedOptions: types.PruneNothing,
},
{
name: "custom pruning options",
initParams: func() {
viper.Set(FlagPruning, types.PruningOptionCustom)
viper.Set(FlagPruningKeepRecent, 1234)
viper.Set(FlagPruningKeepEvery, 4321)
viper.Set(FlagPruningInterval, 10)
initParams: func() *viper.Viper {
v := viper.New()
v.Set(FlagPruning, types.PruningOptionCustom)
v.Set(FlagPruningKeepRecent, 1234)
v.Set(FlagPruningKeepEvery, 4321)
v.Set(FlagPruningInterval, 10)

return v
},
expectedOptions: types.PruningOptions{
KeepRecent: 1234,
Expand All @@ -38,8 +43,12 @@ func TestGetPruningOptionsFromFlags(t *testing.T) {
},
},
{
name: types.PruningOptionDefault,
initParams: func() {},
name: types.PruningOptionDefault,
initParams: func() *viper.Viper {
v := viper.New()
v.Set(FlagPruning, types.PruningOptionDefault)
return v
},
expectedOptions: types.PruneDefault,
},
}
Expand All @@ -50,9 +59,9 @@ func TestGetPruningOptionsFromFlags(t *testing.T) {
t.Run(tt.name, func(j *testing.T) {
viper.Reset()
viper.SetDefault(FlagPruning, types.PruningOptionDefault)
tt.initParams()
v := tt.initParams()

opts, err := GetPruningOptionsFromFlags()
opts, err := GetPruningOptionsFromFlags(v)
if tt.wantErr {
require.Error(t, err)
return
Expand Down
Loading

0 comments on commit 9ebda4e

Please sign in to comment.