Skip to content

Commit

Permalink
Update to 0.30 SDK (Agoric#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzampolin authored Jan 31, 2019
1 parent f7294d8 commit 7f26489
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 124 deletions.
242 changes: 193 additions & 49 deletions Gopkg.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

[[constraint]]
name = "github.com/cosmos/cosmos-sdk"
version = "v0.27.0"
revision = "3864d5b1bd9d7081fb5f8bc3f9a014e9a0462756"

[[override]]
name = "github.com/golang/protobuf"
Expand All @@ -46,11 +46,11 @@

[[override]]
name = "github.com/tendermint/iavl"
version = "=v0.12.0"
version = "v0.12.0"

[[override]]
name = "github.com/tendermint/tendermint"
version = "v0.27.0-dev1"
version = "v0.29.0"

[[override]]
name = "golang.org/x/crypto"
Expand Down
36 changes: 32 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/sdk-application-tutorial/x/nameservice"

bam "github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -33,10 +34,13 @@ type nameServiceApp struct {
keyNSowners *sdk.KVStoreKey
keyNSprices *sdk.KVStoreKey
keyFeeCollection *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
tkeyParams *sdk.TransientStoreKey

accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
feeCollectionKeeper auth.FeeCollectionKeeper
paramsKeeper params.Keeper
nsKeeper nameservice.Keeper
}

Expand All @@ -60,17 +64,27 @@ func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {
keyNSowners: sdk.NewKVStoreKey("ns_owners"),
keyNSprices: sdk.NewKVStoreKey("ns_prices"),
keyFeeCollection: sdk.NewKVStoreKey("fee_collection"),
keyParams: sdk.NewKVStoreKey("params"),
tkeyParams: sdk.NewTransientStoreKey("transient_params"),
}

// The ParamsKeeper handles parameter storage for the application
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams)

// The AccountKeeper handles address -> account lookups
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
app.keyAccount,
app.paramsKeeper.Subspace(auth.DefaultParamspace),
auth.ProtoBaseAccount,
)

// The BankKeeper allows you perform sdk.Coins interactions
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.bankKeeper = bank.NewBaseKeeper(
app.accountKeeper,
app.paramsKeeper.Subspace(bank.DefaultParamspace),
bank.DefaultCodespace,
)

// The FeeCollectionKeeper collects transaction fees and renders them to the fee distribution module
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(cdc, app.keyFeeCollection)
Expand Down Expand Up @@ -107,8 +121,12 @@ func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {
app.keyNSnames,
app.keyNSowners,
app.keyNSprices,
app.keyFeeCollection,
app.keyParams,
)

app.MountStoresTransient(app.tkeyParams)

err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
Expand All @@ -119,6 +137,8 @@ func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {

// GenesisState represents chain state at the start of the chain. Any initial state (account balances) are stored here.
type GenesisState struct {
AuthData auth.GenesisState `json:"auth"`
BankData bank.GenesisState `json:"bank"`
Accounts []*auth.BaseAccount `json:"accounts"`
}

Expand All @@ -136,6 +156,9 @@ func (app *nameServiceApp) initChainer(ctx sdk.Context, req abci.RequestInitChai
app.accountKeeper.SetAccount(ctx, acc)
}

auth.InitGenesis(ctx, app.accountKeeper, app.feeCollectionKeeper, genesisState.AuthData)
bank.InitGenesis(ctx, app.bankKeeper, genesisState.BankData)

return abci.ResponseInitChain{}
}

Expand All @@ -156,7 +179,12 @@ func (app *nameServiceApp) ExportAppStateAndValidators() (appState json.RawMessa

app.accountKeeper.IterateAccounts(ctx, appendAccountsFn)

genState := GenesisState{Accounts: accounts}
genState := GenesisState{
Accounts: accounts,
AuthData: auth.DefaultGenesisState(),
BankData: bank.DefaultGenesisState(),
}

appState, err = codec.MarshalJSONIndent(app.cdc, genState)
if err != nil {
return nil, nil, err
Expand All @@ -171,7 +199,7 @@ func MakeCodec() *codec.Codec {
auth.RegisterCodec(cdc)
bank.RegisterCodec(cdc)
nameservice.RegisterCodec(cdc)
stake.RegisterCodec(cdc)
staking.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
Expand Down
32 changes: 31 additions & 1 deletion cmd/nscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"path"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"

Expand Down Expand Up @@ -51,9 +53,14 @@ func main() {
Short: "nameservice Client",
}

// Add --chain-id to persistent flags and mark it required
rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node")
rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error {
return initConfig(rootCmd)
}

// Construct Root Command
rootCmd.AddCommand(
rpc.InitClientCommand(),
rpc.StatusCommand(),
client.ConfigCmd(),
queryCmd(cdc, mc),
Expand Down Expand Up @@ -126,3 +133,26 @@ func txCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {

return txCmd
}

func initConfig(cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(cli.HomeFlag)
if err != nil {
return err
}

cfgFile := path.Join(home, "config", "config.toml")
if _, err := os.Stat(cfgFile); err == nil {
viper.SetConfigFile(cfgFile)

if err := viper.ReadInConfig(); err != nil {
return err
}
}
if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil {
return err
}
if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil {
return err
}
return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag))
}
11 changes: 8 additions & 3 deletions cmd/nsd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
Expand Down Expand Up @@ -86,7 +87,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6))
}

_, _, err := gaiaInit.InitializeNodeValidatorFiles(config)
_, pk, err := gaiaInit.InitializeNodeValidatorFiles(config)
if err != nil {
return err
}
Expand All @@ -98,12 +99,16 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}

appState, err = codec.MarshalJSONIndent(cdc, app.GenesisState{})
genesis := app.GenesisState{
AuthData: auth.DefaultGenesisState(),
BankData: bank.DefaultGenesisState(),
}

appState, err = codec.MarshalJSONIndent(cdc, genesis)
if err != nil {
return err
}

pk := gaiaInit.ReadOrCreatePrivValidator(config.PrivValidatorFile())
_, _, validator, err := server.SimpleAppGenTx(cdc, pk)
if err != nil {
return err
Expand Down
62 changes: 47 additions & 15 deletions tutorial/app-complete.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/sdk-application-tutorial/x/nameservice"

bam "github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -25,11 +26,11 @@ import (
dbm "github.com/tendermint/tendermint/libs/db"
tmtypes "github.com/tendermint/tendermint/types"
)

```
Next you need to add the stores' keys as well as the `Keepers` in your `nameServiceApp` struct, and update the constructor accordingly

```go

const (
appName = "nameservice"
)
Expand All @@ -44,10 +45,13 @@ type nameServiceApp struct {
keyNSowners *sdk.KVStoreKey
keyNSprices *sdk.KVStoreKey
keyFeeCollection *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
tkeyParams *sdk.TransientStoreKey

accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
feeCollectionKeeper auth.FeeCollectionKeeper
paramsKeeper params.Keeper
nsKeeper nameservice.Keeper
}

Expand All @@ -60,17 +64,19 @@ func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc))

// Here you initialize your application with the store keys it requires
var app = &nameServiceApp{
BaseApp: bApp,
cdc: cdc,

keyMain: sdk.NewKVStoreKey("main"),
keyAccount: sdk.NewKVStoreKey("acc"),
keyNSnames: sdk.NewKVStoreKey("ns_names"),
keyNSowners: sdk.NewKVStoreKey("ns_owners"),
keyNSprices: sdk.NewKVStoreKey("ns_prices"),
keyFeeCollection: sdk.NewKVStoreKey("fee_collection"),
}
var app = &nameServiceApp{
BaseApp: bApp,
cdc: cdc,

keyMain: sdk.NewKVStoreKey("main"),
keyAccount: sdk.NewKVStoreKey("acc"),
keyNSnames: sdk.NewKVStoreKey("ns_names"),
keyNSowners: sdk.NewKVStoreKey("ns_owners"),
keyNSprices: sdk.NewKVStoreKey("ns_prices"),
keyFeeCollection: sdk.NewKVStoreKey("fee_collection"),
keyParams: sdk.NewKVStoreKey("params"),
tkeyParams: sdk.NewTransientStoreKey("transient_params"),
}

return app
}
Expand Down Expand Up @@ -108,17 +114,27 @@ func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {
keyNSowners: sdk.NewKVStoreKey("ns_owners"),
keyNSprices: sdk.NewKVStoreKey("ns_prices"),
keyFeeCollection: sdk.NewKVStoreKey("fee_collection"),
keyParams: sdk.NewKVStoreKey("params"),
tkeyParams: sdk.NewTransientStoreKey("transient_params"),
}

// The ParamsKeeper handles parameter storage for the application
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams)

// The AccountKeeper handles address -> account lookups
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
app.keyAccount,
app.paramsKeeper.Subspace(auth.DefaultParamspace),
auth.ProtoBaseAccount,
)

// The BankKeeper allows you perform sdk.Coins interactions
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.bankKeeper = bank.NewBaseKeeper(
app.accountKeeper,
app.paramsKeeper.Subspace(bank.DefaultParamspace),
bank.DefaultCodespace,
)

// The FeeCollectionKeeper collects transaction fees and renders them to the fee distribution module
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(cdc, app.keyFeeCollection)
Expand Down Expand Up @@ -155,8 +171,12 @@ func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {
app.keyNSnames,
app.keyNSowners,
app.keyNSprices,
app.keyFeeCollection,
app.keyParams,
)

app.MountStoresTransient(app.tkeyParams)

err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
Expand All @@ -166,13 +186,17 @@ func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {
}
```

> _*NOTE*_: The TransientStore mentioned above is an in-memory implementation of the KVStore for state that is not persisted.
The `initChainer` defines how accounts in `genesis.json` are mapped into the application state on initial chain start. The `ExportAppStateAndValidators` function helps bootstrap the initial state for application. You don't need to worry too much about either of these for now.

The constructor registers the `initChainer` function, but it isn't defined yet. Go ahead and create it:

```go
// GenesisState represents chain state at the start of the chain. Any initial state (account balances) are stored here.
type GenesisState struct {
AuthData auth.GenesisState `json:"auth"`
BankData bank.GenesisState `json:"bank"`
Accounts []*auth.BaseAccount `json:"accounts"`
}

Expand All @@ -190,6 +214,9 @@ func (app *nameServiceApp) initChainer(ctx sdk.Context, req abci.RequestInitChai
app.accountKeeper.SetAccount(ctx, acc)
}

auth.InitGenesis(ctx, app.accountKeeper, app.feeCollectionKeeper, genesisState.AuthData)
bank.InitGenesis(ctx, app.bankKeeper, genesisState.BankData)

return abci.ResponseInitChain{}
}

Expand All @@ -210,7 +237,12 @@ func (app *nameServiceApp) ExportAppStateAndValidators() (appState json.RawMessa

app.accountKeeper.IterateAccounts(ctx, appendAccountsFn)

genState := GenesisState{Accounts: accounts}
genState := GenesisState{
Accounts: accounts,
AuthData: auth.DefaultGenesisState(),
BankData: bank.DefaultGenesisState(),
}

appState, err = codec.MarshalJSONIndent(app.cdc, genState)
if err != nil {
return nil, nil, err
Expand Down
Loading

0 comments on commit 7f26489

Please sign in to comment.