Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
aanand1 committed Sep 11, 2024
2 parents 6b02a1a + f53b38a commit 0647544
Show file tree
Hide file tree
Showing 11 changed files with 547 additions and 54 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GOLANGCI_LINT_VERSION=v1.59.1

.PHONY: lint
lint: golangci-lint
@golangci-lint run --timeout 10m0s

.PHONY: golangci-lint
golangci-lint:
@if ! command -v golangci-lint >/dev/null 2>&1; then \
echo "golangci-lint not found, installing..."; \
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}; \
fi
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ go 1.22.5

require (
github.com/FastLane-Labs/atlas-sdk-go v0.0.0-20240905084332-938389daf445
github.com/bloXroute-Labs/bloxroute-sdk-go v1.5.0
github.com/bloXroute-Labs/bloxroute-sdk-go v1.5.1
github.com/cornelk/hashmap v1.0.8
github.com/ethereum/go-ethereum v1.14.8
github.com/go-playground/validator/v10 v10.19.0
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.1
github.com/gorilla/websocket v1.5.3
github.com/sourcegraph/jsonrpc2 v0.2.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/valyala/fastjson v1.6.4
Expand All @@ -20,7 +23,7 @@ require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/bloXroute-Labs/gateway/v2 v2.129.2 // indirect
github.com/bloXroute-Labs/gateway/v2 v2.129.19 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down Expand Up @@ -65,7 +68,6 @@ require (
github.com/satori/go.uuid v1.2.1-0.20181016170032-d91630c85102 // indirect
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/sourcegraph/jsonrpc2 v0.2.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE=
github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bloXroute-Labs/bloxroute-sdk-go v1.5.0 h1:ntvWgXSMYqNIct0RBp7mhdHRGXNQEENpncFP7ql6IDI=
github.com/bloXroute-Labs/bloxroute-sdk-go v1.5.0/go.mod h1:mHSTWuy3XgU+V2l1gFRjSKCe5zinZmnGZp6cM++6MO0=
github.com/bloXroute-Labs/gateway/v2 v2.129.2 h1:cMvcktKaAXqb5LzGaqOjKI5g+RDs4+vc2KoTjpDDICk=
github.com/bloXroute-Labs/gateway/v2 v2.129.2/go.mod h1:rd5kyusOE0Q+HeIGi8ii8BzzmVSdHRflLl5B0Ps2vZQ=
github.com/bloXroute-Labs/bloxroute-sdk-go v1.5.1 h1:EdeXQuXmxyjadtA/CPaYdyFXHjIRMBDfexQUozhFWRE=
github.com/bloXroute-Labs/bloxroute-sdk-go v1.5.1/go.mod h1:YkrHVnCJaJaic3hAXVrKy3v7derreK2LbFmUWphukjY=
github.com/bloXroute-Labs/gateway/v2 v2.129.19 h1:KSDIynNIAAFwIWvNj3OITQNTlEQZTKQS2QAZ3K6e4tA=
github.com/bloXroute-Labs/gateway/v2 v2.129.19/go.mod h1:rd5kyusOE0Q+HeIGi8ii8BzzmVSdHRflLl5B0Ps2vZQ=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ=
Expand Down Expand Up @@ -41,6 +41,8 @@ github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH
github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M=
github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cornelk/hashmap v1.0.8 h1:nv0AWgw02n+iDcawr5It4CjQIAcdMMKRrs10HOJYlrc=
github.com/cornelk/hashmap v1.0.8/go.mod h1:RfZb7JO3RviW/rT6emczVuC/oxpdz4UsSB2LJSclR1k=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c h1:uQYC5Z1mdLRPrZhHjHxufI8+2UG/i25QG92j0Er9p6I=
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c/go.mod h1:geZJZH3SzKCqnz5VT0q/DyIG/tvu/dZk+VIfXicupJs=
Expand Down Expand Up @@ -100,11 +102,13 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.1 h1:RoziI+96HlQWrbaVhgOOdFYUHtX81pwA6tCgDS9FNRo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.1/go.mod h1:Rj8lEaVgLiPn1jTMVXEhATiZhuyXJq167bMYPbJM1CY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
51 changes: 43 additions & 8 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"fmt"
"log/slog"
"os"
"strings"
Expand All @@ -13,20 +14,54 @@ func InitLogger(level string) {
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stdout, parseLevel(strings.ToLower(level)), true)))
}

func Debug(format string, v ...interface{}) {
log.Debug(format, v...)
func Debug(msg string, v ...interface{}) {
log.Debug(msg, v...)
}

func Info(format string, v ...interface{}) {
log.Info(format, v...)
func Info(msg string, v ...interface{}) {
log.Info(msg, v...)
}

func Warn(format string, v ...interface{}) {
log.Warn(format, v...)
func Warn(msg string, v ...interface{}) {
log.Warn(msg, v...)
}

func Error(format string, v ...interface{}) {
log.Error(format, v...)
func Error(msg string, v ...interface{}) {
log.Error(msg, v...)
}

type Instance struct{}

func (l *Instance) Debug(args ...interface{}) {
Debug(fmt.Sprint(args...))
}

func (l *Instance) Debugf(msg string, args ...interface{}) {
Debug(fmt.Sprintf(msg, args...))
}

func (l *Instance) Info(args ...interface{}) {
Info(fmt.Sprint(args...))
}

func (l *Instance) Infof(msg string, args ...interface{}) {
Info(fmt.Sprintf(msg, args...))
}

func (l *Instance) Warn(args ...interface{}) {
Warn(fmt.Sprint(args...))
}

func (l *Instance) Warnf(msg string, args ...interface{}) {
Warn(fmt.Sprintf(msg, args...))
}

func (l *Instance) Error(args ...interface{}) {
Error(fmt.Sprint(args...))
}

func (l *Instance) Errorf(msg string, args ...interface{}) {
Error(fmt.Sprintf(msg, args...))
}

func parseLevel(level string) slog.Level {
Expand Down
29 changes: 18 additions & 11 deletions relay/server/dapp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"encoding/json"
"fmt"
"net/http"

"github.com/FastLane-Labs/atlas-sdk-go/types"
Expand All @@ -11,24 +13,30 @@ func (s *Server) userOperation(w http.ResponseWriter, r *http.Request) {
var req types.UserOperationWithHintsRaw
err := parseRequest(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
log.Error("failed to parse request", "error", err)
writeErrResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid request: %v", err))
return
}

chainID, userOp, hints := req.Decode()
partialOperation, err := types.NewUserOperationPartialRaw(chainID, userOp, hints)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
log.Error("failed to create user operation partial", "error", err)
writeErrResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid user operation parameters: %v", err))
return
}

intentID, err := s.intentService.SubmitIntent(r.Context(), partialOperation)
data, err := json.Marshal(partialOperation)
if err != nil {
log.Error("failed to marshal user operation partial", "error", err)
writeInternalErrResponse(w)
return
}

intentID, err := s.intentService.SubmitIntent(r.Context(), data)
if err != nil {
log.Error("failed to submit intent", "error", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
writeInternalErrResponse(w)
return
}

Expand All @@ -41,16 +49,15 @@ func (s *Server) solverOperations(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
intentID := q.Get("intent_id")
if intentID == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("intent_id is required"))
log.Error("intent_id is required")
writeErrResponse(w, http.StatusBadRequest, "intent_id is required")
return
}

resp, err := s.intentService.GetIntentSolutions(r.Context(), intentID)
if err != nil {
log.Error("failed to get intent solutions", "error", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
writeInternalErrResponse(w)
return
}

Expand Down
9 changes: 9 additions & 0 deletions relay/server/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package server

type pingResponse struct {
Pong string `json:"pong"`
}

type subscribeResponse struct {
SubscriptionID string `json:"subscription_id"`
}
46 changes: 39 additions & 7 deletions relay/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@ import (

// Server handler http calls
type Server struct {
server *http.Server
cfg *config.Config
intentService *service.Intent
server *http.Server
cfg *config.Config
intentService *service.Intent
subscriptionService *service.SubscriptionManager
}

// NewServer creates and returns a new websocket server managed by feedManager
func NewServer(ctx context.Context, cfg *config.Config) (*Server, error) {
intentService, err := service.NewIntent(ctx, cfg)
subsManager := service.NewSubscriptionManager()
intentService, err := service.NewIntent(ctx, cfg, subsManager)
if err != nil {
return nil, fmt.Errorf("failed to create intent service: %v", err)
}

// subscribe to intents right away
err = intentService.SubscribeToIntents(ctx)
if err != nil {
return nil, fmt.Errorf("failed to subscribe to intents: %v", err)
}

return &Server{
cfg: cfg,
intentService: intentService,
cfg: cfg,
intentService: intentService,
subscriptionService: subsManager,
}, nil
}

Expand Down Expand Up @@ -76,13 +85,32 @@ func (s *Server) Shutdown() {
if err != nil {
logger.Error("failed to close intent service", "error", err)
}

s.subscriptionService.Close()
}

func writeResponseData(w http.ResponseWriter, data interface{}) {
b, err := json.Marshal(data)
if err != nil {
logger.Error("failed to marshal response data", "error", err)
writeErrResponse(w, http.StatusInternalServerError, err.Error())
}

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
_, err = w.Write(b)
if err != nil {
logger.Error("failed to write response", "error", err)
}
}

func writeErrResponse(w http.ResponseWriter, status int, errMessage string) {
resp := map[string]string{
"error": errMessage,
}

b, err := json.Marshal(resp)
if err != nil {
logger.Error("failed to marshal error response data", "error", err)
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write([]byte(err.Error()))
if err != nil {
Expand All @@ -91,13 +119,17 @@ func writeResponseData(w http.ResponseWriter, data interface{}) {
return
}

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(status)
_, err = w.Write(b)
if err != nil {
logger.Error("failed to write response", "error", err)
}
}

func writeInternalErrResponse(w http.ResponseWriter) {
writeErrResponse(w, http.StatusInternalServerError, "something went wrong, please try again later")
}

func parseRequest(r *http.Request, v interface{}) error {
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
Expand Down
22 changes: 21 additions & 1 deletion relay/server/solver.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package server

import (
"context"
"net/http"

"github.com/gorilla/websocket"
"github.com/sourcegraph/jsonrpc2"
jsonrpc2_ws "github.com/sourcegraph/jsonrpc2/websocket"

"github.com/bloXroute-Labs/bdn-operations-relay/logger"
)

const (
Expand All @@ -19,6 +24,21 @@ var upgrader = websocket.Upgrader{
},
}

func (s *Server) websocketSolver(w http.ResponseWriter, _ *http.Request) {
func (s *Server) websocketSolver(w http.ResponseWriter, r *http.Request) {
connection, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logger.Error("failed upgrading connection", "err", err)
writeInternalErrResponse(w)

return
}

h := &wsConnHandler{
remoteAddress: r.RemoteAddr,
intentService: s.intentService,
subscriptionService: s.subscriptionService,
}

asyncHandler := jsonrpc2.AsyncHandler(h)
_ = jsonrpc2.NewConn(context.Background(), jsonrpc2_ws.NewObjectStream(connection), asyncHandler)
}
Loading

0 comments on commit 0647544

Please sign in to comment.