From 1da76a8eb4db27dba31e2c1abca51b4163a02e8d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 2 Dec 2019 11:02:52 +0300 Subject: [PATCH] network: rework inventory handling, check for item presence Don't ask peers about the items we already have. --- pkg/network/server.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index 28e1c71a9..3c4012a76 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -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) - return p.WriteMsg(NewMessage(s.Net, CMDGetData, payload)) + 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.