Skip to content

Commit

Permalink
Merge pull request #126 from spacemeshos/piersy/validate-compute-batc…
Browse files Browse the repository at this point in the history
…h-size

Add validation for compute batch size
  • Loading branch information
piersy authored Apr 24, 2023
2 parents 0f5fe63 + 2e0377c commit 490e20c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 6 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
const (
DefaultDataDirName = "data"

// DefaultComputeBatchSize value must be divisible by 8, to guarantee that writing to disk
// and file truncating is byte-granular.
DefaultComputeBatchSize = 1 << 20

MinBitsPerLabel = 1
Expand Down Expand Up @@ -71,7 +69,8 @@ type InitOpts struct {
ComputeProviderID int
Throttle bool
Scrypt ScryptParams
ComputeBatchSize uint64
// ComputeBatchSize must be greater than 0
ComputeBatchSize uint64
}

type ScryptParams struct {
Expand Down Expand Up @@ -136,6 +135,10 @@ func Validate(cfg Config, opts InitOpts) error {
return fmt.Errorf("invalid `opts.MaxFileSize`; expected: >= %d, given: %d", minFileSize, opts.MaxFileSize)
}

if opts.ComputeBatchSize == 0 {
return fmt.Errorf("invalid `opts.ComputeBatchSize` expected: > 0, given: %d", opts.ComputeBatchSize)
}

if res := shared.Uint64MulOverflow(cfg.LabelsPerUnit, uint64(opts.NumUnits)); res {
return fmt.Errorf("uint64 overflow: `cfg.LabelsPerUnit` (%v) * `opts.NumUnits` (%v) exceeds the range allowed by uint64",
cfg.LabelsPerUnit, opts.NumUnits)
Expand Down
17 changes: 17 additions & 0 deletions initialization/initialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,23 @@ func TestStop(t *testing.T) {
}
}

func TestValidateComputeBatchSize(t *testing.T) {
cfg := config.DefaultConfig()
opts := config.DefaultInitOpts()

// Set invalid value of 0
opts.ComputeBatchSize = 0

_, err := NewInitializer(
WithNodeId(nodeId),
WithCommitmentAtxId(commitmentAtxId),
WithConfig(cfg),
WithInitOpts(opts),
WithLogger(testLogger{t: t}),
)
assert.Error(t, err)
}

func assertNumLabelsWritten(ctx context.Context, t *testing.T, init *Initializer) func() error {
return func() error {
timer := time.NewTimer(50 * time.Millisecond)
Expand Down

0 comments on commit 490e20c

Please sign in to comment.