From 49da42983af7a775695166689e5bf701bcec4f81 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Wed, 19 Oct 2016 13:04:55 +0200 Subject: p2p/discv5: added new topic discovery package --- les/backend.go | 2 +- les/handler.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- les/helper_test.go | 2 +- les/server.go | 5 +++-- 4 files changed, 53 insertions(+), 5 deletions(-) (limited to 'les') diff --git a/les/backend.go b/les/backend.go index 8011a4b31..33c48e92a 100644 --- a/les/backend.go +++ b/les/backend.go @@ -196,7 +196,7 @@ func (s *LightEthereum) Protocols() []p2p.Protocol { // Ethereum protocol implementation. func (s *LightEthereum) Start(srvr *p2p.Server) error { s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId) - s.protocolManager.Start() + s.protocolManager.Start(srvr) return nil } diff --git a/les/handler.go b/les/handler.go index ef18af4d8..d2ed48870 100644 --- a/les/handler.go +++ b/les/handler.go @@ -23,6 +23,7 @@ import ( "fmt" "math/big" "sync" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" @@ -36,6 +37,7 @@ import ( "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" + "github.com/ethereum/go-ethereum/p2p/discv5" "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -99,6 +101,10 @@ type ProtocolManager struct { odr *LesOdr server *LesServer + topicDisc *discv5.Network + lesTopic discv5.Topic + p2pServer *p2p.Server + downloader *downloader.Downloader fetcher *lightFetcher peers *peerSet @@ -229,11 +235,52 @@ func (pm *ProtocolManager) removePeer(id string) { } } -func (pm *ProtocolManager) Start() { +func (pm *ProtocolManager) findServers() { + if pm.p2pServer == nil { + return + } + enodes := make(chan string, 100) + stop := make(chan struct{}) + go pm.topicDisc.SearchTopic(pm.lesTopic, stop, enodes) + go func() { + added := make(map[string]bool) + for { + select { + case enode := <-enodes: + if !added[enode] { + fmt.Println("Found LES server:", enode) + added[enode] = true + if node, err := discover.ParseNode(enode); err == nil { + pm.p2pServer.AddPeer(node) + } + } + case <-stop: + return + } + } + }() + time.Sleep(time.Second * 20) + close(stop) +} + +func (pm *ProtocolManager) Start(srvr *p2p.Server) { + pm.p2pServer = srvr + if srvr != nil { + pm.topicDisc = srvr.DiscV5 + } + pm.lesTopic = discv5.Topic("LES@" + common.Bytes2Hex(pm.blockchain.Genesis().Hash().Bytes()[0:8])) if pm.lightSync { // start sync handler + go pm.findServers() go pm.syncer() } else { + if pm.topicDisc != nil { + go func() { + fmt.Println("Starting topic register") + pm.topicDisc.RegisterTopic(pm.lesTopic, pm.quitSync) + fmt.Println("Stopped topic register") + }() + } go func() { for range pm.newPeerCh { } diff --git a/les/helper_test.go b/les/helper_test.go index 9817e5004..0b7576f6a 100644 --- a/les/helper_test.go +++ b/les/helper_test.go @@ -148,7 +148,7 @@ func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *cor srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000) srv.fcCostStats = newCostStats(nil) } - pm.Start() + pm.Start(nil) return pm, db, odr, nil } diff --git a/les/server.go b/les/server.go index bc5dd0837..874586a7e 100644 --- a/les/server.go +++ b/les/server.go @@ -66,8 +66,9 @@ func (s *LesServer) Protocols() []p2p.Protocol { return s.protocolManager.SubProtocols } -func (s *LesServer) Start() { - s.protocolManager.Start() +func (s *LesServer) Start(srvr *p2p.Server) { + s.protocolManager.Start(srvr) + } func (s *LesServer) Stop() { -- cgit