2019-09-20 21:27:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-10-17 04:11:47 +00:00
|
|
|
"time"
|
2019-09-20 21:27:40 +00:00
|
|
|
|
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"
|
2019-10-17 08:12:53 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2019-09-20 21:27:40 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-17 08:12:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2019-10-18 04:47:41 +00:00
|
|
|
"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-17 04:11:47 +00:00
|
|
|
limiter: NewLimiter(LimiterConfig{
|
|
|
|
TotalRate: time.Second,
|
|
|
|
TotalBurst: 20,
|
2019-10-17 14:28:03 +00:00
|
|
|
IPRate: time.Minute,
|
2019-10-17 04:11:47 +00:00
|
|
|
IPBurst: 5,
|
2019-10-17 14:28:03 +00:00
|
|
|
WalletRate: 15 * time.Minute,
|
2019-10-17 04:11:47 +00:00
|
|
|
WalletBurst: 1,
|
|
|
|
}),
|
|
|
|
colLimiter: NewLimiter(LimiterConfig{
|
|
|
|
TotalRate: time.Second,
|
|
|
|
TotalBurst: 20,
|
2019-10-17 14:28:03 +00:00
|
|
|
IPRate: 10 * time.Minute,
|
2019-10-17 04:11:47 +00:00
|
|
|
IPBurst: 1,
|
2019-10-17 14:28:03 +00:00
|
|
|
WalletRate: 1 * time.Hour,
|
2019-10-17 04:11:47 +00:00
|
|
|
WalletBurst: 1,
|
|
|
|
}),
|
2019-09-20 21:27:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
2019-10-17 04:11:47 +00:00
|
|
|
|
|
|
|
limiter *Limiter
|
|
|
|
colLimiter *Limiter
|
2019-09-20 21:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) send(w http.ResponseWriter, r *http.Request) {
|
2019-10-17 04:11:47 +00:00
|
|
|
// General limiter to allow throttling all messages that can make it into the mpool
|
|
|
|
if !h.limiter.Allow() {
|
|
|
|
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit based on IP
|
|
|
|
limiter := h.limiter.GetIPLimiter(r.RemoteAddr)
|
|
|
|
if !limiter.Allow() {
|
|
|
|
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:27:40 +00:00
|
|
|
to, err := address.NewFromString(r.FormValue("address"))
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-17 04:11:47 +00:00
|
|
|
// Limit based on wallet address
|
|
|
|
limiter = h.limiter.GetWalletLimiter(to.String())
|
|
|
|
if !limiter.Allow() {
|
|
|
|
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:27:40 +00:00
|
|
|
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) {
|
2019-10-17 08:12:53 +00:00
|
|
|
// General limiter owner allow throttling all messages that can make it into the mpool
|
|
|
|
if !h.colLimiter.Allow() {
|
|
|
|
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit based on IP
|
|
|
|
limiter := h.colLimiter.GetIPLimiter(r.RemoteAddr)
|
|
|
|
if !limiter.Allow() {
|
|
|
|
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, err := address.NewFromString(r.FormValue("address"))
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if owner.Protocol() != address.BLS {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte("Miner address must use BLS"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:28:03 +00:00
|
|
|
log.Infof("mkactor on %s", owner)
|
2019-10-17 08:12:53 +00:00
|
|
|
|
|
|
|
// Limit based on wallet address
|
|
|
|
limiter = h.colLimiter.GetWalletLimiter(owner.String())
|
|
|
|
if !limiter.Allow() {
|
|
|
|
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
collateral, err := h.api.StatePledgeCollateral(r.Context(), nil)
|
|
|
|
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: owner,
|
|
|
|
|
|
|
|
GasPrice: types.NewInt(0),
|
|
|
|
GasLimit: types.NewInt(1000),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte("pushfunds: " + err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("push funds to %s: %s", owner, smsg.Cid())
|
|
|
|
|
|
|
|
mw, err := h.api.StateWaitMsg(r.Context(), smsg.Cid())
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if mw.Receipt.ExitCode != 0 {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(xerrors.Errorf("create storage miner failed: exit code %d", mw.Receipt.ExitCode).Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("sendto %s ok", owner)
|
|
|
|
|
|
|
|
params, err := actors.SerializeParams(&actors.CreateStorageMinerParams{
|
|
|
|
Owner: owner,
|
|
|
|
Worker: owner,
|
|
|
|
SectorSize: build.SectorSizes[0], // TODO: dropdown allowing selection
|
|
|
|
PeerID: peer.ID("SETME"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
createStorageMinerMsg := &types.Message{
|
|
|
|
To: actors.StorageMarketAddress,
|
|
|
|
From: h.from,
|
|
|
|
Value: collateral,
|
|
|
|
|
|
|
|
Method: actors.SPAMethods.CreateStorageMiner,
|
|
|
|
Params: params,
|
|
|
|
|
|
|
|
GasLimit: types.NewInt(10000000),
|
|
|
|
GasPrice: types.NewInt(0),
|
|
|
|
}
|
|
|
|
|
|
|
|
signed, err := h.api.MpoolPushMessage(r.Context(), createStorageMinerMsg)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("smc %s", owner)
|
|
|
|
|
|
|
|
mw, err = h.api.StateWaitMsg(r.Context(), signed.Cid())
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if mw.Receipt.ExitCode != 0 {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(xerrors.Errorf("create storage miner failed: exit code %d", mw.Receipt.ExitCode).Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := address.NewFromBytes(mw.Receipt.Return)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
w.WriteHeader(200)
|
|
|
|
fmt.Fprintf(w, "New storage miners address is: %s\n", addr)
|
2019-10-17 14:28:03 +00:00
|
|
|
fmt.Fprintf(w, "Run lotus-storage-miner init --actor=%s --owner=%s", addr, owner)
|
2019-10-13 07:33:25 +00:00
|
|
|
}
|