From 01423226f561d119989bd66d7fa6013d02d14762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Mon, 20 May 2024 15:38:37 +0200 Subject: [PATCH 01/13] feat: mine overlay and clean localstore and kademlia-metrics --- pkg/node/node.go | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 7cbfad14cdb..0ec966041cf 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -17,6 +17,7 @@ import ( "math/big" "net" "net/http" + "os" "path/filepath" "runtime" "sync" @@ -274,11 +275,24 @@ func NewBee( return nil, fmt.Errorf("compute overlay address: %w", err) } - if nonceExists && o.TargetNeighborhood != "" { - logger.Warning("an overlay has already been created before, skipping targeting the selected neighborhood") - } + if o.TargetNeighborhood != "" { + if nonceExists { + logger.Info("Override nonce %d and clean state for neighborhood %s", nonce, o.TargetNeighborhood) + logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...") + time.Sleep(10 * time.Second) - if !nonceExists { + const ( + localstore = "localstore" + kademlia = "kademlia-metrics" + ) + dirsToNuke := []string{localstore, kademlia} + for _, dir := range dirsToNuke { + err = removeContent(filepath.Join(o.DataDir, dir)) + if err != nil { + return nil, fmt.Errorf("delete %s: %w", dir, err) + } + } + } // mine the overlay targetNeighborhood := o.TargetNeighborhood if o.TargetNeighborhood == "" && o.NeighborhoodSuggester != "" { @@ -1238,3 +1252,28 @@ func isChainEnabled(o *Options, swapEndpoint string, logger log.Logger) bool { logger.Info("starting with an enabled chain backend") return true // all other modes operate require chain enabled } + +// removeContent removes all files in path. Copied function from cmd/db.go +func removeContent(path string) error { + dir, err := os.Open(path) + if errors.Is(err, os.ErrNotExist) { + return nil + } + if err != nil { + return err + } + defer dir.Close() + + subpaths, err := dir.Readdirnames(0) + if err != nil { + return err + } + + for _, sub := range subpaths { + err = os.RemoveAll(filepath.Join(path, sub)) + if err != nil { + return err + } + } + return nil +} From a596e0675399cfc3d047179198222af96318e527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Mon, 27 May 2024 15:24:13 +0200 Subject: [PATCH 02/13] feat: nuke statestore --- pkg/node/node.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 0ec966041cf..b9766a605a4 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -284,8 +284,9 @@ func NewBee( const ( localstore = "localstore" kademlia = "kademlia-metrics" + statestore = "statestore" ) - dirsToNuke := []string{localstore, kademlia} + dirsToNuke := []string{localstore, kademlia, statestore} for _, dir := range dirsToNuke { err = removeContent(filepath.Join(o.DataDir, dir)) if err != nil { From 9e70aff0d9c68dc3461a3d0700d2ea4bd436b2e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 5 Jun 2024 10:27:02 +0200 Subject: [PATCH 03/13] refactor: move removeContent to ioutil --- pkg/node/node.go | 28 +--------------------------- pkg/util/ioutil/ioutil.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index b9766a605a4..bc688ab1ae2 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -17,7 +17,6 @@ import ( "math/big" "net" "net/http" - "os" "path/filepath" "runtime" "sync" @@ -288,7 +287,7 @@ func NewBee( ) dirsToNuke := []string{localstore, kademlia, statestore} for _, dir := range dirsToNuke { - err = removeContent(filepath.Join(o.DataDir, dir)) + err = ioutil.RemoveContent(filepath.Join(o.DataDir, dir)) if err != nil { return nil, fmt.Errorf("delete %s: %w", dir, err) } @@ -1253,28 +1252,3 @@ func isChainEnabled(o *Options, swapEndpoint string, logger log.Logger) bool { logger.Info("starting with an enabled chain backend") return true // all other modes operate require chain enabled } - -// removeContent removes all files in path. Copied function from cmd/db.go -func removeContent(path string) error { - dir, err := os.Open(path) - if errors.Is(err, os.ErrNotExist) { - return nil - } - if err != nil { - return err - } - defer dir.Close() - - subpaths, err := dir.Readdirnames(0) - if err != nil { - return err - } - - for _, sub := range subpaths { - err = os.RemoveAll(filepath.Join(path, sub)) - if err != nil { - return err - } - } - return nil -} diff --git a/pkg/util/ioutil/ioutil.go b/pkg/util/ioutil/ioutil.go index bd2c98fc208..3311b60cdc1 100644 --- a/pkg/util/ioutil/ioutil.go +++ b/pkg/util/ioutil/ioutil.go @@ -4,6 +4,12 @@ package ioutil +import ( + "errors" + "os" + "path/filepath" +) + // The WriterFunc type is an adapter to allow the use of // ordinary functions as io.Writer Write method. If f is // a function with the appropriate signature, WriterFunc(f) @@ -14,3 +20,28 @@ type WriterFunc func([]byte) (int, error) func (f WriterFunc) Write(p []byte) (n int, err error) { return f(p) } + +// RemoveContent removes all files in path. Copied function from cmd/db.go +func RemoveContent(path string) error { + dir, err := os.Open(path) + if errors.Is(err, os.ErrNotExist) { + return nil + } + if err != nil { + return err + } + defer dir.Close() + + subpaths, err := dir.Readdirnames(0) + if err != nil { + return err + } + + for _, sub := range subpaths { + err = os.RemoveAll(filepath.Join(path, sub)) + if err != nil { + return err + } + } + return nil +} From fe6463202ea85c0e0aff5b6e9a2188d5852715cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 5 Jun 2024 11:07:33 +0200 Subject: [PATCH 04/13] fix: targetNeighborhood handling --- pkg/node/node.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index bc688ab1ae2..75855d688b7 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -274,9 +274,18 @@ func NewBee( return nil, fmt.Errorf("compute overlay address: %w", err) } - if o.TargetNeighborhood != "" { + targetNeighborhood := o.TargetNeighborhood + if targetNeighborhood == "" && o.NeighborhoodSuggester != "" { + logger.Info("fetching target neighborhood from suggester", "url", o.NeighborhoodSuggester) + targetNeighborhood, err = nbhdutil.FetchNeighborhood(&http.Client{}, o.NeighborhoodSuggester) + if err != nil { + return nil, fmt.Errorf("neighborhood suggestion: %w", err) + } + } + + if targetNeighborhood != "" { if nonceExists { - logger.Info("Override nonce %d and clean state for neighborhood %s", nonce, o.TargetNeighborhood) + logger.Info("Override nonce %d and clean state for neighborhood %s", nonce, targetNeighborhood) logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...") time.Sleep(10 * time.Second) @@ -294,21 +303,10 @@ func NewBee( } } // mine the overlay - targetNeighborhood := o.TargetNeighborhood - if o.TargetNeighborhood == "" && o.NeighborhoodSuggester != "" { - logger.Info("fetching target neighborhood from suggester", "url", o.NeighborhoodSuggester) - targetNeighborhood, err = nbhdutil.FetchNeighborhood(&http.Client{}, o.NeighborhoodSuggester) - if err != nil { - return nil, fmt.Errorf("neighborhood suggestion: %w", err) - } - } - - if targetNeighborhood != "" { - logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood) - swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood) - if err != nil { - return nil, fmt.Errorf("mine overlay address: %w", err) - } + logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood) + swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood) + if err != nil { + return nil, fmt.Errorf("mine overlay address: %w", err) } err = setOverlay(stateStore, swarmAddress, nonce) From a2c7d0a606a9b158129a870e7ba2b02ff63ddc40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 5 Jun 2024 13:19:07 +0200 Subject: [PATCH 05/13] fix: not nuke if not necessary --- pkg/node/node.go | 63 +++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 75855d688b7..9401a328d4c 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -283,32 +283,51 @@ func NewBee( } } + // set overlay if nonce is not saved or mine if targetNeighborhood != "" { - if nonceExists { - logger.Info("Override nonce %d and clean state for neighborhood %s", nonce, targetNeighborhood) - logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...") - time.Sleep(10 * time.Second) - - const ( - localstore = "localstore" - kademlia = "kademlia-metrics" - statestore = "statestore" - ) - dirsToNuke := []string{localstore, kademlia, statestore} - for _, dir := range dirsToNuke { - err = ioutil.RemoveContent(filepath.Join(o.DataDir, dir)) - if err != nil { - return nil, fmt.Errorf("delete %s: %w", dir, err) - } - } - } - // mine the overlay - logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood) - swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood) + neighborhood, err := swarm.ParseBitStrAddress(targetNeighborhood) if err != nil { - return nil, fmt.Errorf("mine overlay address: %w", err) + return nil, fmt.Errorf("invalid neighborhood. %s", targetNeighborhood) } + if swarm.Proximity(swarmAddress.Bytes(), neighborhood.Bytes()) < uint8(len(targetNeighborhood)) { + if nonceExists { + logger.Info("Override nonce %d and clean state for neighborhood %s", nonce, targetNeighborhood) + logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...") + time.Sleep(10 * time.Second) + + const ( + localstore = "localstore" + kademlia = "kademlia-metrics" + statestore = "statestore" + ) + dirsToNuke := []string{localstore, kademlia, statestore} + for _, dir := range dirsToNuke { + err = ioutil.RemoveContent(filepath.Join(o.DataDir, dir)) + if err != nil { + return nil, fmt.Errorf("delete %s: %w", dir, err) + } + } + } + + // mine the overlay + logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood) + swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood) + if err != nil { + return nil, fmt.Errorf("mine overlay address: %w", err) + } + + err = setOverlay(stateStore, swarmAddress, nonce) + if err != nil { + return nil, fmt.Errorf("statestore: save new overlay: %w", err) + } + } else if !nonceExists { + err = setOverlay(stateStore, swarmAddress, nonce) + if err != nil { + return nil, fmt.Errorf("statestore: save new overlay: %w", err) + } + } + } else if targetNeighborhood == "" && !nonceExists { err = setOverlay(stateStore, swarmAddress, nonce) if err != nil { return nil, fmt.Errorf("statestore: save new overlay: %w", err) From 92d0cc23596fa5eec93332ec4952d165ad3e7300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 5 Jun 2024 13:23:25 +0200 Subject: [PATCH 06/13] refactor: setOverlay is done by checkOverlay --- pkg/node/node.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 9401a328d4c..2f628d772bc 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -321,21 +321,12 @@ func NewBee( if err != nil { return nil, fmt.Errorf("statestore: save new overlay: %w", err) } - } else if !nonceExists { - err = setOverlay(stateStore, swarmAddress, nonce) - if err != nil { - return nil, fmt.Errorf("statestore: save new overlay: %w", err) - } - } - } else if targetNeighborhood == "" && !nonceExists { - err = setOverlay(stateStore, swarmAddress, nonce) - if err != nil { - return nil, fmt.Errorf("statestore: save new overlay: %w", err) } } logger.Info("using overlay address", "address", swarmAddress) + // this will set overlay if it was not set before if err = checkOverlay(stateStore, swarmAddress); err != nil { return nil, fmt.Errorf("check overlay address: %w", err) } From 8f016ec7df1a95df825954ca7438243e87f029d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 5 Jun 2024 13:29:18 +0200 Subject: [PATCH 07/13] chore: remove comment --- pkg/node/node.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 2f628d772bc..ca5254e0013 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -283,7 +283,6 @@ func NewBee( } } - // set overlay if nonce is not saved or mine if targetNeighborhood != "" { neighborhood, err := swarm.ParseBitStrAddress(targetNeighborhood) if err != nil { From ca1aaaf3d37e20e51ac9179933a851983139cbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Thu, 6 Jun 2024 07:01:05 +0200 Subject: [PATCH 08/13] feat: use neighborhoodsuggester only if nonce does not exist --- pkg/node/node.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index ca5254e0013..e7057fb66b8 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -275,7 +275,7 @@ func NewBee( } targetNeighborhood := o.TargetNeighborhood - if targetNeighborhood == "" && o.NeighborhoodSuggester != "" { + if targetNeighborhood == "" && !nonceExists && o.NeighborhoodSuggester != "" { logger.Info("fetching target neighborhood from suggester", "url", o.NeighborhoodSuggester) targetNeighborhood, err = nbhdutil.FetchNeighborhood(&http.Client{}, o.NeighborhoodSuggester) if err != nil { From 7b2cffb8eb7b657c7a77b5b135bec67a997fea97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Mon, 10 Jun 2024 11:54:02 +0200 Subject: [PATCH 09/13] fix: reinit statestore --- pkg/node/node.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index e7057fb66b8..3e97aae4e34 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -248,16 +248,6 @@ func NewBee( if err != nil { return nil, err } - b.stateStoreCloser = stateStore - - // Check if the batchstore exists. If not, we can assume it's missing - // due to a migration or it's a fresh install. - batchStoreExists, err := batchStoreExists(stateStore) - if err != nil { - return nil, fmt.Errorf("batchstore: exists: %w", err) - } - - addressbook := addressbook.New(stateStore) pubKey, err := signer.PublicKey() if err != nil { @@ -290,8 +280,15 @@ func NewBee( } if swarm.Proximity(swarmAddress.Bytes(), neighborhood.Bytes()) < uint8(len(targetNeighborhood)) { + // mine the overlay + logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood) + newSwarmAddress, newNonce, err := nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood) + if err != nil { + return nil, fmt.Errorf("mine overlay address: %w", err) + } + if nonceExists { - logger.Info("Override nonce %d and clean state for neighborhood %s", nonce, targetNeighborhood) + logger.Info("Override nonce %d to %d and clean state for neighborhood %s", nonce, newNonce, targetNeighborhood) logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...") time.Sleep(10 * time.Second) @@ -302,18 +299,19 @@ func NewBee( ) dirsToNuke := []string{localstore, kademlia, statestore} for _, dir := range dirsToNuke { - err = ioutil.RemoveContent(filepath.Join(o.DataDir, dir)) + err := ioutil.RemoveContent(filepath.Join(o.DataDir, dir)) if err != nil { return nil, fmt.Errorf("delete %s: %w", dir, err) } } } - // mine the overlay - logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood) - swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood) + // reinit states variables + swarmAddress = newSwarmAddress + nonce = newNonce + stateStore, stateStoreMetrics, err = InitStateStore(logger, o.DataDir, o.StatestoreCacheCapacity) if err != nil { - return nil, fmt.Errorf("mine overlay address: %w", err) + return nil, err } err = setOverlay(stateStore, swarmAddress, nonce) @@ -323,6 +321,16 @@ func NewBee( } } + b.stateStoreCloser = stateStore + // Check if the batchstore exists. If not, we can assume it's missing + // due to a migration or it's a fresh install. + batchStoreExists, err := batchStoreExists(stateStore) + if err != nil { + return nil, fmt.Errorf("batchstore: exists: %w", err) + } + + addressbook := addressbook.New(stateStore) + logger.Info("using overlay address", "address", swarmAddress) // this will set overlay if it was not set before From f484d4980404be8aa5997f83ce23f6a4a401434a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 12 Jun 2024 12:40:05 +0200 Subject: [PATCH 10/13] feat: clearForHopping --- pkg/node/node.go | 11 +++-------- pkg/node/statestore.go | 2 +- pkg/statestore/storeadapter/storeadapter.go | 16 ++++++++++++++++ pkg/storage/statestore.go | 8 ++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 3e97aae4e34..3a9f11f067b 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -295,25 +295,20 @@ func NewBee( const ( localstore = "localstore" kademlia = "kademlia-metrics" - statestore = "statestore" ) - dirsToNuke := []string{localstore, kademlia, statestore} + dirsToNuke := []string{localstore, kademlia} for _, dir := range dirsToNuke { err := ioutil.RemoveContent(filepath.Join(o.DataDir, dir)) if err != nil { return nil, fmt.Errorf("delete %s: %w", dir, err) } } + + stateStore.ClearForHopping() } - // reinit states variables swarmAddress = newSwarmAddress nonce = newNonce - stateStore, stateStoreMetrics, err = InitStateStore(logger, o.DataDir, o.StatestoreCacheCapacity) - if err != nil { - return nil, err - } - err = setOverlay(stateStore, swarmAddress, nonce) if err != nil { return nil, fmt.Errorf("statestore: save new overlay: %w", err) diff --git a/pkg/node/statestore.go b/pkg/node/statestore.go index 5e42badd318..626848cf88e 100644 --- a/pkg/node/statestore.go +++ b/pkg/node/statestore.go @@ -21,7 +21,7 @@ import ( // InitStateStore will initialize the stateStore with the given path to the // data directory. When given an empty directory path, the function will instead // initialize an in-memory state store that will not be persisted. -func InitStateStore(logger log.Logger, dataDir string, cacheCapacity uint64) (storage.StateStorer, metrics.Collector, error) { +func InitStateStore(logger log.Logger, dataDir string, cacheCapacity uint64) (storage.StateStorerManager, metrics.Collector, error) { if dataDir == "" { logger.Warning("using in-mem state store, no node state will be persisted") } else { diff --git a/pkg/statestore/storeadapter/storeadapter.go b/pkg/statestore/storeadapter/storeadapter.go index 7dafb10d4f6..6f979933475 100644 --- a/pkg/statestore/storeadapter/storeadapter.go +++ b/pkg/statestore/storeadapter/storeadapter.go @@ -198,6 +198,22 @@ func (s *StateStorerAdapter) Nuke() error { return s.deleteKeys(keys) } +func (s *StateStorerAdapter) ClearForHopping() error { + var ( + prefixesToPreserve = []string{ + "swap_chequebook", + } + keys []string + err error + ) + + keys, err = s.collectKeysExcept(prefixesToPreserve) + if err != nil { + return fmt.Errorf("collect keys except: %w", err) + } + return s.deleteKeys(keys) +} + func (s *StateStorerAdapter) collectKeysExcept(prefixesToPreserve []string) (keys []string, err error) { if err := s.Iterate("", func(k, v []byte) (bool, error) { stk := string(k) diff --git a/pkg/storage/statestore.go b/pkg/storage/statestore.go index 4e39359ff33..53520dea1be 100644 --- a/pkg/storage/statestore.go +++ b/pkg/storage/statestore.go @@ -32,4 +32,12 @@ type StateStorer interface { type StateStorerCleaner interface { // Nuke the store so that only the bare essential entries are left. Nuke() error + // ClearForHopping removes all data not required in a new neighborhood + ClearForHopping() error +} + +// StateStorerManager defines all external methods of the state storage +type StateStorerManager interface { + StateStorer + StateStorerCleaner } From 6000bddfed17be9303cb71033c7f4ab244f246be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Wed, 12 Jun 2024 14:46:37 +0200 Subject: [PATCH 11/13] chore: add more prefixes to preserve --- pkg/statestore/storeadapter/storeadapter.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/statestore/storeadapter/storeadapter.go b/pkg/statestore/storeadapter/storeadapter.go index 6f979933475..2f05f1f31c7 100644 --- a/pkg/statestore/storeadapter/storeadapter.go +++ b/pkg/statestore/storeadapter/storeadapter.go @@ -201,7 +201,9 @@ func (s *StateStorerAdapter) Nuke() error { func (s *StateStorerAdapter) ClearForHopping() error { var ( prefixesToPreserve = []string{ - "swap_chequebook", + "swap_chequebook", // to not redeploy chequebook contract + "batchstore", // avoid unnecessary syncing + "transaction", // to not resync blockchain transactions } keys []string err error From 4a1886ba02237f7510f0efc1b8a9915fe49c75a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Thu, 13 Jun 2024 08:19:32 +0200 Subject: [PATCH 12/13] fix: error clearForHopping checking --- pkg/node/node.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 3a9f11f067b..e196d9a2c1a 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -304,7 +304,9 @@ func NewBee( } } - stateStore.ClearForHopping() + if err := stateStore.ClearForHopping(); err != nil { + return nil, fmt.Errorf("clearing stateStore %w", err) + } } swarmAddress = newSwarmAddress From b8bcef0b4ab83ecb953a0646c0483e37817d26c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Thu, 13 Jun 2024 12:18:05 +0200 Subject: [PATCH 13/13] refactor: move db path constants to ioutil --- cmd/bee/cmd/db.go | 11 ++++++----- pkg/node/devnode.go | 2 +- pkg/node/node.go | 8 ++------ pkg/topology/kademlia/kademlia.go | 3 ++- pkg/util/ioutil/ioutil.go | 6 ++++++ 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/bee/cmd/db.go b/cmd/bee/cmd/db.go index 5b07f43c571..73a53b47330 100644 --- a/cmd/bee/cmd/db.go +++ b/cmd/bee/cmd/db.go @@ -25,6 +25,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/storer" "github.com/ethersphere/bee/v2/pkg/storer/migration" "github.com/ethersphere/bee/v2/pkg/swarm" + "github.com/ethersphere/bee/v2/pkg/util/ioutil" "github.com/spf13/cobra" ) @@ -159,7 +160,7 @@ func dbCompactCmd(cmd *cobra.Command) { time.Sleep(10 * time.Second) logger.Warning("proceeding with database compaction...") - localstorePath := path.Join(dataDir, "localstore") + localstorePath := path.Join(dataDir, ioutil.DataPathLocalstore) err = storer.Compact(context.Background(), localstorePath, &storer.Options{ Logger: logger, @@ -214,7 +215,7 @@ func dbValidatePinsCmd(cmd *cobra.Command) { return fmt.Errorf("read location option: %w", err) } - localstorePath := path.Join(dataDir, "localstore") + localstorePath := path.Join(dataDir, ioutil.DataPathLocalstore) err = storer.ValidatePinCollectionChunks(context.Background(), localstorePath, providedPin, outputLoc, &storer.Options{ Logger: logger, @@ -340,7 +341,7 @@ func dbValidateCmd(cmd *cobra.Command) { logger.Warning(" Progress logged at Info level.") logger.Warning(" SOC chunks logged at Debug level.") - localstorePath := path.Join(dataDir, "localstore") + localstorePath := path.Join(dataDir, ioutil.DataPathLocalstore) err = storer.ValidateRetrievalIndex(context.Background(), localstorePath, &storer.Options{ Logger: logger, @@ -768,8 +769,8 @@ func dbNukeCmd(cmd *cobra.Command) { optionNameForgetOverlay = "forget-overlay" optionNameForgetStamps = "forget-stamps" - localstore = "localstore" - kademlia = "kademlia-metrics" + localstore = ioutil.DataPathLocalstore + kademlia = ioutil.DataPathKademlia statestore = "statestore" stamperstore = "stamperstore" ) diff --git a/pkg/node/devnode.go b/pkg/node/devnode.go index fe9476d9d00..c8e35936920 100644 --- a/pkg/node/devnode.go +++ b/pkg/node/devnode.go @@ -425,7 +425,7 @@ func (b *DevBee) Shutdown() error { tryClose(b.pssCloser, "pss") tryClose(b.tracerCloser, "tracer") tryClose(b.stateStoreCloser, "statestore") - tryClose(b.localstoreCloser, "localstore") + tryClose(b.localstoreCloser, ioutil.DataPathLocalstore) return mErr } diff --git a/pkg/node/node.go b/pkg/node/node.go index e196d9a2c1a..b0c64e21592 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -292,11 +292,7 @@ func NewBee( logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...") time.Sleep(10 * time.Second) - const ( - localstore = "localstore" - kademlia = "kademlia-metrics" - ) - dirsToNuke := []string{localstore, kademlia} + dirsToNuke := []string{ioutil.DataPathLocalstore, ioutil.DataPathKademlia} for _, dir := range dirsToNuke { err := ioutil.RemoveContent(filepath.Join(o.DataDir, dir)) if err != nil { @@ -698,7 +694,7 @@ func NewBee( if o.DataDir != "" { logger.Info("using datadir", "path", o.DataDir) - path = filepath.Join(o.DataDir, "localstore") + path = filepath.Join(o.DataDir, ioutil.DataPathLocalstore) } lo := &storer.Options{ diff --git a/pkg/topology/kademlia/kademlia.go b/pkg/topology/kademlia/kademlia.go index 5fe6f7f2f4d..2e1f88c4b5e 100644 --- a/pkg/topology/kademlia/kademlia.go +++ b/pkg/topology/kademlia/kademlia.go @@ -26,6 +26,7 @@ import ( im "github.com/ethersphere/bee/v2/pkg/topology/kademlia/internal/metrics" "github.com/ethersphere/bee/v2/pkg/topology/kademlia/internal/waitnext" "github.com/ethersphere/bee/v2/pkg/topology/pslice" + "github.com/ethersphere/bee/v2/pkg/util/ioutil" ma "github.com/multiformats/go-multiaddr" "golang.org/x/sync/errgroup" ) @@ -209,7 +210,7 @@ func New( if o.DataDir == "" { logger.Warning("using in-mem store for kademlia metrics, no state will be persisted") } else { - o.DataDir = filepath.Join(o.DataDir, "kademlia-metrics") + o.DataDir = filepath.Join(o.DataDir, ioutil.DataPathKademlia) } sdb, err := shed.NewDB(o.DataDir, nil) if err != nil { diff --git a/pkg/util/ioutil/ioutil.go b/pkg/util/ioutil/ioutil.go index 3311b60cdc1..31418669f1e 100644 --- a/pkg/util/ioutil/ioutil.go +++ b/pkg/util/ioutil/ioutil.go @@ -10,6 +10,12 @@ import ( "path/filepath" ) +// DB folders paths from bee datadir +const ( + DataPathLocalstore = "localstore" + DataPathKademlia = "kademlia-metrics" +) + // The WriterFunc type is an adapter to allow the use of // ordinary functions as io.Writer Write method. If f is // a function with the appropriate signature, WriterFunc(f)