Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R Client Generalization (+ associated module refactor) #4451

Merged
merged 28 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c5b89c3
porting changes from first branch
rigelrozanski May 30, 2019
f4e2f93
fix import cycles
rigelrozanski May 30, 2019
ca8f4cd
circle dep cont.
rigelrozanski May 30, 2019
8206f89
working on compile errors
rigelrozanski May 30, 2019
32e14a5
simapp addition
rigelrozanski May 30, 2019
93d6e60
...
rigelrozanski May 30, 2019
bf09851
working through compile errors
rigelrozanski May 31, 2019
f6decfb
resolve all compile errors
rigelrozanski May 31, 2019
abfbe47
fix bugs
rigelrozanski May 31, 2019
0ed158d
Merge remote-tracking branch 'origin/master' into rigel/client-genera…
rigelrozanski May 31, 2019
5c62e49
gov proposal handler type
rigelrozanski May 31, 2019
d010ed7
client routes
rigelrozanski May 31, 2019
f7246b6
cl
rigelrozanski May 31, 2019
27a52d4
@jleni comments
rigelrozanski Jun 3, 2019
71bf692
@fedekunze pr comments
rigelrozanski Jun 3, 2019
9013c97
Merge remote-tracking branch 'origin/master' into rigel/client-genera…
rigelrozanski Jun 3, 2019
8cc66cc
@jackzampolin comments
rigelrozanski Jun 3, 2019
e5d0b71
Update x/auth/keeper.go
alexanderbez Jun 4, 2019
3b82dc5
Update x/auth/keeper.go
alexanderbez Jun 4, 2019
0e650ae
add auth and bank tx/query subcommands
rigelrozanski Jun 4, 2019
f2ee34c
Merge branch 'rigel/client-generalization2' of https://github.com/cos…
rigelrozanski Jun 4, 2019
bc64c7e
resolve cli issues, global cdc for commands
rigelrozanski Jun 4, 2019
ac9fdc7
Merge remote-tracking branch 'origin/master' into rigel/client-genera…
rigelrozanski Jun 4, 2019
b646cfa
update alias
rigelrozanski Jun 4, 2019
9d81db8
@fedekunze PR comments (round 2)
rigelrozanski Jun 5, 2019
394ecd3
@alexanderbez PR comments (and marko comment on queryRoute)
rigelrozanski Jun 5, 2019
4740a8c
Merge remote-tracking branch 'origin/master' into rigel/client-genera…
rigelrozanski Jun 5, 2019
02ab8f6
Fix pending file
alexanderbez Jun 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4451-RegisterRoutes-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4451 RegisterRoutes and ModuleClient to BasicModule pattern
10 changes: 5 additions & 5 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/x/auth"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/spf13/viper"
"gopkg.in/yaml.v2"
Expand All @@ -36,7 +36,7 @@ var (
// transaction handling and queries.
type CLIContext struct {
Codec *codec.Codec
AccDecoder auth.AccountDecoder
AccDecoder authtypes.AccountDecoder
Client rpcclient.Client
Keybase cryptokeys.Keybase
Output io.Writer
Expand Down Expand Up @@ -90,7 +90,7 @@ func NewCLIContextWithFrom(from string) CLIContext {
Client: rpc,
Output: os.Stdout,
NodeURI: nodeURI,
AccountStore: auth.StoreKey,
AccountStore: authtypes.StoreKey,
From: viper.GetString(flags.FlagFrom),
OutputFormat: viper.GetString(cli.OutputFlag),
Height: viper.GetInt64(flags.FlagHeight),
Expand Down Expand Up @@ -165,8 +165,8 @@ func (ctx CLIContext) WithCodec(cdc *codec.Codec) CLIContext {
}

// GetAccountDecoder gets the account decoder for auth.DefaultAccount.
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
return func(accBytes []byte) (acct auth.Account, err error) {
func GetAccountDecoder(cdc *codec.Codec) authtypes.AccountDecoder {
return func(accBytes []byte) (acct authtypes.Account, err error) {
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
if err != nil {
panic(err)
Expand Down
10 changes: 5 additions & 5 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// GetNode returns an RPC client. If the context's client is not defined, an
Expand Down Expand Up @@ -59,7 +59,7 @@ func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sd

// GetAccount queries for an account given an address and a block height. An
// error is returned if the query or decoding fails.
func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
func (ctx CLIContext) GetAccount(address []byte) (authtypes.Account, error) {
if ctx.AccDecoder == nil {
return nil, errors.New("account decoder required but not provided")
}
Expand All @@ -69,7 +69,7 @@ func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
return nil, err
}

var account auth.Account
var account authtypes.Account
if err := ctx.Codec.UnmarshalJSON(res, &account); err != nil {
return nil, err
}
Expand Down Expand Up @@ -127,12 +127,12 @@ func (ctx CLIContext) EnsureAccountExistsFromAddr(addr sdk.AccAddress) error {
// queryAccount queries an account using custom query endpoint of auth module
// returns an error if result is `null` otherwise account data
func (ctx CLIContext) queryAccount(addr sdk.AccAddress) ([]byte, error) {
bz, err := ctx.Codec.MarshalJSON(auth.NewQueryAccountParams(addr))
bz, err := ctx.Codec.MarshalJSON(authtypes.NewQueryAccountParams(addr))
if err != nil {
return nil, err
}

route := fmt.Sprintf("custom/%s/%s", ctx.AccountStore, auth.QueryAccount)
route := fmt.Sprintf("custom/%s/%s", ctx.AccountStore, authtypes.QueryAccount)

res, err := ctx.QueryWithData(route, bz)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions client/keys/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var cdc *codec.Codec
func init() {
cdc = codec.New()
codec.RegisterCrypto(cdc)
cdc.Seal()
}

// marshal keys
Expand Down
14 changes: 14 additions & 0 deletions client/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package client

import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
)

// Register routes
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
RegisterRPCRoutes(cliCtx, r)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we remove the need for a codec as an argument, this should really only be RegisterRoutes 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that will be possible given that client doesn't define it's own "client" level cdc

RegisterTxRoutes(cliCtx, r, cdc)
}
6 changes: 4 additions & 2 deletions crypto/keys/codec.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package keys

import (
amino "github.com/tendermint/go-amino"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
)

var cdc = amino.NewCodec()
var cdc *codec.Codec

func init() {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
cdc = codec.New()
cryptoAmino.RegisterAmino(cdc)
cdc.RegisterInterface((*Info)(nil), nil)
cdc.RegisterConcrete(hd.BIP44Params{}, "crypto/keys/hd/BIP44Params", nil)
cdc.RegisterConcrete(localInfo{}, "crypto/keys/localInfo", nil)
cdc.RegisterConcrete(ledgerInfo{}, "crypto/keys/ledgerInfo", nil)
cdc.RegisterConcrete(offlineInfo{}, "crypto/keys/offlineInfo", nil)
cdc.RegisterConcrete(multiInfo{}, "crypto/keys/multiInfo", nil)
cdc.Seal()
}
14 changes: 7 additions & 7 deletions crypto/ledger_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"github.com/btcsuite/btcd/btcec"
"github.com/pkg/errors"

secp256k1 "github.com/tendermint/btcd/btcec"
jleni marked this conversation as resolved.
Show resolved Hide resolved
"github.com/tendermint/tendermint/crypto"
tmsecp256k1 "github.com/tendermint/tendermint/crypto/secp256k1"

bip39 "github.com/cosmos/go-bip39"

"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/types"

secp256k1 "github.com/tendermint/btcd/btcec"
"github.com/tendermint/tendermint/crypto"
tmsecp256k1 "github.com/tendermint/tendermint/crypto/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
)

// If ledger support (build tag) has been enabled, which implies a CGO dependency,
Expand All @@ -41,7 +41,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) (
if derivationPath[0] != 44 {
return nil, errors.New("Invalid derivation path")
}
if derivationPath[1] != types.CoinType {
if derivationPath[1] != sdk.CoinType {
return nil, errors.New("Invalid derivation path")
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3
copy(compressedPublicKey[:], cmp.SerializeCompressed())

// Generate the bech32 addr using existing tmcrypto/etc.
addr := types.AccAddress(compressedPublicKey.Address()).String()
addr := sdk.AccAddress(compressedPublicKey.Address()).String()
return pk, addr, err
}

Expand Down
8 changes: 4 additions & 4 deletions crypto/ledger_secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"github.com/btcsuite/btcd/btcec"
"github.com/pkg/errors"

"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/types"

tmbtcec "github.com/tendermint/btcd/btcec"
tmcrypto "github.com/tendermint/tendermint/crypto"
tmsecp256k1 "github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
Expand Down Expand Up @@ -119,7 +119,7 @@ func LedgerShowAddress(path hd.BIP44Params, expectedPubKey tmcrypto.PubKey) erro
return fmt.Errorf("the key's pubkey does not match with the one retrieved from Ledger. Check that the HD path and device are the correct ones")
}

pubKey2, _, err := getPubKeyAddrSafe(device, path, types.Bech32PrefixAccAddr)
pubKey2, _, err := getPubKeyAddrSafe(device, path, sdk.Bech32PrefixAccAddr)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions crypto/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/tests"
"github.com/stretchr/testify/require"

tmcrypto "github.com/tendermint/tendermint/crypto"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8
github.com/cosmos/ledger-cosmos-go v0.10.3
github.com/fortytw2/leaktest v1.3.0 // indirect
github.com/go-kit/kit v0.8.0
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/gogo/protobuf v1.1.1
github.com/golang/protobuf v1.3.0
Expand Down
29 changes: 14 additions & 15 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@ import (
"io"
"os"

abci "github.com/tendermint/tendermint/abci/types"
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"

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/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/genaccounts"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"

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"
)

const appName = "SimApp"
Expand All @@ -35,27 +38,23 @@ var (
// default home directories for the application daemon
DefaultNodeHome = os.ExpandEnv("$HOME/.simapp")

// The ModuleBasicManager is in charge of setting up basic,
// The module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
ModuleBasics sdk.ModuleBasicManager
)

func init() {
ModuleBasics = sdk.NewModuleBasicManager(
ModuleBasics = module.NewBasicManager(
genaccounts.AppModuleBasic{},
genutil.AppModuleBasic{},
auth.AppModuleBasic{},
bank.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.AppModuleBasic{},
gov.NewAppModuleBasic(paramsclient.ProposalHandler, distrclient.ProposalHandler),
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
)
}
)

// custom tx codec
func MakeCodec() *codec.Codec {
Expand Down Expand Up @@ -100,7 +99,7 @@ type SimApp struct {
paramsKeeper params.Keeper

// the module manager
mm *sdk.ModuleManager
mm *module.Manager
}

// NewSimApp returns a reference to an initialized SimApp.
Expand Down Expand Up @@ -169,7 +168,7 @@ func NewSimApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bo
app.stakingKeeper = *stakingKeeper.SetHooks(
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks()))

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
19 changes: 6 additions & 13 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@ import (
func TestSimAppExport(t *testing.T) {
db := db.NewMemDB()
app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0)
setGenesis(app)

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0)
_, _, err := app2.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}

func setGenesis(app *SimApp) error {
genesisState := NewDefaultGenesisState()
stateBytes, err := codec.MarshalJSONIndent(app.cdc, genesisState)
if err != nil {
return err
}
require.NoError(t, err)

// Initialize the chain
app.InitChain(
Expand All @@ -38,7 +28,10 @@ func setGenesis(app *SimApp) error {
AppStateBytes: stateBytes,
},
)

app.Commit()
return nil

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0)
_, _, err = app2.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}
4 changes: 4 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ func (config *Config) SetAddressVerifier(addressVerifier func([]byte) error) {
config.addressVerifier = addressVerifier
}

// Set the BIP-0044 CoinType code on the config
func (config *Config) SetCoinType(coinType uint32) {
config.assertNotSealed()
config.coinType = coinType
}

// Set the FullFundraiserPath (BIP44Prefix) on the config
func (config *Config) SetFullFundraiserPath(fullFundraiserPath string) {
config.assertNotSealed()
config.fullFundraiserPath = fullFundraiserPath
Expand Down Expand Up @@ -144,10 +146,12 @@ func (config *Config) GetAddressVerifier() func([]byte) error {
return config.addressVerifier
}

// Get the BIP-0044 CoinType code on the config
func (config *Config) GetCoinType() uint32 {
return config.coinType
}

// Get the FullFundraiserPath (BIP44Prefix) on the config
func (config *Config) GetFullFundraiserPath() string {
return config.fullFundraiserPath
}
2 changes: 1 addition & 1 deletion types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ type cloner interface {
Clone() interface{} // deep copy
}

// XXX add description
// TODO add description
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
type Op struct {
// type is always 'with'
gen int
Expand Down
Loading