Skip to content

Commit

Permalink
Don't lock keys on read only ops
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia committed Oct 21, 2018
1 parent cca2f9d commit dd84674
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ FEATURES
* [x/stake] [\#1672](https://github.com/cosmos/cosmos-sdk/issues/1672) Implement
basis for the validator commission model.
* [x/auth] Support account removal in the account mapper.
* [client/keys] [\#2544](https://github.com/cosmos/cosmos-sdk/issues/2544) Add `GetReadOnlyKeyBase()` to acquire read-only LevelDB databases.

* Tendermint

Expand Down
2 changes: 1 addition & 1 deletion client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func fromFields(from string) (fromAddr types.AccAddress, fromName string) {
return nil, ""
}

keybase, err := keys.GetKeyBase()
keybase, err := keys.GetReadOnlyKeyBase()
if err != nil {
fmt.Println("no keybase found")
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with their associated name and address.`,
}

func runListCmd(cmd *cobra.Command, args []string) error {
kb, err := GetKeyBase()
kb, err := GetReadOnlyKeyBase()
if err != nil {
return err
}
Expand All @@ -36,7 +36,7 @@ func runListCmd(cmd *cobra.Command, args []string) error {
// query key list REST handler
func QueryKeysRequestHandler(indent bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
kb, err := GetKeyBase()
kb, err := GetReadOnlyKeyBase()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down
21 changes: 16 additions & 5 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 @@ -27,16 +28,22 @@ 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
// GetReadOnlyKeyBase initializes a keybase based on the configuration.
func GetKeyBase() (keys.Keybase, error) {
rootDir := viper.GetString(cli.HomeFlag)
return GetKeyBaseFromDir(rootDir)
return GetKeyBaseFromDir(rootDir, false)
}

// GetReadOnlyKeyBase initializes a read-only keybase based on the configuration.
func GetReadOnlyKeyBase() (keys.Keybase, error) {
rootDir := viper.GetString(cli.HomeFlag)
return GetKeyBaseFromDir(rootDir, true)
}

// 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) {
keybase, err := GetKeyBase()
keybase, err := GetReadOnlyKeyBase()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,9 +90,13 @@ func ReadPassphraseFromStdin(name string) (string, error) {
}

// initialize a keybase based on the configuration
func GetKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
func GetKeyBaseFromDir(rootDir string, readonly bool) (keys.Keybase, error) {
var opts *opt.Options
if readonly {
opts = &opt.Options{ReadOnly: true}
}
if keybase == nil {
db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "keys"))
db, err := dbm.NewGoLevelDBWithOpts(KeyDBName, filepath.Join(rootDir, "keys"), opts)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msg
func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, stdTx auth.StdTx, appendSig bool, offline bool) (auth.StdTx, error) {
var signedStdTx auth.StdTx

keybase, err := keys.GetKeyBase()
keybase, err := keys.GetReadOnlyKeyBase()
if err != nil {
return signedStdTx, err
}
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.GetKeyBaseFromDir(clientRoot, false)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/client/txbuilder/txbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (bldr TxBuilder) BuildWithPubKey(name string, msgs []sdk.Msg) ([]byte, erro
return nil, err
}

keybase, err := keys.GetKeyBase()
keybase, err := keys.GetReadOnlyKeyBase()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (bldr TxBuilder) SignStdTx(name, passphrase string, stdTx auth.StdTx, appen

// MakeSignature builds a StdSignature given key name, passphrase, and a StdSignMsg.
func MakeSignature(name, passphrase string, msg StdSignMsg) (sig auth.StdSignature, err error) {
keybase, err := keys.GetKeyBase()
keybase, err := keys.GetReadOnlyKeyBase()
if err != nil {
return
}
Expand Down

0 comments on commit dd84674

Please sign in to comment.