Skip to content

Commit

Permalink
Merge pull request #11 from filecoin-project/feat/some-chain-cli
Browse files Browse the repository at this point in the history
Some chain CLI & API
  • Loading branch information
magik6k authored Jul 9, 2019
2 parents 0e42705 + cb35547 commit fb62c6d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
8 changes: 6 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions api/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"

ma "github.com/multiformats/go-multiaddr"
)

Expand Down
34 changes: 34 additions & 0 deletions cli/chain.go
Original file line number Diff line number Diff line change
@@ -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
},
}
1 change: 1 addition & 0 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func reqContext(cctx *cli.Context) context.Context {
}

var Commands = []*cli.Command{
chainCmd,
netCmd,
versionCmd,
}
21 changes: 20 additions & 1 deletion node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fb62c6d

Please sign in to comment.