lotus/node/rpc.go

186 lines
5.2 KiB
Go
Raw Normal View History

package node
2019-07-18 23:18:26 +00:00
import (
2019-09-17 14:23:08 +00:00
"context"
2020-10-21 08:37:50 +00:00
"net"
2019-07-18 23:18:26 +00:00
"net/http"
2019-09-30 23:25:45 +00:00
_ "net/http/pprof"
"runtime"
"strconv"
2022-11-25 21:24:09 +00:00
"time"
2019-07-18 23:18:26 +00:00
"github.com/gorilla/mux"
logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
2020-10-21 08:37:50 +00:00
"go.opencensus.io/tag"
"golang.org/x/xerrors"
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/go-jsonrpc"
2020-05-20 18:23:51 +00:00
"github.com/filecoin-project/go-jsonrpc/auth"
2020-05-20 17:43:22 +00:00
2021-03-25 14:09:50 +00:00
"github.com/filecoin-project/lotus/api"
2021-03-23 15:40:22 +00:00
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/api/v1api"
2021-06-14 09:27:54 +00:00
"github.com/filecoin-project/lotus/lib/rpcenc"
2020-10-21 08:10:27 +00:00
"github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/lotus/metrics/proxy"
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/lotus/node/impl"
2019-07-18 23:18:26 +00:00
)
var rpclog = logging.Logger("rpc")
2019-09-17 14:23:08 +00:00
// ServeRPC serves an HTTP handler over the supplied listen multiaddr.
2021-05-23 11:40:33 +00:00
//
// This function spawns a goroutine to run the server, and returns immediately.
2021-05-23 11:40:33 +00:00
// It returns the stop function to be called to terminate the endpoint.
//
// The supplied ID is used in tracing, by inserting a tag in the context.
func ServeRPC(h http.Handler, id string, addr multiaddr.Multiaddr) (StopFunc, error) {
// Start listening to the addr; if invalid or occupied, we will fail early.
lst, err := manet.Listen(addr)
if err != nil {
return nil, xerrors.Errorf("could not listen: %w", err)
}
// Instantiate the server and start listening.
srv := &http.Server{
2022-11-25 21:24:09 +00:00
Handler: h,
2022-11-29 15:31:45 +00:00
ReadHeaderTimeout: 30 * time.Second,
BaseContext: func(listener net.Listener) context.Context {
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, id))
return ctx
},
2020-11-17 13:39:53 +00:00
}
go func() {
err = srv.Serve(manet.NetListener(lst))
if err != http.ErrServerClosed {
rpclog.Warnf("rpc server failed: %s", err)
}
}()
return srv.Shutdown, err
}
// FullNodeHandler returns a full node handler, to be mounted as-is on the server.
func FullNodeHandler(a v1api.FullNode, permissioned bool, opts ...jsonrpc.ServerOption) (http.Handler, error) {
m := mux.NewRouter()
2021-03-23 15:40:22 +00:00
serveRpc := func(path string, hnd interface{}) {
rpcServer := jsonrpc.NewServer(append(opts, jsonrpc.WithReverseClient[api.EthSubscriberMethods]("Filecoin"), jsonrpc.WithServerErrors(api.RPCErrors))...)
2021-03-23 15:40:22 +00:00
rpcServer.Register("Filecoin", hnd)
2022-05-27 11:23:32 +00:00
rpcServer.AliasMethod("rpc.discover", "Filecoin.Discover")
2019-07-24 13:40:42 +00:00
2023-01-13 19:08:35 +00:00
api.CreateEthRPCAliases(rpcServer)
var handler http.Handler = rpcServer
if permissioned {
handler = &auth.Handler{Verify: a.AuthVerify, Next: rpcServer.ServeHTTP}
2021-03-23 15:40:22 +00:00
}
m.Handle(path, handler)
2019-07-24 13:40:42 +00:00
}
fnapi := proxy.MetricedFullAPI(a)
if permissioned {
fnapi = api.PermissionedFullAPI(fnapi)
}
2021-03-23 15:40:22 +00:00
2023-01-19 16:02:09 +00:00
var v0 v0api.FullNode = &(struct{ v0api.FullNode }{&v0api.WrapperV1Full{FullNode: fnapi}})
serveRpc("/rpc/v1", fnapi)
2023-01-19 16:02:09 +00:00
serveRpc("/rpc/v0", v0)
2019-09-17 14:23:08 +00:00
// debugging
m.Handle("/debug/metrics", metrics.Exporter())
m.Handle("/debug/pprof-set/block", handleFractionOpt("BlockProfileRate", runtime.SetBlockProfileRate))
m.Handle("/debug/pprof-set/mutex", handleFractionOpt("MutexProfileFraction", func(x int) {
runtime.SetMutexProfileFraction(x)
}))
2022-05-21 01:38:17 +00:00
m.Handle("/health/livez", NewLiveHandler(a))
m.Handle("/health/readyz", NewReadyHandler(a))
m.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
2020-02-23 22:54:49 +00:00
return m, nil
}
// MinerHandler returns a miner handler, to be mounted as-is on the server.
func MinerHandler(a api.StorageMiner, permissioned bool) (http.Handler, error) {
mapi := proxy.MetricedStorMinerAPI(a)
if permissioned {
mapi = api.PermissionedStorMinerAPI(mapi)
}
2021-06-14 09:27:54 +00:00
readerHandler, readerServerOpt := rpcenc.ReaderParamDecoder()
2022-07-18 16:36:51 +00:00
rpcServer := jsonrpc.NewServer(jsonrpc.WithServerErrors(api.RPCErrors), readerServerOpt)
rpcServer.Register("Filecoin", mapi)
2022-05-27 11:23:32 +00:00
rpcServer.AliasMethod("rpc.discover", "Filecoin.Discover")
2019-09-17 14:23:08 +00:00
rootMux := mux.NewRouter()
// remote storage
2024-02-02 20:00:28 +00:00
if _, realImpl := a.(*impl.StorageMinerAPI); realImpl {
m := mux.NewRouter()
m.PathPrefix("/remote").HandlerFunc(a.(*impl.StorageMinerAPI).ServeRemote(permissioned))
var hnd http.Handler = m
if permissioned {
hnd = &auth.Handler{
Verify: a.StorageAuthVerify,
Next: m.ServeHTTP,
}
}
rootMux.PathPrefix("/remote").Handler(hnd)
}
// local APIs
{
m := mux.NewRouter()
m.Handle("/rpc/v0", rpcServer)
m.Handle("/rpc/streams/v0/push/{uuid}", readerHandler)
// debugging
m.Handle("/debug/metrics", metrics.Exporter())
m.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
var hnd http.Handler = m
if permissioned {
hnd = &auth.Handler{
Verify: a.AuthVerify,
Next: m.ServeHTTP,
}
}
rootMux.PathPrefix("/").Handler(hnd)
}
return rootMux, nil
2019-07-18 23:18:26 +00:00
}
2019-10-23 09:18:22 +00:00
func handleFractionOpt(name string, setter func(int)) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(rw, "only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
asfr := r.Form.Get("x")
if len(asfr) == 0 {
http.Error(rw, "parameter 'x' must be set", http.StatusBadRequest)
return
}
fr, err := strconv.Atoi(asfr)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
rpclog.Infof("setting %s to %d", name, fr)
setter(fr)
}
}