Skip to content

Commit

Permalink
Remove decode flag but retain codec abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyknox committed Jun 6, 2024
1 parent ae67e1b commit 5c99021
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions api/clients/codecs/default_blob_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (v DefaultBlobCodec) EncodeBlob(rawData []byte) ([]byte, error) {
}

func (v DefaultBlobCodec) DecodeBlob(data []byte) ([]byte, error) {
if len(data) < 32 {
return nil, fmt.Errorf("blob does not contain 32 header bytes, meaning it is malformed")
}

length := binary.BigEndian.Uint32(data[2:6])

// decode raw data modulo bn254
Expand Down
3 changes: 3 additions & 0 deletions api/clients/codecs/ifft_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (v IFFTCodec) EncodeBlob(data []byte) ([]byte, error) {
}

func (v IFFTCodec) DecodeBlob(data []byte) ([]byte, error) {
if len(data) == 0 {
return nil, fmt.Errorf("blob has length 0, meaning it is malformed")
}
var err error
data, err = FFT(data)
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions api/clients/eigenda_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

type IEigenDAClient interface {
GetBlob(ctx context.Context, batchHeaderHash []byte, blobIndex uint32, decode bool) ([]byte, error)
GetBlob(ctx context.Context, batchHeaderHash []byte, blobIndex uint32) ([]byte, error)
PutBlob(ctx context.Context, txData []byte) (*grpcdisperser.BlobInfo, error)
GetCodec() codecs.BlobCodec
}

type EigenDAClient struct {
Expand Down Expand Up @@ -64,13 +65,17 @@ func NewEigenDAClient(log log.Logger, config EigenDAClientConfig) (*EigenDAClien
}, nil
}

func (m EigenDAClient) GetCodec() codecs.BlobCodec {
return m.Codec
}

// GetBlob retrieves a blob from the EigenDA service using the provided context,
// batch header hash, and blob index. If decode is set to true, the function
// decodes the retrieved blob data. If set to false it returns the encoded blob
// data, which is necessary for generating KZG proofs for data's correctness.
// The function handles potential errors during blob retrieval, data length
// checks, and decoding processes.
func (m EigenDAClient) GetBlob(ctx context.Context, batchHeaderHash []byte, blobIndex uint32, decode bool) ([]byte, error) {
func (m EigenDAClient) GetBlob(ctx context.Context, batchHeaderHash []byte, blobIndex uint32) ([]byte, error) {
data, err := m.Client.RetrieveBlob(ctx, batchHeaderHash, blobIndex)
if err != nil {
return nil, err
Expand All @@ -80,10 +85,6 @@ func (m EigenDAClient) GetBlob(ctx context.Context, batchHeaderHash []byte, blob
return nil, fmt.Errorf("blob has length zero")
}

if !decode {
return data, nil
}

decodedData, err := m.Codec.DecodeBlob(data)
if err != nil {
return nil, fmt.Errorf("error getting blob: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion api/clients/eigenda_client_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestClientUsingTestnet(t *testing.T) {
assert.NoError(t, err)
batchHeaderHash := blobInfo.BlobVerificationProof.BatchMetadata.BatchHeaderHash
blobIndex := blobInfo.BlobVerificationProof.BlobIndex
blob, err := client.GetBlob(context.Background(), batchHeaderHash, blobIndex, true)
blob, err := client.GetBlob(context.Background(), batchHeaderHash, blobIndex)
assert.NoError(t, err)
assert.Equal(t, data, string(blob))
}
10 changes: 6 additions & 4 deletions api/clients/eigenda_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestPutRetrieveBlobIFFTSuccess(t *testing.T) {
require.NotNil(t, blobInfo)
assert.Equal(t, finalizedBlobInfo, blobInfo)

resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100, true)
resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100)
require.NoError(t, err)
require.Equal(t, expectedBlob, resultBlob)
}
Expand Down Expand Up @@ -143,10 +143,12 @@ func TestPutRetrieveBlobIFFTNoDecodeSuccess(t *testing.T) {
require.NotNil(t, blobInfo)
assert.Equal(t, finalizedBlobInfo, blobInfo)

resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100, false)
resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100)
require.NoError(t, err)
encodedBlob, err := eigendaClient.GetCodec().EncodeBlob(resultBlob)
require.NoError(t, err)

resultBlob, err = codecs.NewIFFTCodec(codecs.NewDefaultBlobCodec()).DecodeBlob(resultBlob)
resultBlob, err = codecs.NewIFFTCodec(codecs.NewDefaultBlobCodec()).DecodeBlob(encodedBlob)
require.NoError(t, err)
require.Equal(t, expectedBlob, resultBlob)
}
Expand Down Expand Up @@ -211,7 +213,7 @@ func TestPutRetrieveBlobNoIFFTSuccess(t *testing.T) {
require.NotNil(t, blobInfo)
assert.Equal(t, finalizedBlobInfo, blobInfo)

resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100, true)
resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100)
require.NoError(t, err)
require.Equal(t, expectedBlob, resultBlob)
}
Expand Down

0 comments on commit 5c99021

Please sign in to comment.