Skip to content

Commit

Permalink
feat: initial shot
Browse files Browse the repository at this point in the history
  • Loading branch information
vcastellm committed Jan 31, 2025
1 parent 87ef647 commit 42417b1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
Empty file.
2 changes: 1 addition & 1 deletion proposer/op/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/prometheus/client_golang v1.20.2
github.com/slack-go/slack v0.14.0
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/urfave/cli/v2 v2.27.4
golang.org/x/sync v0.8.0
)
Expand Down
2 changes: 2 additions & 0 deletions proposer/op/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4=
github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
Expand Down
5 changes: 5 additions & 0 deletions proposer/op/op_proposer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

# Currently, configured to generate a proof once per minute.

# Required environment variables:
# L2_NODE_RPC: The RPC endpoint of the L2 node.
# DB_PATH: The path to the database.
/usr/local/bin/op-proposer-server &

/usr/local/bin/op-proposer \
--poll-interval=${POLL_INTERVAL:-20s} \
--rollup-rpc=${L2_NODE_RPC} \
Expand Down
3 changes: 2 additions & 1 deletion proposer/op/proposer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ LDFLAGS := -ldflags "$(LDFLAGSSTRING)"

op-proposer:
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) CGO_ENABLED=1 go build -v $(LDFLAGS) -o ./bin/op-proposer ./cmd
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) CGO_ENABLED=1 go build -v $(LDFLAGS) -o ./bin/op-proposer-server ../server

clean:
rm bin/op-proposer
Expand All @@ -28,4 +29,4 @@ test:
.PHONY: \
clean \
op-proposer \
test
test
Binary file added proposer/op/proposer/bin/op-proposer-server
Binary file not shown.
3 changes: 3 additions & 0 deletions proposer/op/proposer/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type SpanBatchRange struct {
End uint64 `json:"end"`
}

type RequestSpanProofsResponse struct {
}

// BatchDecoderConfig is a struct that holds the configuration for the batch decoder.
type BatchDecoderConfig struct {
L2GenesisTime uint64
Expand Down
81 changes: 81 additions & 0 deletions proposer/op/server/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"
"net/http"
"os"
"path/filepath"
"sort"

"github.com/ethereum-optimism/optimism/op-service/dial"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/succinctlabs/op-succinct-go/proposer/db"
"github.com/succinctlabs/op-succinct-go/proposer/db/ent"
"github.com/succinctlabs/op-succinct-go/proposer/db/ent/proofrequest"
"github.com/succinctlabs/op-succinct-go/proposer/utils"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -32,14 +38,65 @@ type SpanBatchResponse struct {
Ranges []utils.SpanBatchRange `json:"ranges"`
}

// Span batch request is a request to find all span batches in a given block range.
type FetchSpanProofRequest struct {
StartBlock uint64 `json:"startBlock"`
EndBlock uint64 `json:"endBlock"`
}

// Span batch request is a request to find all span batches in a given block range.
type FetchSpanProofResponse struct {
Proofs []*ent.ProofRequest `json:"proofs"`
}

type server struct {
db *db.ProofDB
}

func main() {
s, err := newServer()
if err != nil {
log.Fatal(err)
}

r := mux.NewRouter()
r.HandleFunc("/span-batch-ranges", handleSpanBatchRanges).Methods("POST")
r.HandleFunc("/fetch-span-proofs", s.handleFetchSpanProofs).Methods("POST")

fmt.Println("Server is running on :8089")
log.Fatal(http.ListenAndServe(":8089", r))
}

func newServer() (*server, error) {
ctx := context.Background()

// Load environment variables
l2rpc := os.Getenv("L2_RPC")
dbPath := os.Getenv("DB_PATH")

// Get the L2 chain ID from the rollup config
rollupClient, err := dial.DialRollupClientWithTimeout(ctx, dial.DefaultDialTimeout, nil, l2rpc)
if err != nil {
log.Fatal(err)
}

rollupConfig, err := rollupClient.RollupConfig(ctx)
if err != nil {
log.Fatal(err)
}

dbPath = filepath.Join(dbPath, fmt.Sprintf("%d", rollupConfig.L2ChainID.Uint64()), "proofs.db")

db, err := db.InitDB(dbPath, false)
if err != nil {
return nil, err
}

return &server{
db: db,
}, nil
}

// Return all of the span batches in a given L2 block range.
func handleSpanBatchRanges(w http.ResponseWriter, r *http.Request) {
var req SpanBatchRequest
Expand Down Expand Up @@ -101,3 +158,27 @@ func handleSpanBatchRanges(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}

// Return the requested span proofs from the DB.
func (s *server) handleFetchSpanProofs(w http.ResponseWriter, r *http.Request) {
var req FetchSpanProofRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

preqs, err := s.db.GetProofRequestsWithBlockRangeAndStatus(proofrequest.TypeSPAN, req.StartBlock, req.EndBlock, proofrequest.StatusCOMPLETE)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

response := FetchSpanProofResponse{
Proofs: preqs,
}

fmt.Printf("Response: %v\n", response)

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}

0 comments on commit 42417b1

Please sign in to comment.