From 3d1af2886aaab70487c474cb80c91ea914700db7 Mon Sep 17 00:00:00 2001 From: rickyyangz Date: Wed, 16 Jan 2019 10:21:40 +0800 Subject: [PATCH 1/9] export iavlStore's tree --- store/codec.go | 1 + store/iavlstore.go | 4 ++++ types/store.go | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/store/codec.go b/store/codec.go index 516f1bdf9..29f3739eb 100644 --- a/store/codec.go +++ b/store/codec.go @@ -11,6 +11,7 @@ type ( Store = types.Store Committer = types.Committer CommitStore = types.CommitStore + TreeStore = types.TreeStore MultiStore = types.MultiStore CacheMultiStore = types.CacheMultiStore CommitMultiStore = types.CommitMultiStore diff --git a/store/iavlstore.go b/store/iavlstore.go index 1eb60ac91..78453cbcd 100644 --- a/store/iavlstore.go +++ b/store/iavlstore.go @@ -66,6 +66,10 @@ func newIAVLStore(tree *iavl.MutableTree, numRecent int64, storeEvery int64) *ia return st } +func (st *iavlStore) GetImmutableTree() *iavl.ImmutableTree { + return st.tree.ImmutableTree +} + // Implements Committer. func (st *iavlStore) Commit() CommitID { // Save a new version. diff --git a/types/store.go b/types/store.go index 053e80a99..fdc5af469 100644 --- a/types/store.go +++ b/types/store.go @@ -4,6 +4,7 @@ import ( "fmt" "io" + "github.com/tendermint/iavl" abci "github.com/tendermint/tendermint/abci/types" cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" @@ -43,6 +44,10 @@ type CommitStore interface { Store } +type TreeStore interface { + GetImmutableTree() *iavl.ImmutableTree +} + // Queryable allows a Store to expose internal state to the abci.Query // interface. Multistore can route requests to the proper Store. // From 1d0ca98b4aa7f203966e71186800d8d2ae002b32 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Thu, 21 Feb 2019 20:54:58 +0800 Subject: [PATCH 2/9] add hooks for gov --- x/gov/client/cli/tx.go | 8 ++++++++ x/gov/errors.go | 5 +++++ x/gov/handler.go | 6 ++++++ x/gov/hooks.go | 17 +++++++++++++++++ x/gov/keeper.go | 12 ++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 x/gov/hooks.go diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 7efe18d84..aa0ec4d31 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -560,6 +560,14 @@ func GetCmdSubmitListProposal(cdc *codec.Codec) *cobra.Command { initPrice := viper.GetInt64(flagInitPrice) expireTimestamp := viper.GetInt64(flagExpireTime) + if tradeAsset == "" { + return errors.New("base asset should not be empty") + } + + if quoteAsset == "" { + return errors.New("quote asset should not be empty") + } + if initPrice <= 0 { return errors.New("init price should greater than 0") } diff --git a/x/gov/errors.go b/x/gov/errors.go index cd1cf8a17..335859b1d 100644 --- a/x/gov/errors.go +++ b/x/gov/errors.go @@ -21,11 +21,16 @@ const ( CodeInvalidVote sdk.CodeType = 9 CodeInvalidGenesis sdk.CodeType = 10 CodeInvalidProposalStatus sdk.CodeType = 11 + CodeInvalidProposal sdk.CodeType = 12 ) //---------------------------------------- // Error constructors +func ErrInvalidProposal(codespace sdk.CodespaceType, msg string) sdk.Error { + return sdk.NewError(codespace, CodeInvalidProposal, fmt.Sprintf("Invalid proposal: %s", msg)) +} + func ErrUnknownProposal(codespace sdk.CodespaceType, proposalID int64) sdk.Error { return sdk.NewError(codespace, CodeUnknownProposal, fmt.Sprintf("Unknown proposal with id %d", proposalID)) } diff --git a/x/gov/handler.go b/x/gov/handler.go index b4f783bb0..d045148d3 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -27,6 +27,12 @@ func NewHandler(keeper Keeper) sdk.Handler { func handleMsgSubmitProposal(ctx sdk.Context, keeper Keeper, msg MsgSubmitProposal) sdk.Result { proposal := keeper.NewTextProposal(ctx, msg.Title, msg.Description, msg.ProposalType) + + hooksErr := keeper.OnProposalSubmitted(ctx, proposal) + if hooksErr != nil { + return ErrInvalidProposal(keeper.codespace, hooksErr.Error()).Result() + } + proposalID := proposal.GetProposalID() proposalIDBytes := []byte(fmt.Sprintf("%d", proposalID)) diff --git a/x/gov/hooks.go b/x/gov/hooks.go new file mode 100644 index 000000000..adc5b0bef --- /dev/null +++ b/x/gov/hooks.go @@ -0,0 +1,17 @@ +package gov + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// event hooks for governance +type GovHooks interface { + OnProposalSubmitted(ctx sdk.Context, proposal Proposal) error // Must be called when a proposal submitted +} + +func (keeper Keeper) OnProposalSubmitted(ctx sdk.Context, proposal Proposal) error { + if keeper.hooks != nil { + return keeper.hooks.OnProposalSubmitted(ctx, proposal) + } + return nil +} diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 87c75d4d4..3e262ef65 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -52,6 +52,9 @@ type Keeper struct { // The codec codec for binary encoding/decoding. cdc *codec.Codec + // Hooks registered + hooks GovHooks + // Reserved codespace codespace sdk.CodespaceType @@ -78,6 +81,15 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, p } } +// SetHooks set hooks for gov keeper +func (keeper Keeper) SetHooks(hooks GovHooks) Keeper { + if keeper.hooks != nil { + panic("cannot set gov hooks twice") + } + keeper.hooks = hooks + return keeper +} + // ===================================================== // Proposals From a8bd417cf3ac1257e1162d66fbf9b790f95a0dcd Mon Sep 17 00:00:00 2001 From: yutianwu Date: Fri, 22 Feb 2019 14:26:10 +0800 Subject: [PATCH 3/9] change sub commands of gov command --- x/gov/client/cli/tx.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index aa0ec4d31..53e06821f 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -7,10 +7,6 @@ import ( "strings" "time" - "github.com/pkg/errors" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" @@ -19,6 +15,9 @@ import ( authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov/client" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) const ( @@ -369,7 +368,7 @@ func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { // GetCmdQueryVote implements the query proposal vote command. func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "vote", + Use: "query-vote", Short: "Query details of a single vote", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) @@ -441,7 +440,7 @@ func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { // GetCmdQueryDeposit implements the query proposal deposit command. func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "deposit", + Use: "query-deposit", Short: "Query details of a deposit", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) From 80fa18a523d34ccd2ce9c013013ce82b2447a0a3 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Fri, 22 Feb 2019 14:28:07 +0800 Subject: [PATCH 4/9] fix imports --- x/gov/client/cli/tx.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 53e06821f..52495555c 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -7,6 +7,10 @@ import ( "strings" "time" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" @@ -15,9 +19,6 @@ import ( authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov/client" - "github.com/pkg/errors" - "github.com/spf13/cobra" - "github.com/spf13/viper" ) const ( From 14f273c114cb0e851786dfa2a654657434fd0fe6 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Fri, 22 Feb 2019 14:39:08 +0800 Subject: [PATCH 5/9] make query commands consistent --- x/gov/client/cli/tx.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 52495555c..5c262b50f 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -257,7 +257,7 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command { // GetCmdQueryProposal implements the query proposal command. func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "proposal", + Use: "query-proposal", Short: "Query details of a single proposal", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) @@ -290,7 +290,7 @@ func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { // GetCmdQueryProposals implements a query proposals command. func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "proposals", + Use: "query-proposals", Short: "Query proposals with optional filters", RunE: func(cmd *cobra.Command, args []string) error { bechDepositerAddr := viper.GetString(flagDepositer) @@ -408,7 +408,7 @@ func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { // GetCmdQueryVotes implements the command to query for proposal votes. func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "votes", + Use: "query-votes", Short: "Query votes on a proposal", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) @@ -480,7 +480,7 @@ func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { // GetCmdQueryDeposits implements the command to query for proposal deposits. func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "deposits", + Use: "query-deposits", Short: "Query deposits on a proposal", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) From 8d07ed2ca127236c338dc97ef1b47dfc811209d6 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Fri, 22 Feb 2019 19:48:22 +0800 Subject: [PATCH 6/9] do some refactor --- x/gov/hooks.go | 8 ++++++-- x/gov/keeper.go | 16 ++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/x/gov/hooks.go b/x/gov/hooks.go index adc5b0bef..475f72f22 100644 --- a/x/gov/hooks.go +++ b/x/gov/hooks.go @@ -10,8 +10,12 @@ type GovHooks interface { } func (keeper Keeper) OnProposalSubmitted(ctx sdk.Context, proposal Proposal) error { - if keeper.hooks != nil { - return keeper.hooks.OnProposalSubmitted(ctx, proposal) + hs := keeper.hooks[proposal.GetProposalType()] + for _, hooks := range hs { + err := hooks.OnProposalSubmitted(ctx, proposal) + if err != nil { + return err + } } return nil } diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 3e262ef65..cffc3ed64 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -2,6 +2,7 @@ package gov import ( "fmt" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" @@ -53,7 +54,7 @@ type Keeper struct { cdc *codec.Codec // Hooks registered - hooks GovHooks + hooks map[ProposalKind][]GovHooks // Reserved codespace codespace sdk.CodespaceType @@ -74,6 +75,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, p paramSpace: paramSpace.WithTypeTable(ParamTypeTable()), ck: ck, ds: ds, + hooks: make(map[ProposalKind][]GovHooks), vs: ds.GetValidatorSet(), cdc: cdc, codespace: codespace, @@ -81,12 +83,14 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, p } } -// SetHooks set hooks for gov keeper -func (keeper Keeper) SetHooks(hooks GovHooks) Keeper { - if keeper.hooks != nil { - panic("cannot set gov hooks twice") +// AddHooks add hooks for gov keeper +func (keeper Keeper) AddHooks(proposalType ProposalKind, hooks GovHooks) Keeper { + hs := keeper.hooks[proposalType] + if hs == nil { + hs = make([]GovHooks, 0, 0) } - keeper.hooks = hooks + hs = append(hs, hooks) + keeper.hooks[proposalType] = hs return keeper } From 5490e4544f7cdc13253e5d9833bd324882c9c681 Mon Sep 17 00:00:00 2001 From: Luke Plaster Date: Thu, 21 Feb 2019 18:40:19 +0800 Subject: [PATCH 7/9] keys cli: remove `keys new` It's not in cosmos https://github.com/cosmos/cosmos-sdk/blob/19f0f92c20bfb6025f3b136b50890a55078d8c19/client/keys/root.go#L24 --- client/keys/root.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client/keys/root.go b/client/keys/root.go index b10cd2b55..bca6f50e8 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -20,7 +20,6 @@ func Commands() *cobra.Command { } cmd.AddCommand( mnemonicKeyCommand(), - newKeyCommand(), addKeyCommand(), listKeysCmd, showKeysCmd(), From 53d9798b25d6f97bf7a4fb38275c439c7ca984a7 Mon Sep 17 00:00:00 2001 From: Luke Plaster Date: Thu, 21 Feb 2019 19:05:01 +0800 Subject: [PATCH 8/9] Delete new.go --- client/keys/new.go | 188 --------------------------------------------- 1 file changed, 188 deletions(-) delete mode 100644 client/keys/new.go diff --git a/client/keys/new.go b/client/keys/new.go deleted file mode 100644 index 6d7422688..000000000 --- a/client/keys/new.go +++ /dev/null @@ -1,188 +0,0 @@ -package keys - -import ( - "fmt" - - "github.com/bartekn/go-bip39" - "github.com/pkg/errors" - "github.com/spf13/cobra" - "github.com/spf13/viper" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/crypto/keys" - "github.com/cosmos/cosmos-sdk/crypto/keys/hd" -) - -const ( - flagNewDefault = "default" - flagBIP44Path = "bip44-path" -) - -func newKeyCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "new", - Short: "Interactive command to derive a new private key, encrypt it, and save to disk", - Long: `Derive a new private key using an interactive command that will prompt you for each input. -Optionally specify a bip39 mnemonic, a bip39 passphrase to further secure the mnemonic, -and a bip32 HD path to derive a specific account. The key will be stored under the given name -and encrypted with the given password. The only input that is required is the encryption password.`, - Args: cobra.ExactArgs(1), - RunE: runNewCmd, - } - cmd.Flags().Bool(flagNewDefault, false, "Skip the prompts and just use the default values for everything") - cmd.Flags().Bool(client.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device") - cmd.Flags().String(flagBIP44Path, "44'/714'/0'/0/0", "BIP44 path from which to derive a private key") - return cmd -} - -/* -input - - bip39 mnemonic - - bip39 passphrase - - bip44 path - - local encryption password -output - - armor encrypted private key (saved to file) -*/ -func runNewCmd(cmd *cobra.Command, args []string) error { - name := args[0] - kb, err := GetKeyBaseWithWritePerm() - if err != nil { - return err - } - - buf := client.BufferStdin() - - _, err = kb.Get(name) - if err == nil { - // account exists, ask for user confirmation - if response, err := client.GetConfirmation( - fmt.Sprintf("> override the existing name %s", name), buf); err != nil || !response { - return err - } - } - - flags := cmd.Flags() - useDefaults, _ := flags.GetBool(flagNewDefault) - bipFlag := flags.Lookup(flagBIP44Path) - - bip44Params, err := getBIP44ParamsAndPath(bipFlag.Value.String(), bipFlag.Changed || useDefaults) - if err != nil { - return err - } - - // If we're using ledger, only thing we need is the path. So generate key and - // we're done. - if viper.GetBool(client.FlagUseLedger) { - algo := keys.Secp256k1 - path := bip44Params.DerivationPath() // ccrypto.DerivationPath{44, 118, account, 0, index} - - info, err := kb.CreateLedger(name, path, algo) - if err != nil { - return err - } - - printCreate(info, "") - return nil - } - - var mnemonic string - - if !useDefaults { - mnemonic, err = client.GetString("Enter your bip39 mnemonic, or hit enter to generate one.", buf) - if err != nil { - return err - } - } - - if len(mnemonic) == 0 { - // read entropy seed straight from crypto.Rand and convert to mnemonic - entropySeed, err := bip39.NewEntropy(mnemonicEntropySize) - if err != nil { - return err - } - - mnemonic, err = bip39.NewMnemonic(entropySeed[:]) - if err != nil { - return err - } - } - - // get bip39 passphrase - var bip39Passphrase string - if !useDefaults { - printStep() - printPrefixed("Enter your bip39 passphrase. This is combined with the mnemonic to derive the seed") - - bip39Passphrase, err = client.GetString("Most users should just hit enter to use the default, \"\"", buf) - if err != nil { - return err - } - - // if they use one, make them re-enter it - if len(bip39Passphrase) != 0 { - p2, err := client.GetString("Repeat the passphrase:", buf) - if err != nil { - return err - } - - if bip39Passphrase != p2 { - return errors.New("passphrases don't match") - } - } - } - - printStep() - - // get the encryption password - encryptPassword, err := client.GetCheckPassword( - "> Enter a passphrase to encrypt your key to disk:", - "> Repeat the passphrase:", buf) - if err != nil { - return err - } - - info, err := kb.Derive(name, mnemonic, bip39Passphrase, encryptPassword, *bip44Params) - if err != nil { - return err - } - - _ = info - return nil -} - -func getBIP44ParamsAndPath(path string, flagSet bool) (*hd.BIP44Params, error) { - buf := client.BufferStdin() - bip44Path := path - - // if it wasn't set in the flag, give it a chance to overide interactively - if !flagSet { - var err error - - printStep() - - bip44Path, err = client.GetString(fmt.Sprintf("Enter your bip44 path. Default is %s\n", path), buf) - if err != nil { - return nil, err - } - - if len(bip44Path) == 0 { - bip44Path = path - } - } - - bip44params, err := hd.NewParamsFromPath(bip44Path) - if err != nil { - return nil, err - } - - return bip44params, nil -} - -func printPrefixed(msg string) { - fmt.Printf("> %s\n", msg) -} - -func printStep() { - printPrefixed("-------------------------------------") -} From 7166963389d561eaac26f47b237f130c11319501 Mon Sep 17 00:00:00 2001 From: Luke Plaster Date: Thu, 21 Feb 2019 19:50:11 +0800 Subject: [PATCH 9/9] client/keys/mnemonic: add printStep, printPrefixed --- client/keys/mnemonic.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/client/keys/mnemonic.go b/client/keys/mnemonic.go index 33270a087..c978b289b 100644 --- a/client/keys/mnemonic.go +++ b/client/keys/mnemonic.go @@ -76,3 +76,12 @@ func runMnemonicCmd(cmd *cobra.Command, args []string) error { return nil } + +func printPrefixed(msg string) { + fmt.Printf("> %s\n", msg) +} + +func printStep() { + printPrefixed("-------------------------------------") +} +