eth: clean the block request packet handling a bit

This commit is contained in:
Péter Szilágyi 2015-06-18 16:09:34 +03:00
parent 5ec6ecc511
commit 629705ad53

@ -252,35 +252,33 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
} }
case GetBlocksMsg: case GetBlocksMsg:
var blocks []*types.Block // Decode the retrieval message
msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size)) msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
if _, err := msgStream.List(); err != nil { if _, err := msgStream.List(); err != nil {
return err return err
} }
// Gather blocks until the fetch or network limits is reached
var ( var (
i int hash common.Hash
totalsize common.StorageSize bytes common.StorageSize
blocks []*types.Block
) )
for { for {
i++
var hash common.Hash
err := msgStream.Decode(&hash) err := msgStream.Decode(&hash)
if err == rlp.EOL { if err == rlp.EOL {
break break
} else if err != nil { } else if err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err) return errResp(ErrDecode, "msg %v: %v", msg, err)
} }
// Retrieve the requested block, stopping if enough was found
block := pm.chainman.GetBlock(hash) if block := pm.chainman.GetBlock(hash); block != nil {
if block != nil {
blocks = append(blocks, block) blocks = append(blocks, block)
totalsize += block.Size() bytes += block.Size()
} if len(blocks) >= downloader.MaxBlockFetch || bytes > maxBlockRespSize {
if i == downloader.MaxBlockFetch || totalsize > maxBlockRespSize {
break break
} }
} }
}
return p.sendBlocks(blocks) return p.sendBlocks(blocks)
case BlocksMsg: case BlocksMsg:
@ -360,8 +358,10 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
// BroadcastBlock will propagate the block to a subset of its connected peers, // BroadcastBlock will propagate the block to a subset of its connected peers,
// only notifying the rest of the block's appearance. // only notifying the rest of the block's appearance.
func (pm *ProtocolManager) BroadcastBlock(block *types.Block) { func (pm *ProtocolManager) BroadcastBlock(block *types.Block) {
hash := block.Hash()
// Retrieve all the target peers and split between full broadcast or only notification // Retrieve all the target peers and split between full broadcast or only notification
peers := pm.peers.PeersWithoutBlock(block.Hash()) peers := pm.peers.PeersWithoutBlock(hash)
split := int(math.Sqrt(float64(len(peers)))) split := int(math.Sqrt(float64(len(peers))))
transfer := peers[:split] transfer := peers[:split]
@ -369,14 +369,14 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block) {
// Send out the data transfers and the notifications // Send out the data transfers and the notifications
for _, peer := range notify { for _, peer := range notify {
peer.sendNewBlockHashes([]common.Hash{block.Hash()}) peer.sendNewBlockHashes([]common.Hash{hash})
} }
glog.V(logger.Detail).Infoln("broadcast hash to", len(notify), "peers.") glog.V(logger.Detail).Infof("broadcast hash %x to %d peers.", hash[:4], len(notify))
for _, peer := range transfer { for _, peer := range transfer {
peer.sendNewBlock(block) peer.sendNewBlock(block)
} }
glog.V(logger.Detail).Infoln("broadcast block to", len(transfer), "peers. Total processing time:", time.Since(block.ReceivedAt)) glog.V(logger.Detail).Infof("broadcast block %x to %d peers. Total processing time: %v", hash[:4], len(transfer), time.Since(block.ReceivedAt))
} }
// BroadcastTx will propagate the block to its connected peers. It will sort // BroadcastTx will propagate the block to its connected peers. It will sort