Some chain CLI & API

This commit is contained in:
Łukasz Magiera 2019-07-09 17:19:27 +02:00
parent 271c268e28
commit cb3554735a
6 changed files with 76 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -2,6 +2,7 @@ package api
import (
"encoding/json"
ma "github.com/multiformats/go-multiaddr"
)

34
cli/chain.go Normal file
View File

@ -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
},
}

View File

@ -43,6 +43,7 @@ func reqContext(cctx *cli.Context) context.Context {
}
var Commands = []*cli.Command{
chainCmd,
netCmd,
versionCmd,
}

View File

@ -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) {