diff --git a/les/handler.go b/les/handler.go index c612fd22ee..7aacdaa5e6 100644 --- a/les/handler.go +++ b/les/handler.go @@ -353,6 +353,13 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { return errResp(ErrExtraStatusMsg, "uncontrolled status message") // Block header query, collect the requested headers and reply + case NewBlockHashesMsg: + var req newBlockHashesData + if err := msg.Decode(&req); err != nil { + return errResp(ErrDecode, "%v: %v", msg, err) + } +fmt.Println("RECEIVED", req[0].Number, req[0].Hash, req[0].Td) + case GetBlockHeadersMsg: glog.V(logger.Debug).Infof("LES: received GetBlockHeadersMsg from peer %v", p.id) // Decode the complex header query @@ -734,3 +741,29 @@ func (self *ProtocolManager) NodeInfo() *eth.EthNodeInfo { Head: self.blockchain.LastBlockHash(), } } + +func (pm *ProtocolManager) broadcastBlockLoop() { + sub := pm.eventMux.Subscribe( core.ChainEvent{}) + go func() { + for { + select { + case ev := <-sub.Chan(): + peers := pm.peers.AllPeers() + if len(peers) > 0 { + header := ev.Data.(core.ChainEvent).Block.Header() + hash := header.Hash() + number := header.Number.Uint64() + td := core.GetTd(pm.chainDb, hash, number) +fmt.Println("BROADCAST", number, hash, td) + announce := newBlockHashesData{{Hash: hash, Number: number, Td: td}} + for _, p := range peers { + go p.SendNewBlockHashes(announce) + } + } + case <-pm.quitSync: + sub.Unsubscribe() + return + } + } + }() +} \ No newline at end of file diff --git a/les/peer.go b/les/peer.go index 2c356cdf99..ea6f64a364 100644 --- a/les/peer.go +++ b/les/peer.go @@ -124,6 +124,12 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 { return p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount) } +// SendNewBlockHashes announces the availability of a number of blocks through +// a hash notification. +func (p *peer) SendNewBlockHashes(request newBlockHashesData) error { + return p2p.Send(p.rw, NewBlockHashesMsg, request) +} + // SendBlockHeaders sends a batch of block headers to the remote peer. func (p *peer) SendBlockHeaders(reqID, bv uint64, headers []*types.Header) error { return sendResponse(p.rw, BlockHeadersMsg, reqID, bv, headers) diff --git a/les/protocol.go b/les/protocol.go index dd63bd3902..ca4dd51b29 100644 --- a/les/protocol.go +++ b/les/protocol.go @@ -127,6 +127,7 @@ type statusData struct { type newBlockHashesData []struct { Hash common.Hash // Hash of one particular block being announced Number uint64 // Number of one particular block being announced + Td *big.Int // Total difficulty of one particular block being announced } // getBlockHashesData is the network packet for the hash based hash retrieval. diff --git a/les/server.go b/les/server.go index 53989269cb..95769657e5 100644 --- a/les/server.go +++ b/les/server.go @@ -39,6 +39,8 @@ func NewLesServer(eth *eth.FullNodeService, config *eth.Config) (*LesServer, err if err != nil { return nil, err } + pm.broadcastBlockLoop() + srv := &LesServer{protocolManager: pm} pm.server = srv