Skip to content

Commit

Permalink
Implement using rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
vcastellm committed Jan 31, 2025
1 parent 42417b1 commit f4e4ec5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 90 deletions.
5 changes: 0 additions & 5 deletions proposer/op/op_proposer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

# 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
1 change: 0 additions & 1 deletion proposer/op/proposer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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 Down
43 changes: 43 additions & 0 deletions proposer/op/proposer/proofs_rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package proposer

import (
"context"

gethrpc "github.com/ethereum/go-ethereum/rpc"
"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"
)

// Define a new Proofs API that will provider the Proofs API to the RPC server
type ProofsAPI struct {
db *db.ProofDB
}

func NewProofsAPI(dbPath string, useCachedDb bool) (*ProofsAPI, error) {
db, err := db.InitDB(dbPath, useCachedDb)
if err != nil {
return nil, err
}

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

// GetProofsAPI returns the ProofsAPI struct
func GetProofsAPI(api *ProofsAPI) gethrpc.API {
return gethrpc.API{
Namespace: "proofs",
Service: api,
}
}

func (pa *ProofsAPI) GetSpanProof(ctx context.Context, startBlock, endBlock uint64) ([]*ent.ProofRequest, error) {
preqs, err := pa.db.GetProofRequestsWithBlockRangeAndStatus(proofrequest.TypeSPAN, startBlock, endBlock, proofrequest.StatusCOMPLETE)
if err != nil {
return nil, err
}

return preqs, nil
}
8 changes: 8 additions & 0 deletions proposer/op/proposer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ func (ps *ProposerService) initRPCServer(cfg *CLIConfig) error {
server.AddAPI(rpc.GetAdminAPI(adminAPI))
ps.Log.Info("Admin RPC enabled")
}

// Instantiate a new proofs API and add it to the server
proofsAPI, err := NewProofsAPI(cfg.DbPath, cfg.UseCachedDb)
if err != nil {
return fmt.Errorf("failed to create ProofsAPI: %w", err)
}
server.AddAPI(GetProofsAPI(proofsAPI))

ps.Log.Info("Starting JSON-RPC server")
if err := server.Start(); err != nil {
return fmt.Errorf("unable to start RPC server: %w", err)
Expand Down
3 changes: 0 additions & 3 deletions proposer/op/proposer/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ 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: 0 additions & 81 deletions proposer/op/server/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
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 @@ -38,65 +32,14 @@ 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 @@ -158,27 +101,3 @@ 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 f4e4ec5

Please sign in to comment.