2021-05-21 20:34:17 +00:00
|
|
|
package node
|
2019-07-18 23:18:26 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-17 14:23:08 +00:00
|
|
|
"context"
|
2019-10-23 09:18:22 +00:00
|
|
|
"encoding/json"
|
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"
|
2020-10-02 16:42:44 +00:00
|
|
|
"runtime"
|
2021-05-21 20:34:17 +00:00
|
|
|
"strconv"
|
2019-07-18 23:18:26 +00:00
|
|
|
|
2021-05-23 13:48:42 +00:00
|
|
|
"github.com/gorilla/mux"
|
2019-10-22 17:18:06 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-10-22 17:18:06 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-08-25 23:46:31 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2020-10-21 08:37:50 +00:00
|
|
|
"go.opencensus.io/tag"
|
2019-10-22 17:18:06 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-02-23 22:35:29 +00:00
|
|
|
|
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"
|
2021-10-01 12:37:27 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics/proxy"
|
2020-05-20 17:43:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/impl"
|
2021-11-11 16:17:11 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/impl/client"
|
2019-07-18 23:18:26 +00:00
|
|
|
)
|
|
|
|
|
2021-05-21 20:34:17 +00:00
|
|
|
var rpclog = logging.Logger("rpc")
|
2019-09-17 14:23:08 +00:00
|
|
|
|
2021-05-23 13:48:42 +00:00
|
|
|
// ServeRPC serves an HTTP handler over the supplied listen multiaddr.
|
2021-05-23 11:40:33 +00:00
|
|
|
//
|
2021-05-23 13:48:42 +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.
|
|
|
|
//
|
2021-05-23 13:48:42 +00:00
|
|
|
// 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{
|
|
|
|
Handler: h,
|
|
|
|
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
|
|
|
}
|
2021-05-23 13:48:42 +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.
|
2021-06-09 22:42:24 +00:00
|
|
|
func FullNodeHandler(a v1api.FullNode, permissioned bool, opts ...jsonrpc.ServerOption) (http.Handler, error) {
|
2021-05-23 13:48:42 +00:00
|
|
|
m := mux.NewRouter()
|
|
|
|
|
2021-03-23 15:40:22 +00:00
|
|
|
serveRpc := func(path string, hnd interface{}) {
|
2022-07-18 16:36:51 +00:00
|
|
|
rpcServer := jsonrpc.NewServer(append(opts, 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
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
// TODO: use reflect to automatically register all the eth aliases
|
|
|
|
rpcServer.AliasMethod("eth_accounts", "Filecoin.EthAccounts")
|
|
|
|
rpcServer.AliasMethod("eth_blockNumber", "Filecoin.EthBlockNumber")
|
|
|
|
rpcServer.AliasMethod("eth_getBlockTransactionCountByNumber", "Filecoin.EthGetBlockTransactionCountByNumber")
|
|
|
|
rpcServer.AliasMethod("eth_getBlockTransactionCountByHash", "Filecoin.EthGetBlockTransactionCountByHash")
|
|
|
|
|
|
|
|
rpcServer.AliasMethod("eth_getBlockByHash", "Filecoin.EthGetBlockByHash")
|
|
|
|
rpcServer.AliasMethod("eth_getBlockByNumber", "Filecoin.EthGetBlockByNumber")
|
|
|
|
rpcServer.AliasMethod("eth_getTransactionByHash", "Filecoin.EthGetTransactionByHash")
|
|
|
|
rpcServer.AliasMethod("eth_getTransactionCount", "Filecoin.EthGetTransactionCount")
|
|
|
|
rpcServer.AliasMethod("eth_getTransactionReceipt", "Filecoin.EthGetTransactionReceipt")
|
|
|
|
rpcServer.AliasMethod("eth_getTransactionByBlockHashAndIndex", "Filecoin.EthGetTransactionByBlockHashAndIndex")
|
|
|
|
rpcServer.AliasMethod("eth_getTransactionByBlockNumberAndIndex", "Filecoin.EthGetTransactionByBlockNumberAndIndex")
|
|
|
|
|
|
|
|
rpcServer.AliasMethod("eth_getCode", "Filecoin.EthGetCode")
|
|
|
|
rpcServer.AliasMethod("eth_getStorageAt", "Filecoin.EthGetStorageAt")
|
|
|
|
rpcServer.AliasMethod("eth_getBalance", "Filecoin.EthGetBalance")
|
|
|
|
rpcServer.AliasMethod("eth_chainId", "Filecoin.EthChainId")
|
2022-10-22 15:31:58 +00:00
|
|
|
rpcServer.AliasMethod("eth_feeHistory", "Filecoin.EthFeeHistory")
|
2022-09-09 17:59:00 +00:00
|
|
|
rpcServer.AliasMethod("eth_protocolVersion", "Filecoin.EthProtocolVersion")
|
|
|
|
rpcServer.AliasMethod("eth_maxPriorityFeePerGas", "Filecoin.EthMaxPriorityFeePerGas")
|
|
|
|
rpcServer.AliasMethod("eth_gasPrice", "Filecoin.EthGasPrice")
|
|
|
|
rpcServer.AliasMethod("eth_sendRawTransaction", "Filecoin.EthSendRawTransaction")
|
2022-09-12 21:46:15 +00:00
|
|
|
rpcServer.AliasMethod("eth_estimateGas", "Filecoin.EthEstimateGas")
|
|
|
|
rpcServer.AliasMethod("eth_call", "Filecoin.EthCall")
|
2022-09-09 17:59:00 +00:00
|
|
|
|
|
|
|
rpcServer.AliasMethod("net_version", "Filecoin.NetVersion")
|
|
|
|
rpcServer.AliasMethod("net_listening", "Filecoin.NetListening")
|
|
|
|
|
2021-06-09 22:42:24 +00:00
|
|
|
var handler http.Handler = rpcServer
|
|
|
|
if permissioned {
|
|
|
|
handler = &auth.Handler{Verify: a.AuthVerify, Next: rpcServer.ServeHTTP}
|
2021-03-23 15:40:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 22:42:24 +00:00
|
|
|
m.Handle(path, handler)
|
2019-07-24 13:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 12:37:27 +00:00
|
|
|
fnapi := proxy.MetricedFullAPI(a)
|
2021-06-09 22:42:24 +00:00
|
|
|
if permissioned {
|
|
|
|
fnapi = api.PermissionedFullAPI(fnapi)
|
|
|
|
}
|
2021-03-23 15:40:22 +00:00
|
|
|
|
2021-06-09 22:42:24 +00:00
|
|
|
serveRpc("/rpc/v1", fnapi)
|
|
|
|
serveRpc("/rpc/v0", &v0api.WrapperV1Full{FullNode: fnapi})
|
2019-09-17 14:23:08 +00:00
|
|
|
|
2021-06-09 22:42:24 +00:00
|
|
|
// Import handler
|
|
|
|
handleImportFunc := handleImport(a.(*impl.FullNodeAPI))
|
2021-11-11 16:17:11 +00:00
|
|
|
handleExportFunc := handleExport(a.(*impl.FullNodeAPI))
|
2021-06-09 22:42:24 +00:00
|
|
|
if permissioned {
|
|
|
|
importAH := &auth.Handler{
|
|
|
|
Verify: a.AuthVerify,
|
|
|
|
Next: handleImportFunc,
|
|
|
|
}
|
|
|
|
m.Handle("/rest/v0/import", importAH)
|
2021-11-11 16:17:11 +00:00
|
|
|
|
|
|
|
exportAH := &auth.Handler{
|
|
|
|
Verify: a.AuthVerify,
|
|
|
|
Next: handleExportFunc,
|
|
|
|
}
|
|
|
|
m.Handle("/rest/v0/export", exportAH)
|
2021-06-09 22:42:24 +00:00
|
|
|
} else {
|
|
|
|
m.HandleFunc("/rest/v0/import", handleImportFunc)
|
2021-11-11 16:17:11 +00:00
|
|
|
m.HandleFunc("/rest/v0/export", handleExportFunc)
|
2019-10-23 09:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 13:48:42 +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) {
|
2021-05-21 20:34:17 +00:00
|
|
|
runtime.SetMutexProfileFraction(x)
|
|
|
|
}))
|
2022-05-21 01:38:17 +00:00
|
|
|
m.Handle("/health/livez", NewLiveHandler(a))
|
|
|
|
m.Handle("/health/readyz", NewReadyHandler(a))
|
2021-05-23 13:48:42 +00:00
|
|
|
m.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
|
2020-02-23 22:54:49 +00:00
|
|
|
|
2021-05-23 13:48:42 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
2019-10-22 17:18:06 +00:00
|
|
|
|
2021-05-23 13:48:42 +00:00
|
|
|
// MinerHandler returns a miner handler, to be mounted as-is on the server.
|
2021-06-09 22:42:24 +00:00
|
|
|
func MinerHandler(a api.StorageMiner, permissioned bool) (http.Handler, error) {
|
2021-10-01 12:37:27 +00:00
|
|
|
mapi := proxy.MetricedStorMinerAPI(a)
|
2021-06-09 22:42:24 +00:00
|
|
|
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)
|
2021-06-09 22:42:24 +00:00
|
|
|
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
|
|
|
|
2022-07-06 20:15:16 +00:00
|
|
|
rootMux := mux.NewRouter()
|
2021-05-23 13:48:42 +00:00
|
|
|
|
2022-07-06 20:15:16 +00:00
|
|
|
// remote storage
|
|
|
|
{
|
|
|
|
m := mux.NewRouter()
|
|
|
|
m.PathPrefix("/remote").HandlerFunc(a.(*impl.StorageMinerAPI).ServeRemote(permissioned))
|
2021-05-23 13:48:42 +00:00
|
|
|
|
2022-07-06 20:15:16 +00:00
|
|
|
var hnd http.Handler = m
|
|
|
|
if permissioned {
|
|
|
|
hnd = &auth.Handler{
|
|
|
|
Verify: a.StorageAuthVerify,
|
|
|
|
Next: m.ServeHTTP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rootMux.PathPrefix("/remote").Handler(hnd)
|
2021-06-09 22:42:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 20:15:16 +00:00
|
|
|
// 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)
|
2021-05-23 13:48:42 +00:00
|
|
|
}
|
2022-07-06 20:15:16 +00:00
|
|
|
|
|
|
|
return rootMux, nil
|
2019-07-18 23:18:26 +00:00
|
|
|
}
|
2019-10-23 09:18:22 +00:00
|
|
|
|
|
|
|
func handleImport(a *impl.FullNodeAPI) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "PUT" {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
2021-03-25 14:09:50 +00:00
|
|
|
if !auth.HasPerm(r.Context(), nil, api.PermWrite) {
|
2019-10-23 09:18:22 +00:00
|
|
|
w.WriteHeader(401)
|
2020-05-27 20:53:20 +00:00
|
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
|
2019-10-23 09:18:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := a.ClientImportLocal(r.Context(), r.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
2020-05-27 20:53:20 +00:00
|
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{err.Error()})
|
2019-10-23 09:18:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
|
|
|
err = json.NewEncoder(w).Encode(struct{ Cid cid.Cid }{c})
|
|
|
|
if err != nil {
|
2021-05-23 13:48:42 +00:00
|
|
|
rpclog.Errorf("/rest/v0/import: Writing response failed: %+v", err)
|
2019-10-23 09:18:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 20:34:17 +00:00
|
|
|
|
2021-11-11 16:17:11 +00:00
|
|
|
func handleExport(a *impl.FullNodeAPI) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "GET" {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !auth.HasPerm(r.Context(), nil, api.PermWrite) {
|
|
|
|
w.WriteHeader(401)
|
|
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var eref api.ExportRef
|
|
|
|
if err := json.Unmarshal([]byte(r.FormValue("export")), &eref); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
car := r.FormValue("car") == "true"
|
|
|
|
|
|
|
|
err := a.ClientExportInto(r.Context(), eref, car, client.ExportDest{Writer: w})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 20:34:17 +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
|
|
|
|
}
|
2021-05-23 13:48:42 +00:00
|
|
|
rpclog.Infof("setting %s to %d", name, fr)
|
2021-05-21 20:34:17 +00:00
|
|
|
setter(fr)
|
|
|
|
}
|
|
|
|
}
|