2019-11-15 16:38:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-14 20:04:27 +00:00
|
|
|
"database/sql"
|
2020-02-21 18:49:21 +00:00
|
|
|
_ "net/http/pprof"
|
2019-11-19 12:57:16 +00:00
|
|
|
"os"
|
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
_ "github.com/lib/pq"
|
2020-06-05 22:59:01 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-19 12:57:16 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2020-07-14 20:04:27 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/processor"
|
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/syncer"
|
2019-11-15 16:38:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("chainwatch")
|
|
|
|
|
|
|
|
func main() {
|
2020-06-02 14:29:39 +00:00
|
|
|
_ = logging.SetLogLevel("*", "INFO")
|
2020-06-30 17:26:41 +00:00
|
|
|
if err := logging.SetLogLevel("rpc", "error"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-15 16:38:56 +00:00
|
|
|
|
|
|
|
log.Info("Starting chainwatch")
|
|
|
|
|
|
|
|
local := []*cli.Command{
|
2019-11-18 20:11:43 +00:00
|
|
|
dotCmd,
|
2020-07-14 20:04:27 +00:00
|
|
|
runCmd,
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "lotus-chainwatch",
|
|
|
|
Usage: "Devnet token distribution utility",
|
2020-06-01 18:43:51 +00:00
|
|
|
Version: build.UserVersion(),
|
2019-11-15 16:38:56 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
|
|
|
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
|
|
|
|
},
|
2019-12-05 11:58:19 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "db",
|
|
|
|
EnvVars: []string{"LOTUS_DB"},
|
2019-12-10 23:42:36 +00:00
|
|
|
Value: "",
|
2019-12-05 11:58:19 +00:00
|
|
|
},
|
2019-11-15 16:38:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Commands: local,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
2019-11-16 19:51:54 +00:00
|
|
|
log.Warnf("%+v", err)
|
2020-07-10 15:58:41 +00:00
|
|
|
os.Exit(1)
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Start lotus chainwatch",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "front",
|
|
|
|
Value: "127.0.0.1:8418",
|
|
|
|
},
|
2020-02-21 18:49:49 +00:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "max-batch",
|
|
|
|
Value: 1000,
|
|
|
|
},
|
2019-11-15 16:38:56 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
|
|
|
|
v, err := api.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-09 18:18:30 +00:00
|
|
|
log.Infof("Remote version: %s", v.Version)
|
|
|
|
|
2020-02-21 18:49:49 +00:00
|
|
|
maxBatch := cctx.Int("max-batch")
|
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
db, err := sql.Open("postgres", cctx.String("db"))
|
2019-11-15 16:38:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-14 20:04:27 +00:00
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Errorw("Failed to close database", "error", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-11-15 16:38:56 +00:00
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
return xerrors.Errorf("Database failed to respond to ping (is it online?): %w", err)
|
2019-11-16 19:47:06 +00:00
|
|
|
}
|
2020-07-14 20:04:27 +00:00
|
|
|
db.SetMaxOpenConns(1350)
|
2019-11-16 19:47:06 +00:00
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
sync := syncer.NewSyncer(db, api)
|
|
|
|
sync.Start(ctx)
|
2019-11-16 19:47:06 +00:00
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
proc := processor.NewProcessor(db, api, maxBatch)
|
|
|
|
proc.Start(ctx)
|
2019-11-15 16:38:56 +00:00
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
2019-11-15 16:38:56 +00:00
|
|
|
},
|
|
|
|
}
|