oracle: unify filtering and add log for error case
Just to simplify debugging.
This commit is contained in:
parent
019af7de46
commit
6de6415a53
2 changed files with 13 additions and 14 deletions
|
@ -7,7 +7,6 @@ import (
|
||||||
|
|
||||||
json "github.com/nspcc-dev/go-ordered-json"
|
json "github.com/nspcc-dev/go-ordered-json"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/services/oracle/jsonpath"
|
"github.com/nspcc-dev/neo-go/pkg/services/oracle/jsonpath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -32,13 +31,9 @@ func filter(value []byte, path string) ([]byte, error) {
|
||||||
return json.Marshal(result)
|
return json.Marshal(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterRequest(result []byte, req *state.OracleRequest) (transaction.OracleResponseCode, []byte) {
|
func filterRequest(result []byte, req *state.OracleRequest) ([]byte, error) {
|
||||||
if req.Filter != nil {
|
if req.Filter != nil {
|
||||||
var err error
|
return filter(result, *req.Filter)
|
||||||
result, err = filter(result, *req.Filter)
|
|
||||||
if err != nil {
|
|
||||||
return transaction.Error, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return transaction.Success, result
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,7 +112,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
|
||||||
if incTx == nil {
|
if incTx == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
resp := &transaction.OracleResponse{ID: req.ID}
|
resp := &transaction.OracleResponse{ID: req.ID, Code: transaction.Success}
|
||||||
u, err := url.ParseRequestURI(req.Req.URL)
|
u, err := url.ParseRequestURI(req.Req.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
o.Log.Warn("malformed oracle request", zap.String("url", req.Req.URL), zap.Error(err))
|
o.Log.Warn("malformed oracle request", zap.String("url", req.Req.URL), zap.Error(err))
|
||||||
|
@ -149,7 +149,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := readResponse(r.Body, transaction.MaxOracleResultSize)
|
resp.Result, err = readResponse(r.Body, transaction.MaxOracleResultSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, ErrResponseTooLarge) {
|
if errors.Is(err, ErrResponseTooLarge) {
|
||||||
resp.Code = transaction.ResponseTooLarge
|
resp.Code = transaction.ResponseTooLarge
|
||||||
|
@ -159,7 +159,6 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
|
||||||
o.Log.Warn("failed to read data for oracle request", zap.String("url", req.Req.URL), zap.Error(err))
|
o.Log.Warn("failed to read data for oracle request", zap.String("url", req.Req.URL), zap.Error(err))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
resp.Code, resp.Result = filterRequest(result, req.Req)
|
|
||||||
case http.StatusForbidden:
|
case http.StatusForbidden:
|
||||||
resp.Code = transaction.Forbidden
|
resp.Code = transaction.Forbidden
|
||||||
case http.StatusNotFound:
|
case http.StatusNotFound:
|
||||||
|
@ -173,18 +172,23 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), o.MainCfg.NeoFS.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), o.MainCfg.NeoFS.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
index := (int(req.ID) + incTx.attempts) % len(o.MainCfg.NeoFS.Nodes)
|
index := (int(req.ID) + incTx.attempts) % len(o.MainCfg.NeoFS.Nodes)
|
||||||
res, err := neofs.Get(ctx, priv, u, o.MainCfg.NeoFS.Nodes[index])
|
resp.Result, err = neofs.Get(ctx, priv, u, o.MainCfg.NeoFS.Nodes[index])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err))
|
o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err))
|
||||||
resp.Code = transaction.Error
|
resp.Code = transaction.Error
|
||||||
} else {
|
|
||||||
resp.Code, resp.Result = filterRequest(res, req.Req)
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
resp.Code = transaction.ProtocolNotSupported
|
resp.Code = transaction.ProtocolNotSupported
|
||||||
o.Log.Warn("unknown oracle request scheme", zap.String("url", req.Req.URL))
|
o.Log.Warn("unknown oracle request scheme", zap.String("url", req.Req.URL))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if resp.Code == transaction.Success {
|
||||||
|
resp.Result, err = filterRequest(resp.Result, req.Req)
|
||||||
|
if err != nil {
|
||||||
|
o.Log.Warn("oracle filter failed", zap.Uint64("request", req.ID), zap.Error(err))
|
||||||
|
resp.Code = transaction.Error
|
||||||
|
}
|
||||||
|
}
|
||||||
o.Log.Debug("oracle request processed", zap.String("url", req.Req.URL), zap.Int("code", int(resp.Code)), zap.String("result", string(resp.Result)))
|
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()
|
currentHeight := o.Chain.BlockHeight()
|
||||||
|
|
Loading…
Reference in a new issue