2020-09-12 03:01:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-12 22:50:53 +00:00
|
|
|
"fmt"
|
2020-09-12 03:01:37 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.opencensus.io/stats/view"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
|
2020-09-12 03:01:37 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2021-05-13 18:00:42 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-10-21 08:37:50 +00:00
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
|
|
|
|
2021-06-07 22:17:44 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2021-05-12 22:50:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2020-09-12 03:01:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2021-05-12 22:50:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-12 03:01:37 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2021-06-02 09:58:33 +00:00
|
|
|
cliutil "github.com/filecoin-project/lotus/cli/util"
|
2021-05-23 17:37:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/gateway"
|
2020-09-12 03:01:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/lotuslog"
|
2020-10-21 08:37:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
2021-05-23 17:37:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/node"
|
2020-09-12 03:01:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("gateway")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
lotuslog.SetupLogLevels()
|
|
|
|
|
|
|
|
local := []*cli.Command{
|
|
|
|
runCmd,
|
2021-05-12 22:50:53 +00:00
|
|
|
checkCmd,
|
2020-09-12 03:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "lotus-gateway",
|
|
|
|
Usage: "Public API server for lotus",
|
|
|
|
Version: build.UserVersion(),
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
|
|
|
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Commands: local,
|
|
|
|
}
|
|
|
|
app.Setup()
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
2021-05-12 22:50:53 +00:00
|
|
|
log.Errorf("%+v", err)
|
|
|
|
os.Exit(1)
|
2020-09-12 03:01:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 22:50:53 +00:00
|
|
|
var checkCmd = &cli.Command{
|
|
|
|
Name: "check",
|
|
|
|
Usage: "performs a simple check to verify that a connection can be made to a gateway",
|
|
|
|
ArgsUsage: "[apiInfo]",
|
|
|
|
Description: `Any valid value for FULLNODE_API_INFO is a valid argument to the check command.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
- ws://127.0.0.1:2346
|
|
|
|
- http://127.0.0.1:2346
|
|
|
|
- /ip4/127.0.0.1/tcp/2346`,
|
|
|
|
Flags: []cli.Flag{},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ainfo := cliutil.ParseApiInfo(cctx.Args().First())
|
|
|
|
|
|
|
|
darg, err := ainfo.DialArgs("v1")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
api, closer, err := client.NewFullNodeRPCV1(ctx, darg, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
addr, err := address.NewIDAddress(100)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
laddr, err := api.StateLookupID(ctx, addr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if laddr != addr {
|
|
|
|
return fmt.Errorf("looked up addresses does not match returned address, %s != %s", addr, laddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:01:37 +00:00
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Start api server",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "listen",
|
|
|
|
Usage: "host address and port the api server will listen on",
|
2020-09-15 09:45:03 +00:00
|
|
|
Value: "0.0.0.0:2346",
|
2020-09-12 03:01:37 +00:00
|
|
|
},
|
2020-11-17 13:39:53 +00:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "api-max-req-size",
|
|
|
|
Usage: "maximum API request size accepted by the JSON RPC server",
|
|
|
|
},
|
2021-05-13 18:00:42 +00:00
|
|
|
&cli.DurationFlag{
|
|
|
|
Name: "api-max-lookback",
|
|
|
|
Usage: "maximum duration allowable for tipset lookbacks",
|
2021-05-19 16:30:06 +00:00
|
|
|
Value: gateway.DefaultLookbackCap,
|
2021-05-13 18:00:42 +00:00
|
|
|
},
|
|
|
|
&cli.Int64Flag{
|
|
|
|
Name: "api-wait-lookback-limit",
|
|
|
|
Usage: "maximum number of blocks to search back through for message inclusion",
|
2021-05-19 16:30:06 +00:00
|
|
|
Value: int64(gateway.DefaultStateWaitLookbackLimit),
|
2021-05-13 18:00:42 +00:00
|
|
|
},
|
2022-04-20 01:35:19 +00:00
|
|
|
&cli.Int64Flag{
|
|
|
|
Name: "rate-limit",
|
|
|
|
Usage: "rate-limit API calls. Use 0 to disable",
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
&cli.DurationFlag{
|
|
|
|
Name: "rate-limit-timeout",
|
|
|
|
Usage: "the maximum time to wait for the rate limter before returning an error to clients",
|
|
|
|
Value: gateway.DefaultRateLimitTimeout,
|
|
|
|
},
|
2020-09-12 03:01:37 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-09-15 09:45:03 +00:00
|
|
|
log.Info("Starting lotus gateway")
|
2020-09-12 03:01:37 +00:00
|
|
|
|
2020-10-21 08:37:50 +00:00
|
|
|
// Register all metric views
|
|
|
|
if err := view.Register(
|
2022-04-21 02:48:41 +00:00
|
|
|
metrics.GatewayNodeViews...,
|
2020-10-21 08:37:50 +00:00
|
|
|
); err != nil {
|
|
|
|
log.Fatalf("Cannot register the view: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:47:10 +00:00
|
|
|
api, closer, err := lcli.GetFullNodeAPIV1(cctx)
|
2020-09-12 03:01:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
var (
|
2022-04-20 01:35:19 +00:00
|
|
|
lookbackCap = cctx.Duration("api-max-lookback")
|
|
|
|
address = cctx.String("listen")
|
|
|
|
waitLookback = abi.ChainEpoch(cctx.Int64("api-wait-lookback-limit"))
|
|
|
|
rateLimit = cctx.Int64("rate-limit")
|
|
|
|
rateLimitTimeout = cctx.Duration("rate-limit-timeout")
|
2021-05-23 17:37:53 +00:00
|
|
|
)
|
2020-09-12 03:01:37 +00:00
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
serverOptions := make([]jsonrpc.ServerOption, 0)
|
|
|
|
if maxRequestSize := cctx.Int("api-max-req-size"); maxRequestSize != 0 {
|
|
|
|
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(int64(maxRequestSize)))
|
2020-11-17 13:39:53 +00:00
|
|
|
}
|
2020-09-12 03:01:37 +00:00
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
log.Info("setting up API endpoint at " + address)
|
2021-02-01 21:13:20 +00:00
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
addr, err := net.ResolveTCPAddr("tcp", address)
|
2021-02-01 21:13:20 +00:00
|
|
|
if err != nil {
|
2021-05-23 17:37:53 +00:00
|
|
|
return xerrors.Errorf("failed to resolve endpoint address: %w", err)
|
2021-02-01 21:13:20 +00:00
|
|
|
}
|
2020-09-12 03:01:37 +00:00
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
maddr, err := manet.FromNetAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to convert endpoint address to multiaddr: %w", err)
|
2020-09-12 03:01:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 01:35:19 +00:00
|
|
|
gwapi := gateway.NewNode(api, lookbackCap, waitLookback, rateLimit, rateLimitTimeout)
|
2021-05-23 17:37:53 +00:00
|
|
|
h, err := gateway.Handler(gwapi, serverOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to set up gateway HTTP handler")
|
|
|
|
}
|
2020-09-12 03:01:37 +00:00
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
stopFunc, err := node.ServeRPC(h, "lotus-gateway", maddr)
|
2020-09-12 03:01:37 +00:00
|
|
|
if err != nil {
|
2021-05-23 17:37:53 +00:00
|
|
|
return xerrors.Errorf("failed to serve rpc endpoint: %w", err)
|
2020-09-12 03:01:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 17:37:53 +00:00
|
|
|
<-node.MonitorShutdown(nil, node.ShutdownHandler{
|
|
|
|
Component: "rpc",
|
|
|
|
StopFunc: stopFunc,
|
|
|
|
})
|
|
|
|
return nil
|
2020-09-12 03:01:37 +00:00
|
|
|
},
|
|
|
|
}
|