-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package initialization | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest" | ||
|
||
"github.com/spacemeshos/post/config" | ||
"github.com/spacemeshos/post/internal/postrs" | ||
) | ||
|
||
func TestVerifyPos(t *testing.T) { | ||
r := require.New(t) | ||
cfg := config.DefaultConfig() | ||
cfg.LabelsPerUnit = 128 | ||
|
||
opts := config.DefaultInitOpts() | ||
opts.DataDir = t.TempDir() | ||
opts.NumUnits = 5 | ||
opts.MaxFileSize = 2 * cfg.UnitSize() | ||
opts.ProviderID = int(CPUProviderID()) | ||
opts.Scrypt.N = 2 | ||
|
||
init, err := NewInitializer( | ||
WithNodeId(nodeId), | ||
WithCommitmentAtxId(commitmentAtxId), | ||
WithConfig(cfg), | ||
WithInitOpts(opts), | ||
WithLogger(zaptest.NewLogger(t, zaptest.Level(zap.DebugLevel))), | ||
) | ||
r.NoError(err) | ||
err = init.Initialize(context.Background()) | ||
r.NoError(err) | ||
|
||
scryptParams := postrs.TranslateScryptParams(opts.Scrypt.N, opts.Scrypt.R, opts.Scrypt.P) | ||
|
||
t.Run("valid", func(t *testing.T) { | ||
err = postrs.VerifyPos(opts.DataDir, scryptParams, postrs.WithFraction(100.0)) | ||
r.NoError(err) | ||
}) | ||
t.Run("invalid N", func(t *testing.T) { | ||
wrongScrypt := postrs.TranslateScryptParams(4, opts.Scrypt.R, opts.Scrypt.P) | ||
err = postrs.VerifyPos(opts.DataDir, wrongScrypt, postrs.WithFraction(100.0)) | ||
r.ErrorContains(err, "invalid POS") | ||
}) | ||
t.Run("corrupted data", func(t *testing.T) { | ||
file, err := os.OpenFile(opts.DataDir+"/postdata_0.bin", os.O_WRONLY, 0) | ||
r.NoError(err) | ||
_, err = file.WriteAt([]byte("1234567890123456"), 0) | ||
r.NoError(err) | ||
|
||
err = postrs.VerifyPos(opts.DataDir, scryptParams, postrs.WithFraction(100.0)) | ||
r.ErrorContains(err, "invalid POS") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package postrs | ||
|
||
// #cgo LDFLAGS: -lpost | ||
// #include <stdlib.h> | ||
// #include "post.h" | ||
import "C" | ||
|
||
import ( | ||
"errors" | ||
"unsafe" | ||
) | ||
|
||
type VerifyPosOptions struct { | ||
fromFile *uint32 | ||
toFile *uint32 | ||
fraction float64 | ||
} | ||
|
||
var ErrInvalidPos = errors.New("invalid POS") | ||
|
||
type VerifyPosOptionsFunc func(*VerifyPosOptions) error | ||
|
||
func FromFile(fromFile uint32) VerifyPosOptionsFunc { | ||
return func(o *VerifyPosOptions) error { | ||
o.fromFile = &fromFile | ||
return nil | ||
} | ||
} | ||
|
||
func ToFile(toFile uint32) VerifyPosOptionsFunc { | ||
return func(o *VerifyPosOptions) error { | ||
o.toFile = &toFile | ||
return nil | ||
} | ||
} | ||
|
||
func WithFraction(fraction float64) VerifyPosOptionsFunc { | ||
return func(o *VerifyPosOptions) error { | ||
o.fraction = fraction | ||
return nil | ||
} | ||
} | ||
|
||
func VerifyPos(dataDir string, scryptParams ScryptParams, o ...VerifyPosOptionsFunc) error { | ||
opts := &VerifyPosOptions{ | ||
fraction: 5.0, | ||
} | ||
|
||
for _, opt := range o { | ||
if err := opt(opts); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
dataDirPtr := C.CString(dataDir) | ||
defer C.free(unsafe.Pointer(dataDirPtr)) | ||
|
||
result := C.verify_pos(dataDirPtr, (*C.uint32_t)(opts.fromFile), (*C.uint32_t)(opts.toFile), C.double(opts.fraction), scryptParams) | ||
switch result { | ||
case C.Ok: | ||
return nil | ||
case C.Invalid: | ||
return ErrInvalidPos | ||
case C.InvalidArgument: | ||
return ErrInvalidArgument | ||
default: | ||
return ErrUnknown | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters