Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nodebuilder): fix nil pointer exception and simplify synchronization #2466

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions nodebuilder/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,18 @@ func (f *fsStore) PutConfig(cfg *Config) error {
}

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()
func (f *fsStore) Datastore() (datastore.Batching, error) {
f.dataMu.Lock()
defer f.dataMu.Unlock()
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

Expand All @@ -144,29 +138,31 @@ func (f *fsStore) Datastore() (_ datastore.Batching, err error) {
// TODO(@Wondertan): Make configurable with more conservative defaults for Light Node
opts.MaxTableSize = 64 << 20

f.data, err = dsbadger.NewDatastore(dataPath(f.path), &opts)
ds, 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
renaynay marked this conversation as resolved.
Show resolved Hide resolved
f.data = ds
return ds, nil
}

func (f *fsStore) Close() (err error) {
err = errors.Join(err, f.dirLock.Unlock())
f.dataMu.Lock()
if f.data != nil {
err = errors.Join(err, f.data.Close())
}
f.dataMu.Unlock()
return
}

type fsStore struct {
path string

data datastore.Batching
keys keystore.Keystore

lock sync.RWMutex // protects all the fields
dataMu sync.Mutex
data datastore.Batching
keys keystore.Keystore
dirLock *fslock.Locker // protects directory
}

Expand Down