Skip to content

Commit

Permalink
export-stake command works
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Apr 24, 2018
1 parent 77843aa commit 00d1b15
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
5 changes: 5 additions & 0 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
return abci.ResponseInitChain{}
}

// custom logic for export
func (app *GaiaApp) ExportStake() stake.GenesisState {
return app.stakeKeeper.WriteGenesis(app.NewContext(true, abci.Header{}))
}

//__________________________________________________________

// State to Unmarshal
Expand Down
20 changes: 19 additions & 1 deletion cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/server"
stake "github.com/cosmos/cosmos-sdk/x/stake"
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/commands"
)

// rootCmd is the entry point for this binary
Expand All @@ -38,8 +40,24 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
func main() {
server.AddCommands(rootCmd, app.DefaultGenAppState, generateApp, context)

// prepare and add flags
rootDir := os.ExpandEnv("$HOME/.gaiad")

export := func() (stake.GenesisState, error) {
dataDir := filepath.Join(rootDir, "data")
db, err := dbm.NewGoLevelDB("gaia", dataDir)
if err != nil {
return stake.GenesisState{}, err
}
app := app.NewGaiaApp(log.NewNopLogger(), db)
if err != nil {
return stake.GenesisState{}, err
}
return app.ExportStake(), nil
}

rootCmd.AddCommand(stakecmd.GetCmdExport(export, app.MakeCodec()))

// prepare and add flags
executor := cli.PrepareBaseCmd(rootCmd, "GA", rootDir)
executor.Execute()
}
11 changes: 6 additions & 5 deletions x/stake/commands/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (

"github.com/spf13/cobra"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" // XXX fix
"github.com/cosmos/cosmos-sdk/x/stake"
)

// get the command to export state
func GetCmdExport(load func() (stake.Keeper, sdk.Context), cdc *wire.Codec) *cobra.Command {
func GetCmdExport(export func() (stake.GenesisState, error), cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Use: "export-stake",
Short: "Export stake module state as JSON to stdout",
RunE: func(cmd *cobra.Command, args []string) error {
keeper, ctx := load()
genesis := keeper.WriteGenesis(ctx)
genesis, err := export()
if err != nil {
return err
}
output, err := wire.MarshalJSONIndent(cdc, genesis)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions x/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data GenesisState) {
func (k Keeper) WriteGenesis(ctx sdk.Context) GenesisState {
pool := k.GetPool(ctx)
params := k.GetParams(ctx)
candidates := k.GetCandidates(ctx, -1)
bonds := k.GetBonds(ctx, -1)
candidates := k.GetCandidates(ctx, 32767)
bonds := k.GetBonds(ctx, 32767)
return GenesisState{
pool,
params,
Expand Down
8 changes: 4 additions & 4 deletions x/stake/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ func (k Keeper) GetCandidate(ctx sdk.Context, addr sdk.Address) (candidate Candi
return candidate, true
}

// Get the set of all candidates, retrieve a maxRetrieve number of records, -1 maxRetrieve = no limit
// Get the set of all candidates, retrieve a maxRetrieve number of records
func (k Keeper) GetCandidates(ctx sdk.Context, maxRetrieve int16) (candidates Candidates) {
store := ctx.KVStore(k.storeKey)
iterator := store.Iterator(subspace(CandidatesKey))

candidates = make([]Candidate, maxRetrieve)
i := 0
for ; ; i++ {
if !iterator.Valid() || (maxRetrieve >= 0 && i > int(maxRetrieve-1)) {
if !iterator.Valid() || i > int(maxRetrieve-1) {
iterator.Close()
break
}
Expand Down Expand Up @@ -371,15 +371,15 @@ func (k Keeper) GetDelegatorBond(ctx sdk.Context,
return bond, true
}

// load all bonds, -1 maxRetrieve = no limit
// load all bonds
func (k Keeper) GetBonds(ctx sdk.Context, maxRetrieve int16) (bonds []DelegatorBond) {
store := ctx.KVStore(k.storeKey)
iterator := store.Iterator(subspace(DelegatorBondKeyPrefix))

bonds = make([]DelegatorBond, maxRetrieve)
i := 0
for ; ; i++ {
if !iterator.Valid() || (maxRetrieve >= 0 && i > int(maxRetrieve-1)) {
if !iterator.Valid() || i > int(maxRetrieve-1) {
iterator.Close()
break
}
Expand Down

0 comments on commit 00d1b15

Please sign in to comment.