Skip to content

Commit

Permalink
export-stake command works
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes authored and rigelrozanski committed Apr 28, 2018
1 parent 203d2c1 commit 55c82e4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
5 changes: 5 additions & 0 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ 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{}))
}
2 changes: 2 additions & 0 deletions cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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"
)

func main() {
Expand Down
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 55c82e4

Please sign in to comment.