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

Fail new tree creation when layer files exist #414

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
mshared "github.com/spacemeshos/merkle-tree/shared"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/exp/maps"

"github.com/spacemeshos/poet/logging"
"github.com/spacemeshos/poet/shared"
Expand Down Expand Up @@ -119,7 +120,17 @@
)
}

// Create a new merkle tree for proof generation.
func makeProofTree(treeCfg TreeConfig, merkleHashFunc merkle.HashFunc) (*merkle.Tree, *cache.Writer, error) {
// Make sure there are no existing layer files.
layersFiles, err := getLayersFiles(treeCfg.Datadir)
if err != nil {
return nil, nil, err
}

Check warning on line 129 in prover/prover.go

View check run for this annotation

Codecov / codecov/patch

prover/prover.go#L128-L129

Added lines #L128 - L129 were not covered by tests
if len(layersFiles) > 0 {
return nil, nil, fmt.Errorf("datadir is not empty. %v layer files exist", maps.Values(layersFiles))
}

minMemoryLayer := max(treeCfg.MinMemoryLayer, LowestMerkleMinMemoryLayer)
treeCache := cache.NewWriter(
cache.Combine(
Expand All @@ -136,6 +147,7 @@
return tree, treeCache, nil
}

// Recover merkle tree from existing cache files.
func makeRecoveryProofTree(
ctx context.Context,
treeCfg TreeConfig,
Expand Down
20 changes: 20 additions & 0 deletions prover/prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,23 @@ func TestTreeRecovery(t *testing.T) {
test(t, 1000)
})
}

func TestNewTreeFailOnDirectoryNotEmpty(t *testing.T) {
t.Parallel()

r := require.New(t)
// Create a tree and add some nodes to it
treeCfg := TreeConfig{
MinMemoryLayer: 3,
Datadir: t.TempDir(),
}
merklehashFuncFunc := hash.GenMerkleHashFunc([]byte{})
tree, treeCache, err := makeProofTree(treeCfg, merklehashFuncFunc)
r.NoError(err)
r.NoError(tree.AddLeaf(bytes.Repeat([]byte{7}, 32)))
treeCache.Close()

// try to create a new tree in the same directory
_, _, err = makeProofTree(treeCfg, merklehashFuncFunc)
r.Error(err)
}