-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement op-node/op-batcher frax DA
- Loading branch information
Showing
22 changed files
with
419 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package fraxda | ||
|
||
type daSubmitResponse struct { | ||
ID string `json:"id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.