Merge pull request #533 from nspcc-dev/inventory-handling

network: rework inventory handling, check for item presence
This commit is contained in:
Roman Khimov 2019-12-02 12:30:53 +03:00 committed by GitHub
commit b989b167f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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