lotus/node/api.go

80 lines
1.7 KiB
Go
Raw Normal View History

2019-07-08 19:07:16 +00:00
package node
import (
"context"
2019-07-08 19:07:16 +00:00
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/build"
2019-07-09 15:19:27 +00:00
"github.com/filecoin-project/go-lotus/chain"
2019-07-08 19:07:16 +00:00
2019-07-09 15:19:27 +00:00
"github.com/ipfs/go-cid"
2019-07-08 19:07:16 +00:00
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
2019-07-09 15:19:27 +00:00
pubsub "github.com/libp2p/go-libp2p-pubsub"
2019-07-08 21:01:15 +00:00
ma "github.com/multiformats/go-multiaddr"
2019-07-08 19:07:16 +00:00
)
2019-07-09 10:58:13 +00:00
type API struct {
2019-07-09 15:19:27 +00:00
Host host.Host
Chain *chain.ChainStore
PubSub *pubsub.PubSub
2019-07-09 22:58:51 +00:00
Mpool *chain.MessagePool
2019-07-09 15:19:27 +00:00
}
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
2019-07-08 19:07:16 +00:00
}
2019-07-09 10:58:13 +00:00
func (a *API) ID(context.Context) (peer.ID, error) {
return a.Host.ID(), nil
2019-07-08 19:07:16 +00:00
}
2019-07-09 10:58:13 +00:00
func (a *API) Version(context.Context) (api.Version, error) {
return api.Version{
Version: build.Version,
}, nil
2019-07-08 19:07:16 +00:00
}
2019-07-09 22:58:51 +00:00
func (a *API) MpoolPending(context.Context) ([]*chain.SignedMessage, error) {
return a.Mpool.Pending(), nil
}
2019-07-09 10:58:13 +00:00
func (a *API) NetPeers(context.Context) ([]peer.AddrInfo, error) {
conns := a.Host.Network().Conns()
out := make([]peer.AddrInfo, len(conns))
2019-07-08 19:07:16 +00:00
2019-07-09 10:58:13 +00:00
for i, conn := range conns {
out[i] = peer.AddrInfo{
ID: conn.RemotePeer(),
Addrs: []ma.Multiaddr{
conn.RemoteMultiaddr(),
},
2019-07-08 19:07:16 +00:00
}
}
2019-07-09 10:58:13 +00:00
return out, nil
2019-07-08 19:07:16 +00:00
}
2019-07-09 10:58:13 +00:00
func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error {
return a.Host.Connect(ctx, p)
2019-07-08 19:07:16 +00:00
}
2019-07-08 21:01:15 +00:00
2019-07-09 17:03:36 +00:00
func (a *API) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
return peer.AddrInfo{
ID: a.Host.ID(),
Addrs: a.Host.Addrs(),
}, nil
2019-07-08 21:01:15 +00:00
}
2019-07-09 10:58:13 +00:00
var _ api.API = &API{}