From 3f4a0502d548e6e90d543613e92a2517a8ea1254 Mon Sep 17 00:00:00 2001 From: Bruno Moura Date: Fri, 6 Nov 2026 07:47:06 +0000 Subject: [PATCH] llo/dev/v31: select the framing codec per payload kind --- llo/dev/v31/limits_test.go | 4 ++- llo/dev/v31/payloadcodec.go | 52 ++++++++++++++++++++++++-------- llo/dev/v31/payloadcodec_test.go | 37 +++++++++++++++++++++++ llo/dev/v31/precursor.go | 2 +- 4 files changed, 80 insertions(+), 15 deletions(-) diff --git a/llo/dev/v31/limits_test.go b/llo/dev/v31/limits_test.go index e216165..e16de32 100644 --- a/llo/dev/v31/limits_test.go +++ b/llo/dev/v31/limits_test.go @@ -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) { diff --git a/llo/dev/v31/payloadcodec.go b/llo/dev/v31/payloadcodec.go index 45a237d..a95baf9 100644 --- a/llo/dev/v31/payloadcodec.go +++ b/llo/dev/v31/payloadcodec.go @@ -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 @@ -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...) @@ -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 @@ -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. diff --git a/llo/dev/v31/payloadcodec_test.go b/llo/dev/v31/payloadcodec_test.go index 84c0663..1997938 100644 --- a/llo/dev/v31/payloadcodec_test.go +++ b/llo/dev/v31/payloadcodec_test.go @@ -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++ { diff --git a/llo/dev/v31/precursor.go b/llo/dev/v31/precursor.go index 9349a21..dba7144 100644 --- a/llo/dev/v31/precursor.go +++ b/llo/dev/v31/precursor.go @@ -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