Skip to content

Commit

Permalink
#1255 make keybase opened with readonly option to support better para…
Browse files Browse the repository at this point in the history
…llelization between gaiacli
  • Loading branch information
ackratos committed Oct 20, 2018
1 parent 57277d1 commit 6c623b2
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ IMPROVEMENTS
* Gaia CLI (`gaiacli`)
* [cli] #2060 removed `--select` from `block` command
* [cli] #2128 fixed segfault when exporting directly after `gaiad init`
* [cli] [\#1255](https://github.com/cosmos/cosmos-sdk/issues/1255) make keybase opened with readonly option for query-purpose cli commands

* Gaia
* [x/stake] [#2023](https://github.com/cosmos/cosmos-sdk/pull/2023) Terminate iteration loop in `UpdateBondedValidators` and `UpdateBondedValidatorsFull` when the first revoked validator is encountered and perform a sanity check.
Expand Down
6 changes: 3 additions & 3 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
return errMissingName()
}
name = args[0]
kb, err = GetKeyBase()
kb, err = GetKeyBaseWithWritePerm()
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func AddNewKeyRequestHandler(indent bool) http.HandlerFunc {
var kb keys.Keybase
var m NewKeyBody

kb, err := GetKeyBase()
kb, err := GetKeyBaseWithWritePerm()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down Expand Up @@ -311,7 +311,7 @@ func RecoverRequestHandler(indent bool) http.HandlerFunc {
return
}

kb, err := GetKeyBase()
kb, err := GetKeyBaseWithWritePerm()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down
4 changes: 2 additions & 2 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func deleteKeyCommand() *cobra.Command {
func runDeleteCmd(cmd *cobra.Command, args []string) error {
name := args[0]

kb, err := GetKeyBase()
kb, err := GetKeyBaseWithWritePerm()
if err != nil {
return err
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}

kb, err = GetKeyBase()
kb, err = GetKeyBaseWithWritePerm()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down
4 changes: 2 additions & 2 deletions client/keys/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func runUpdateCmd(cmd *cobra.Command, args []string) error {
name := args[0]

buf := client.BufferStdin()
kb, err := GetKeyBase()
kb, err := GetKeyBaseWithWritePerm()
if err != nil {
return err
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func UpdateKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}

kb, err = GetKeyBase()
kb, err = GetKeyBaseWithWritePerm()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down
31 changes: 21 additions & 10 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keys

import (
"fmt"
"github.com/syndtr/goleveldb/leveldb/opt"
"path/filepath"

"github.com/spf13/viper"
Expand All @@ -25,14 +26,6 @@ var keybase keys.Keybase

type bechKeyOutFn func(keyInfo keys.Info) (KeyOutput, error)

// TODO make keybase take a database not load from the directory

// initialize a keybase based on the configuration
func GetKeyBase() (keys.Keybase, error) {
rootDir := viper.GetString(cli.HomeFlag)
return GetKeyBaseFromDir(rootDir)
}

// GetKeyInfo returns key info for a given name. An error is returned if the
// keybase cannot be retrieved or getting the info fails.
func GetKeyInfo(name string) (keys.Info, error) {
Expand Down Expand Up @@ -82,10 +75,28 @@ func ReadPassphraseFromStdin(name string) (string, error) {
return passphrase, nil
}

// TODO make keybase take a database not load from the directory

// initialize a keybase based on the configuration
func GetKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
func GetKeyBase() (keys.Keybase, error) {
rootDir := viper.GetString(cli.HomeFlag)
return getKeyBaseFromDirWithOpts(rootDir, &opt.Options{ReadOnly: true})
}

// initialize a keybase based on the configuration with write permission
func GetKeyBaseWithWritePerm() (keys.Keybase, error) {
rootDir := viper.GetString(cli.HomeFlag)
return GetKeyBaseFromDirWithWritePerm(rootDir)
}

// initialize a keybase at particular dir with write permission
func GetKeyBaseFromDirWithWritePerm(rootDir string) (keys.Keybase, error) {
return getKeyBaseFromDirWithOpts(rootDir, &opt.Options{ReadOnly: false})
}

func getKeyBaseFromDirWithOpts(rootDir string, o *opt.Options) (keys.Keybase, error) {
if keybase == nil {
db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "keys"))
db, err := dbm.NewGoLevelDBWithOpts(KeyDBName, filepath.Join(rootDir, "keys"), o)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func GetKeyBase(t *testing.T) crkeys.Keybase {

viper.Set(cli.HomeFlag, dir)

keybase, err := keys.GetKeyBase()
keybase, err := keys.GetKeyBaseWithWritePerm()
require.NoError(t, err)

return keybase
Expand Down
2 changes: 1 addition & 1 deletion server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func GenerateCoinKey() (sdk.AccAddress, string, error) {
func GenerateSaveCoinKey(clientRoot, keyName, keyPass string, overwrite bool) (sdk.AccAddress, string, error) {

// get the keystore from the client
keybase, err := clkeys.GetKeyBaseFromDir(clientRoot)
keybase, err := clkeys.GetKeyBaseFromDirWithWritePerm(clientRoot)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
Expand Down

0 comments on commit 6c623b2

Please sign in to comment.