-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
main.go
215 lines (184 loc) · 5.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"context"
"fmt"
"net"
"os"
logging "github.com/ipfs/go-log/v2"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/urfave/cli/v2"
"go.opencensus.io/stats/view"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/lotus/gateway"
"github.com/filecoin-project/lotus/lib/lotuslog"
"github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/lotus/node"
)
var log = logging.Logger("gateway")
func main() {
lotuslog.SetupLogLevels()
local := []*cli.Command{
runCmd,
checkCmd,
}
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 {
log.Errorf("%+v", err)
os.Exit(1)
return
}
}
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
},
}
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",
Value: "0.0.0.0:2346",
},
&cli.IntFlag{
Name: "api-max-req-size",
Usage: "maximum API request size accepted by the JSON RPC server",
},
&cli.DurationFlag{
Name: "api-max-lookback",
Usage: "maximum duration allowable for tipset lookbacks",
Value: gateway.DefaultLookbackCap,
},
&cli.Int64Flag{
Name: "api-wait-lookback-limit",
Usage: "maximum number of blocks to search back through for message inclusion",
Value: int64(gateway.DefaultStateWaitLookbackLimit),
},
&cli.Int64Flag{
Name: "rate-limit",
Usage: "rate-limit API calls. Use 0 to disable",
Value: 0,
},
&cli.Int64Flag{
Name: "per-conn-rate-limit",
Usage: "rate-limit API calls per each connection. 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,
},
&cli.Int64Flag{
Name: "conn-per-minute",
Usage: "The number of incomming connections to accept from a single IP per minute. Use 0 to disable",
Value: 0,
},
},
Action: func(cctx *cli.Context) error {
log.Info("Starting lotus gateway")
// Register all metric views
if err := view.Register(
metrics.GatewayNodeViews...,
); err != nil {
log.Fatalf("Cannot register the view: %v", err)
}
api, closer, err := lcli.GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
var (
lookbackCap = cctx.Duration("api-max-lookback")
address = cctx.String("listen")
waitLookback = abi.ChainEpoch(cctx.Int64("api-wait-lookback-limit"))
rateLimit = cctx.Int64("rate-limit")
perConnRateLimit = cctx.Int64("per-conn-rate-limit")
rateLimitTimeout = cctx.Duration("rate-limit-timeout")
connPerMinute = cctx.Int64("conn-per-minute")
)
serverOptions := make([]jsonrpc.ServerOption, 0)
if maxRequestSize := cctx.Int("api-max-req-size"); maxRequestSize != 0 {
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(int64(maxRequestSize)))
}
log.Info("setting up API endpoint at " + address)
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return xerrors.Errorf("failed to resolve endpoint address: %w", err)
}
maddr, err := manet.FromNetAddr(addr)
if err != nil {
return xerrors.Errorf("failed to convert endpoint address to multiaddr: %w", err)
}
gwapi := gateway.NewNode(api, lookbackCap, waitLookback, rateLimit, rateLimitTimeout)
h, err := gateway.Handler(gwapi, api, perConnRateLimit, connPerMinute, serverOptions...)
if err != nil {
return xerrors.Errorf("failed to set up gateway HTTP handler")
}
stopFunc, err := node.ServeRPC(h, "lotus-gateway", maddr)
if err != nil {
return xerrors.Errorf("failed to serve rpc endpoint: %w", err)
}
<-node.MonitorShutdown(nil, node.ShutdownHandler{
Component: "rpc",
StopFunc: stopFunc,
})
return nil
},
}