Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Add --jail-whitelist to gaiad export #3454

Merged
merged 3 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ IMPROVEMENTS
* [\#3418](https://github.com/cosmos/cosmos-sdk/issues/3418) Add vesting account
genesis validation checks to `GaiaValidateGenesisState`.
* [\#3420](https://github.com/cosmos/cosmos-sdk/issues/3420) Added maximum length to governance proposal descriptions and titles
* [\#3454](https://github.com/cosmos/cosmos-sdk/pull/3454) Add `--jail-whitelist` to `gaiad export` to enable testing of complex exports

* SDK
* \#3435 Test that store implementations do not allow nil values
Expand All @@ -67,7 +68,7 @@ BUG FIXES
- [\#3345](https://github.com/cosmos/cosmos-sdk/issues/3345) Upgrade ledger-cosmos-go dependency to v0.9.3 to pull
https://github.com/ZondaX/ledger-cosmos-go/commit/ed9aa39ce8df31bad1448c72d3d226bf2cb1a8d1 in order to fix a derivation path issue that causes `gaiacli keys add --recover`
to malfunction.
- [\#3419](https://github.com/cosmos/cosmos-sdk/pull/3419) Fix `q distr slashes` panic
- [\#3419](https://github.com/cosmos/cosmos-sdk/pull/3419) Fix `q distr slashes` panic

* Gaia

Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ func TestGaiadExport(t *testing.T) {

// Making a new app object with the db, so that initchain hasn't been called
newGapp := NewGaiaApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true)
_, _, err := newGapp.ExportAppStateAndValidators(false)
_, _, err := newGapp.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}
28 changes: 25 additions & 3 deletions cmd/gaia/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"encoding/json"
"log"

abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
Expand All @@ -18,14 +19,14 @@ import (
)

// export the state of gaia for a genesis file
func (app *GaiaApp) ExportAppStateAndValidators(forZeroHeight bool) (
func (app *GaiaApp) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string) (
appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {

// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})

if forZeroHeight {
app.prepForZeroHeightGenesis(ctx)
app.prepForZeroHeightGenesis(ctx, jailWhiteList)
}

// iterate to get the accounts
Expand Down Expand Up @@ -56,7 +57,23 @@ func (app *GaiaApp) ExportAppStateAndValidators(forZeroHeight bool) (
}

// prepare for fresh start at zero height
func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context) {
func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) {
applyWhiteList := false

//Check if there is a whitelist
if len(jailWhiteList) > 0 {
applyWhiteList = true
}

whiteListMap := make(map[string]bool)

for _, addr := range jailWhiteList {
_, err := sdk.ValAddressFromBech32(addr)
if err != nil {
log.Fatal(err)
}
whiteListMap[addr] = true
}

/* Just to be safe, assert the invariants on current state. */
app.assertRuntimeInvariantsOnContext(ctx)
Expand Down Expand Up @@ -136,13 +153,18 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context) {
validator.BondHeight = 0
validator.UnbondingHeight = 0
valConsAddrs = append(valConsAddrs, validator.ConsAddress())
if applyWhiteList && !whiteListMap[addr.String()] {
validator.Jailed = true
}

app.stakingKeeper.SetValidator(ctx, validator)
counter++
}

iter.Close()

_ = app.stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)

/* Handle slashing state. */

// reset start height on signing infos
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestGaiaImportExport(t *testing.T) {

fmt.Printf("Exporting genesis...\n")

appState, _, err := app.ExportAppStateAndValidators(false)
appState, _, err := app.ExportAppStateAndValidators(false, []string{})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -520,7 +520,7 @@ func TestGaiaSimulationAfterImport(t *testing.T) {

fmt.Printf("Exporting genesis...\n")

appState, _, err := app.ExportAppStateAndValidators(true)
appState, _, err := app.ExportAppStateAndValidators(true, []string{})
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application
}

func exportAppStateAndTMValidators(
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool,
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string,
) (json.RawMessage, []tmtypes.GenesisValidator, error) {
if height != -1 {
gApp := app.NewGaiaApp(logger, db, traceStore, false)
err := gApp.LoadHeight(height)
if err != nil {
return nil, nil, err
}
return gApp.ExportAppStateAndValidators(forZeroHeight)
return gApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
}
gApp := app.NewGaiaApp(logger, db, traceStore, true)
return gApp.ExportAppStateAndValidators(forZeroHeight)
return gApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
}
2 changes: 1 addition & 1 deletion server/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (

// AppExporter is a function that dumps all app state to
// JSON-serializable structure and returns the current validator set.
AppExporter func(log.Logger, dbm.DB, io.Writer, int64, bool) (json.RawMessage, []tmtypes.GenesisValidator, error)
AppExporter func(log.Logger, dbm.DB, io.Writer, int64, bool, []string) (json.RawMessage, []tmtypes.GenesisValidator, error)
)

func openDB(rootDir string) (dbm.DB, error) {
Expand Down
5 changes: 4 additions & 1 deletion server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
const (
flagHeight = "height"
flagForZeroHeight = "for-zero-height"
flagJailWhitelist = "jail-whitelist"
)

// ExportCmd dumps app state to JSON.
Expand Down Expand Up @@ -54,7 +55,8 @@ func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.C
}
height := viper.GetInt64(flagHeight)
forZeroHeight := viper.GetBool(flagForZeroHeight)
appState, validators, err := appExporter(ctx.Logger, db, traceWriter, height, forZeroHeight)
jailWhiteList := viper.GetStringSlice(flagJailWhitelist)
appState, validators, err := appExporter(ctx.Logger, db, traceWriter, height, forZeroHeight, jailWhiteList)
if err != nil {
return errors.Errorf("error exporting state: %v\n", err)
}
Expand All @@ -78,6 +80,7 @@ func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.C
}
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")
return cmd
}

Expand Down