fountain: Create miner enpoint

This commit is contained in:
Łukasz Magiera 2019-10-17 10:12:53 +02:00
parent d753c39133
commit 43e1752816
3 changed files with 133 additions and 4 deletions

View File

@ -9,11 +9,13 @@ import (
rice "github.com/GeertJohan/go.rice" rice "github.com/GeertJohan/go.rice"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
peer "github.com/libp2p/go-libp2p-peer"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "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/address"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" 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) { func (h *handler) mkminer(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400) // General limiter owner allow throttling all messages that can make it into the mpool
// todo 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)
} }

View File

@ -1,4 +1,3 @@
package main package main
import ( import (

View File

@ -14,7 +14,9 @@
<form action='/send' method='get'> <form action='/send' method='get'>
<span>Enter destination address:</span> <span>Enter destination address:</span>
<input type='text' name='address' style="width: 300px"> <input type='text' name='address' style="width: 300px">
<button type='submit'>Send</button> <button type='submit'>Send Funds</button>
<button type='submit'>Create storage miner</button>
</form> </form>
<span>When creating storage miner, DO NOT REFRESH THE PAGE, wait for it to load. This can take more than 5min.</span>
</body> </body>
</html> </html>