Skip to content

Commit

Permalink
address go feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
greywolve committed Dec 20, 2024
1 parent 0921fc1 commit 8ca6183
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ const (
MaxTicketExtrinsicSize = 16 // The maximum number of tickets which may be submitted in a single extrinsic.
MaxHistoricalTimeslotsForPreimageMeta = 3 // () Maximum number of historical timeslots for preimage metadata
SizeOfSegment = 4104 // WG = WP*WE = 4104: The size of a segment in octets.
MaxWorkPackageSize = 12 * 1 << 20 // WB = 12 MB: The maximum size of an encoded work-package in octets (including extrinsic data and import implications).
MaxAllocatedGasAccumulation = 100_000 // GA = 100,000: The gas allocated to invoke a work-report’s Accumulation logic.
)
3 changes: 2 additions & 1 deletion internal/common/constants_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
ValidatorRotationPeriod = jamtime.Timeslot(10)
MaxTicketExtrinsicSize = 16
MaxHistoricalTimeslotsForPreimageMeta = 3
MaxAllocatedGasAccumulation = 100_000
SizeOfSegment = 4104
MaxWorkPackageSize = 12 * 1 << 20 // 12 MB: The maximum size of an encoded work-package in octets (including extrinsic data and import implications).
MaxAllocatedGasAccumulation = 100_000
)
27 changes: 23 additions & 4 deletions internal/erasurecoding/reedsolomon/reedsolomon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import (
"C"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"runtime"

"github.com/eigerco/strawberry/internal/common"

"github.com/ebitengine/purego"
)

const (
MaxShards = 65535 // Original + Recovery shards should equal this, a limitation of the reed-solomon-simd library.
MaxShardSize = 1024 // The graypayer calls for a shard size of 2 so this is a decent sized maximum if this changes.
)

var (
reedSolomonEncode func(
originalShardsCount C.size_t,
Expand Down Expand Up @@ -45,11 +53,19 @@ type Encoder struct {

// Create a new reed solomon enocder with the given original shards count and
// recovery shards count.
func New(originalShardsCount, recoveryShardsCount int) *Encoder {
func New(originalShardsCount, recoveryShardsCount int) (*Encoder, error) {
if recoveryShardsCount > math.MaxInt-originalShardsCount {
return nil, fmt.Errorf("shard count overflow")
}

if originalShardsCount+recoveryShardsCount > MaxShards {
return nil, fmt.Errorf("too many total shards")
}

return &Encoder{
originalShardsCount: originalShardsCount,
recoveryShardsCount: recoveryShardsCount,
}
}, nil
}

// Takes a slice of data to encode and chunks the data into shards. The
Expand All @@ -58,6 +74,9 @@ func New(originalShardsCount, recoveryShardsCount int) *Encoder {
// length of the original shards count + the recovery shards count. Data is not
// copied, so the input data should not be modified after.
func (r *Encoder) Chunk(data []byte) ([][]byte, error) {
if len(data) > common.MaxWorkPackageSize {
return nil, errors.New("data length too long")
}
// Need at least two bytes per chunk.
if len(data) < r.originalShardsCount*2 {
return nil, errors.New("data length too short")
Expand Down Expand Up @@ -96,7 +115,7 @@ func (r *Encoder) Encode(
}

shardSize := shardSize(shards)
if shardSize == 0 {
if shardSize == 0 || shardSize > MaxShardSize {
return errors.New("invalid shard size")
}

Expand Down Expand Up @@ -140,7 +159,7 @@ func (r *Encoder) Decode(shards [][]byte) error {
return errors.New("too few shards")
}
shardSize := shardSize(shards)
if shardSize == 0 {
if shardSize == 0 || shardSize > MaxShardSize {
return errors.New("invalid shard size")
}

Expand Down
4 changes: 3 additions & 1 deletion internal/erasurecoding/reedsolomon/reedsolomon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func TestRoundTrip(t *testing.T) {

data := testutils.MustFromHex(t, tv.Data)

rs := New(342, 681)
rs, err := New(342, 681)
require.NoError(t, err)

shards, err := rs.Chunk(data)
require.NoError(t, err)

Expand Down

0 comments on commit 8ca6183

Please sign in to comment.