network: rework inventory handling, check for item presence

Don't ask peers about the items we already have.
This commit is contained in:
Roman Khimov 2019-12-02 11:02:52 +03:00
parent bc5beb438f
commit 1da76a8eb4

View file

@ -387,8 +387,27 @@ func (s *Server) handleBlockCmd(p Peer, block *core.Block) error {
// handleInvCmd processes the received inventory.
func (s *Server) handleInvCmd(p Peer, inv *payload.Inventory) error {
payload := payload.NewInventory(inv.Type, inv.Hashes)
reqHashes := make([]util.Uint256, 0)
var typExists = map[payload.InventoryType]func(util.Uint256) bool{
payload.TXType: s.chain.HasTransaction,
payload.BlockType: s.chain.HasBlock,
payload.ConsensusType: func(h util.Uint256) bool {
cp := s.consensus.GetPayload(h)
return cp != nil
},
}
if exists := typExists[inv.Type]; exists != nil {
for _, hash := range inv.Hashes {
if !exists(hash) {
reqHashes = append(reqHashes, hash)
}
}
}
if len(reqHashes) > 0 {
payload := payload.NewInventory(inv.Type, reqHashes)
return p.WriteMsg(NewMessage(s.Net, CMDGetData, payload))
}
return nil
}
// handleInvCmd processes the received inventory.