2019-07-03 17:39:07 +00:00
|
|
|
package hello
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-05 14:46:21 +00:00
|
|
|
"fmt"
|
2019-07-03 17:39:07 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-08 15:14:36 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-07-03 17:39:07 +00:00
|
|
|
inet "github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain"
|
|
|
|
"github.com/filecoin-project/go-lotus/lib/cborrpc"
|
2019-07-03 17:39:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const ProtocolID = "/fil/hello/1.0.0"
|
2019-07-03 17:41:54 +00:00
|
|
|
|
2019-07-03 17:39:07 +00:00
|
|
|
var log = logging.Logger("hello")
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cbor.RegisterCborType(Message{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
HeaviestTipSet []cid.Cid
|
|
|
|
HeaviestTipSetWeight uint64
|
|
|
|
GenesisHash cid.Cid
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2019-07-05 14:46:21 +00:00
|
|
|
newStream chain.NewStreamFunc
|
2019-07-03 17:39:07 +00:00
|
|
|
|
2019-07-05 14:46:21 +00:00
|
|
|
cs *chain.ChainStore
|
|
|
|
syncer *chain.Syncer
|
2019-07-03 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 13:36:43 +00:00
|
|
|
func NewHelloService(h host.Host, cs *chain.ChainStore, syncer *chain.Syncer) *Service {
|
2019-07-03 17:39:07 +00:00
|
|
|
return &Service{
|
|
|
|
newStream: h.NewStream,
|
2019-07-08 13:36:43 +00:00
|
|
|
|
|
|
|
cs: cs,
|
|
|
|
syncer: syncer,
|
2019-07-03 17:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *Service) HandleStream(s inet.Stream) {
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
var hmsg Message
|
2019-07-08 12:46:30 +00:00
|
|
|
if err := cborrpc.ReadCborRPC(s, &hmsg); err != nil {
|
2019-07-04 14:47:46 +00:00
|
|
|
log.Infow("failed to read hello message", "error", err)
|
2019-07-03 17:39:07 +00:00
|
|
|
return
|
|
|
|
}
|
2019-07-04 14:47:46 +00:00
|
|
|
log.Debugw("genesis from hello",
|
|
|
|
"tipset", hmsg.HeaviestTipSet,
|
|
|
|
"peer", s.Conn().RemotePeer(),
|
|
|
|
"hash", hmsg.GenesisHash)
|
2019-07-03 17:39:07 +00:00
|
|
|
|
2019-07-05 14:46:21 +00:00
|
|
|
if hmsg.GenesisHash != hs.syncer.Genesis.Cids()[0] {
|
2019-07-03 17:39:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:46:21 +00:00
|
|
|
hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts)
|
2019-07-03 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error {
|
2019-07-05 14:46:21 +00:00
|
|
|
s, err := hs.newStream(ctx, pid, ProtocolID)
|
2019-07-03 17:39:07 +00:00
|
|
|
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())
|
|
|
|
|
2019-07-08 12:46:30 +00:00
|
|
|
if err := cborrpc.WriteCborRPC(s, hmsg); err != nil {
|
2019-07-03 17:39:07 +00:00
|
|
|
return err
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
2019-07-03 17:39:07 +00:00
|
|
|
|
|
|
|
return nil
|
2019-07-03 17:41:54 +00:00
|
|
|
}
|