mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-26 18:04:22 +00:00
cleanup: Reduce and normalize import path aliasing. (#6975)
The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
This commit is contained in:
@@ -12,8 +12,8 @@ import (
|
||||
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/coretypes"
|
||||
"github.com/tendermint/tendermint/rpc/jsonrpc/types"
|
||||
"github.com/tendermint/tendermint/rpc/coretypes"
|
||||
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
|
||||
)
|
||||
|
||||
// HTTP + JSON handler
|
||||
@@ -23,7 +23,7 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
res := types.RPCInvalidRequestError(nil,
|
||||
res := rpctypes.RPCInvalidRequestError(nil,
|
||||
fmt.Errorf("error reading request body: %w", err),
|
||||
)
|
||||
if wErr := WriteRPCResponseHTTPError(w, res); wErr != nil {
|
||||
@@ -41,20 +41,20 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
|
||||
|
||||
// first try to unmarshal the incoming request as an array of RPC requests
|
||||
var (
|
||||
requests []types.RPCRequest
|
||||
responses []types.RPCResponse
|
||||
requests []rpctypes.RPCRequest
|
||||
responses []rpctypes.RPCResponse
|
||||
)
|
||||
if err := json.Unmarshal(b, &requests); err != nil {
|
||||
// next, try to unmarshal as a single request
|
||||
var request types.RPCRequest
|
||||
var request rpctypes.RPCRequest
|
||||
if err := json.Unmarshal(b, &request); err != nil {
|
||||
res := types.RPCParseError(fmt.Errorf("error unmarshaling request: %w", err))
|
||||
res := rpctypes.RPCParseError(fmt.Errorf("error unmarshaling request: %w", err))
|
||||
if wErr := WriteRPCResponseHTTPError(w, res); wErr != nil {
|
||||
logger.Error("failed to write response", "res", res, "err", wErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
requests = []types.RPCRequest{request}
|
||||
requests = []rpctypes.RPCRequest{request}
|
||||
}
|
||||
|
||||
// Set the default response cache to true unless
|
||||
@@ -77,25 +77,25 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
|
||||
if len(r.URL.Path) > 1 {
|
||||
responses = append(
|
||||
responses,
|
||||
types.RPCInvalidRequestError(request.ID, fmt.Errorf("path %s is invalid", r.URL.Path)),
|
||||
rpctypes.RPCInvalidRequestError(request.ID, fmt.Errorf("path %s is invalid", r.URL.Path)),
|
||||
)
|
||||
c = false
|
||||
continue
|
||||
}
|
||||
rpcFunc, ok := funcMap[request.Method]
|
||||
if !ok || rpcFunc.ws {
|
||||
responses = append(responses, types.RPCMethodNotFoundError(request.ID))
|
||||
responses = append(responses, rpctypes.RPCMethodNotFoundError(request.ID))
|
||||
c = false
|
||||
continue
|
||||
}
|
||||
ctx := &types.Context{JSONReq: &request, HTTPReq: r}
|
||||
ctx := &rpctypes.Context{JSONReq: &request, HTTPReq: r}
|
||||
args := []reflect.Value{reflect.ValueOf(ctx)}
|
||||
if len(request.Params) > 0 {
|
||||
fnArgs, err := jsonParamsToArgs(rpcFunc, request.Params)
|
||||
if err != nil {
|
||||
responses = append(
|
||||
responses,
|
||||
types.RPCInvalidParamsError(request.ID, fmt.Errorf("error converting json params to arguments: %w", err)),
|
||||
rpctypes.RPCInvalidParamsError(request.ID, fmt.Errorf("error converting json params to arguments: %w", err)),
|
||||
)
|
||||
c = false
|
||||
continue
|
||||
@@ -114,22 +114,22 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
|
||||
switch e := err.(type) {
|
||||
// if no error then return a success response
|
||||
case nil:
|
||||
responses = append(responses, types.NewRPCSuccessResponse(request.ID, result))
|
||||
responses = append(responses, rpctypes.NewRPCSuccessResponse(request.ID, result))
|
||||
|
||||
// if this already of type RPC error then forward that error
|
||||
case *types.RPCError:
|
||||
responses = append(responses, types.NewRPCErrorResponse(request.ID, e.Code, e.Message, e.Data))
|
||||
case *rpctypes.RPCError:
|
||||
responses = append(responses, rpctypes.NewRPCErrorResponse(request.ID, e.Code, e.Message, e.Data))
|
||||
c = false
|
||||
default: // we need to unwrap the error and parse it accordingly
|
||||
switch errors.Unwrap(err) {
|
||||
// check if the error was due to an invald request
|
||||
case ctypes.ErrZeroOrNegativeHeight, ctypes.ErrZeroOrNegativePerPage,
|
||||
ctypes.ErrPageOutOfRange, ctypes.ErrInvalidRequest:
|
||||
responses = append(responses, types.RPCInvalidRequestError(request.ID, err))
|
||||
case coretypes.ErrZeroOrNegativeHeight, coretypes.ErrZeroOrNegativePerPage,
|
||||
coretypes.ErrPageOutOfRange, coretypes.ErrInvalidRequest:
|
||||
responses = append(responses, rpctypes.RPCInvalidRequestError(request.ID, err))
|
||||
c = false
|
||||
// lastly default all remaining errors as internal errors
|
||||
default: // includes ctypes.ErrHeightNotAvailable and ctypes.ErrHeightExceedsChainHead
|
||||
responses = append(responses, types.RPCInternalError(request.ID, err))
|
||||
responses = append(responses, rpctypes.RPCInternalError(request.ID, err))
|
||||
c = false
|
||||
}
|
||||
}
|
||||
@@ -277,7 +277,7 @@ func writeListOfEndpoints(w http.ResponseWriter, r *http.Request, funcMap map[st
|
||||
w.Write(buf.Bytes()) // nolint: errcheck
|
||||
}
|
||||
|
||||
func hasDefaultHeight(r types.RPCRequest, h []reflect.Value) bool {
|
||||
func hasDefaultHeight(r rpctypes.RPCRequest, h []reflect.Value) bool {
|
||||
switch r.Method {
|
||||
case "block", "block_results", "commit", "consensus_params", "validators":
|
||||
return len(h) < 2 || h[1].IsZero()
|
||||
|
||||
Reference in New Issue
Block a user