From b98b07aa3f39b18a810f1e5a1eaf885841f1c694 Mon Sep 17 00:00:00 2001 From: litt3 <102969658+litt3@users.noreply.github.com> Date: Tue, 14 Jan 2025 10:42:57 -0500 Subject: [PATCH] Massage naming Signed-off-by: litt3 <102969658+litt3@users.noreply.github.com> --- api/clients/codecs/blob_codec.go | 30 ++++++++++++++++++--------- api/clients/codecs/blob_codec_test.go | 6 +++--- api/clients/eigenda_client.go | 4 ++-- api/clients/eigenda_client_test.go | 4 ++-- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/api/clients/codecs/blob_codec.go b/api/clients/codecs/blob_codec.go index 2d30f4551..9d41ba126 100644 --- a/api/clients/codecs/blob_codec.go +++ b/api/clients/codecs/blob_codec.go @@ -17,9 +17,15 @@ const ( DefaultBlobEncoding BlobEncodingVersion = 0x0 ) -// EncodeBlob can never return an error, but to maintain the interface it is included -// so that it can be swapped for the IFFTCodec without changing the interface -func EncodeBlob(rawData []byte) []byte { +// EncodePayload accepts an arbitrary payload byte array, and encodes it. +// +// The returned bytes may be interpreted as a polynomial in evaluation form, where each contained field element of +// length 32 represents the evaluation of the polynomial at an expanded root of unity +// +// The returned bytes may or may not represent a blob. If the system is configured to distribute blobs in Coeff form, +// then the data returned from this function must be IFFTed to produce the final blob. If the system is configured to +// distribute blobs in Eval form, then the data returned from this function is the final blob representation. +func EncodePayload(payload []byte) []byte { codecBlobHeader := make([]byte, 32) // first byte is always 0 to ensure the codecBlobHeader is a valid bn254 element // encode version byte @@ -28,10 +34,10 @@ func EncodeBlob(rawData []byte) []byte { // encode length as uint32 binary.BigEndian.PutUint32( codecBlobHeader[2:6], - uint32(len(rawData))) // uint32 should be more than enough to store the length (approx 4gb) + uint32(len(payload))) // uint32 should be more than enough to store the length (approx 4gb) - // encode raw data modulo bn254 - rawDataPadded := codec.ConvertByPaddingEmptyByte(rawData) + // encode payload modulo bn254 + rawDataPadded := codec.ConvertByPaddingEmptyByte(payload) // append raw data encodedData := append(codecBlobHeader, rawDataPadded...) @@ -39,15 +45,19 @@ func EncodeBlob(rawData []byte) []byte { return encodedData } -func DecodeBlob(data []byte) ([]byte, error) { - if len(data) < 32 { +// DecodePayload accepts bytes representing an encoded payload, and returns the decoded payload +// +// This function expects the parameter bytes to be a polynomial in Eval form. In other words, if blobs in the system +// are being distributed in Coeff form, a blob must be FFTed prior to being passed into the function. +func DecodePayload(encodedPayload []byte) ([]byte, error) { + if len(encodedPayload) < 32 { return nil, fmt.Errorf("blob does not contain 32 header bytes, meaning it is malformed") } - length := binary.BigEndian.Uint32(data[2:6]) + length := binary.BigEndian.Uint32(encodedPayload[2:6]) // decode raw data modulo bn254 - decodedData := codec.RemoveEmptyByteFromPaddedBytes(data[32:]) + decodedData := codec.RemoveEmptyByteFromPaddedBytes(encodedPayload[32:]) // get non blob header data reader := bytes.NewReader(decodedData) diff --git a/api/clients/codecs/blob_codec_test.go b/api/clients/codecs/blob_codec_test.go index 1dd7510ad..bc9ad7b60 100644 --- a/api/clients/codecs/blob_codec_test.go +++ b/api/clients/codecs/blob_codec_test.go @@ -19,7 +19,7 @@ func randomByteSlice(length int64) []byte { return b } -// TestIFFTCodec tests the encoding and decoding of random byte streams +// TestCodec tests the encoding and decoding of random byte streams func TestCodec(t *testing.T) { // Number of test iterations const iterations = 100 @@ -33,10 +33,10 @@ func TestCodec(t *testing.T) { originalData := randomByteSlice(length.Int64() + 1) // ensure it's not length 0 // Encode the original data - encodedData := codecs.EncodeBlob(originalData) + encodedData := codecs.EncodePayload(originalData) // Decode the encoded data - decodedData, err := codecs.DecodeBlob(encodedData) + decodedData, err := codecs.DecodePayload(encodedData) if err != nil { t.Fatalf("Iteration %d: failed to decode blob: %v", i, err) } diff --git a/api/clients/eigenda_client.go b/api/clients/eigenda_client.go index e6db5e8db..b288e5623 100644 --- a/api/clients/eigenda_client.go +++ b/api/clients/eigenda_client.go @@ -156,7 +156,7 @@ func (m *EigenDAClient) GetBlob(ctx context.Context, batchHeaderHash []byte, blo return nil, fmt.Errorf("receive polynomial: %w", err) } - decodedData, err := codecs.DecodeBlob(encodedBlob) + decodedData, err := codecs.DecodePayload(encodedBlob) if err != nil { return nil, fmt.Errorf("error decoding blob: %w", err) } @@ -223,7 +223,7 @@ func (m *EigenDAClient) putBlob( errChan chan error) { m.Log.Info("Attempting to disperse blob to EigenDA") - encodedPayload := codecs.EncodeBlob(rawData) + encodedPayload := codecs.EncodePayload(rawData) polynomial, err := m.preparePolynomial(encodedPayload) if err != nil { diff --git a/api/clients/eigenda_client_test.go b/api/clients/eigenda_client_test.go index 6b5fa3835..be01fdf92 100644 --- a/api/clients/eigenda_client_test.go +++ b/api/clients/eigenda_client_test.go @@ -149,11 +149,11 @@ func TestPutRetrieveBlobIFFTNoDecodeSuccess(t *testing.T) { resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100) require.NoError(t, err) - ifftBlob, err := codecs.IFFT(codecs.EncodeBlob(resultBlob)) + ifftBlob, err := codecs.IFFT(codecs.EncodePayload(resultBlob)) require.NoError(t, err) encodedBlob, err := codecs.FFT(ifftBlob) require.NoError(t, err) - resultBlob, err = codecs.DecodeBlob(encodedBlob) + resultBlob, err = codecs.DecodePayload(encodedBlob) require.NoError(t, err) require.Equal(t, expectedBlob, resultBlob) }