diff --git a/api/api.go b/api/api.go index 31c32ffb1..25bd5c9d6 100644 --- a/api/api.go +++ b/api/api.go @@ -3,6 +3,9 @@ package api import ( "context" + "github.com/filecoin-project/go-lotus/chain" + + "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" ) @@ -17,7 +20,8 @@ type Version struct { type API interface { // chain - // // head + ChainHead(context.Context) ([]cid.Cid, error) + ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error // TODO: check serialization // messages @@ -34,7 +38,7 @@ type API interface { // network - NetPeers(context.Context) ([]peer.AddrInfo, error) // TODO: check serialization + NetPeers(context.Context) ([]peer.AddrInfo, error) NetConnect(context.Context, peer.AddrInfo) error NetAddrsListen(context.Context) (MultiaddrSlice, error) // // ping diff --git a/api/struct.go b/api/struct.go index bde66014c..400d55ab3 100644 --- a/api/struct.go +++ b/api/struct.go @@ -3,6 +3,9 @@ package api import ( "context" + "github.com/filecoin-project/go-lotus/chain" + + "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" ) @@ -12,6 +15,9 @@ type Struct struct { ID func(context.Context) (peer.ID, error) Version func(context.Context) (Version, error) + ChainSubmitBlock func(ctx context.Context, blk *chain.BlockMsg) error + ChainHead func(context.Context) ([]cid.Cid, error) + NetPeers func(context.Context) ([]peer.AddrInfo, error) NetConnect func(context.Context, peer.AddrInfo) error NetAddrsListen func(context.Context) (MultiaddrSlice, error) @@ -30,6 +36,14 @@ func (c *Struct) NetAddrsListen(ctx context.Context) (MultiaddrSlice, error) { return c.Internal.NetAddrsListen(ctx) } +func (c *Struct) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error { + return c.Internal.ChainSubmitBlock(ctx, blk) +} + +func (c *Struct) ChainHead(ctx context.Context) ([]cid.Cid, error) { + return c.Internal.ChainHead(ctx) +} + // ID implements API.ID func (c *Struct) ID(ctx context.Context) (peer.ID, error) { return c.Internal.ID(ctx) diff --git a/api/types.go b/api/types.go index 3faf6f109..449a3ae79 100644 --- a/api/types.go +++ b/api/types.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + ma "github.com/multiformats/go-multiaddr" ) diff --git a/cli/chain.go b/cli/chain.go new file mode 100644 index 000000000..1e23a0c3e --- /dev/null +++ b/cli/chain.go @@ -0,0 +1,34 @@ +package cli + +import ( + "fmt" + + "gopkg.in/urfave/cli.v2" +) + +var chainCmd = &cli.Command{ + Name: "chain", + Usage: "Interact with filecoin blockchain", + Subcommands: []*cli.Command{ + chainHeadCmd, + }, +} + +var chainHeadCmd = &cli.Command{ + Name: "head", + Usage: "Print chain head", + Action: func(cctx *cli.Context) error { + api := getApi(cctx) + ctx := reqContext(cctx) + + head, err := api.ChainHead(ctx) + if err != nil { + return err + } + + for _, c := range head { + fmt.Println(c) + } + return nil + }, +} diff --git a/cli/cmd.go b/cli/cmd.go index d7972e021..e76669a4f 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -43,6 +43,7 @@ func reqContext(cctx *cli.Context) context.Context { } var Commands = []*cli.Command{ + chainCmd, netCmd, versionCmd, } diff --git a/node/api.go b/node/api.go index 8b5a56b9e..d86f17f07 100644 --- a/node/api.go +++ b/node/api.go @@ -5,14 +5,33 @@ import ( "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/build" + "github.com/filecoin-project/go-lotus/chain" + "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" ma "github.com/multiformats/go-multiaddr" ) type API struct { - Host host.Host + Host host.Host + Chain *chain.ChainStore + PubSub *pubsub.PubSub +} + +func (a *API) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error { + b, err := blk.Serialize() + if err != nil { + return err + } + + // TODO: anything else to do here? + 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) ID(context.Context) (peer.ID, error) {