Skip to content

Commit

Permalink
feat: proxy support muit avs (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
fyInALT authored May 12, 2024
1 parent 0e29e8a commit c5870df
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 31 deletions.
90 changes: 59 additions & 31 deletions generic-operator-proxy/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ func NewAlertProxyRpcServer(
return server
}

func (s *ProxyHashRpcServer) handerConfigReq(w http.ResponseWriter, rpcRequest jsonrpc2.Request) {
var req []string
if err := json.Unmarshal(*rpcRequest.Params, &req); err != nil {
s.logger.Error("the unmarshal", "err", err)
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to unmarshal req bundle params: %s", err.Error()))
return
}

if len(req) != 1 {
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to unmarshal req bundle params"))
return
}

baseService, ok := s.baseServers[req[0]]
if !ok || baseService == nil {
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to found the avs config"))
return
}

avsCfg, ok := s.avsCfgs[req[0]]
if !ok {
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to found the avs config"))
return
}

cfg := avsCfg.OperatorConfigs
if cfg == "" {
cfg = "{}"
}

operator.WriteJSON(s.logger, w, rpcRequest.ID, http.StatusOK, json.RawMessage(cfg))
}

func (s *ProxyHashRpcServer) HttpRPCHandler(w http.ResponseWriter, r *http.Request) {
rpcRequest := jsonrpc2.Request{}
err := json.NewDecoder(r.Body).Decode(&rpcRequest)
Expand All @@ -104,47 +137,42 @@ func (s *ProxyHashRpcServer) HttpRPCHandler(w http.ResponseWriter, r *http.Reque
}

if rpcRequest.Method == "operator_getConfig" {
var req []string
if err = json.Unmarshal(*rpcRequest.Params, &req); err != nil {
s.logger.Error("the unmarshal", "err", err)
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to unmarshal req bundle params: %s", err.Error()))
return
}

if len(req) != 1 {
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to unmarshal req bundle params"))
return
}

baseService, ok := s.baseServers[req[0]]
if !ok || baseService == nil {
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to found the avs config"))
return
}

avsCfg, ok := s.avsCfgs[req[0]]
if !ok {
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to found the avs config"))
return
}

cfg := avsCfg.OperatorConfigs
if cfg == "" {
cfg = "{}"
}

operator.WriteJSON(s.logger, w, rpcRequest.ID, http.StatusOK, json.RawMessage(cfg))
s.handerConfigReq(w, rpcRequest)
} else {
s.rpcServer.HttpRPCHandlerRequest(w, rpcRequest)
}

}

func (s *ProxyHashRpcServer) Start(ctx context.Context) error {
s.logger.Info("start rpc server for got alert")

mux := http.NewServeMux()
mux.HandleFunc("/", s.HttpRPCHandler)

for _, avsCfg := range s.avsCfgs {
avsName := avsCfg.AVSName
mux.HandleFunc(fmt.Sprintf("/%s", avsName), func(w http.ResponseWriter, r *http.Request) {
rpcRequest := jsonrpc2.Request{}
err := json.NewDecoder(r.Body).Decode(&rpcRequest)
if err != nil {
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 1, err)
return
}

if rpcRequest.Params == nil {
err := errors.New("failed to unmarshal request.Params for mevBundle from mev-builder, error: EOF")
operator.WriteErrorJSON(s.logger, w, rpcRequest.ID, http.StatusBadRequest, 1, err)
return
}

if rpcRequest.Method == "operator_getConfig" {
s.handerConfigReq(w, rpcRequest)
} else {
s.rpcServer.HttpRPCHandlerRequestByAVS(avsName, w, rpcRequest)
}
})
}

s.rpcServer.SetHandler(mux)

if err := s.rpcServer.StartServer(ctx); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions operator/jsonrpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (s *RpcServer) HttpRPCHandler(w http.ResponseWriter, r *http.Request) {
}

func (s *RpcServer) HttpRPCHandlerRequest(w http.ResponseWriter, rpcRequest jsonrpc2.Request) {
s.HttpRPCHandlerRequestByAVS("", w, rpcRequest)
}

func (s *RpcServer) HttpRPCHandlerRequestByAVS(avsName string, w http.ResponseWriter, rpcRequest jsonrpc2.Request) {
switch rpcRequest.Method {
case "alert_blockMismatch":
{
Expand All @@ -103,6 +107,9 @@ func (s *RpcServer) HttpRPCHandlerRequest(w http.ResponseWriter, rpcRequest json
s.writeErrorJSON(w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to unmarshal alert bundle params: %s", err.Error()))
return
}
if alert.AVSName == "" && avsName != "" {
alert.AVSName = avsName
}

res := s.AlertBlockMismatch(&alert)
if res.Err != nil {
Expand All @@ -125,6 +132,9 @@ func (s *RpcServer) HttpRPCHandlerRequest(w http.ResponseWriter, rpcRequest json
s.writeErrorJSON(w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to unmarshal alert bundle params: %s", err.Error()))
return
}
if alert.AVSName == "" && avsName != "" {
alert.AVSName = avsName
}

res := s.AlertBlockOutputOracleMismatch(&alert)
if res.Err != nil {
Expand All @@ -147,6 +157,9 @@ func (s *RpcServer) HttpRPCHandlerRequest(w http.ResponseWriter, rpcRequest json
s.writeErrorJSON(w, rpcRequest.ID, http.StatusBadRequest, 3, fmt.Errorf("failed to unmarshal alert bundle params: %s", err.Error()))
return
}
if alert.AVSName == "" && avsName != "" {
alert.AVSName = avsName
}

res := s.AlertBlockHashMismatch(&alert)
if res.Err != nil {
Expand Down

0 comments on commit c5870df

Please sign in to comment.