-
Notifications
You must be signed in to change notification settings - Fork 336
/
statestore.go
48 lines (39 loc) · 1.48 KB
/
statestore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package node
import (
"errors"
"fmt"
"path/filepath"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/statestore/leveldb"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
// 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(log logging.Logger, dataDir string) (storage.StateStorer, error) {
if dataDir == "" {
log.Warning("using in-mem state store, no node state will be persisted")
return leveldb.NewInMemoryStateStore(log)
}
return leveldb.NewStateStore(filepath.Join(dataDir, "statestore"), log)
}
const secureOverlayKey = "non-mineable-overlay"
// CheckOverlayWithStore checks the overlay is the same as stored in the statestore
func CheckOverlayWithStore(overlay swarm.Address, storer storage.StateStorer) error {
var storedOverlay swarm.Address
err := storer.Get(secureOverlayKey, &storedOverlay)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return err
}
return storer.Put(secureOverlayKey, overlay)
}
if !storedOverlay.Equal(overlay) {
return fmt.Errorf("overlay address changed. was %s before but now is %s", storedOverlay, overlay)
}
return nil
}