Implement initial mining logic

This commit is contained in:
whyrusleeping
2019-07-10 19:36:43 -07:00
parent e09a379c3b
commit d381025ccc
14 changed files with 203 additions and 166 deletions
+40 -4
View File
@@ -6,8 +6,9 @@ import (
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/build"
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/miner"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
@@ -22,6 +23,10 @@ type API struct {
}
func (a *API) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {
if err := a.Chain.AddBlock(blk.Header); err != nil {
return err
}
b, err := blk.Serialize()
if err != nil {
return err
@@ -31,8 +36,13 @@ func (a *API) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {
return a.PubSub.Publish("/fil/blocks", b)
}
func (a *API) ChainHead(context.Context) ([]cid.Cid, error) {
return a.Chain.GetHeaviestTipSet().Cids(), nil
func (a *API) ChainHead(context.Context) (*chain.TipSet, error) {
return a.Chain.GetHeaviestTipSet(), nil
}
func (a *API) ChainGetRandomness(ctx context.Context, pts *chain.TipSet) ([]byte, error) {
// TODO: this needs to look back in the chain for the right random beacon value
return []byte("foo bar random"), nil
}
func (a *API) ID(context.Context) (peer.ID, error) {
@@ -45,10 +55,36 @@ func (a *API) Version(context.Context) (api.Version, error) {
}, nil
}
func (a *API) MpoolPending(context.Context) ([]*chain.SignedMessage, error) {
func (a *API) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*chain.SignedMessage, error) {
// TODO: need to make sure we don't return messages that were already included in the referenced chain
// also need to accept ts == nil just fine, assume nil == chain.Head()
return a.Mpool.Pending(), nil
}
func (a *API) MinerStart(ctx context.Context, addr address.Address) error {
// hrm...
m := miner.NewMiner(a, addr)
go m.Mine(context.TODO())
return nil
}
func (a *API) MinerCreateBlock(ctx context.Context, addr address.Address, parents *chain.TipSet, tickets []chain.Ticket, proof chain.ElectionProof, msgs []*chain.SignedMessage) (*chain.BlockMsg, error) {
fblk, err := chain.MinerCreateBlock(a.Chain, addr, parents, tickets, proof, msgs)
if err != nil {
return nil, err
}
var out chain.BlockMsg
out.Header = fblk.Header
for _, msg := range fblk.Messages {
out.Messages = append(out.Messages, msg.Cid())
}
return &out, nil
}
func (a *API) NetPeers(context.Context) ([]peer.AddrInfo, error) {
conns := a.Host.Network().Conns()
out := make([]peer.AddrInfo, len(conns))
+3 -2
View File
@@ -80,8 +80,9 @@ type Settings struct {
// type, and must be applied in correct order
invokes []fx.Option
Online bool // Online option applied
Config bool // Config option applied
Online bool // Online option applied
Bootstrap bool // Start chain syncer bootstrapped
Config bool // Config option applied
}
// Override option changes constructor for a given type