2020-07-16 14:45:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-08-27 00:46:43 +00:00
|
|
|
"fmt"
|
2020-07-22 19:54:52 +00:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2020-07-16 14:45:31 +00:00
|
|
|
"os"
|
2020-08-27 00:46:43 +00:00
|
|
|
"strings"
|
2020-07-16 14:45:31 +00:00
|
|
|
|
2021-04-06 11:36:16 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
|
|
|
|
2020-07-16 15:48:15 +00:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
|
2020-08-27 00:46:43 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2020-07-16 14:45:31 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-08-27 00:46:43 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2020-07-16 14:45:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/processor"
|
2020-07-23 02:37:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/scheduler"
|
2020-07-16 14:45:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/syncer"
|
2020-08-27 00:46:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/util"
|
2020-07-16 14:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Start lotus chainwatch",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "max-batch",
|
2020-08-23 17:01:10 +00:00
|
|
|
Value: 50,
|
2020-07-16 14:45:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-07-22 19:54:52 +00:00
|
|
|
go func() {
|
2020-08-20 04:49:10 +00:00
|
|
|
http.ListenAndServe(":6060", nil) //nolint:errcheck
|
2020-07-22 19:54:52 +00:00
|
|
|
}()
|
2020-07-16 14:45:31 +00:00
|
|
|
ll := cctx.String("log-level")
|
|
|
|
if err := logging.SetLogLevel("*", ll); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-17 02:57:51 +00:00
|
|
|
if err := logging.SetLogLevel("rpc", "error"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-16 14:45:31 +00:00
|
|
|
|
2021-04-05 18:12:47 +00:00
|
|
|
var api v0api.FullNode
|
2020-08-27 00:46:43 +00:00
|
|
|
var closer jsonrpc.ClientCloser
|
|
|
|
var err error
|
|
|
|
if tokenMaddr := cctx.String("api"); tokenMaddr != "" {
|
|
|
|
toks := strings.Split(tokenMaddr, ":")
|
|
|
|
if len(toks) != 2 {
|
|
|
|
return fmt.Errorf("invalid api tokens, expected <token>:<maddr>, got: %s", tokenMaddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
api, closer, err = util.GetFullNodeAPIUsingCredentials(cctx.Context, toks[1], toks[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
api, closer, err = lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-16 14:45:31 +00:00
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
|
|
|
|
v, err := api.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Remote version: %s", v.Version)
|
|
|
|
|
|
|
|
maxBatch := cctx.Int("max-batch")
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", cctx.String("db"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Errorw("Failed to close database", "error", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
return xerrors.Errorf("Database failed to respond to ping (is it online?): %w", err)
|
|
|
|
}
|
|
|
|
db.SetMaxOpenConns(1350)
|
|
|
|
|
2020-08-27 18:45:42 +00:00
|
|
|
sync := syncer.NewSyncer(db, api, 1400)
|
2020-07-16 14:45:31 +00:00
|
|
|
sync.Start(ctx)
|
|
|
|
|
2020-07-30 17:31:21 +00:00
|
|
|
proc := processor.NewProcessor(ctx, db, api, maxBatch)
|
2020-07-16 14:45:31 +00:00
|
|
|
proc.Start(ctx)
|
|
|
|
|
2020-07-23 02:37:34 +00:00
|
|
|
sched := scheduler.PrepareScheduler(db)
|
|
|
|
sched.Start(ctx)
|
|
|
|
|
2020-07-16 14:45:31 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|