-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
268 additions
and
2 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
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,32 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt" | ||
"github.com/mdehoog/op-nitro/op-da/da" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/ethereum-optimism/optimism/op-proposer/flags" | ||
"github.com/ethereum-optimism/optimism/op-service/cliapp" | ||
oplog "github.com/ethereum-optimism/optimism/op-service/log" | ||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
func main() { | ||
oplog.SetupDefaults() | ||
|
||
app := cli.NewApp() | ||
app.Flags = cliapp.ProtectFlags(flags.Flags) | ||
app.Name = "op-da" | ||
app.Usage = "Alt DA server" | ||
app.Action = cliapp.LifecycleCmd(da.Main) | ||
|
||
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background()) | ||
err := app.RunContext(ctx, os.Args) | ||
if err != nil { | ||
log.Crit("Application failed", "message", err) | ||
} | ||
} |
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,22 @@ | ||
package da | ||
|
||
import ( | ||
"github.com/mdehoog/op-nitro/op-da/flags" | ||
"github.com/urfave/cli/v2" | ||
|
||
oplog "github.com/ethereum-optimism/optimism/op-service/log" | ||
) | ||
|
||
type CLIConfig struct { | ||
Port int | ||
DAURL string | ||
LogConfig oplog.CLIConfig | ||
} | ||
|
||
func NewConfig(ctx *cli.Context) *CLIConfig { | ||
return &CLIConfig{ | ||
Port: ctx.Int(flags.PortFlag.Name), | ||
DAURL: ctx.String(flags.DAURLFlag.Name), | ||
LogConfig: oplog.ReadCLIConfig(ctx), | ||
} | ||
} |
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,30 @@ | ||
package da | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"os" | ||
"path/filepath" | ||
|
||
altda "github.com/ethereum-optimism/optimism/op-alt-da" | ||
) | ||
|
||
type filestore struct { | ||
path string | ||
} | ||
|
||
var _ altda.KVStore = &filestore{} | ||
|
||
func NewFilestore(path string) altda.KVStore { | ||
return &filestore{ | ||
path: path, | ||
} | ||
} | ||
|
||
func (s *filestore) Get(ctx context.Context, key []byte) ([]byte, error) { | ||
return os.ReadFile(filepath.Join(s.path, base64.RawURLEncoding.EncodeToString(key))) | ||
} | ||
|
||
func (s *filestore) Put(ctx context.Context, key []byte, value []byte) error { | ||
return os.WriteFile(filepath.Join(s.path, base64.RawURLEncoding.EncodeToString(key)), value, 0644) | ||
} |
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,57 @@ | ||
package da | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"path/filepath" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
altda "github.com/ethereum-optimism/optimism/op-alt-da" | ||
) | ||
|
||
type s3store struct { | ||
bucket string | ||
path string | ||
downloader *s3manager.Downloader | ||
uploader *s3manager.Uploader | ||
} | ||
|
||
var _ altda.KVStore = &s3store{} | ||
|
||
func NewS3store(bucket, path string) altda.KVStore { | ||
sess := session.Must(session.NewSession()) | ||
downloader := s3manager.NewDownloader(sess) | ||
uploader := s3manager.NewUploader(sess) | ||
return &s3store{ | ||
bucket: bucket, | ||
path: path, | ||
downloader: downloader, | ||
uploader: uploader, | ||
} | ||
} | ||
|
||
func (s *s3store) Get(ctx context.Context, key []byte) ([]byte, error) { | ||
buf := aws.NewWriteAtBuffer([]byte{}) | ||
_, err := s.downloader.DownloadWithContext(ctx, buf, &s3.GetObjectInput{ | ||
Bucket: aws.String(s.bucket), | ||
Key: aws.String(filepath.Join(s.path, base64.RawURLEncoding.EncodeToString(key))), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (s *s3store) Put(ctx context.Context, key []byte, value []byte) error { | ||
b := bytes.NewBuffer(value) | ||
_, err := s.uploader.UploadWithContext(ctx, &s3manager.UploadInput{ | ||
Bucket: aws.String(s.bucket), | ||
Key: aws.String(filepath.Join(s.path, base64.RawURLEncoding.EncodeToString(key))), | ||
Body: b, | ||
}) | ||
return err | ||
} |
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,77 @@ | ||
package da | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
"sync/atomic" | ||
|
||
altda "github.com/ethereum-optimism/optimism/op-alt-da" | ||
opservice "github.com/ethereum-optimism/optimism/op-service" | ||
"github.com/ethereum-optimism/optimism/op-service/cliapp" | ||
oplog "github.com/ethereum-optimism/optimism/op-service/log" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/mdehoog/op-nitro/op-da/flags" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func Main(cliCtx *cli.Context, _ context.CancelCauseFunc) (cliapp.Lifecycle, error) { | ||
cfg := NewConfig(cliCtx) | ||
|
||
l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.LogConfig) | ||
oplog.SetGlobalLogHandler(l.Handler()) | ||
opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, l) | ||
|
||
l.Info("Initializing alt-DA server") | ||
return ServiceFromCLIConfig(cliCtx.Context, cfg, l) | ||
} | ||
|
||
func ServiceFromCLIConfig(ctx context.Context, cfg *CLIConfig, l log.Logger) (cliapp.Lifecycle, error) { | ||
store, err := newStore(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
server := altda.NewDAServer("", cfg.Port, store, l, false) | ||
|
||
return &service{ | ||
server: server, | ||
}, nil | ||
} | ||
|
||
type service struct { | ||
server *altda.DAServer | ||
stopped atomic.Bool | ||
} | ||
|
||
func (s *service) Start(ctx context.Context) error { | ||
return s.server.Start() | ||
} | ||
|
||
func (s *service) Stop(ctx context.Context) error { | ||
if s.stopped.Swap(true) { | ||
return nil | ||
} | ||
return s.server.Stop() | ||
} | ||
|
||
func (s *service) Stopped() bool { | ||
return s.stopped.Load() | ||
} | ||
|
||
func newStore(cfg *CLIConfig) (altda.KVStore, error) { | ||
u, err := url.Parse(cfg.DAURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse DA URL: %w", err) | ||
} | ||
switch u.Scheme { | ||
case "s3": | ||
split := strings.SplitN(u.Path, "/", 2) | ||
return NewS3store(split[0], split[1]), nil | ||
case "file": | ||
return NewFilestore(u.Path), nil | ||
default: | ||
return nil, fmt.Errorf("unsupported DA scheme: %s", u.Scheme) | ||
} | ||
} |
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,39 @@ | ||
package flags | ||
|
||
import ( | ||
opservice "github.com/ethereum-optimism/optimism/op-service" | ||
oplog "github.com/ethereum-optimism/optimism/op-service/log" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
const EnvVarPrefix = "OP_DA" | ||
|
||
func prefixEnvVar(name string) []string { | ||
return opservice.PrefixEnvVar(EnvVarPrefix, name) | ||
} | ||
|
||
var ( | ||
PortFlag = &cli.IntFlag{ | ||
Name: "port", | ||
Usage: "Port to listen on", | ||
EnvVars: prefixEnvVar("PORT"), | ||
Required: true, | ||
} | ||
DAURLFlag = &cli.StringFlag{ | ||
Name: "da-url", | ||
Usage: "URL for DA (file, http, S3)", | ||
EnvVars: prefixEnvVar("DA_URL"), | ||
Required: true, | ||
} | ||
) | ||
|
||
var requiredFlags = []cli.Flag{ | ||
PortFlag, | ||
DAURLFlag, | ||
} | ||
|
||
func init() { | ||
Flags = append(requiredFlags, oplog.CLIFlags(EnvVarPrefix)...) | ||
} | ||
|
||
var Flags []cli.Flag |