Skip to content

Commit

Permalink
Refactor with edit in response to Ethen's review
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyknox committed Jun 10, 2024
1 parent 2e91bb2 commit 6c2516a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 63 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *client) Health() error {

// GetData fetches blob data associated with a DA certificate
func (c *client) GetData(ctx context.Context, comm []byte, domain server.DomainType, commitmentType server.CommitmentType) ([]byte, error) {
url := fmt.Sprintf("%s/get/0x%x?domain=%s&commitment_type=%s", c.cfg.URL, comm, domain.String(), commitmentType.String())
url := fmt.Sprintf("%s/get/0x%x?domain=%s&commitment_type=%s", c.cfg.URL, comm, domain.String(), commitmentType)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
Expand All @@ -87,7 +87,7 @@ func (c *client) GetData(ctx context.Context, comm []byte, domain server.DomainT

// SetData writes raw byte data to DA and returns the respective certificate
func (c *client) SetData(ctx context.Context, b []byte, commitmentType server.CommitmentType) ([]byte, error) {
url := fmt.Sprintf("%s/put/?commitment_type=%s", c.cfg.URL, commitmentType.String())
url := fmt.Sprintf("%s/put/?commitment_type=%s", c.cfg.URL, commitmentType)
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)
Expand Down
2 changes: 1 addition & 1 deletion commitments/da_service_op_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type DAServiceOPCommitment struct {

var _ Commitment = (*DAServiceOPCommitment)(nil)

func EigenDAWrapperCommitment(value EigenDACommitment) DAServiceOPCommitment {
func OptimismEigenDACommitment(value EigenDACommitment) DAServiceOPCommitment {
return DAServiceOPCommitment{eigendaCommitment: &value}
}

Expand Down
10 changes: 5 additions & 5 deletions commitments/eigenda_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

// Define the parent and child types
type EigenDAEncodingCommitmentType byte
type CertEncodingVersion byte

const (
EigenDAEncoding0Byte EigenDAEncodingCommitmentType = 0
CertEncodingV0 CertEncodingVersion = 0
)

type EigenDACommitment struct {
Expand All @@ -36,7 +36,7 @@ func (e EigenDACommitment) MustCertV0Value() []byte {

func (e EigenDACommitment) Marshal() ([]byte, error) {
if e.IsCertV0() {
return append([]byte{byte(EigenDAEncoding0Byte)}, e.certV0...), nil
return append([]byte{byte(CertEncodingV0)}, e.certV0...), nil
} else {
return nil, fmt.Errorf("EigenDADAServiceOPCommitment is of unknown type")
}
Expand All @@ -46,10 +46,10 @@ func (e *EigenDACommitment) Unmarshal(bz []byte) error {
if len(bz) < 1 {
return fmt.Errorf("OP commitment does not contain eigenda commitment encoding version prefix byte")
}
head := EigenDAEncodingCommitmentType(bz[0])
head := CertEncodingVersion(bz[0])
tail := bz[1:]
switch head {
case EigenDAEncoding0Byte:
case CertEncodingV0:
e.certV0 = tail
default:
return fmt.Errorf("unrecognized EigenDA commitment encoding type byte: %x", bz[0])
Expand Down
20 changes: 10 additions & 10 deletions commitments/op_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

type OPCommitment struct {
keccak256Commitment []byte
daServiceCommitment *DAServiceOPCommitment
genericCommitment *DAServiceOPCommitment
}

var _ Commitment = (*OPCommitment)(nil)
Expand All @@ -26,15 +26,15 @@ func Keccak256Commitment(value []byte) OPCommitment {
}

func DAServiceCommitment(value DAServiceOPCommitment) OPCommitment {
return OPCommitment{daServiceCommitment: &value}
return OPCommitment{genericCommitment: &value}
}

func (e OPCommitment) IsKeccak256() bool {
func (e OPCommitment) IsKeccak256Commitment() bool {
return e.keccak256Commitment != nil
}

func (e OPCommitment) IsDAService() bool {
return e.daServiceCommitment != nil
func (e OPCommitment) IsGenericCommitment() bool {
return e.genericCommitment != nil
}

func (e OPCommitment) MustKeccak256Value() []byte {
Expand All @@ -46,21 +46,21 @@ func (e OPCommitment) MustKeccak256Value() []byte {
}

func (e OPCommitment) MustDAServiceValue() DAServiceOPCommitment {
if e.daServiceCommitment != nil {
return *e.daServiceCommitment
if e.genericCommitment != nil {
return *e.genericCommitment
}
log.Panic("OPCommitment does not contain a DAServiceCommitment value")
return DAServiceOPCommitment{} // This will never be reached, but is required for compilation.
}

func (e OPCommitment) Marshal() ([]byte, error) {
if e.IsDAService() {
if e.IsGenericCommitment() {
bytes, err := e.MustDAServiceValue().Marshal()
if err != nil {
return nil, err
}
return append([]byte{byte(DAServiceByte)}, bytes...), nil
} else if e.IsKeccak256() {
} else if e.IsKeccak256Commitment() {
return append([]byte{byte(Keccak256Byte)}, e.MustKeccak256Value()...), nil
} else {
return nil, fmt.Errorf("OPCommitment is neither a Keccak256 commitment nor a DA service commitment")
Expand All @@ -82,7 +82,7 @@ func (e *OPCommitment) Unmarshal(bz []byte) error {
if err != nil {
return err
}
e.daServiceCommitment = &daServiceCommitment
e.genericCommitment = &daServiceCommitment
default:
return fmt.Errorf("unrecognized commitment type byte: %x", bz[0])
}
Expand Down
70 changes: 30 additions & 40 deletions server/commitment_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,26 @@ import (
"github.com/Layr-Labs/eigenda-proxy/commitments"
)

type CommitmentType uint8
type CommitmentType string

const (
OptimismCommitmentType CommitmentType = iota
NakedCommitmentType
UnknownCommitmentType
Optimism CommitmentType = "optimism"
Default CommitmentType = "default"
)

func (c CommitmentType) String() string {
switch c {
case OptimismCommitmentType:
return "optimism"
case NakedCommitmentType:
return "naked"
default:
return "unknown"
}
}

func StrToCommitmentType(s string) CommitmentType {
func StringToCommitmentType(s string) (CommitmentType, error) {
switch s {
case "optimism":
return OptimismCommitmentType
case "naked":
return NakedCommitmentType
case string(Optimism):
return Optimism, nil
case string(Default):
return Default, nil
default:
return UnknownCommitmentType
return "", fmt.Errorf("unknown commitment type: %s", s)
}
}

func StringToCommit(key string, c CommitmentType) ([]byte, error) {
if len(key) == 0 {
func StringToCommitment(key string, c CommitmentType) ([]byte, error) {
if len(key) <= 2 {
return nil, fmt.Errorf("commitment is empty")
}

Expand All @@ -53,13 +41,13 @@ func StringToCommit(key string, c CommitmentType) ([]byte, error) {
}

switch c {
case OptimismCommitmentType:
case Optimism:
var comm commitments.OPCommitment
err = comm.Unmarshal(b)
if err != nil {
return nil, err
}
if !comm.IsDAService() {
if !comm.IsGenericCommitment() {
return nil, fmt.Errorf("commitment is not a OP DA service commitment")
}
daComm := comm.MustDAServiceValue()
Expand All @@ -71,22 +59,29 @@ func StringToCommit(key string, c CommitmentType) ([]byte, error) {
return nil, fmt.Errorf("commitment is not a supported EigenDA cert encoding")
}
return eigendaComm.MustCertV0Value(), nil
case NakedCommitmentType:
return b, nil
case Default:
var eigendaComm commitments.EigenDACommitment
err = eigendaComm.Unmarshal(b)
if err != nil {
return nil, err
}
if !eigendaComm.IsCertV0() {
return nil, fmt.Errorf("commitment is not a supported EigenDA cert encoding")
}
return eigendaComm.MustCertV0Value(), nil
default:
return nil, fmt.Errorf("unknown commitment type")
}
}

func EncodeCommitment(s []byte, c CommitmentType) ([]byte, error) {
switch c {
case OptimismCommitmentType:
comm := commitments.EigenDAWrapperCommitment(commitments.EigenDACertV0(s))
case Optimism:
comm := commitments.OptimismEigenDACommitment(commitments.EigenDACertV0(s))
return comm.Marshal()
case Default:
comm := commitments.EigenDACertV0(s)
return comm.Marshal()
case NakedCommitmentType:
return s, nil
case UnknownCommitmentType:
return nil, fmt.Errorf("unknown commitment type")
default:
return nil, fmt.Errorf("unknown commitment type")
}
Expand All @@ -96,12 +91,7 @@ func ReadCommitmentType(r *http.Request) (CommitmentType, error) {
query := r.URL.Query()
key := query.Get(CommitmentTypeKey)
if key == "" { // default
return OptimismCommitmentType, nil
return Optimism, nil
}
dt := StrToCommitmentType(key)
if dt == UnknownCommitmentType {
return UnknownCommitmentType, ErrInvalidDomainType
}

return dt, nil
return StringToCommitmentType(key)
}
3 changes: 2 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (svr *Server) HandleGet(w http.ResponseWriter, r *http.Request) error {
}

key := path.Base(r.URL.Path)
comm, err := StringToCommit(key, commitmentType)
comm, err := StringToCommitment(key, commitmentType)
if err != nil {
svr.log.Info("failed to decode commitment", "err", err, "key", key)
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -205,6 +205,7 @@ func (svr *Server) HandlePut(w http.ResponseWriter, r *http.Request) error {
return err
}

fmt.Printf("write cert: %x\n", comm)
// write out encoded commitment
svr.WriteResponse(w, comm)
return nil
Expand Down
8 changes: 4 additions & 4 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func createTestSuite(t *testing.T, useTestnet bool) (TestSuite, func()) {
}, kill
}

func TestWithPlasmaClient(t *testing.T) {
func TestWithOptimismPlasmaClient(t *testing.T) {
configs := []struct {
name string
useTestnet bool
Expand Down Expand Up @@ -188,17 +188,17 @@ func TestProxyClient(t *testing.T) {
var testPreimage = []byte("inter-subjective and not objective!")

t.Log("Setting input data on proxy server...")
cert, err := daClient.SetData(ts.ctx, testPreimage, server.NakedCommitmentType)
cert, err := daClient.SetData(ts.ctx, testPreimage, server.Default)
require.NoError(t, err)

// 2 - fetch data from EigenDA for generated commitment key
t.Log("Getting input data from proxy server...")
preimage, err := daClient.GetData(ts.ctx, cert, server.BinaryDomain, server.NakedCommitmentType)
preimage, err := daClient.GetData(ts.ctx, cert, server.BinaryDomain, server.Default)
require.NoError(t, err)
require.Equal(t, testPreimage, preimage)

// 3 - fetch iFFT representation of preimage
iFFTPreimage, err := daClient.GetData(ts.ctx, cert, server.PolyDomain, server.NakedCommitmentType)
iFFTPreimage, err := daClient.GetData(ts.ctx, cert, server.PolyDomain, server.Default)
require.NoError(t, err)
require.NotEqual(t, preimage, iFFTPreimage)

Expand Down

0 comments on commit 6c2516a

Please sign in to comment.