aboutsummaryrefslogtreecommitdiffstats
path: root/les/handler.go
diff options
context:
space:
mode:
authorZsolt Felfoldi <zsfelfoldi@gmail.com>2016-10-19 19:04:55 +0800
committerFelix Lange <fjl@twurst.com>2016-11-09 09:12:53 +0800
commit49da42983af7a775695166689e5bf701bcec4f81 (patch)
tree2c325a4852695697cc9616a3eac99a432711abdb /les/handler.go
parent7db7109a5b53c339f00e9c05ac826b3dbd1f98e1 (diff)
downloaddexon-49da42983af7a775695166689e5bf701bcec4f81.tar
dexon-49da42983af7a775695166689e5bf701bcec4f81.tar.gz
dexon-49da42983af7a775695166689e5bf701bcec4f81.tar.bz2
dexon-49da42983af7a775695166689e5bf701bcec4f81.tar.lz
dexon-49da42983af7a775695166689e5bf701bcec4f81.tar.xz
dexon-49da42983af7a775695166689e5bf701bcec4f81.tar.zst
dexon-49da42983af7a775695166689e5bf701bcec4f81.zip
p2p/discv5: added new topic discovery package
Diffstat (limited to 'les/handler.go')
-rw-r--r--les/handler.go49
1 files changed, 48 insertions, 1 deletions
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 {
}