lotus/cmd/lotus-fountain/main.go

144 lines
2.7 KiB
Go
Raw Normal View History

2019-09-20 21:27:40 +00:00
package main
import (
"context"
"fmt"
"net/http"
"os"
2019-10-13 07:33:25 +00:00
rice "github.com/GeertJohan/go.rice"
2019-09-20 21:27:40 +00:00
logging "github.com/ipfs/go-log"
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/address"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
2019-09-20 21:27:40 +00:00
)
var log = logging.Logger("main")
2019-09-25 18:40:10 +00:00
var sendPerRequest = types.NewInt(500_000_000)
2019-09-20 21:27:40 +00:00
func main() {
logging.SetLogLevel("*", "INFO")
log.Info("Starting fountain")
local := []*cli.Command{
runCmd,
}
app := &cli.App{
Name: "lotus-fountain",
Usage: "Devnet token distribution utility",
Version: build.Version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
EnvVars: []string{"LOTUS_PATH"},
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
},
},
Commands: local,
}
if err := app.Run(os.Args); err != nil {
log.Warn(err)
return
}
}
var runCmd = &cli.Command{
Name: "run",
Usage: "Start lotus fountain",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "front",
Value: "127.0.0.1:7777",
},
&cli.StringFlag{
Name: "from",
},
},
Action: func(cctx *cli.Context) error {
2019-10-04 22:43:04 +00:00
nodeApi, closer, err := lcli.GetFullNodeAPI(cctx)
2019-09-20 21:27:40 +00:00
if err != nil {
return err
}
2019-10-04 16:02:25 +00:00
defer closer()
2019-09-20 21:27:40 +00:00
ctx := lcli.ReqContext(cctx)
v, err := nodeApi.Version(ctx)
if err != nil {
return err
}
log.Info("Remote version: %s", v.Version)
from, err := address.NewFromString(cctx.String("from"))
if err != nil {
return xerrors.Errorf("parsing source address (provide correct --from flag!): %w", err)
}
h := &handler{
ctx: ctx,
api: nodeApi,
from: from,
}
2019-10-13 07:33:25 +00:00
http.Handle("/", http.FileServer(rice.MustFindBox("site").HTTPBox()))
2019-09-20 21:27:40 +00:00
http.HandleFunc("/send", h.send)
2019-10-17 00:43:38 +00:00
http.HandleFunc("/mkminer", h.mkminer)
2019-09-20 21:27:40 +00:00
fmt.Printf("Open http://%s\n", cctx.String("front"))
go func() {
<-ctx.Done()
os.Exit(0)
}()
return http.ListenAndServe(cctx.String("front"), nil)
},
}
type handler struct {
ctx context.Context
api api.FullNode
from address.Address
}
func (h *handler) send(w http.ResponseWriter, r *http.Request) {
to, err := address.NewFromString(r.FormValue("address"))
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
smsg, err := h.api.MpoolPushMessage(h.ctx, &types.Message{
Value: sendPerRequest,
From: h.from,
To: to,
GasPrice: types.NewInt(0),
GasLimit: types.NewInt(1000),
})
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(smsg.Cid().String()))
}
2019-10-13 07:33:25 +00:00
2019-10-17 00:43:38 +00:00
func (h *handler) mkminer(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
// todo
2019-10-13 07:33:25 +00:00
}