Skip to content

Commit

Permalink
Merge PR cosmos#4757: update x/genutil to match module spec
Browse files Browse the repository at this point in the history
* reestructure genutil

* use auth and staking types packages

* cleanup

* rename pkg

* chacha cha

* update clog tags and pending

* address comments from code review
  • Loading branch information
fedekunze authored and rigelrozanski committed Jul 24, 2019
1 parent 231fefa commit bfb6445
Show file tree
Hide file tree
Showing 27 changed files with 268 additions and 220 deletions.
1 change: 1 addition & 0 deletions .clog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ tags:
- abci
- rest
- cli
- modules
1 change: 1 addition & 0 deletions .pending/improvements/modules/_4751-update-genutil
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4751 update `x/genutils` to match module spec
2 changes: 1 addition & 1 deletion x/genaccounts/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package genaccounts

import (
types "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types"
"github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
types "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types"
"github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types"
)

// InitGenesis initializes accounts and deliver genesis transactions
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/internal/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package genaccounts
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/internal/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package genaccounts
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/internal/types/genesis_account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package genaccounts
package types

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/internal/types/genesis_account_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package genaccounts
package types

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/internal/types/genesis_state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package genaccounts
package types

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/internal/types/genesis_state_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package genaccounts
package types

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion x/genaccounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/exported"
types "github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types"
"github.com/cosmos/cosmos-sdk/x/genaccounts/internal/types"
)

var (
Expand Down
36 changes: 36 additions & 0 deletions x/genutil/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/genutil/types
package genutil

import (
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

const (
ModuleName = types.ModuleName
)

var (
// functions aliases
NewGenesisState = types.NewGenesisState
NewGenesisStateFromStdTx = types.NewGenesisStateFromStdTx
NewInitConfig = types.NewInitConfig
GetGenesisStateFromAppState = types.GetGenesisStateFromAppState
SetGenesisStateInAppState = types.SetGenesisStateInAppState
GenesisStateFromGenDoc = types.GenesisStateFromGenDoc
GenesisStateFromGenFile = types.GenesisStateFromGenFile
ValidateGenesis = types.ValidateGenesis

// variable aliases
ModuleCdc = types.ModuleCdc
)

type (
GenesisState = types.GenesisState
AppMap = types.AppMap
MigrationCallback = types.MigrationCallback
MigrationMap = types.MigrationMap
InitConfig = types.InitConfig
)
3 changes: 2 additions & 1 deletion x/genutil/client/cli/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

const flagGenTxDir = "gentx-dir"

// CollectGenTxsCmd - return the cobra command to collect genesis transactions
func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec,
genAccIterator genutil.GenesisAccountsIterator, defaultNodeHome string) *cobra.Command {
genAccIterator types.GenesisAccountsIterator, defaultNodeHome string) *cobra.Command {

cmd := &cobra.Command{
Use: "collect-gentxs",
Expand Down
5 changes: 3 additions & 2 deletions x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (

"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

// helpers for message building gen-tx command
// StakingMsgBuildingHelpers helpers for message building gen-tx command
type StakingMsgBuildingHelpers interface {
CreateValidatorMsgHelpers(ipDefault string) (fs *flag.FlagSet, nodeIDFlag, pubkeyFlag, amountFlag, defaultsDesc string)
PrepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, chainID string, valPubKey crypto.PubKey)
Expand All @@ -41,7 +42,7 @@ type StakingMsgBuildingHelpers interface {
// GenTxCmd builds the application's gentx command.
// nolint: errcheck
func GenTxCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, smbh StakingMsgBuildingHelpers,
genAccIterator genutil.GenesisAccountsIterator, defaultNodeHome, defaultCLIHome string) *cobra.Command {
genAccIterator types.GenesisAccountsIterator, defaultNodeHome, defaultCLIHome string) *cobra.Command {

ipDefault, _ := server.ExternalIP()
fsCreateValidator, flagNodeID, flagPubKey, flagAmount, defaultsDesc := smbh.CreateValidatorMsgHelpers(ipDefault)
Expand Down
22 changes: 0 additions & 22 deletions x/genutil/codec.go

This file was deleted.

62 changes: 12 additions & 50 deletions x/genutil/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,20 @@ import (
"strings"

cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/staking"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// common config options for init
type InitConfig struct {
ChainID string
GenTxsDir string
Name string
NodeID string
ValPubKey crypto.PubKey
}

// NewInitConfig creates a new InitConfig object
func NewInitConfig(chainID, genTxsDir, name, nodeID string, valPubKey crypto.PubKey) InitConfig {
return InitConfig{
ChainID: chainID,
GenTxsDir: genTxsDir,
Name: name,
NodeID: nodeID,
ValPubKey: valPubKey,
}
}

// get the genesis app state from the config
// GenAppStateFromConfig gets the genesis app state from the config
func GenAppStateFromConfig(cdc *codec.Codec, config *cfg.Config,
initCfg InitConfig, genDoc tmtypes.GenesisDoc,
genAccIterator GenesisAccountsIterator,
genAccIterator types.GenesisAccountsIterator,
) (appState json.RawMessage, err error) {

// process genesis transactions, else create default genesis.json
Expand Down Expand Up @@ -83,30 +64,11 @@ func GenAppStateFromConfig(cdc *codec.Codec, config *cfg.Config,
return appState, err
}

// Set the genesis transactions int the app genesis state
func SetGenTxsInAppGenesisState(cdc *codec.Codec, appGenesisState map[string]json.RawMessage,
genTxs []auth.StdTx) (map[string]json.RawMessage, error) {

genesisState := GetGenesisStateFromAppState(cdc, appGenesisState)
// convert all the GenTxs to JSON
var genTxsBz []json.RawMessage
for _, genTx := range genTxs {
txBz, err := cdc.MarshalJSON(genTx)
if err != nil {
return appGenesisState, err
}
genTxsBz = append(genTxsBz, txBz)
}

genesisState.GenTxs = genTxsBz
return SetGenesisStateInAppState(cdc, appGenesisState, genesisState), nil
}

// CollectStdTxs processes and validates application's genesis StdTxs and returns
// the list of appGenTxs, and persistent peers required to generate genesis.json.
func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string,
genDoc tmtypes.GenesisDoc, genAccIterator GenesisAccountsIterator,
) (appGenTxs []auth.StdTx, persistentPeers string, err error) {
genDoc tmtypes.GenesisDoc, genAccIterator types.GenesisAccountsIterator,
) (appGenTxs []authtypes.StdTx, persistentPeers string, err error) {

var fos []os.FileInfo
fos, err = ioutil.ReadDir(genTxsDir)
Expand All @@ -121,9 +83,9 @@ func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string,
return appGenTxs, persistentPeers, err
}

addrMap := make(map[string]auth.Account)
addrMap := make(map[string]authexported.Account)
genAccIterator.IterateGenesisAccounts(cdc, appState,
func(acc auth.Account) (stop bool) {
func(acc authexported.Account) (stop bool) {
addrMap[acc.GetAddress().String()] = acc
return false
},
Expand All @@ -143,7 +105,7 @@ func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string,
if jsonRawTx, err = ioutil.ReadFile(filename); err != nil {
return appGenTxs, persistentPeers, err
}
var genStdTx auth.StdTx
var genStdTx authtypes.StdTx
if err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx); err != nil {
return appGenTxs, persistentPeers, err
}
Expand All @@ -166,7 +128,7 @@ func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string,
}

// TODO abstract out staking message validation back to staking
msg := msgs[0].(staking.MsgCreateValidator)
msg := msgs[0].(stakingtypes.MsgCreateValidator)
// validate delegator and validator addresses and funds against the accounts in the state
delAddr := msg.DelegatorAddress.String()
valAddr := sdk.AccAddress(msg.ValidatorAddress).String()
Expand Down
32 changes: 0 additions & 32 deletions x/genutil/expected.go

This file was deleted.

5 changes: 3 additions & 2 deletions x/genutil/init.go → x/genutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

// initialize accounts and deliver genesis transactions
func InitGenesis(ctx sdk.Context, cdc *codec.Codec, stakingKeeper StakingKeeper,
// InitGenesis - initialize accounts and deliver genesis transactions
func InitGenesis(ctx sdk.Context, cdc *codec.Codec, stakingKeeper types.StakingKeeper,
deliverTx deliverTxfn, genesisState GenesisState) []abci.ValidatorUpdate {

var validators []abci.ValidatorUpdate
Expand Down
47 changes: 0 additions & 47 deletions x/genutil/genesis_state_test.go

This file was deleted.

Loading

0 comments on commit bfb6445

Please sign in to comment.