Skip to content

Commit

Permalink
Implement op-node/op-batcher frax DA
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalex88 committed Feb 4, 2024
1 parent bcee1e6 commit 7f9a3c2
Show file tree
Hide file tree
Showing 22 changed files with 419 additions and 107 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ devnet-test: pre-devnet
.PHONY: devnet-test

devnet-down:
@(cd ./ops-bedrock && GENESIS_TIMESTAMP=$(shell date +%s) docker compose stop)
@(cd ./ops-bedrock && GENESIS_TIMESTAMP=$(shell date +%s) docker compose rm -sf)
.PHONY: devnet-down

devnet-clean:
rm -rf ./packages/contracts-bedrock/deployments/devnetL1
rm -rf ./.devnet
cd ./ops-bedrock && docker compose down
cd ./ops-bedrock && docker compose rm -sf
docker image ls 'ops-bedrock*' --format='{{.Repository}}' | xargs -r docker rmi
docker volume ls --filter name=ops-bedrock --format='{{.Name}}' | xargs -r docker volume rm
.PHONY: devnet-clean
Expand Down
73 changes: 73 additions & 0 deletions frax-da/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package fraxda

import (
"fmt"
"net/url"

"github.com/urfave/cli/v2"

opservice "github.com/ethereum-optimism/optimism/op-service"
)

const (
DaRpcFlagName = "da.rpc"
)

var (
defaultDaRpc = "https://da-rpc.mainnet.frax.com"
)

func CLIFlags(envPrefix string) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: DaRpcFlagName,
Usage: "DA endpoint",
Value: defaultDaRpc,
EnvVars: opservice.PrefixEnvVar(envPrefix, "DA_RPC"),
},
}
}

type Config struct {
DaRpc string
}

func (c Config) Check() error {
if c.DaRpc == "" {
c.DaRpc = defaultDaRpc
}

if _, err := url.Parse(c.DaRpc); err != nil {
return fmt.Errorf("invalid da rpc url: %w", err)
}

return nil
}

type CLIConfig struct {
DaRpc string
}

func (c CLIConfig) Check() error {
if c.DaRpc == "" {
c.DaRpc = defaultDaRpc
}

if _, err := url.Parse(c.DaRpc); err != nil {
return fmt.Errorf("invalid da rpc url: %w", err)
}

return nil
}

func NewCLIConfig() CLIConfig {
return CLIConfig{
DaRpc: defaultDaRpc,
}
}

func ReadCLIConfig(ctx *cli.Context) CLIConfig {
return CLIConfig{
DaRpc: ctx.String(DaRpcFlagName),
}
}
103 changes: 103 additions & 0 deletions frax-da/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package fraxda

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/multiformats/go-multibase"
)

type DAClient struct {
baseUrl *url.URL
httpClient *http.Client
}

func NewDAClient(rpc string) (*DAClient, error) {
baseUrl, err := url.Parse(rpc)
if err != nil {
return nil, fmt.Errorf("unable to parse DA endpoint: %s", err)
}

httpClient := &http.Client{
Timeout: 30 * time.Second,
}

return &DAClient{
baseUrl: baseUrl,
httpClient: httpClient,
}, nil
}

func (c DAClient) Read(ctx context.Context, id []byte) ([]byte, error) {
ipfsCID, err := multibase.Encode(multibase.Base32, id)
if err != nil {
return nil, fmt.Errorf("unable to decode CID: %w", err)
}

fetchUrl := c.baseUrl.ResolveReference(&url.URL{Path: fmt.Sprintf("/v1/blobs/%s", ipfsCID)})
request, err := http.NewRequestWithContext(ctx, "GET", fetchUrl.String(), nil)
if err != nil {
return nil, fmt.Errorf("unable to create request to fetch data from DA: %w", err)
}
resp, err := c.httpClient.Do(request)

if err != nil {
return nil, fmt.Errorf("unable to fetch DA data: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unable to fetch DA data, got status code %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read DA data fetch response: %w", err)
}

return body, nil
}

func (c DAClient) Write(ctx context.Context, data []byte) ([]byte, error) {
submitUrl := c.baseUrl.ResolveReference(&url.URL{Path: "/v1/blobs"})

request, err := http.NewRequestWithContext(ctx, "POST", submitUrl.String(), bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("unable to create request to submit data to DA: %w", err)
}

resp, err := c.httpClient.Do(request)

if err != nil {
return nil, fmt.Errorf("unable to submit data to DA: %w", err)
}
if resp.StatusCode > 299 {
return nil, fmt.Errorf("unable to submit data to DA, got status code %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read DA data submit response: %w", err)
}

var respDto daSubmitResponse
err = json.Unmarshal(body, &respDto)
if err != nil {
return nil, fmt.Errorf("unable to parse DA data submit response json: %w", err)
}

if respDto.ID == "" {
return nil, fmt.Errorf("DA data submit response returned empty ID")
}

_, ipfsCID, err := multibase.Decode(respDto.ID)
if err != nil {
return nil, fmt.Errorf("DA data submit response returned invalid multibase encoded value: %w", err)
}

return ipfsCID, nil
}
4 changes: 4 additions & 0 deletions frax-da/da.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package fraxda

// DerivationVersionFraxDa is a byte prefix of frax DA based references
const DerivationVersionFraxDa = 0xfc
5 changes: 5 additions & 0 deletions frax-da/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fraxda

type daSubmitResponse struct {
ID string `json:"id"`
}
61 changes: 31 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,34 @@ require (
github.com/google/gofuzz v1.2.1-0.20220503160820-4a35382e8fc8
github.com/google/uuid v1.5.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru/v2 v2.0.5
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/hashicorp/raft v1.6.0
github.com/hashicorp/raft-boltdb v0.0.0-20231211162105-6c830fa4535e
github.com/holiman/uint256 v1.2.3
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-leveldb v0.5.0
github.com/jackc/pgtype v1.14.0
github.com/jackc/pgx/v5 v5.5.1
github.com/libp2p/go-libp2p v0.32.0
github.com/libp2p/go-libp2p v0.32.2
github.com/libp2p/go-libp2p-mplex v0.9.0
github.com/libp2p/go-libp2p-pubsub v0.10.0
github.com/libp2p/go-libp2p-testing v0.12.0
github.com/mattn/go-isatty v0.0.20
github.com/multiformats/go-base32 v0.1.0
github.com/multiformats/go-multiaddr v0.12.0
github.com/multiformats/go-multiaddr v0.12.1
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multibase v0.2.0
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.7.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.26.0
golang.org/x/crypto v0.17.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/sync v0.5.0
golang.org/x/term v0.15.0
golang.org/x/crypto v0.18.0
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
golang.org/x/sync v0.6.0
golang.org/x/term v0.16.0
golang.org/x/time v0.5.0
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
Expand Down Expand Up @@ -84,16 +85,18 @@ require (
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dop251/goja v0.0.0-20230806174421-c933cf95e127 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/fgprof v0.9.3 // indirect
github.com/fjl/memsize v0.0.1 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/flynn/noise v1.0.1 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/gballet/go-verkle v0.0.0-20230607174250-df487255f46b // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-stack/stack v1.8.1 // indirect
Expand All @@ -104,7 +107,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b // indirect
github.com/google/pprof v0.0.0-20231229205709-960ae82b1e42 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/graph-gophers/graphql-go v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -113,7 +116,7 @@ require (
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/go-msgpack/v2 v2.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/golang-lru/arc/v2 v2.0.5 // indirect
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
Expand All @@ -134,16 +137,15 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/karalabe/usb v0.0.3-0.20230711191512-61db3e06439c // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect
github.com/libp2p/go-mplex v0.7.0 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.2.0 // indirect
Expand All @@ -153,8 +155,8 @@ require (
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.56 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/miekg/dns v1.1.57 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
Expand All @@ -164,25 +166,24 @@ require (
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 // indirect
github.com/onsi/ginkgo/v2 v2.13.0 // indirect
github.com/onsi/ginkgo/v2 v2.13.2 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.4 // indirect
github.com/quic-go/quic-go v0.39.3 // indirect
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
github.com/quic-go/quic-go v0.40.1 // indirect
github.com/quic-go/webtransport-go v0.6.0 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
Expand All @@ -203,15 +204,15 @@ require (
go.uber.org/automaxprocs v1.5.2 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/fx v1.20.1 // indirect
go.uber.org/mock v0.3.0 // indirect
go.uber.org/mock v0.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 7f9a3c2

Please sign in to comment.