hello: Move from f2

This commit is contained in:
Łukasz Magiera
2019-07-03 19:39:07 +02:00
parent 46a5019c8d
commit fdde4db217
13 changed files with 208 additions and 7 deletions
+11
View File
@@ -6,6 +6,7 @@ import (
"time"
"github.com/ipfs/go-datastore"
logging "github.com/ipfs/go-log"
ci "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
@@ -13,11 +14,14 @@ import (
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/build"
"github.com/filecoin-project/go-lotus/node/hello"
"github.com/filecoin-project/go-lotus/node/modules"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
"github.com/filecoin-project/go-lotus/node/modules/lp2p"
)
var log = logging.Logger("builder")
var defaultListenAddrs = []string{ // TODO: better defaults?
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
@@ -42,6 +46,7 @@ func New(ctx context.Context) (api.API, error) {
fx.Provide(
pstoremem.NewPeerstore,
// libp2p
lp2p.DefaultTransports,
lp2p.PNet,
lp2p.Host,
@@ -59,6 +64,10 @@ func New(ctx context.Context) (api.API, error) {
lp2p.NatPortMap,
lp2p.ConnectionManager(50, 200, 20*time.Second),
// filecoin protocols
hello.NewHelloService,
),
fx.Invoke(
@@ -69,6 +78,8 @@ func New(ctx context.Context) (api.API, error) {
fx.Invoke(versionAPI(&resAPI.Internal.Version)),
fx.Invoke(idAPI(&resAPI.Internal.ID)),
fx.Logger(&debugPrinter{log}),
)
if err := app.Start(ctx); err != nil {
+17
View File
@@ -0,0 +1,17 @@
package node
import (
logging "github.com/ipfs/go-log"
"go.uber.org/fx"
)
type debugPrinter struct {
l logging.StandardLogger
}
func (p *debugPrinter) Printf(f string, a ...interface{}) {
p.l.Debugf(f, a...)
}
var _ fx.Printer = new(debugPrinter)
+99
View File
@@ -0,0 +1,99 @@
package hello
import (
"bufio"
"context"
"github.com/filecoin-project/go-lotus/cborrpc"
"github.com/libp2p/go-libp2p-core/host"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
)
const ProtocolID = "/fil/hello/1.0.0"
var log = logging.Logger("hello")
func init() {
cbor.RegisterCborType(Message{})
}
type Message struct {
HeaviestTipSet []cid.Cid
HeaviestTipSetWeight uint64
GenesisHash cid.Cid
}
type NewStreamFunc func(context.Context, peer.ID, ...protocol.ID) (inet.Stream, error)
type Service struct {
newStream NewStreamFunc
//cs *ChainStore
//syncer *Syncer
}
func NewHelloService(h host.Host) *Service {
return &Service{
newStream: h.NewStream,
}
}
func (hs *Service) HandleStream(s inet.Stream) {
defer s.Close()
log.Debugw("Handling hello")
var hmsg Message
if err := cborrpc.ReadCborRPC(bufio.NewReader(s), &hmsg); err != nil {
log.Error("failed to read hello message: ", err)
return
}
log.Debugw("heaviest tipset", "tipset", hmsg.HeaviestTipSet)
log.Debugw("got genesis from hello", "hash", hmsg.GenesisHash)
/*if hmsg.GenesisHash != hs.syncer.genesis.Cids()[0] {
log.Error("other peer has different genesis!")
s.Conn().Close()
return
}
ts, err := hs.syncer.FetchTipSet(context.Background(), s.Conn().RemotePeer(), hmsg.HeaviestTipSet)
if err != nil {
log.Errorf("failed to fetch tipset from peer during hello: %s", err)
return
}
hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts)*/
}
func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error {
/*s, err := hs.newStream(ctx, pid, ProtocolID)
if err != nil {
return err
}
hts := hs.cs.GetHeaviestTipSet()
weight := hs.cs.Weight(hts)
gen, err := hs.cs.GetGenesis()
if err != nil {
return err
}
hmsg := &Message{
HeaviestTipSet: hts.Cids(),
HeaviestTipSetWeight: weight,
GenesisHash: gen.Cid(),
}
fmt.Println("SENDING HELLO MESSAGE: ", hts.Cids())
fmt.Println("hello message genesis: ", gen.Cid())
if err := WriteCborRPC(s, hmsg); err != nil {
return err
}*/
return nil
}
+3
View File
@@ -1,10 +1,13 @@
package modules
import (
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/peerstore"
record "github.com/libp2p/go-libp2p-record"
)
var log = logging.Logger("modules")
// RecordValidator provides namesys compatible routing record validator
func RecordValidator(ps peerstore.Peerstore) record.Validator {
return record.NamespacedValidator{
+25
View File
@@ -0,0 +1,25 @@
package modules
import (
"github.com/filecoin-project/go-lotus/node/hello"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"go.uber.org/fx"
)
func HandleHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello.Service) error {
h.SetStreamHandler(hello.ProtocolID, svc.HandleStream)
bundle := inet.NotifyBundle{
ConnectedF: func(_ inet.Network, c inet.Conn) {
go func() {
if err := svc.SayHello(helpers.LifecycleCtx(mctx, lc), c.RemotePeer()); err != nil {
log.Warnw("failed to say hello", "error", err)
return
}
}()
},
}
h.Network().Notify(&bundle)
}