github.com/ethersphere/bee/v2@v2.2.0/pkg/node/statestore.go (about) 1 // Copyright 2021 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package node 6 7 import ( 8 "errors" 9 "fmt" 10 "path/filepath" 11 12 "github.com/ethersphere/bee/v2/pkg/log" 13 "github.com/ethersphere/bee/v2/pkg/metrics" 14 "github.com/ethersphere/bee/v2/pkg/statestore/storeadapter" 15 "github.com/ethersphere/bee/v2/pkg/storage" 16 "github.com/ethersphere/bee/v2/pkg/storage/cache" 17 "github.com/ethersphere/bee/v2/pkg/storage/leveldbstore" 18 "github.com/ethersphere/bee/v2/pkg/swarm" 19 ) 20 21 // InitStateStore will initialize the stateStore with the given path to the 22 // data directory. When given an empty directory path, the function will instead 23 // initialize an in-memory state store that will not be persisted. 24 func InitStateStore(logger log.Logger, dataDir string, cacheCapacity uint64) (storage.StateStorerManager, metrics.Collector, error) { 25 if dataDir == "" { 26 logger.Warning("using in-mem state store, no node state will be persisted") 27 } else { 28 dataDir = filepath.Join(dataDir, "statestore") 29 } 30 ldb, err := leveldbstore.New(dataDir, nil) 31 if err != nil { 32 return nil, nil, err 33 } 34 35 caching, err := cache.Wrap(ldb, int(cacheCapacity)) 36 if err != nil { 37 return nil, nil, err 38 } 39 40 stateStore, err := storeadapter.NewStateStorerAdapter(caching) 41 42 return stateStore, caching, err 43 } 44 45 // InitStamperStore will create new stamper store with the given path to the 46 // data directory. When given an empty directory path, the function will instead 47 // initialize an in-memory state store that will not be persisted. 48 func InitStamperStore(logger log.Logger, dataDir string, stateStore storage.StateStorer) (storage.Store, error) { 49 if dataDir == "" { 50 logger.Warning("using in-mem stamper store, no node state will be persisted") 51 } else { 52 dataDir = filepath.Join(dataDir, "stamperstore") 53 } 54 stamperStore, err := leveldbstore.New(dataDir, nil) 55 if err != nil { 56 return nil, err 57 } 58 59 return stamperStore, nil 60 } 61 62 const ( 63 overlayNonce = "overlayV2_nonce" 64 noncedOverlayKey = "nonce-overlay" 65 ) 66 67 // checkOverlay checks the overlay is the same as stored in the statestore 68 func checkOverlay(storer storage.StateStorer, overlay swarm.Address) error { 69 70 var storedOverlay swarm.Address 71 err := storer.Get(noncedOverlayKey, &storedOverlay) 72 if err != nil { 73 if !errors.Is(err, storage.ErrNotFound) { 74 return err 75 } 76 return storer.Put(noncedOverlayKey, overlay) 77 } 78 79 if !storedOverlay.Equal(overlay) { 80 return fmt.Errorf("overlay address changed. was %s before but now is %s", storedOverlay, overlay) 81 } 82 83 return nil 84 } 85 86 func overlayNonceExists(s storage.StateStorer) ([]byte, bool, error) { 87 nonce := make([]byte, 32) 88 if err := s.Get(overlayNonce, &nonce); err != nil { 89 if errors.Is(err, storage.ErrNotFound) { 90 return nonce, false, nil 91 } 92 return nil, false, err 93 } 94 return nonce, true, nil 95 } 96 97 func setOverlay(s storage.StateStorer, overlay swarm.Address, nonce []byte) error { 98 return errors.Join( 99 s.Put(overlayNonce, nonce), 100 s.Put(noncedOverlayKey, overlay), 101 ) 102 }