diff --git a/client/client.go b/client/client.go index 6f7d90fa..fadd8363 100644 --- a/client/client.go +++ b/client/client.go @@ -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 { @@ -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) diff --git a/commitments/da_service_op_commitment.go b/commitments/da_service_op_commitment.go index ada7b4c1..d0b89a9e 100644 --- a/commitments/da_service_op_commitment.go +++ b/commitments/da_service_op_commitment.go @@ -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} } diff --git a/commitments/eigenda_commitment.go b/commitments/eigenda_commitment.go index 70ac150d..6d92d65f 100644 --- a/commitments/eigenda_commitment.go +++ b/commitments/eigenda_commitment.go @@ -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 { @@ -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") } @@ -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]) diff --git a/commitments/op_commitment.go b/commitments/op_commitment.go index e3972ef2..d53f4e0f 100644 --- a/commitments/op_commitment.go +++ b/commitments/op_commitment.go @@ -16,7 +16,7 @@ const ( type OPCommitment struct { keccak256Commitment []byte - daServiceCommitment *DAServiceOPCommitment + genericCommitment *DAServiceOPCommitment } var _ Commitment = (*OPCommitment)(nil) @@ -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 { @@ -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") @@ -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]) } diff --git a/server/commitment_type.go b/server/commitment_type.go index acefe1c1..eb53f5af 100644 --- a/server/commitment_type.go +++ b/server/commitment_type.go @@ -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") } @@ -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() @@ -71,8 +59,16 @@ 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") } @@ -80,13 +76,12 @@ func StringToCommit(key string, c CommitmentType) ([]byte, error) { 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") } @@ -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) } diff --git a/server/server.go b/server/server.go index 821d9be7..6ec39ce1 100644 --- a/server/server.go +++ b/server/server.go @@ -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) @@ -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 diff --git a/test/e2e_test.go b/test/e2e_test.go index 34376ab8..44c07a0a 100644 --- a/test/e2e_test.go +++ b/test/e2e_test.go @@ -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 @@ -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)