diff --git a/cmd/lotus-fountain/main.go b/cmd/lotus-fountain/main.go index eccc4ac5c..bee3e8b71 100644 --- a/cmd/lotus-fountain/main.go +++ b/cmd/lotus-fountain/main.go @@ -9,11 +9,13 @@ import ( rice "github.com/GeertJohan/go.rice" logging "github.com/ipfs/go-log" + peer "github.com/libp2p/go-libp2p-peer" "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/actors" "github.com/filecoin-project/lotus/chain/address" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" @@ -178,6 +180,132 @@ func (h *handler) send(w http.ResponseWriter, r *http.Request) { } func (h *handler) mkminer(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(400) - // todo + // 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 + } + + log.Infof("mkactoer on %s", owner) + + // 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) + fmt.Fprintf(w, "Run lotus-storage-miner init --actor=%s -owner=%s", addr, owner) } diff --git a/cmd/lotus-fountain/rate_limiter.go b/cmd/lotus-fountain/rate_limiter.go index 5058049fc..eb7215780 100644 --- a/cmd/lotus-fountain/rate_limiter.go +++ b/cmd/lotus-fountain/rate_limiter.go @@ -1,4 +1,3 @@ - package main import ( diff --git a/cmd/lotus-fountain/site/index.html b/cmd/lotus-fountain/site/index.html index 0a3d3577b..632470b7f 100644 --- a/cmd/lotus-fountain/site/index.html +++ b/cmd/lotus-fountain/site/index.html @@ -14,7 +14,9 @@
Enter destination address: - + +
+When creating storage miner, DO NOT REFRESH THE PAGE, wait for it to load. This can take more than 5min.