Skip to content

Commit

Permalink
Merge metadata code into single package
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Aug 12, 2023
1 parent 2356d00 commit 0e03876
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 74 deletions.
4 changes: 2 additions & 2 deletions cmd/postcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ func cmdVerifyPos(opts config.InitOpts, fraction float64, logger *zap.Logger) {
}
pub := ed25519.NewKeyFromSeed(dst[:ed25519.SeedSize]).Public().(ed25519.PublicKey)

metafile := filepath.Join(opts.DataDir, initialization.MetadataFileName)
meta, err := initialization.LoadMetadata(opts.DataDir)
metafile := filepath.Join(opts.DataDir, shared.MetadataFileName)
meta, err := shared.LoadMetadata(opts.DataDir)
if err != nil {
log.Fatalf("failed to load metadata from %s: %s\n", opts.DataDir, err)
}
Expand Down
8 changes: 4 additions & 4 deletions initialization/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func removeRedundantFiles(cfg config.Config, opts config.InitOpts, logger *zap.L
for _, file := range files {
name := file.Name()
fileIndex, err := shared.ParseFileIndex(name)
if err != nil && name != MetadataFileName {
if err != nil && name != shared.MetadataFileName {
// TODO(mafa): revert back to warning, see https://github.com/spacemeshos/go-spacemesh/issues/4789
logger.Debug("found unrecognized file", zap.String("fileName", name))
continue
Expand Down Expand Up @@ -460,7 +460,7 @@ func (init *Initializer) Reset() error {
continue
}
name := file.Name()
if shared.IsInitFile(info) || name == MetadataFileName {
if shared.IsInitFile(info) || name == shared.MetadataFileName {
path := filepath.Join(init.opts.DataDir, name)
if err := os.Remove(path); err != nil {
return fmt.Errorf("failed to delete file (%v): %w", path, err)
Expand Down Expand Up @@ -692,9 +692,9 @@ func (init *Initializer) saveMetadata() error {
if init.nonceValue.Load() != nil {
v.NonceValue = *init.nonceValue.Load()
}
return SaveMetadata(init.opts.DataDir, &v)
return shared.SaveMetadata(init.opts.DataDir, &v)
}

func (init *Initializer) loadMetadata() (*shared.PostMetadata, error) {
return LoadMetadata(init.opts.DataDir)
return shared.LoadMetadata(init.opts.DataDir)
}
1 change: 0 additions & 1 deletion initialization/initialization_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
var (
ErrAlreadyInitializing = errors.New("already initializing")
ErrCannotResetWhileInitializing = errors.New("cannot reset while initializing")
ErrStateMetadataFileMissing = errors.New("metadata file is missing")
)

type ErrReferenceLabelMismatch struct {
Expand Down
22 changes: 11 additions & 11 deletions initialization/initialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func TestInitialize_BeforeNonceValue(t *testing.T) {
cancel()
require.Equal(t, uint64(cfg.MinNumUnits)*cfg.LabelsPerUnit, init.NumLabelsWritten())

meta, err := LoadMetadata(opts.DataDir)
meta, err := shared.LoadMetadata(opts.DataDir)
require.NoError(t, err)
require.NotNil(t, meta.Nonce)
require.NotNil(t, meta.NonceValue)
nonceValue := meta.NonceValue

// delete nonce value
meta.NonceValue = nil
require.NoError(t, SaveMetadata(opts.DataDir, meta))
require.NoError(t, shared.SaveMetadata(opts.DataDir, meta))

// just creating a new initializer should update the metadata
init, err = NewInitializer(
Expand All @@ -114,7 +114,7 @@ func TestInitialize_BeforeNonceValue(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, init)

meta, err = LoadMetadata(opts.DataDir)
meta, err = shared.LoadMetadata(opts.DataDir)
require.NoError(t, err)
require.NotNil(t, meta.Nonce)
require.NotNil(t, meta.NonceValue)
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestInitialize_ContinueWithLastPos(t *testing.T) {
r.NoError(init.Initialize(context.Background()))
r.Equal(uint64(cfg.MinNumUnits)*cfg.LabelsPerUnit, init.NumLabelsWritten())

m, err := LoadMetadata(opts.DataDir)
m, err := shared.LoadMetadata(opts.DataDir)
r.NoError(err)
r.Equal(origNonce, *m.Nonce)
r.EqualValues(origNonceValue, m.NonceValue)
Expand All @@ -208,7 +208,7 @@ func TestInitialize_ContinueWithLastPos(t *testing.T) {
// lastPos lower than numLabels is ignored
m.LastPosition = new(uint64)
*m.LastPosition = uint64(cfg.MinNumUnits)*cfg.LabelsPerUnit - 10
r.NoError(SaveMetadata(opts.DataDir, m))
r.NoError(shared.SaveMetadata(opts.DataDir, m))

init, err = NewInitializer(
WithNodeId(nodeId),
Expand All @@ -222,7 +222,7 @@ func TestInitialize_ContinueWithLastPos(t *testing.T) {
r.NoError(init.Initialize(context.Background()))
r.Equal(uint64(cfg.MinNumUnits)*cfg.LabelsPerUnit, init.NumLabelsWritten())

m, err = LoadMetadata(opts.DataDir)
m, err = shared.LoadMetadata(opts.DataDir)
r.NoError(err)
r.Equal(origNonce, *m.Nonce)

Expand All @@ -232,7 +232,7 @@ func TestInitialize_ContinueWithLastPos(t *testing.T) {
// the range of the PoST
m.Nonce = nil
m.LastPosition = nil
r.NoError(SaveMetadata(opts.DataDir, m))
r.NoError(shared.SaveMetadata(opts.DataDir, m))

init, err = NewInitializer(
WithNodeId(nodeId),
Expand All @@ -246,7 +246,7 @@ func TestInitialize_ContinueWithLastPos(t *testing.T) {
r.NoError(init.Initialize(context.Background()))
r.Equal(uint64(cfg.MinNumUnits)*cfg.LabelsPerUnit, init.NumLabelsWritten())

m, err = LoadMetadata(opts.DataDir)
m, err = shared.LoadMetadata(opts.DataDir)
r.NoError(err)
r.NotNil(m.Nonce)
r.NotNil(m.NonceValue)
Expand All @@ -266,7 +266,7 @@ func TestInitialize_ContinueWithLastPos(t *testing.T) {
lastPos := *m.Nonce + 10
*m.LastPosition = lastPos
m.Nonce = nil
r.NoError(SaveMetadata(opts.DataDir, m))
r.NoError(shared.SaveMetadata(opts.DataDir, m))

init, err = NewInitializer(
WithNodeId(nodeId),
Expand All @@ -280,7 +280,7 @@ func TestInitialize_ContinueWithLastPos(t *testing.T) {
r.NoError(init.Initialize(context.Background()))
r.Equal(uint64(cfg.MinNumUnits)*cfg.LabelsPerUnit, init.NumLabelsWritten())

m, err = LoadMetadata(opts.DataDir)
m, err = shared.LoadMetadata(opts.DataDir)
r.NoError(err)
r.NotNil(m.Nonce)
r.NotNil(m.LastPosition)
Expand Down Expand Up @@ -1028,7 +1028,7 @@ func TestInitializeSubset_NoNonce(t *testing.T) {
require.Nil(t, init.Nonce())
require.Nil(t, init.NonceValue())

meta, err := LoadMetadata(opts.DataDir)
meta, err := shared.LoadMetadata(opts.DataDir)
require.NoError(t, err)
require.Nil(t, meta.Nonce)

Expand Down
49 changes: 0 additions & 49 deletions initialization/metadata.go

This file was deleted.

4 changes: 2 additions & 2 deletions initialization/vrf_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func SearchForNonce(ctx context.Context, cfg Config, initOpts InitOpts, opts ...
}
logger := options.logger

metadata, err := LoadMetadata(initOpts.DataDir)
metadata, err := shared.LoadMetadata(initOpts.DataDir)
if err != nil {
return 0, nil, fmt.Errorf("failed to load metadata: %w", err)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func persistNonce(nonce uint64, label []byte, metadata *shared.PostMetadata, dat
logger.Info("found nonce: updating postdata_metadata.json", zap.Uint64("nonce", nonce), zap.String("NonceValue", hex.EncodeToString(label)))
metadata.Nonce = &nonce
metadata.NonceValue = shared.NonceValue(label)
if err := SaveMetadata(datadir, metadata); err != nil {
if err := shared.SaveMetadata(datadir, metadata); err != nil {
return fmt.Errorf("failed to save metadata: %w", err)
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions initialization/vrf_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spacemeshos/post/config"
"github.com/spacemeshos/post/internal/postrs"
"github.com/spacemeshos/post/oracle"
"github.com/spacemeshos/post/shared"
)

func TestCheckLabel(t *testing.T) {
Expand Down Expand Up @@ -88,15 +89,15 @@ func TestSearchForNonce(t *testing.T) {
err = init.Initialize(context.Background())
require.NoError(t, err)

metadata, err := LoadMetadata(opts.DataDir)
metadata, err := shared.LoadMetadata(opts.DataDir)
require.NoError(t, err)

expectedNonce := metadata.Nonce
expectedNonceValue := metadata.NonceValue
// remove Nonce and NonceValue from metadata
metadata.Nonce = nil
metadata.NonceValue = nil
err = SaveMetadata(opts.DataDir, metadata)
err = shared.SaveMetadata(opts.DataDir, metadata)
require.NoError(t, err)

nonce, value, err := SearchForNonce(
Expand All @@ -110,7 +111,7 @@ func TestSearchForNonce(t *testing.T) {
require.EqualValues(t, expectedNonceValue, value)

// Verify that nonce was written to the metatada file
metadata, err = LoadMetadata(opts.DataDir)
metadata, err = shared.LoadMetadata(opts.DataDir)
require.NoError(t, err)
require.Equal(t, expectedNonce, metadata.Nonce)
require.EqualValues(t, expectedNonceValue, metadata.NonceValue)
Expand Down
3 changes: 1 addition & 2 deletions proving/proving_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"

"github.com/spacemeshos/post/config"
"github.com/spacemeshos/post/initialization"
"github.com/spacemeshos/post/shared"
)

Expand Down Expand Up @@ -39,7 +38,7 @@ type OptionFunc func(*option) error
// WithDataSource sets the data source to use for the proof.
func WithDataSource(cfg config.Config, nodeId, commitmentAtxId []byte, datadir string) OptionFunc {
return func(o *option) error {
m, err := initialization.LoadMetadata(datadir)
m, err := shared.LoadMetadata(datadir)
if err != nil {
return err
}
Expand Down
48 changes: 48 additions & 0 deletions shared/post_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ package shared
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)

// ErrStateMetadataFileMissing is returned when the metadata file is missing.
var ErrStateMetadataFileMissing = errors.New("metadata file is missing")

// PostMetadata is the data associated with the PoST init procedure, persisted in the datadir next to the init files.
type PostMetadata struct {
Version int `json:",omitempty"`

NodeId []byte
CommitmentAtxId []byte

Expand All @@ -32,3 +41,42 @@ func (n *NonceValue) UnmarshalJSON(data []byte) (err error) {
*n, err = hex.DecodeString(hexString)
return
}

const MetadataFileName = "postdata_metadata.json"

func SaveMetadata(dir string, v *PostMetadata) error {
err := os.MkdirAll(dir, OwnerReadWriteExec)
if err != nil && !os.IsExist(err) {
return fmt.Errorf("dir creation failure: %w", err)
}

data, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("serialization failure: %w", err)
}

err = os.WriteFile(filepath.Join(dir, MetadataFileName), data, OwnerReadWrite)
if err != nil {
return fmt.Errorf("write to disk failure: %w", err)
}

return nil
}

func LoadMetadata(dir string) (*PostMetadata, error) {
filename := filepath.Join(dir, MetadataFileName)
data, err := os.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, ErrStateMetadataFileMissing
}
return nil, fmt.Errorf("read file failure: %w", err)
}

metadata := PostMetadata{}
if err := json.Unmarshal(data, &metadata); err != nil {
return nil, err
}

return &metadata, nil
}

0 comments on commit 0e03876

Please sign in to comment.