Skip to content

Commit

Permalink
Add test for codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyknox committed May 16, 2024
1 parent 4aa651a commit 4e59c15
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
12 changes: 8 additions & 4 deletions api/clients/eigenda_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewEigenDAClient(log log.Logger, config EigenDAClientConfig) (*EigenDAClien
llConfig := NewConfig(host, port, config.ResponseTimeout, !config.DisableTLS)
llClient := NewDisperserClient(llConfig, signer)

codec, err := EncodingVersionToCodec(config.PutBlobEncodingVersion)
codec, err := BlobEncodingVersionToCodec(config.PutBlobEncodingVersion)
if err != nil {
return nil, fmt.Errorf("error initializing EigenDA client: %w", err)
}
Expand All @@ -67,14 +67,14 @@ func (m EigenDAClient) GetBlob(ctx context.Context, BatchHeaderHash []byte, Blob
}

version := BlobEncodingVersion(data[0])
codec, err := EncodingVersionToCodec(version)
codec, err := BlobEncodingVersionToCodec(version)
if err != nil {
fmt.Errorf("error getting blob: %w", err)
return nil, fmt.Errorf("error getting blob: %w", err)
}

rawData, err := codec.DecodeBlob(data)
if err != nil {
fmt.Errorf("error getting blob: %w", err)
return nil, fmt.Errorf("error getting blob: %w", err)
}

return rawData, nil
Expand All @@ -101,6 +101,10 @@ func (m EigenDAClient) putBlob(ctx context.Context, rawData []byte, resultChan c
m.Log.Info("Attempting to disperse blob to EigenDA")

// encode blob
if m.PutCodec == nil {
errChan <- fmt.Errorf("PutCodec cannot be nil")
return
}
data := m.PutCodec.EncodeBlob(rawData)

customQuorumNumbers := make([]uint8, len(m.Config.CustomQuorumIDs))
Expand Down
32 changes: 24 additions & 8 deletions api/clients/eigenda_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ func TestPutRetrieveBlobSuccess(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
expectedBlob := []byte("dc49e7df326cfb2e7da5cf68f263e1898443ec2e862350606e7dfbda55ad10b5d61ed1d54baf6ae7a86279c1b4fa9c49a7de721dacb211264c1f5df31bade51c")
cert, err := eigendaClient.PutBlob(context.Background(), expectedBlob)
Expand Down Expand Up @@ -100,8 +102,10 @@ func TestPutBlobFailDispersal(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
cert, err := eigendaClient.PutBlob(context.Background(), []byte("hello"))
require.Error(t, err)
Expand Down Expand Up @@ -130,8 +134,10 @@ func TestPutBlobFailureInsufficentSignatures(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
cert, err := eigendaClient.PutBlob(context.Background(), []byte("hello"))
require.Error(t, err)
Expand Down Expand Up @@ -160,8 +166,10 @@ func TestPutBlobFailureGeneral(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
cert, err := eigendaClient.PutBlob(context.Background(), []byte("hello"))
require.Error(t, err)
Expand Down Expand Up @@ -190,8 +198,10 @@ func TestPutBlobFailureUnknown(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
cert, err := eigendaClient.PutBlob(context.Background(), []byte("hello"))
require.Error(t, err)
Expand Down Expand Up @@ -222,8 +232,10 @@ func TestPutBlobFinalizationTimeout(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
cert, err := eigendaClient.PutBlob(context.Background(), []byte("hello"))
require.Error(t, err)
Expand Down Expand Up @@ -279,8 +291,10 @@ func TestPutBlobIndividualRequestTimeout(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
cert, err := eigendaClient.PutBlob(context.Background(), []byte("hello"))

Expand Down Expand Up @@ -345,8 +359,10 @@ func TestPutBlobTotalTimeout(t *testing.T) {
CustomQuorumIDs: []uint{},
SignerPrivateKeyHex: "75f9e29cac7f5774d106adb355ef294987ce39b7863b75bb3f2ea42ca160926d",
DisableTLS: false,
PutBlobEncodingVersion: clients.DefaultBlobEncoding,
},
Client: disperserClient,
Client: disperserClient,
PutCodec: clients.DefaultBlobEncodingCodec{},
}
cert, err := eigendaClient.PutBlob(context.Background(), []byte("hello"))

Expand Down
14 changes: 7 additions & 7 deletions api/clients/encodings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ type BlobCodec interface {
EncodeBlob(rawData []byte) []byte
}

func EncodingVersionToCodec(version BlobEncodingVersion) (BlobCodec, error) {
func BlobEncodingVersionToCodec(version BlobEncodingVersion) (BlobCodec, error) {
switch version {
case DefaultBlobEncoding:
return NoIFFTCodec{}, nil
return DefaultBlobEncodingCodec{}, nil
default:
return nil, fmt.Errorf("unsupported blob encoding version: %x", version)
}
}

type NoIFFTCodec struct{}
type DefaultBlobEncodingCodec struct{}

var _ BlobCodec = NoIFFTCodec{}
var _ BlobCodec = DefaultBlobEncodingCodec{}

func (v NoIFFTCodec) EncodeBlob(rawData []byte) []byte {
func (v DefaultBlobEncodingCodec) EncodeBlob(rawData []byte) []byte {
// encode current blob encoding version byte
encodedData := make([]byte, 0, 1+8+len(rawData))

Expand All @@ -52,7 +52,7 @@ func (v NoIFFTCodec) EncodeBlob(rawData []byte) []byte {
return encodedData
}

func (v NoIFFTCodec) DecodeBlob(encodedData []byte) ([]byte, error) {
func (v DefaultBlobEncodingCodec) DecodeBlob(encodedData []byte) ([]byte, error) {
// decode modulo bn254
decodedData := codec.RemoveEmptyByteFromPaddedBytes(encodedData)

Expand All @@ -74,7 +74,7 @@ func (v NoIFFTCodec) DecodeBlob(encodedData []byte) ([]byte, error) {
rawData := make([]byte, length)
n, err := reader.Read(rawData)
if err != nil {
return nil, fmt.Errorf("failed to copy unpadded data into final buffer")
return nil, fmt.Errorf("failed to copy unpadded data into final buffer, length: %d, bytes read: %d", length, n)
}
if uint64(n) != length {
return nil, fmt.Errorf("data length does not match length prefix")
Expand Down
47 changes: 47 additions & 0 deletions api/clients/encodings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package clients

import (
"bytes"
"crypto/rand"
"math/big"
"testing"
)

// Helper function to generate a random byte slice of a given length
func randomByteSlice(length int64) []byte {
b := make([]byte, length)
rand.Read(b)
return b
}

// TestDefaultBlobEncodingCodec tests the encoding and decoding of random byte streams
func TestDefaultBlobEncodingCodec(t *testing.T) {
// Create an instance of the DefaultBlobEncodingCodec
codec := DefaultBlobEncodingCodec{}

// Number of test iterations
const iterations = 100

for i := 0; i < iterations; i++ {
// Generate a random length for the byte slice
length, err := rand.Int(rand.Reader, big.NewInt(1024)) // Random length between 0 and 1023
if err != nil {
panic(err)
}
originalData := randomByteSlice(length.Int64() + 1) // ensure it's not length 0

// Encode the original data
encodedData := codec.EncodeBlob(originalData)

// Decode the encoded data
decodedData, err := codec.DecodeBlob(encodedData)
if err != nil {
t.Fatalf("Iteration %d: failed to decode blob: %v", i, err)
}

// Compare the original data with the decoded data
if !bytes.Equal(originalData, decodedData) {
t.Fatalf("Iteration %d: original and decoded data do not match\nOriginal: %v\nDecoded: %v", i, originalData, decodedData)
}
}
}

0 comments on commit 4e59c15

Please sign in to comment.