Skip to content

Commit

Permalink
Pull block mining commands out of node api. (#2268)
Browse files Browse the repository at this point in the history
* Pull mining once out of node api and make it block.API
* Following Porcelain/Pluminb pattern, use an apiDeps struct. 
* Make apiDeps members private.
  • Loading branch information
shannonwells authored Mar 15, 2019
1 parent ca1113d commit 7a81c51
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 137 deletions.
1 change: 0 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type API interface {
Daemon() Daemon
Dag() Dag
Miner() Miner
Mining() Mining
Ping() Ping
RetrievalClient() RetrievalClient
Swarm() Swarm
Expand Down
6 changes: 0 additions & 6 deletions api/impl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type nodeAPI struct {
daemon *nodeDaemon
dag *nodeDag
miner *nodeMiner
mining *nodeMining
ping *nodePing
retrievalClient *nodeRetrievalClient
swarm *nodeSwarm
Expand All @@ -44,7 +43,6 @@ func New(node *node.Node) api.API {
api.daemon = newNodeDaemon(api)
api.dag = newNodeDag(api)
api.miner = newNodeMiner(api, porcelainAPI)
api.mining = newNodeMining(api)
api.ping = newNodePing(api)
api.retrievalClient = newNodeRetrievalClient(api, porcelainAPI)
api.swarm = newNodeSwarm(api)
Expand Down Expand Up @@ -76,10 +74,6 @@ func (api *nodeAPI) Miner() api.Miner {
return api.miner
}

func (api *nodeAPI) Mining() api.Mining {
return api.mining
}

func (api *nodeAPI) Ping() api.Ping {
return api.ping
}
Expand Down
101 changes: 0 additions & 101 deletions api/impl/mining.go

This file was deleted.

14 changes: 0 additions & 14 deletions api/mining.go

This file was deleted.

10 changes: 5 additions & 5 deletions commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ func getRepo(req *cmds.Request) (repo.Repo, error) {
return repo.OpenFSRepo(getRepoDir(req))
}

func runAPIAndWait(ctx context.Context, node *node.Node, config *config.Config, req *cmds.Request) error {
api := impl.New(node)

func runAPIAndWait(ctx context.Context, nd *node.Node, config *config.Config, req *cmds.Request) error {
api := impl.New(nd)
if err := api.Daemon().Start(ctx); err != nil {
return err
}
Expand All @@ -133,7 +132,8 @@ func runAPIAndWait(ctx context.Context, node *node.Node, config *config.Config,
// TODO: should this be the passed in context?
ctx: context.Background(),
api: api,
porcelainAPI: node.PorcelainAPI,
porcelainAPI: nd.PorcelainAPI,
blockpAPI: nd.BlockAPI,
}

cfg := cmdhttp.NewServerConfig()
Expand Down Expand Up @@ -176,7 +176,7 @@ func runAPIAndWait(ctx context.Context, node *node.Node, config *config.Config,

// write our api address to file
// TODO: use api.Repo() once implemented
if err := node.Repo.SetAPIAddr(config.API.Address); err != nil {
if err := nd.Repo.SetAPIAddr(config.API.Address); err != nil {
return errors.Wrap(err, "Could not save API address to repo")
}

Expand Down
8 changes: 8 additions & 0 deletions commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (

"github.com/filecoin-project/go-filecoin/api"
"github.com/filecoin-project/go-filecoin/porcelain"
bapi "github.com/filecoin-project/go-filecoin/protocol/block"
)

// Env is the environment passed to commands. Implements cmds.Environment.
type Env struct {
ctx context.Context
api api.API
porcelainAPI *porcelain.API
blockpAPI *bapi.API
}

var _ cmds.Environment = (*Env)(nil)
Expand All @@ -39,3 +41,9 @@ func GetPorcelainAPI(env cmds.Environment) *porcelain.API {
ce := env.(*Env)
return ce.porcelainAPI
}

// GetProtocolAPI returns the protocol api from the given environment
func GetProtocolAPI(env cmds.Environment) *bapi.API {
ce := env.(*Env)
return ce.blockpAPI
}
8 changes: 3 additions & 5 deletions commands/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var miningCmd = &cmds.Command{

var miningOnceCmd = &cmds.Command{
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
blk, err := GetAPI(env).Mining().Once(req.Context)
blk, err := GetProtocolAPI(env).MiningOnce(req.Context)
if err != nil {
return err
}
Expand All @@ -39,7 +39,7 @@ var miningOnceCmd = &cmds.Command{

var miningStartCmd = &cmds.Command{
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
if err := GetAPI(env).Mining().Start(req.Context); err != nil {
if err := GetProtocolAPI(env).MiningStart(req.Context); err != nil {
return err
}
return re.Emit("Started mining")
Expand All @@ -50,9 +50,7 @@ var miningStartCmd = &cmds.Command{

var miningStopCmd = &cmds.Command{
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
if err := GetAPI(env).Mining().Stop(req.Context); err != nil {
return err
}
GetProtocolAPI(env).MiningStop(req.Context)
return re.Emit("Stopped mining")
},
Encoders: stringEncoderMap,
Expand Down
30 changes: 26 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
circuit "gx/ipfs/QmNaXXRfJ93t4HicX8N2WZPhdE8KU39MPGALuH421GFgKA/go-libp2p-circuit"
"gx/ipfs/QmNf3wujpV2Y7Lnj2hy2UrmuX8bhMDStRHbnSLh7Ypf36h/go-hamt-ipld"
"gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
bstore "gx/ipfs/QmRu7tiRnFk9mMPpVECQTBQJqXtmG132jJxA1w9A7TtpBz/go-ipfs-blockstore"
"gx/ipfs/QmSz8kAe2JCKp2dWSG8gHSWnwSmne8YfRXTeK5HBmc9L7t/go-ipfs-exchange-offline"
libp2ppeer "gx/ipfs/QmTu65MVbemtUxJEWgsTtzv9Zv9P8rvmqNA4eG9TrTRGYc/go-libp2p-peer"
"gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
"gx/ipfs/QmVmDhyTTUcQXFD1rRQ64fGLMSAoaQvNH3hwuaCFAPq2hy/errors"
routing "gx/ipfs/QmWaDSNoSdSXU9b6udyaq9T8y6LkzMwqWxECznFqvtcTsk/go-libp2p-routing"
"gx/ipfs/QmWaDSNoSdSXU9b6udyaq9T8y6LkzMwqWxECznFqvtcTsk/go-libp2p-routing"
"gx/ipfs/QmXixGGfd98hN2dA5YiPHWANY3sjmHfZBQk3mLiQUo6NLJ/go-bitswap"
bsnet "gx/ipfs/QmXixGGfd98hN2dA5YiPHWANY3sjmHfZBQk3mLiQUo6NLJ/go-bitswap/network"
dhtprotocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
Expand All @@ -34,8 +34,8 @@ import (
offroute "gx/ipfs/QmcjqHcsk8E1Gd8RbuaUawWC7ogDtaVcdjLvZF8ysCCiPn/go-ipfs-routing/offline"
"gx/ipfs/Qmd52WKRSwrBK5gUaJKawryZQ5by6UbNB8KVW2Zy6JtbyW/go-libp2p-host"
libp2pps "gx/ipfs/QmepvmmYNM6q4RaUiwEikQFhgMFHXg2PLhx2E9iaRd3jmS/go-libp2p-pubsub"
dht "gx/ipfs/QmfM7kwroZsKhKFmnJagPvM28MZMyKxG3QV2AqfvZvEEqS/go-libp2p-kad-dht"
dhtopts "gx/ipfs/QmfM7kwroZsKhKFmnJagPvM28MZMyKxG3QV2AqfvZvEEqS/go-libp2p-kad-dht/opts"
"gx/ipfs/QmfM7kwroZsKhKFmnJagPvM28MZMyKxG3QV2AqfvZvEEqS/go-libp2p-kad-dht"
"gx/ipfs/QmfM7kwroZsKhKFmnJagPvM28MZMyKxG3QV2AqfvZvEEqS/go-libp2p-kad-dht/opts"

"github.com/filecoin-project/go-filecoin/abi"
"github.com/filecoin-project/go-filecoin/actor/builtin"
Expand All @@ -58,6 +58,7 @@ import (
"github.com/filecoin-project/go-filecoin/porcelain"
"github.com/filecoin-project/go-filecoin/proofs"
"github.com/filecoin-project/go-filecoin/proofs/sectorbuilder"
bapi "github.com/filecoin-project/go-filecoin/protocol/block"
"github.com/filecoin-project/go-filecoin/protocol/hello"
"github.com/filecoin-project/go-filecoin/protocol/retrieval"
"github.com/filecoin-project/go-filecoin/protocol/storage"
Expand Down Expand Up @@ -90,6 +91,7 @@ type Node struct {
Syncer chain.Syncer
PowerTable consensus.PowerTableView

BlockAPI *bapi.API
PorcelainAPI *porcelain.API

// HeavyTipSetCh is a subscription to the heaviest tipset topic on the chain.
Expand Down Expand Up @@ -436,6 +438,26 @@ func (nc *Config) Build(ctx context.Context) (*Node, error) {
Router: router,
}

blockTime, mineDelay := nd.MiningTimes()
blockAPI := bapi.New(
nd.AddNewBlock,
bs,
&cstOffline,
&cstOnline,
chainReader,
nodeConsensus,
blockTime,
mineDelay,
msgPool,
PorcelainAPI,
powerTable,
nd.StartMining,
nd.StopMining,
chainSyncer,
fcWallet)

nd.BlockAPI = &blockAPI

// Bootstrapping network peers.
periodStr := nd.Repo.Config().Bootstrap.Period
period, err := time.ParseDuration(periodStr)
Expand Down
Loading

0 comments on commit 7a81c51

Please sign in to comment.