Skip to content

Commit

Permalink
[cleanup] addressing review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Aug 2, 2023
1 parent a56c2cd commit 0a608f8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
6 changes: 4 additions & 2 deletions persistence/kvstore/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ func TestKVStore_Backup(t *testing.T) {

tmpdir := t.TempDir()
path := filepath.Join(tmpdir, "TestKVStore_Backup_InMemory.bak")
require.NoError(t, store.Backup(path))
err := store.Backup(path)
require.NoError(t, err)

empty, err := isEmpty(t, tmpdir)
require.NoError(t, err)
Expand All @@ -364,7 +365,8 @@ func TestKVStore_Backup(t *testing.T) {

backupDir := t.TempDir()
path := filepath.Join(backupDir, "TestKVStore_Backup_OnDisk_Destination.bak")
require.NoError(t, store.Backup(path))
err = store.Backup(path)
require.NoError(t, err)

empty, err := isEmpty(t, backupDir)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions persistence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ func (*persistenceModule) Create(bus modules.Bus, options ...modules.ModuleOptio
return nil, err
}

treemod, err := trees.Create(
treeMod, err := trees.Create(
bus,
trees.WithTreeStoreDirectory(persistenceCfg.TreesStoreDir),
trees.WithLogger(m.logger))
if err != nil {
return nil, fmt.Errorf("failed to create %s: %w", modules.TreeStoreSubmoduleName, err)
}
if err := treemod.Start(); err != nil {
if err := treeMod.Start(); err != nil {
return nil, fmt.Errorf("failed to start %s: %w", modules.TreeStoreSubmoduleName, err)
}

Expand Down
10 changes: 7 additions & 3 deletions persistence/trees/atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
h1 = "7d5712ea1507915c40e295845fa58773baa405b24b87e9d99761125d826ff915"
)

var testKey = []byte("fiz")

func TestTreeStore_AtomicUpdatesWithSuccessfulRollback(t *testing.T) {
ctrl := gomock.NewController(t)

Expand Down Expand Up @@ -80,7 +82,7 @@ func TestTreeStore_AtomicUpdatesWithSuccessfulRollback(t *testing.T) {

// insert additional test data into all of the trees
for _, treeName := range stateTreeNames {
require.NoError(t, ts.merkleTrees[treeName].tree.Update([]byte("fiz"), []byte("buz")))
require.NoError(t, ts.merkleTrees[treeName].tree.Update(testKey, []byte("buz")))
}

// rollback the changes made to the trees above BEFORE anything was committed
Expand All @@ -91,10 +93,12 @@ func TestTreeStore_AtomicUpdatesWithSuccessfulRollback(t *testing.T) {
hash3 := ts.getStateHash()
require.Equal(t, hash3, hash2)
require.Equal(t, hash3, h1)
require.NoError(t, ts.Rollback())

err = ts.Rollback()
require.NoError(t, err)

// confirm it's not in the tree
v, err := ts.merkleTrees[TransactionsTreeName].tree.Get([]byte("fiz"))
v, err := ts.merkleTrees[TransactionsTreeName].tree.Get(testKey)
require.NoError(t, err)
require.Nil(t, v)
}
Expand Down
25 changes: 9 additions & 16 deletions persistence/trees/trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,11 @@ type worldState struct {
// This enables the caller to import the SMT without changing the one stored unless they call
// `Commit()` to write to the nodestore.
func (t *treeStore) GetTree(name string) ([]byte, kvstore.KVStore) {
if name == RootTreeName {
if t.rootTree != nil {
if t.rootTree.tree != nil {
return t.rootTree.tree.Root(), t.rootTree.nodeStore
}
return nil, nil
}
return nil, nil
if name == RootTreeName && t.rootTree.tree != nil {
return t.rootTree.tree.Root(), t.rootTree.nodeStore
}
if tree, ok := t.merkleTrees[name]; ok {
if tree != nil {
return tree.tree.Root(), tree.nodeStore
}
return nil, nil
if tree, ok := t.merkleTrees[name]; ok && tree != nil {
return tree.tree.Root(), tree.nodeStore
}
return nil, nil
}
Expand Down Expand Up @@ -338,12 +329,13 @@ func (t *treeStore) Rollback() error {
return ErrFailedRollback
}

// Load sets the TreeStore merkle and root trees to the values provided in the worldstate
// Load sets the TreeStore trees to the values provided in the worldstate
func (t *treeStore) Load(w *worldState) error {
t.merkleTrees = make(map[string]*stateTree)

// import root tree
nodeStore, err := kvstore.NewKVStore(fmt.Sprintf("%s/%s_nodes", t.treeStoreDir, RootTreeName))
rootTreePath := fmt.Sprintf("%s/%s_nodes", t.treeStoreDir, RootTreeName)
nodeStore, err := kvstore.NewKVStore(rootTreePath)
if err != nil {
return err
}
Expand All @@ -355,7 +347,8 @@ func (t *treeStore) Load(w *worldState) error {

// import merkle trees
for treeName, treeRootHash := range w.merkleRoots {
nodeStore, err := kvstore.NewKVStore(fmt.Sprintf("%s/%s_nodes", w.treeStoreDir, treeName))
treePath := fmt.Sprintf("%s/%s_nodes", w.treeStoreDir, treeName)
nodeStore, err := kvstore.NewKVStore(treePath)
if err != nil {
return err
}
Expand Down

0 comments on commit 0a608f8

Please sign in to comment.