From f69fd342206e6716c48767118665cf57a6c000df Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 6 Apr 2021 17:02:00 +0300 Subject: [PATCH] oracle: add logging, refactor request processing a bit And return more appropriate errors in some cases. --- pkg/services/oracle/request.go | 61 +++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/pkg/services/oracle/request.go b/pkg/services/oracle/request.go index df7cc82dd..9a354b330 100644 --- a/pkg/services/oracle/request.go +++ b/pkg/services/oracle/request.go @@ -98,20 +98,27 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error { resp := &transaction.OracleResponse{ID: req.ID} u, err := url.ParseRequestURI(req.Req.URL) if err != nil { - resp.Code = transaction.Forbidden - } else if u.Scheme == "https" { - if !o.MainCfg.AllowPrivateHost { - err = o.URIValidator(u) - if err != nil { - resp.Code = transaction.Forbidden + o.Log.Warn("malformed oracle request", zap.String("url", req.Req.URL), zap.Error(err)) + resp.Code = transaction.ProtocolNotSupported + } else { + switch u.Scheme { + case "https": + if !o.MainCfg.AllowPrivateHost { + err = o.URIValidator(u) + if err != nil { + o.Log.Warn("forbidden oracle request", zap.String("url", req.Req.URL)) + resp.Code = transaction.Forbidden + break + } } - } - if err == nil { r, err := o.Client.Get(req.Req.URL) - switch { - case err != nil: + if err != nil { + o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err)) resp.Code = transaction.Error - case r.StatusCode == http.StatusOK: + break + } + switch r.StatusCode { + case http.StatusOK: result, err := readResponse(r.Body, transaction.MaxOracleResultSize) if err != nil { if errors.Is(err, ErrResponseTooLarge) { @@ -119,30 +126,36 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error { } else { resp.Code = transaction.Error } + o.Log.Warn("failed to read data for oracle request", zap.String("url", req.Req.URL), zap.Error(err)) break } resp.Code, resp.Result = filterRequest(result, req.Req) - case r.StatusCode == http.StatusForbidden: + case http.StatusForbidden: resp.Code = transaction.Forbidden - case r.StatusCode == http.StatusNotFound: + case http.StatusNotFound: resp.Code = transaction.NotFound - case r.StatusCode == http.StatusRequestTimeout: + case http.StatusRequestTimeout: resp.Code = transaction.Timeout default: resp.Code = transaction.Error } - } - } else if err == nil && u.Scheme == neofs.URIScheme { - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(o.MainCfg.NeoFS.Timeout)*time.Millisecond) - defer cancel() - index := (int(req.ID) + incTx.attempts) % len(o.MainCfg.NeoFS.Nodes) - res, err := neofs.Get(ctx, priv, u, o.MainCfg.NeoFS.Nodes[index]) - if err != nil { - resp.Code = transaction.Error - } else { - resp.Code, resp.Result = filterRequest(res, req.Req) + case neofs.URIScheme: + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(o.MainCfg.NeoFS.Timeout)*time.Millisecond) + defer cancel() + index := (int(req.ID) + incTx.attempts) % len(o.MainCfg.NeoFS.Nodes) + res, err := neofs.Get(ctx, priv, u, o.MainCfg.NeoFS.Nodes[index]) + if err != nil { + o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err)) + resp.Code = transaction.Error + } else { + resp.Code, resp.Result = filterRequest(res, req.Req) + } + default: + resp.Code = transaction.ProtocolNotSupported + o.Log.Warn("unknown oracle request scheme", zap.String("url", req.Req.URL)) } } + o.Log.Debug("oracle request processed", zap.String("url", req.Req.URL), zap.Int("code", int(resp.Code)), zap.String("result", string(resp.Result))) currentHeight := o.Chain.BlockHeight() _, h, err := o.Chain.GetTransaction(req.Req.OriginalTxID)