-
Notifications
You must be signed in to change notification settings - Fork 0
/
statestore.go
57 lines (47 loc) · 1.74 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
49
50
51
52
53
54
55
56
57
// 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/newswarm-lab/new-bee/pkg/logging"
"github.com/newswarm-lab/new-bee/pkg/statestore/leveldb"
"github.com/newswarm-lab/new-bee/pkg/statestore/mock"
"github.com/newswarm-lab/new-bee/pkg/storage"
"github.com/newswarm-lab/new-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) (ret storage.StateStorer, err error) {
if dataDir == "" {
ret = mock.NewStateStore()
log.Warning("using in-mem state store, no node state will be persisted")
return ret, nil
}
return leveldb.NewStateStore(filepath.Join(dataDir, "statestore"), log)
}
const overlayKey = "overlay"
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 {
// migrate overlay key to new key
err := storer.Delete(overlayKey)
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return err
}
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
}