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: don't lock keybase on lcd startup #3514

Merged
merged 12 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 1 deletion Gopkg.lock

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

6 changes: 6 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ BREAKING CHANGES
* [\#3487](https://github.com/cosmos/cosmos-sdk/pull/3487) Move HTTP/REST utilities out of client/utils into a new dedicated client/rest package.
* [\#3490](https://github.com/cosmos/cosmos-sdk/issues/3490) ReadRESTReq() returns bool to avoid callers to write error responses twice.
* [\#3502](https://github.com/cosmos/cosmos-sdk/pull/3502) Fixes issue when comparing genesis states
* [\#3514](https://github.com/cosmos/cosmos-sdk/pull/3514) Various clean ups:
- Replace all GetKeyBase* functions family in favor of NewKeyBaseFromDir and NewKeyBaseFromHomeFlag.
- Remove Get prefix from all TxBuilder's getters.

* Tendermint

Expand All @@ -48,6 +51,8 @@ FEATURES
* SDK
* \#3270 [x/staking] limit number of ongoing unbonding delegations /redelegations per pair/trio
* [\#3477][distribution] new query endpoint "delegator_validators"
* [\#3514](https://github.com/cosmos/cosmos-sdk/pull/3514) Provided a lazy loading implementation of Keybase that locks the underlying
storage only for the time needed to perform the required operation. Also added Keybase reference to TxBuilder struct.

* Tendermint

Expand All @@ -62,6 +67,7 @@ IMPROVEMENTS
* `from` field in the `base_req` body can be a Keybase name or account address
* [\#3423](https://github.com/cosmos/cosmos-sdk/issues/3423) Allow simulation
(auto gas) to work with generate only.
* [\#3514](https://github.com/cosmos/cosmos-sdk/pull/3514) REST server calls to keybase does not lock the underlying storage anymore.

* Gaia CLI (`gaiacli`)
* [\#3476](https://github.com/cosmos/cosmos-sdk/issues/3476) New `withdraw-all-rewards` command to withdraw all delegations rewards for delegators.
Expand Down
3 changes: 2 additions & 1 deletion client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type CLIContext struct {
Codec *codec.Codec
AccDecoder auth.AccountDecoder
Client rpcclient.Client
Keybase cryptokeys.Keybase
Output io.Writer
OutputFormat string
Height int64
Expand Down Expand Up @@ -276,7 +277,7 @@ func GetFromFields(from string) (sdk.AccAddress, string, error) {
return nil, "", nil
}

keybase, err := keys.GetKeyBase()
keybase, err := keys.NewKeyBaseFromHomeFlag()
if err != nil {
return nil, "", err
}
Expand Down
11 changes: 1 addition & 10 deletions client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys"
)

// GetKeyBase initializes a keybase based on the given db.
// The KeyBase manages all activity requiring access to a key.
func GetKeyBase(db dbm.DB) keys.Keybase {
keybase := keys.New(
db,
)
return keybase
}

// MockKeyBase generates an in-memory keybase that will be discarded
// useful for --dry-run to generate a seed phrase without
// storing the key
func MockKeyBase() keys.Keybase {
return GetKeyBase(dbm.NewMemDB())
return keys.New(dbm.NewMemDB())
}
6 changes: 3 additions & 3 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
kb = client.MockKeyBase()
encryptPassword = app.DefaultKeyPass
} else {
kb, err = GetKeyBaseWithWritePerm()
kb, err = NewKeyBaseFromHomeFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -332,7 +332,7 @@ func AddNewKeyRequestHandler(indent bool) http.HandlerFunc {
var kb keys.Keybase
var m AddNewKey

kb, err := GetKeyBaseWithWritePerm()
kb, err := NewKeyBaseFromHomeFlag()
if CheckAndWriteErrorResponse(w, http.StatusInternalServerError, err) {
return
}
Expand Down Expand Up @@ -435,7 +435,7 @@ func RecoverRequestHandler(indent bool) http.HandlerFunc {
return
}

kb, err := GetKeyBaseWithWritePerm()
kb, err := NewKeyBaseFromHomeFlag()
CheckAndWriteErrorResponse(w, http.StatusInternalServerError, err)

if name == "" {
Expand Down
4 changes: 2 additions & 2 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gaiacli.
func runDeleteCmd(cmd *cobra.Command, args []string) error {
name := args[0]

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

kb, err = GetKeyBaseWithWritePerm()
kb, err = NewKeyBaseFromHomeFlag()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
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 := NewKeyBaseFromHomeFlag()
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 := NewKeyBaseFromHomeFlag()
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 @@ -29,7 +29,7 @@ func runUpdateCmd(cmd *cobra.Command, args []string) error {
name := args[0]

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

kb, err = GetKeyBaseWithWritePerm()
kb, err = NewKeyBaseFromHomeFlag()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand Down
53 changes: 9 additions & 44 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"path/filepath"

"github.com/spf13/viper"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/tendermint/tendermint/libs/cli"
dbm "github.com/tendermint/tendermint/libs/db"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -19,15 +17,12 @@ import (
// KeyDBName is the directory under root where we store the keys
const KeyDBName = "keys"

// keybase is used to make GetKeyBase a singleton
var keybase keys.Keybase
Copy link
Contributor

Choose a reason for hiding this comment

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

++


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

// 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 := NewKeyBaseFromHomeFlag()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -73,49 +68,19 @@ func ReadPassphraseFromStdin(name string) (string, error) {
return passphrase, nil
}

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

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

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

// GetKeyBaseFromDirWithWritePerm initializes a keybase at a particular dir with write permissions.
func GetKeyBaseFromDirWithWritePerm(rootDir string) (keys.Keybase, error) {
return getKeyBaseFromDirWithOpts(rootDir, nil)
return NewKeyBaseFromDir(rootDir)
}

// GetKeyBaseFromDir initializes a read-only keybase at a particular dir.
func GetKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
// Disabled because of the inability to create a new keys database directory
// in the instance of when ReadOnly is set to true.
//
// ref: syndtr/goleveldb#240
// return getKeyBaseFromDirWithOpts(rootDir, &opt.Options{ReadOnly: true})
return getKeyBaseFromDirWithOpts(rootDir, nil)
}

func getKeyBaseFromDirWithOpts(rootDir string, o *opt.Options) (keys.Keybase, error) {
if keybase == nil {
db, err := dbm.NewGoLevelDBWithOpts(KeyDBName, filepath.Join(rootDir, "keys"), o)
if err != nil {
return nil, err
}
keybase = client.GetKeyBase(db)
}
return keybase, nil
// NewKeyBaseFromDir initializes a keybase at a particular dir.
func NewKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
return getLazyKeyBaseFromDir(rootDir)
}

// used to set the keybase manually in test
func SetKeyBase(kb keys.Keybase) {
keybase = kb
func getLazyKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
return keys.NewLazyKeybase(KeyDBName, filepath.Join(rootDir, "keys")), nil
}

// create a list of KeyOutput in bech32 format
Expand Down
41 changes: 0 additions & 41 deletions client/keys/utils_test.go

This file was deleted.

Loading