Skip to content

Commit

Permalink
les: protocol versions and topics for clients and servers
Browse files Browse the repository at this point in the history
  • Loading branch information
zsfelfoldi committed Aug 19, 2017
1 parent 46f1b87 commit 1f5da7d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (s *Service) login(conn *websocket.Conn) error {
protocol = fmt.Sprintf("eth/%d", eth.ProtocolVersions[0])
} else {
network = fmt.Sprintf("%d", infos.Protocols["les"].(*eth.EthNodeInfo).Network)
protocol = fmt.Sprintf("les/%d", les.ProtocolVersions[0])
protocol = fmt.Sprintf("les/%d", les.ClientProtocolVersions[0])
}
auth := &authMsg{
Id: s.node,
Expand Down
4 changes: 4 additions & 0 deletions les/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
}

func lesTopic(genesisHash common.Hash) discv5.Topic {
return discv5.Topic("LES2@" + common.Bytes2Hex(genesisHash.Bytes()[0:8]))
}

func lesV1Topic(genesisHash common.Hash) discv5.Topic {
return discv5.Topic("LES@" + common.Bytes2Hex(genesisHash.Bytes()[0:8]))
}

Expand Down
13 changes: 10 additions & 3 deletions les/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,22 @@ func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, network
manager.retriever = odr.retriever
manager.reqDist = odr.retriever.dist
}
var protocolVersions []uint
if lightSync {
protocolVersions = ClientProtocolVersions
} else {
protocolVersions = ServerProtocolVersions
}

// Initiate a sub-protocol for every implemented version we can handle
manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions))
for i, version := range ProtocolVersions {
manager.SubProtocols = make([]p2p.Protocol, 0, len(protocolVersions))
for _, version := range protocolVersions {
// Compatible, initialize the sub-protocol
version := version // Closure for the run
manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{
Name: "les",
Version: version,
Length: ProtocolLengths[i],
Length: ProtocolLengths[version],
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
var entry *poolEntry
peer := manager.newPeer(int(version), networkId, p, rw)
Expand Down
7 changes: 5 additions & 2 deletions les/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ const (
)

// Supported versions of the les protocol (first is primary).
var ProtocolVersions = []uint{lpv1, lpv2}
var (
ClientProtocolVersions = []uint{lpv2}
ServerProtocolVersions = []uint{lpv1, lpv2}
)

// Number of implemented message corresponding to different protocol versions.
var ProtocolLengths = []uint64{15, 19}
var ProtocolLengths = map[uint]uint64{lpv1: 15, lpv2: 19}

const (
NetworkId = 1
Expand Down
21 changes: 12 additions & 9 deletions les/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type LesServer struct {
fcManager *flowcontrol.ClientManager // nil if our node is client only
fcCostStats *requestCostStats
defParams *flowcontrol.ServerParams
lesTopic discv5.Topic
lesTopics []discv5.Topic
quitSync chan struct{}

chtIndexer, bltIndexer *core.ChainIndexer
Expand All @@ -57,7 +57,7 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
srv := &LesServer{
protocolManager: pm,
quitSync: quitSync,
lesTopic: lesTopic(eth.BlockChain().Genesis().Hash()),
lesTopics: []discv5.Topic{lesV1Topic(eth.BlockChain().Genesis().Hash()), lesTopic(eth.BlockChain().Genesis().Hash())},
chtIndexer: light.NewChtIndexer(eth.ChainDb(), false),
bltIndexer: light.NewBloomTrieIndexer(eth.ChainDb(), false),
}
Expand All @@ -80,13 +80,16 @@ func (s *LesServer) Protocols() []p2p.Protocol {
// Start starts the LES server
func (s *LesServer) Start(srvr *p2p.Server) {
s.protocolManager.Start()
go func() {
logger := log.New("topic", s.lesTopic)
logger.Info("Starting topic registration")
defer logger.Info("Terminated topic registration")

srvr.DiscV5.RegisterTopic(s.lesTopic, s.quitSync)
}()
for _, topic := range s.lesTopics {
topic := topic
go func() {
logger := log.New("topic", topic)
logger.Info("Starting topic registration")
defer logger.Info("Terminated topic registration")

srvr.DiscV5.RegisterTopic(topic, s.quitSync)
}()
}
}

func (s *LesServer) SetBloomBitsIndexer(bbIndexer *core.ChainIndexer) {
Expand Down

0 comments on commit 1f5da7d

Please sign in to comment.