This commit is contained in:
zsfelfoldi 2016-06-23 16:10:29 +02:00
parent 3566a7be6a
commit 0eef519481
4 changed files with 42 additions and 0 deletions

View file

@ -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
}
}
}()
}

View file

@ -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)

View file

@ -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.

View file

@ -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