Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Sep 14, 2023
1 parent 79af945 commit 2d3f091
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 249 deletions.
2 changes: 2 additions & 0 deletions cmd/bench/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func main() {
securityParam := shared.T

tempdir, _ := os.MkdirTemp("", "poet-test")
defer os.RemoveAll(tempdir)

proofGenStarted := time.Now()
end := proofGenStarted.Add(cfg.Duration)
leafs, merkleProof, err := prover.GenerateProofWithoutPersistency(
Expand Down
31 changes: 0 additions & 31 deletions hashfunc_test.go

This file was deleted.

5 changes: 1 addition & 4 deletions poet.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ func poetMain() (err error) {
if err != nil {
return err
}
server.SetupConfig(cfg)

cfg, err = server.SetupConfig(cfg)
if err != nil {
return err
}
// Finally, parse the remaining command line options again to ensure
// they take precedence.
cfg, err = server.ParseFlags(cfg)
Expand Down
27 changes: 27 additions & 0 deletions prover/layer_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package prover

import (
"fmt"
"path/filepath"

"github.com/spacemeshos/merkle-tree/cache"
"github.com/spacemeshos/merkle-tree/cache/readwriters"
)

// GetLayerFactory creates a merkle LayerFactory.
// The minMemoryLayer determines the threshold below which layers are saved on-disk, while layers equal and above -
// in-memory.
func GetLayerFactory(minMemoryLayer uint, datadir string, fileWriterBufSize uint) cache.LayerFactory {
return func(layerHeight uint) (cache.LayerReadWriter, error) {
if layerHeight < minMemoryLayer {
fileName := filepath.Join(datadir, fmt.Sprintf("layercache_%d.bin", layerHeight))
readWriter, err := readwriters.NewFileReadWriter(fileName, int(fileWriterBufSize))
if err != nil {
return nil, err
}

Check warning on line 21 in prover/layer_factory.go

View check run for this annotation

Codecov / codecov/patch

prover/layer_factory.go#L20-L21

Added lines #L20 - L21 were not covered by tests

return readWriter, nil
}
return &readwriters.SliceReadWriter{}, nil
}
}
24 changes: 3 additions & 21 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,11 @@ func makeProofTree(treeCfg TreeConfig, merkleHashFunc merkle.HashFunc) (*merkle.
if treeCfg.MinMemoryLayer < LowestMerkleMinMemoryLayer {
treeCfg.MinMemoryLayer = LowestMerkleMinMemoryLayer
}
metaFactory := NewReadWriterMetaFactory(treeCfg.MinMemoryLayer, treeCfg.Datadir, treeCfg.FileWriterBufSize)

treeCache := cache.NewWriter(
cache.Combine(
cache.SpecificLayersPolicy(map[uint]bool{0: true}),
cache.MinHeightPolicy(MerkleMinCacheLayer)),
metaFactory.GetFactory(),
GetLayerFactory(treeCfg.MinMemoryLayer, treeCfg.Datadir, treeCfg.FileWriterBufSize),
)

tree, err := merkle.NewTreeBuilder().WithHashFunc(merkleHashFunc).WithCacheWriter(treeCache).Build()
Expand All @@ -131,7 +129,7 @@ func makeRecoveryProofTree(
) (*cache.Writer, *merkle.Tree, error) {
// Don't use memory cache. Just utilize the existing files cache.
maxUint := ^uint(0)
layerFactory := NewReadWriterMetaFactory(maxUint, treeCfg.Datadir, treeCfg.FileWriterBufSize).GetFactory()
layerFactory := GetLayerFactory(maxUint, treeCfg.Datadir, treeCfg.FileWriterBufSize)

layersFiles, err := getLayersFiles(treeCfg.Datadir)
if err != nil {
Expand Down Expand Up @@ -384,29 +382,13 @@ func getLayersFiles(datadir string) (map[uint]string, error) {
return files, nil
}

// Calculate the root of a Merkle Tree with given leaves.
func CalcTreeRoot(leaves [][]byte) ([]byte, error) {
tree, err := merkle.NewTreeBuilder().WithHashFunc(shared.HashMembershipTreeNode).Build()
if err != nil {
return nil, fmt.Errorf("failed to generate tree: %w", err)
}
for _, member := range leaves {
err := tree.AddLeaf(member)
if err != nil {
return nil, fmt.Errorf("failed to add leaf: %w", err)
}
}
return tree.Root(), nil
}

// build a small tree with the nodes from the top layer of the cache as leafs.
// this tree will be used to get parked nodes for the merkle tree.
func recoverMemCachedParkedNodes(
layerReader mshared.LayerReader,
merkleHashFunc merkle.HashFunc,
) ([][]byte, mshared.CacheReader, error) {
recoveryTreelayerFactory := NewReadWriterMetaFactory(0, "", 0).GetFactory()
recoveryTreeCache := cache.NewWriter(func(uint) bool { return true }, recoveryTreelayerFactory)
recoveryTreeCache := cache.NewWriter(func(uint) bool { return true }, GetLayerFactory(0, "", 0))

tree, err := merkle.NewTreeBuilder().WithHashFunc(merkleHashFunc).WithCacheWriter(recoveryTreeCache).Build()
if err != nil {
Expand Down
73 changes: 0 additions & 73 deletions prover/readwritermetafactory.go

This file was deleted.

7 changes: 2 additions & 5 deletions rpc/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import (

api "github.com/spacemeshos/poet/release/proto/go/rpc/api/v1"
"github.com/spacemeshos/poet/rpc"
"github.com/spacemeshos/poet/server"
)

func Test_Submit_DoesNotPanicOnMissingPubKey(t *testing.T) {
// Arrange
cfg := server.DefaultConfig()
sv := rpc.NewServer(nil, nil, cfg.Round.PhaseShift, cfg.Round.CycleGap)
sv := rpc.NewServer(nil, nil, 0, 0)

// Act
in := &api.SubmitRequest{}
Expand All @@ -35,8 +33,7 @@ func Test_Submit_DoesNotPanicOnMissingPubKey(t *testing.T) {

func Test_Submit_DoesNotPanicOnMissingSignature(t *testing.T) {
// Arrange
cfg := server.DefaultConfig()
sv := rpc.NewServer(nil, nil, cfg.Round.PhaseShift, cfg.Round.CycleGap)
sv := rpc.NewServer(nil, nil, 0, 0)
pub, _, err := ed25519.GenerateKey(nil)
require.NoError(t, err)

Expand Down
53 changes: 2 additions & 51 deletions server/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Copyright (c) 2017-2023 The Spacemesh developers

package server

import (
"context"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"time"

"github.com/jessevdk/go-flags"
Expand All @@ -35,10 +29,6 @@ const (
defaultCycleGap = 10 * time.Second
)

// Config defines the configuration options for poet.
//
// See loadConfig for further details regarding the
// configuration loading+parsing process.
type Config struct {
Genesis Genesis `long:"genesis-time" description:"Genesis timestamp in RFC3339 format"`
PoetDir string `long:"poetdir" description:"The base directory that contains poet's data, logs, configuration file, etc."`
Expand Down Expand Up @@ -125,8 +115,8 @@ func ReadConfigFile(cfg *Config) (*Config, error) {
return cfg, nil
}

// SetupConfig expands paths and initializes filesystem.
func SetupConfig(cfg *Config) (*Config, error) {
// SetupConfig adjusts the paths in the config to be relative to the poetdir.
func SetupConfig(cfg *Config) {
// If the provided poet directory is not the default, we'll modify the
// path to all of the files and directories that will live within it.
defaultCfg := DefaultConfig()
Expand All @@ -141,45 +131,6 @@ func SetupConfig(cfg *Config) (*Config, error) {
cfg.DbDir = filepath.Join(cfg.PoetDir, defaultDbDirName)
}
}

// Create the poet directory if it doesn't already exist.
if err := os.MkdirAll(cfg.PoetDir, 0o700); err != nil {
return nil, fmt.Errorf("failed to create %v: %w", cfg.PoetDir, err)
}

// As soon as we're done parsing configuration options, ensure all paths
// to directories and files are cleaned and expanded before attempting
// to use them later on.
cfg.DataDir = cleanAndExpandPath(cfg.DataDir)
cfg.LogDir = cleanAndExpandPath(cfg.LogDir)

return cfg, nil
}

// cleanAndExpandPath expands environment variables and leading ~ in the
// passed path, cleans the result, and returns it.
// This function is taken from https://github.com/btcsuite/btcd
func cleanAndExpandPath(path string) string {
if path == "" {
return ""
}

// Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") {
var homeDir string
user, err := user.Current()
if err == nil {
homeDir = user.HomeDir
} else {
homeDir = os.Getenv("HOME")
}

path = strings.Replace(path, "~", homeDir, 1)
}

// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
// but the variables can still be expanded via POSIX-style $VARIABLE.
return filepath.Clean(os.ExpandEnv(path))
}

type RoundConfig struct {
Expand Down
Loading

0 comments on commit 2d3f091

Please sign in to comment.