2019-11-15 16:38:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-19 12:57:16 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2019-11-15 16:38:56 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-11-16 19:51:54 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-15 16:38:56 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
2019-11-19 12:57:16 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2019-11-15 16:38:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("chainwatch")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logging.SetLogLevel("*", "INFO")
|
|
|
|
|
|
|
|
log.Info("Starting chainwatch")
|
|
|
|
|
|
|
|
local := []*cli.Command{
|
|
|
|
runCmd,
|
2019-11-18 20:11:43 +00:00
|
|
|
dotCmd,
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "lotus-chainwatch",
|
|
|
|
Usage: "Devnet token distribution utility",
|
2019-12-11 22:00:39 +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"},
|
|
|
|
Value: "./chainwatch.db",
|
|
|
|
},
|
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)
|
2019-11-15 16:38:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Start lotus chainwatch",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "front",
|
|
|
|
Value: "127.0.0.1:8418",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Remote version: %s", v.Version)
|
|
|
|
|
2019-12-05 11:58:19 +00:00
|
|
|
st, err := openStorage(cctx.String("db"))
|
2019-11-15 16:38:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer st.close()
|
|
|
|
|
|
|
|
runSyncer(ctx, api, st)
|
2019-11-17 12:01:10 +00:00
|
|
|
go subMpool(ctx, api, st)
|
2019-11-19 12:57:16 +00:00
|
|
|
go subBlocks(ctx, api, st)
|
2019-11-15 16:38:56 +00:00
|
|
|
|
2019-11-16 19:47:06 +00:00
|
|
|
h, err := newHandler(api, st)
|
|
|
|
if err != nil {
|
2019-11-16 19:51:54 +00:00
|
|
|
return xerrors.Errorf("handler setup: %w", err)
|
2019-11-16 19:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http.Handle("/", h)
|
|
|
|
|
|
|
|
fmt.Printf("Open http://%s\n", cctx.String("front"))
|
|
|
|
|
2019-11-15 16:38:56 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return http.ListenAndServe(cctx.String("front"), nil)
|
|
|
|
},
|
|
|
|
}
|