Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llo/dev/v31/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func TestLimits_BlobPayloadRejectsOversizedFraming(t *testing.T) {
}
require.Less(t, len(raw), maxDecompressedBlobPayloadBytes)

// Under payloadCodecRaw the raw check fires first, with the codec byte
// reserved, so match the message both checks share.
_, err := encodeBlobPayload(raw)
require.ErrorContains(t, err, "framed blob payload too large")
require.ErrorContains(t, err, "blob payload too large")
}

func TestLimits_HistoryFitsWriteBudgets(t *testing.T) {
Expand Down
52 changes: 39 additions & 13 deletions llo/dev/v31/payloadcodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ const (
payloadCodecZstd byte = 1
)

// Codec each framed payload kind is written with. payloadCodecZstd compresses
// when that shrinks the payload and falls back to raw otherwise.
// payloadCodecRaw never compresses. Readers accept every codec, so these only
// steer the write side. The precursor is digested and signed and KV records
// feed the state root, so changing precursorCodec or kvRecordCodec changes
// consensus bytes and needs every oracle to switch together. blobPayloadCodec
// can change freely: each node decodes blobs on its own.
const (
blobPayloadCodec = payloadCodecZstd
precursorCodec = payloadCodecZstd
kvRecordCodec = payloadCodecZstd
)

// maxDecompressedBlobPayloadBytes bounds the size of a blob payload after
// decompression. A blob is a stream-values-only observation proto, so the same
// bound the v30 observation codec uses applies here: it is generously above any
Expand Down Expand Up @@ -103,23 +116,36 @@ func getPayloadCompressor() (payloadCompressor, error) {
return payloadZstd, payloadZstdErr
}

// encodeFramedPayload frames raw bytes behind a codec byte, compressing them
// when that actually shrinks the payload. The choice depends only on raw, so
// two encoders built from the same library version always agree on it. maxRaw
// bounds raw so a reader with the same bound can decode it, and maxFramed is
// the size libocr accepts. what names the payload in errors.
func encodeFramedPayload(raw []byte, maxRaw, maxFramed int, what string) ([]byte, error) {
// encodeFramedPayload frames raw bytes behind a codec byte. With
// payloadCodecZstd it compresses them when that actually shrinks the payload.
// The choice depends only on raw and codec, so two encoders built from the same
// library version always agree on it. maxRaw bounds raw so a reader with the
// same bound can decode it, and maxFramed is the size libocr accepts. what
// names the payload in errors.
func encodeFramedPayload(raw []byte, codec byte, maxRaw, maxFramed int, what string) ([]byte, error) {
if codec == payloadCodecRaw {
// The codec byte must fit too, so raw gets one byte less than the
// framed limit.
maxRaw = min(maxRaw, maxFramed-1)
}
if len(raw) > maxRaw {
// Refuse to emit a payload every peer would reject on decompression;
// fail loudly here instead.
return nil, fmt.Errorf("%s too large: %d > %d bytes", what, len(raw), maxRaw)
}
c, err := getPayloadCompressor()
if err != nil {
return nil, err
var out []byte
switch codec {
case payloadCodecRaw:
case payloadCodecZstd:
c, err := getPayloadCompressor()
if err != nil {
return nil, err
}
out = c.encoder.EncodeAll(raw, []byte{payloadCodecZstd})
default:
return nil, fmt.Errorf("unknown %s codec %d", what, codec)
}
out := c.encoder.EncodeAll(raw, []byte{payloadCodecZstd})
if len(out) >= len(raw)+1 {
if out == nil || len(out) >= len(raw)+1 {
out = make([]byte, 0, len(raw)+1)
out = append(out, payloadCodecRaw)
out = append(out, raw...)
Expand Down Expand Up @@ -189,7 +215,7 @@ func decodeFramedPayload(payload []byte, maxOut int, what string) ([]byte, error
// precursor, nodes need not agree on these bytes: each blob is fetched and
// decoded on its own.
func encodeBlobPayload(raw []byte) ([]byte, error) {
return encodeFramedPayload(raw, maxDecompressedBlobPayloadBytes, MaxBlobPayloadBytes, "blob payload")
return encodeFramedPayload(raw, blobPayloadCodec, maxDecompressedBlobPayloadBytes, MaxBlobPayloadBytes, "blob payload")
}

// decodeBlobPayload reverses encodeBlobPayload. maxOut is a caller-supplied
Expand All @@ -203,7 +229,7 @@ func decodeBlobPayload(payload []byte, maxOut int) ([]byte, error) {
// state root from these bytes, so they must match on every oracle, as for the
// precursor.
func encodeKVRecord(raw []byte, what string) ([]byte, error) {
return encodeFramedPayload(raw, maxKVRecordBytes, maxKVRecordBytes, what)
return encodeFramedPayload(raw, kvRecordCodec, maxKVRecordBytes, maxKVRecordBytes, what)
}

// decodeKVRecord reverses encodeKVRecord.
Expand Down
37 changes: 37 additions & 0 deletions llo/dev/v31/payloadcodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,43 @@ func Test_BlobPayload_RoundTrip(t *testing.T) {
})
}

func Test_encodeFramedPayload_Codec(t *testing.T) {
compressible := []byte(strings.Repeat("stream-values-are-repetitive", 200))

t.Run("raw codec never compresses", func(t *testing.T) {
framed, err := encodeFramedPayload(compressible, payloadCodecRaw, len(compressible), len(compressible)+1, "test")
require.NoError(t, err)
require.Equal(t, payloadCodecRaw, framed[0])
assert.Equal(t, compressible, framed[1:])

got, err := decodeFramedPayload(framed, len(compressible), "test")
require.NoError(t, err)
assert.Equal(t, compressible, got)
})

t.Run("raw codec reserves the codec byte", func(t *testing.T) {
// maxRaw equals maxFramed, as for the precursor and KV records, so a
// raw payload at the limit cannot fit behind the codec byte.
_, err := encodeFramedPayload(compressible, payloadCodecRaw, len(compressible), len(compressible), "test")
require.ErrorContains(t, err, "test too large")

framed, err := encodeFramedPayload(compressible[1:], payloadCodecRaw, len(compressible), len(compressible), "test")
require.NoError(t, err)
assert.Len(t, framed, len(compressible))
})

t.Run("zstd codec compresses at the shared limit", func(t *testing.T) {
framed, err := encodeFramedPayload(compressible, payloadCodecZstd, len(compressible), len(compressible), "test")
require.NoError(t, err)
require.Equal(t, payloadCodecZstd, framed[0])
})

t.Run("unknown codec", func(t *testing.T) {
_, err := encodeFramedPayload(compressible, 0xff, len(compressible), len(compressible)+1, "test")
require.ErrorContains(t, err, "unknown test codec 255")
})
}

func Test_BlobPayload_MarshalStreamValuesRoundTrip(t *testing.T) {
sv := make(protocol.StreamValues, 500)
for i := uint32(0); i < 500; i++ {
Expand Down
2 changes: 1 addition & 1 deletion llo/dev/v31/precursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func encodePrecursor(p precursor) (ocr3_1types.ReportsPlusPrecursor, error) {
}

func framePrecursor(raw []byte) (ocr3_1types.ReportsPlusPrecursor, error) {
return encodeFramedPayload(raw, maxDecompressedPrecursorBytes, ocr3_1types.MaxMaxReportsPlusPrecursorBytes, "precursor")
return encodeFramedPayload(raw, precursorCodec, maxDecompressedPrecursorBytes, ocr3_1types.MaxMaxReportsPlusPrecursorBytes, "precursor")
}

// marshalPrecursor serializes the precursor deterministically as
Expand Down
Loading