Skip to content

Commit

Permalink
Merge pull request #33 from Layr-Labs/epociask--feat-proxy-client
Browse files Browse the repository at this point in the history
feat: Proxy client
  • Loading branch information
EthenNotEthan authored Jun 9, 2024
2 parents 86bcc4f + b6c518f commit cfac6cc
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 8 deletions.
130 changes: 130 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package client

import (
"bytes"
"context"
"fmt"
"io"
"net/http"

"github.com/Layr-Labs/eigenda-proxy/common"
"github.com/Layr-Labs/eigenda-proxy/eigenda"
"github.com/ethereum/go-ethereum/rlp"
)

const (
// NOTE: this will need to be updated as plasma's implementation changes
decodingOffset = 2
)

// TODO: Add support for custom http client option
type Config struct {
URL string
}

// ProxyClient is an interface for communicating with the EigenDA proxy server
type ProxyClient interface {
Health() error
GetData(ctx context.Context, cert *common.Certificate, domain common.DomainType) ([]byte, error)
SetData(ctx context.Context, b []byte) (*common.Certificate, error)
}

// client is the implementation of ProxyClient
type client struct {
cfg *Config
httpClient *http.Client
}

func New(cfg *Config) ProxyClient {
return &client{
cfg,
http.DefaultClient,
}
}

// Health indicates if server is operational; useful for event based awaits
// when integration testing
func (c *client) Health() error {
url := c.cfg.URL + "/health"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}

resp, err := c.httpClient.Do(req)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received bad status code: %d", resp.StatusCode)
}

return nil
}

// GetData fetches blob data associated with a DA certificate
func (c *client) GetData(ctx context.Context, cert *common.Certificate, domain common.DomainType) ([]byte, error) {
b, err := rlp.EncodeToBytes(cert)
if err != nil {
return nil, err
}

// encode prefix bytes
b = eigenda.GenericPrefix(eigenda.Commitment(b).Encode())

url := fmt.Sprintf("%s/get/0x%x?domain=%s", c.cfg.URL, b, domain.String())

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("failed to construct http request: %e", err)
}

req.Header.Set("Content-Type", "application/octet-stream")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received unexpected response code: %d", resp.StatusCode)
}

return io.ReadAll(resp.Body)
}

// SetData writes raw byte data to DA and returns the respective certificate
func (c *client) SetData(ctx context.Context, b []byte) (*common.Certificate, error) {
url := fmt.Sprintf("%s/put/", c.cfg.URL)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b))
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
req.Header.Set("Content-Type", "application/octet-stream")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to store data: %v", resp.StatusCode)
}

b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if len(b) < decodingOffset {
return nil, fmt.Errorf("read certificate is of invalid length: %d", len(b))
}

var cert *common.Certificate
if err = rlp.DecodeBytes(b[decodingOffset:], &cert); err != nil {
return nil, err
}

return cert, err
}
17 changes: 17 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"fmt"
"strconv"
"strings"

"github.com/Layr-Labs/eigenda/api/grpc/disperser"
)

var (
ErrInvalidDomainType = fmt.Errorf("invalid domain type")
)

type Certificate = disperser.BlobInfo

// DomainType is a enumeration type for the different data domains for which a
// blob can exist between
type DomainType uint8
Expand All @@ -20,6 +24,19 @@ const (
UnknownDomain
)

func (dt DomainType) String() string {
switch dt {
case BinaryDomain:
return "binary"

case PolyDomain:
return "polynomial"

default:
return "unknown"
}
}

func StrToDomainType(s string) DomainType {
switch s {
case "binary":
Expand Down
4 changes: 4 additions & 0 deletions eigenda/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (c Commitment) Encode() []byte {
return append([]byte{byte(EigenDA), byte(EigenV0)}, c...)
}

func GenericPrefix(b []byte) []byte {
return append([]byte{byte(op_plasma.GenericCommitmentType)}, b...)
}

func StringToCommit(key string) (Commitment, error) {
comm, err := hexutil.Decode(key)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Layr-Labs/eigensdk-go v0.1.7-0.20240507215523-7e4891d5099a // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.11 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.11 // indirect
Expand All @@ -35,7 +37,10 @@ require (
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd v0.24.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
Expand All @@ -49,13 +54,15 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3 // indirect
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240522134500-19555bdbdc95 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/fjl/memsize v0.0.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/gammazero/deque v0.2.0 // indirect
github.com/gammazero/workerpool v1.1.3 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
Expand All @@ -67,8 +74,12 @@ require (
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-bexpr v0.1.11 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/holiman/billy v0.0.0-20240216141850-2abb0c79d3c4 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
Expand Down Expand Up @@ -114,10 +125,13 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/urfave/cli v1.22.14 // indirect
github.com/wealdtech/go-merkletree v1.0.1-0.20230205101955-ec7a95ea11ca // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand All @@ -134,6 +148,7 @@ require (
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.59.0 // indirect
Expand Down
Loading

0 comments on commit cfac6cc

Please sign in to comment.