Skip to content

Commit

Permalink
Client Generalization Refactor (#94)
Browse files Browse the repository at this point in the history
* Push ERROR

* Changes for client refactor

- added alias because of cycle imports
- TODO: update tutorial

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* tutorial changes

* tutorial docs update

* add routes to into the tutorial

* lint passing

* comments

* minor change

* go.mod change
  • Loading branch information
tac0turtle committed Jun 12, 2019
1 parent 1c28fd2 commit 3a2d53a
Show file tree
Hide file tree
Showing 26 changed files with 379 additions and 420 deletions.
32 changes: 13 additions & 19 deletions app.go
Expand Up @@ -4,27 +4,25 @@ import (
"encoding/json"
"os"

abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"

"github.com/cosmos/cosmos-sdk/x/auth/genaccounts"

"github.com/cosmos/cosmos-sdk/x/bank"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/sdk-application-tutorial/x/nameservice"

bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
tlog "github.com/tendermint/tendermint/libs/log"
)

const appName = "nameservice"
Expand All @@ -37,12 +35,7 @@ var (
DefaultNodeHome = os.ExpandEnv("$HOME/.nsd")

// ModuleBasicManager is in charge of setting up basic module elemnets
ModuleBasics sdk.ModuleBasicManager
)

// maintains independent module functionality
func init() {
ModuleBasics = sdk.NewModuleBasicManager(
ModuleBasics = module.NewBasicManager(
genaccounts.AppModuleBasic{},
genutil.AppModuleBasic{},
auth.AppModuleBasic{},
Expand All @@ -53,7 +46,7 @@ func init() {
distr.AppModuleBasic{},
slashing.AppModuleBasic{},
)
}
)

// MakeCodec generates the necessary codecs for Amino
func MakeCodec() *codec.Codec {
Expand Down Expand Up @@ -92,11 +85,11 @@ type nameServiceApp struct {
nsKeeper nameservice.Keeper

// Module Manager
mm *sdk.ModuleManager
mm *module.Manager
}

// NewNameServiceApp is a constructor function for nameServiceApp
func NewNameServiceApp(logger tlog.Logger, db dbm.DB) *nameServiceApp {
func NewNameServiceApp(logger log.Logger, db dbm.DB) *nameServiceApp {

// First define the top level codec that will be shared by the different modules
cdc := MakeCodec()
Expand Down Expand Up @@ -193,7 +186,7 @@ func NewNameServiceApp(logger tlog.Logger, db dbm.DB) *nameServiceApp {
app.cdc,
)

app.mm = sdk.NewModuleManager(
app.mm = module.NewManager(
genaccounts.NewAppModule(app.accountKeeper),
genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx),
auth.NewAppModule(app.accountKeeper, app.feeCollectionKeeper),
Expand Down Expand Up @@ -226,6 +219,7 @@ func NewNameServiceApp(logger tlog.Logger, db dbm.DB) *nameServiceApp {
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)

// The AnteHandler handles signature verification and transaction pre-processing
app.SetAnteHandler(
auth.NewAnteHandler(
Expand Down
66 changes: 16 additions & 50 deletions cmd/nscli/main.go
Expand Up @@ -9,31 +9,14 @@ import (
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
"github.com/spf13/viper"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"

sdk "github.com/cosmos/cosmos-sdk/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
distcmd "github.com/cosmos/cosmos-sdk/x/distribution"
distClient "github.com/cosmos/cosmos-sdk/x/distribution/client"
dist "github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
"github.com/cosmos/cosmos-sdk/x/mint"
mintclient "github.com/cosmos/cosmos-sdk/x/mint/client"
mintrest "github.com/cosmos/cosmos-sdk/x/mint/client/rest"
sl "github.com/cosmos/cosmos-sdk/x/slashing"
slashingclient "github.com/cosmos/cosmos-sdk/x/slashing/client"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
st "github.com/cosmos/cosmos-sdk/x/staking"
stakingclient "github.com/cosmos/cosmos-sdk/x/staking/client"
staking "github.com/cosmos/cosmos-sdk/x/staking/client/rest"
app "github.com/cosmos/sdk-application-tutorial"
nsclient "github.com/cosmos/sdk-application-tutorial/x/nameservice/client"
nsrest "github.com/cosmos/sdk-application-tutorial/x/nameservice/client/rest"
"github.com/spf13/cobra"
"github.com/spf13/viper"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"
)

const (
Expand All @@ -53,14 +36,6 @@ func main() {
config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
config.Seal()

mc := []sdk.ModuleClient{
nsclient.NewModuleClient(storeNS, cdc),
stakingclient.NewModuleClient(st.StoreKey, cdc),
distClient.NewModuleClient(distcmd.StoreKey, cdc),
slashingclient.NewModuleClient(sl.StoreKey, cdc),
mintclient.NewModuleClient(mint.StoreKey, cdc),
}

rootCmd := &cobra.Command{
Use: "nscli",
Short: "nameservice Client",
Expand All @@ -76,8 +51,8 @@ func main() {
rootCmd.AddCommand(
rpc.StatusCommand(),
client.ConfigCmd(app.DefaultCLIHome),
queryCmd(cdc, mc),
txCmd(cdc, mc),
queryCmd(cdc),
txCmd(cdc),
client.LineBreak,
lcd.ServeCommand(cdc, registerRoutes),
client.LineBreak,
Expand All @@ -93,42 +68,34 @@ func main() {
}

func registerRoutes(rs *lcd.RestServer) {
rs.CliCtx = rs.CliCtx.WithAccountDecoder(rs.Cdc)
rpc.RegisterRPCRoutes(rs.CliCtx, rs.Mux)
tx.RegisterTxRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, storeAcc)
bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
nsrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, storeNS)
staking.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
dist.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, distcmd.StoreKey)
slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
mintrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
client.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
}

func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command {
func queryCmd(cdc *amino.Codec) *cobra.Command {
queryCmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
}

queryCmd.AddCommand(
authcmd.GetAccountCmd(cdc),
client.LineBreak,
rpc.ValidatorCommand(cdc),
rpc.BlockCommand(),
tx.SearchTxCmd(cdc),
tx.QueryTxCmd(cdc),
client.LineBreak,
authcmd.GetAccountCmd(storeAcc, cdc),
)

for _, m := range mc {
queryCmd.AddCommand(m.GetQueryCmd())
}
// add modules' query commands
app.ModuleBasics.AddQueryCommands(queryCmd, cdc)

return queryCmd
}

func txCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command {
func txCmd(cdc *amino.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
Expand All @@ -144,9 +111,8 @@ func txCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command {
tx.GetEncodeCommand(cdc),
)

for _, m := range mc {
txCmd.AddCommand(m.GetTxCmd())
}
// add modules' tx commands
app.ModuleBasics.AddTxCommands(txCmd, cdc)

return txCmd
}
Expand Down
64 changes: 8 additions & 56 deletions go.mod
Expand Up @@ -3,67 +3,19 @@ module github.com/cosmos/sdk-application-tutorial
go 1.12

require (
bou.ke/monkey v1.0.1 // indirect
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973
github.com/bgentry/speakeasy v0.1.0
github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d
github.com/btcsuite/btcutil v0.0.0-20180706230648-ab6388e0c60a
github.com/cosmos/cosmos-sdk v0.28.2-0.20190604125914-75de63ce3166
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8
github.com/cosmos/ledger-cosmos-go v0.10.3
github.com/davecgh/go-spew v1.1.1
github.com/ethereum/go-ethereum v1.8.23
github.com/fsnotify/fsnotify v1.4.7
github.com/go-kit/kit v0.8.0
github.com/go-logfmt/logfmt v0.4.0
github.com/go-stack/stack v1.8.0
github.com/gogo/protobuf v1.1.1
github.com/golang/protobuf v1.3.0
github.com/golang/snappy v0.0.1
github.com/cosmos/cosmos-sdk v0.28.2-0.20190605232616-5f9c3fdf8895
github.com/gorilla/mux v1.7.0
github.com/gorilla/websocket v1.4.0
github.com/hashicorp/hcl v1.0.0
github.com/inconshreveable/mousetrap v1.0.0
github.com/jmhodges/levigo v1.0.0
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515
github.com/magiconair/properties v1.8.0
github.com/mattn/go-isatty v0.0.7
github.com/matttproud/golang_protobuf_extensions v1.0.1
github.com/mitchellh/mapstructure v1.1.2
github.com/otiai10/copy v0.0.0-20180813032824-7e9a647135a1 // indirect
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 // indirect
github.com/otiai10/mint v1.2.3 // indirect
github.com/pelletier/go-toml v1.2.0
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
github.com/prometheus/common v0.2.0
github.com/prometheus/procfs v0.0.0-20190328153300-af7bedc223fb
github.com/rakyll/statik v0.1.4
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165
github.com/rs/cors v1.6.0
github.com/spf13/afero v1.2.2
github.com/spf13/cast v1.3.0
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/prometheus/procfs v0.0.0-20190328153300-af7bedc223fb // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/jwalterweatherman v1.1.0
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.0.3
github.com/stretchr/testify v1.3.0
github.com/syndtr/goleveldb v1.0.0
github.com/tendermint/btcd v0.1.1
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/tendermint/go-amino v0.15.0
github.com/tendermint/iavl v0.12.2
github.com/tendermint/tendermint v0.31.5
github.com/zondax/hid v0.9.0
github.com/zondax/ledger-go v0.8.0
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65
golang.org/x/text v0.3.0
google.golang.org/genproto v0.0.0-20190327125643-d831d65fe17d
google.golang.org/grpc v1.19.1
gopkg.in/yaml.v2 v2.2.2
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 // indirect
google.golang.org/genproto v0.0.0-20190327125643-d831d65fe17d // indirect
google.golang.org/grpc v1.19.1 // indirect
)

replace golang.org/x/crypto => github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5

0 comments on commit 3a2d53a

Please sign in to comment.