Fix wallet import cycle, better trash key backup

This commit is contained in:
Łukasz Magiera 2021-03-09 16:52:51 +01:00
parent 49c44b70c2
commit 260ddacc47

View File

@ -28,7 +28,6 @@ import (
"github.com/filecoin-project/lotus/extern/sector-storage/stores" "github.com/filecoin-project/lotus/extern/sector-storage/stores"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/node/config" "github.com/filecoin-project/lotus/node/config"
) )
@ -546,19 +545,30 @@ func (fsr *fsLockedRepo) Get(name string) (types.KeyInfo, error) {
return res, nil return res, nil
} }
const KTrashPrefix = "trash-"
// Put saves key info under given name // Put saves key info under given name
func (fsr *fsLockedRepo) Put(name string, info types.KeyInfo) error { func (fsr *fsLockedRepo) Put(name string, info types.KeyInfo) error {
return fsr.put(name, info, 0)
}
func (fsr *fsLockedRepo) put(rawName string, info types.KeyInfo, retries int) error {
if err := fsr.stillValid(); err != nil { if err := fsr.stillValid(); err != nil {
return err return err
} }
name := rawName
if retries > 0 {
name = fmt.Sprintf("%s-%d", rawName, retries)
}
encName := base32.RawStdEncoding.EncodeToString([]byte(name)) encName := base32.RawStdEncoding.EncodeToString([]byte(name))
keyPath := fsr.join(fsKeystore, encName) keyPath := fsr.join(fsKeystore, encName)
_, err := os.Stat(keyPath) _, err := os.Stat(keyPath)
if err == nil && strings.HasPrefix(name, wallet.KTrashPrefix) { if err == nil && strings.HasPrefix(name, KTrashPrefix) {
// Fine to try to write the same trash-prefixed file multiple times // retry writing the trash-prefixed file with a number suffix
return nil return fsr.put(rawName, info, retries+1)
} else if err == nil { } else if err == nil {
return xerrors.Errorf("checking key before put '%s': %w", name, types.ErrKeyExists) return xerrors.Errorf("checking key before put '%s': %w", name, types.ErrKeyExists)
} else if !os.IsNotExist(err) { } else if !os.IsNotExist(err) {