forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
196 lines (160 loc) · 5.22 KB
/
store.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package nodebuilder
import (
"errors"
"fmt"
"path/filepath"
"sync"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/dgraph-io/badger/v2/options"
"github.com/ipfs/go-datastore"
dsbadger "github.com/ipfs/go-ds-badger2"
"github.com/mitchellh/go-homedir"
"go.uber.org/multierr"
"github.com/celestiaorg/celestia-node/libs/fslock"
"github.com/celestiaorg/celestia-node/libs/keystore"
)
var (
// ErrOpened is thrown on attempt to open already open/in-use Store.
ErrOpened = errors.New("node: store is in use")
// ErrNotInited is thrown on attempt to open Store without initialization.
ErrNotInited = errors.New("node: store is not initialized")
)
// Store encapsulates storage for the Node. Basically, it is the Store of all Stores.
// It provides access for the Node data stored in root directory e.g. '~/.celestia'.
type Store interface {
// Path reports the FileSystem path of Store.
Path() string
// Keystore provides a Keystore to access keys.
Keystore() (keystore.Keystore, error)
// Datastore provides a Datastore - a KV store for arbitrary data to be stored on disk.
Datastore() (datastore.Batching, error)
// Config loads the stored Node config.
Config() (*Config, error)
// PutConfig alters the stored Node config.
PutConfig(*Config) error
// Close closes the Store freeing up acquired resources and locks.
Close() error
}
// OpenStore creates new FS Store under the given 'path'.
// To be opened the Store must be initialized first, otherwise ErrNotInited is thrown.
// OpenStore takes a file Lock on directory, hence only one Store can be opened at a time under the
// given 'path', otherwise ErrOpened is thrown.
func OpenStore(path string, ring keyring.Keyring) (Store, error) {
path, err := storePath(path)
if err != nil {
return nil, err
}
flock, err := fslock.Lock(lockPath(path))
if err != nil {
if err == fslock.ErrLocked {
return nil, ErrOpened
}
return nil, err
}
ok := IsInit(path)
if !ok {
flock.Unlock() //nolint: errcheck
return nil, ErrNotInited
}
ks, err := keystore.NewFSKeystore(keysPath(path), ring)
if err != nil {
return nil, err
}
return &fsStore{
path: path,
dirLock: flock,
keys: ks,
}, nil
}
func (f *fsStore) Path() string {
return f.path
}
func (f *fsStore) Config() (*Config, error) {
cfg, err := LoadConfig(configPath(f.path))
if err != nil {
return nil, fmt.Errorf("node: can't load Config: %w", err)
}
return cfg, nil
}
func (f *fsStore) PutConfig(cfg *Config) error {
err := SaveConfig(configPath(f.path), cfg)
if err != nil {
return fmt.Errorf("node: can't save Config: %w", err)
}
return nil
}
func (f *fsStore) Keystore() (_ keystore.Keystore, err error) {
f.lock.RLock()
defer f.lock.RUnlock()
if f.keys == nil {
return nil, fmt.Errorf("node: no Keystore found")
}
return f.keys, nil
}
func (f *fsStore) Datastore() (_ datastore.Batching, err error) {
f.lock.RLock()
if f.data != nil {
f.lock.RUnlock()
return f.data, nil
}
f.lock.RUnlock()
f.lock.Lock()
defer f.lock.Unlock()
opts := dsbadger.DefaultOptions // this should be copied
// Badger sets ValueThreshold to 1K by default and this makes shares being stored in LSM tree
// instead of the value log, so we change the value to be lower than share size,
// so shares are store in value log. For value log and LSM definitions
opts.ValueThreshold = 128
// We always write unique values to Badger transaction so there is no need to detect conflicts.
opts.DetectConflicts = false
// Use MemoryMap for better performance
opts.ValueLogLoadingMode = options.MemoryMap
opts.TableLoadingMode = options.MemoryMap
// Truncate set to true will truncate corrupted data on start if there is any.
// If we don't truncate, the node will refuse to start and will beg for recovering, etc.
// If we truncate, the node will start with any uncorrupted data and reliably sync again what was
// corrupted in most cases.
opts.Truncate = true
// MaxTableSize defines in memory and on disk size of LSM tree
// Bigger values constantly takes more RAM
// TODO(@Wondertan): Make configurable with more conservative defaults for Light Node
opts.MaxTableSize = 64 << 20
// Remove GC as long as we don't have pruning of data to be GCed.
// Currently, we only append data on disk without removing.
// TODO(@Wondertan): Find good enough default, once pruning is shipped.
opts.GcInterval = 0
f.data, err = dsbadger.NewDatastore(dataPath(f.path), &opts)
if err != nil {
return nil, fmt.Errorf("node: can't open Badger Datastore: %w", err)
}
return f.data, nil
}
func (f *fsStore) Close() (err error) {
err = multierr.Append(err, f.dirLock.Unlock())
if f.data != nil {
err = multierr.Append(err, f.data.Close())
}
return
}
type fsStore struct {
path string
data datastore.Batching
keys keystore.Keystore
lock sync.RWMutex // protects all the fields
dirLock *fslock.Locker // protects directory
}
func storePath(path string) (string, error) {
return homedir.Expand(filepath.Clean(path))
}
func configPath(base string) string {
return filepath.Join(base, "config.toml")
}
func lockPath(base string) string {
return filepath.Join(base, "lock")
}
func keysPath(base string) string {
return filepath.Join(base, "keys")
}
func dataPath(base string) string {
return filepath.Join(base, "data")
}