From 49c44b70c2bdc767b960fccb09983010904eb107 Mon Sep 17 00:00:00 2001 From: jennijuju Date: Thu, 29 Oct 2020 17:33:20 -0400 Subject: [PATCH 1/2] Aftering importing a previously deleted key, be able to delete it again. Co-authored-by: Aayush Rajasekaran --- node/repo/fsrepo.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index d96a5e645..cfc80ddaa 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -28,6 +28,7 @@ import ( "github.com/filecoin-project/lotus/extern/sector-storage/stores" "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/chain/wallet" "github.com/filecoin-project/lotus/node/config" ) @@ -555,7 +556,10 @@ func (fsr *fsLockedRepo) Put(name string, info types.KeyInfo) error { keyPath := fsr.join(fsKeystore, encName) _, err := os.Stat(keyPath) - if err == nil { + if err == nil && strings.HasPrefix(name, wallet.KTrashPrefix) { + // Fine to try to write the same trash-prefixed file multiple times + return nil + } else if err == nil { return xerrors.Errorf("checking key before put '%s': %w", name, types.ErrKeyExists) } else if !os.IsNotExist(err) { return xerrors.Errorf("checking key before put '%s': %w", name, err) From 260ddacc47887f9e86701c154e4cad1cb7c18764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 9 Mar 2021 16:52:51 +0100 Subject: [PATCH 2/2] Fix wallet import cycle, better trash key backup --- node/repo/fsrepo.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/node/repo/fsrepo.go b/node/repo/fsrepo.go index cfc80ddaa..a40ae62d0 100644 --- a/node/repo/fsrepo.go +++ b/node/repo/fsrepo.go @@ -28,7 +28,6 @@ import ( "github.com/filecoin-project/lotus/extern/sector-storage/stores" "github.com/filecoin-project/lotus/chain/types" - "github.com/filecoin-project/lotus/chain/wallet" "github.com/filecoin-project/lotus/node/config" ) @@ -546,19 +545,30 @@ func (fsr *fsLockedRepo) Get(name string) (types.KeyInfo, error) { return res, nil } +const KTrashPrefix = "trash-" + // Put saves key info under given name 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 { return err } + name := rawName + if retries > 0 { + name = fmt.Sprintf("%s-%d", rawName, retries) + } + encName := base32.RawStdEncoding.EncodeToString([]byte(name)) keyPath := fsr.join(fsKeystore, encName) _, err := os.Stat(keyPath) - if err == nil && strings.HasPrefix(name, wallet.KTrashPrefix) { - // Fine to try to write the same trash-prefixed file multiple times - return nil + if err == nil && strings.HasPrefix(name, KTrashPrefix) { + // retry writing the trash-prefixed file with a number suffix + return fsr.put(rawName, info, retries+1) } else if err == nil { return xerrors.Errorf("checking key before put '%s': %w", name, types.ErrKeyExists) } else if !os.IsNotExist(err) {