Skip to content

Commit

Permalink
Clean up verifying opts
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Nov 23, 2023
1 parent 9f52d8a commit fb69c2c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 deletions.
18 changes: 3 additions & 15 deletions verifying/verifying.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ func VerifyVRFNonce(nonce *uint64, m *shared.VRFNonceMetadata, opts ...OptionFun
return fmt.Errorf("invalid `commitmentAtxId` length; expected: 32, given: %v", len(m.CommitmentAtxId))
}

options := defaultOpts()
for _, opt := range opts {
if err := opt(options); err != nil {
return err
}
}

options := applyOpts(opts...)
numLabels := uint64(m.NumUnits) * uint64(m.LabelsPerUnit)
difficulty := shared.PowDifficulty(numLabels)

Expand Down Expand Up @@ -68,10 +62,7 @@ type ProofVerifier struct {
// NewProofVerifier creates a new proof verifier.
// The verifier must be closed after use with Close().
func NewProofVerifier(opts ...OptionFunc) (*ProofVerifier, error) {
options, err := applyOpts(opts...)
if err != nil {
return nil, err
}
options := applyOpts(opts...)
inner, err := postrs.NewVerifier(options.powFlags)
if err != nil {
return nil, err
Expand All @@ -83,17 +74,14 @@ func NewProofVerifier(opts ...OptionFunc) (*ProofVerifier, error) {
// Verify ensures the validity of a proof in respect to its metadata.
// It returns nil if the proof is valid or an error describing the failure, otherwise.
func (v *ProofVerifier) Verify(p *shared.Proof, m *shared.ProofMetadata, cfg config.Config, logger *zap.Logger, opts ...OptionFunc) error {
options, err := applyOpts(opts...)
if err != nil {
return err
}
if len(m.NodeId) != 32 {
return fmt.Errorf("invalid `nodeId` length; expected: 32, given: %v", len(m.NodeId))
}
if len(m.CommitmentAtxId) != 32 {
return fmt.Errorf("invalid `commitmentAtxId` length; expected: 32, given: %v", len(m.CommitmentAtxId))
}

options := applyOpts(opts...)
scryptParams := postrs.NewScryptParams(options.labelScrypt.N, options.labelScrypt.R, options.labelScrypt.P)
return v.VerifyProof(p, m, logger, postrs.Config(cfg), scryptParams)
}
36 changes: 7 additions & 29 deletions verifying/verifying_options.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,36 @@
package verifying

import (
"errors"

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

type option struct {
powFlags config.PowFlags
// scrypt parameters for labels initialization
labelScrypt config.ScryptParams

powCreatorId []byte
}

func defaultOpts() *option {
return &option{
func applyOpts(options ...OptionFunc) *option {
opts := &option{
powFlags: config.DefaultVerifyingPowFlags(),
labelScrypt: config.DefaultLabelParams(),
}
}

func applyOpts(options ...OptionFunc) (*option, error) {
opts := defaultOpts()
for _, opt := range options {
if err := opt(opts); err != nil {
return nil, err
}
opt(opts)
}
return opts, nil
return opts
}

type OptionFunc func(*option) error
type OptionFunc func(*option)

func WithLabelScryptParams(params config.ScryptParams) OptionFunc {
return func(o *option) error {
return func(o *option) {
o.labelScrypt = params
return nil
}
}

func WithPowFlags(flags config.PowFlags) OptionFunc {
return func(o *option) error {
return func(o *option) {

Check warning on line 33 in verifying/verifying_options.go

View check run for this annotation

Codecov / codecov/patch

verifying/verifying_options.go#L33

Added line #L33 was not covered by tests
o.powFlags = flags
return nil
}
}

func WithPowCreator(id []byte) OptionFunc {
return func(o *option) error {
if len(id) != 32 {
return errors.New("pow creator id must be 32 bytes")
}
o.powCreatorId = id
return nil
}
}

0 comments on commit fb69c2c

Please sign in to comment.