network: implement getdata message handling in server

To properly implement relaying we need to answer these requests from other
nodes.
This commit is contained in:
Roman Khimov 2019-10-24 10:18:30 +03:00
parent 8d0ed9259c
commit 257ba1c2ad

View file

@ -286,6 +286,39 @@ func (s *Server) handleInvCmd(p Peer, inv *payload.Inventory) error {
return p.WriteMsg(NewMessage(s.Net, CMDGetData, payload))
}
// handleInvCmd processes the received inventory.
func (s *Server) handleGetDataCmd(p Peer, inv *payload.Inventory) error {
if !inv.Type.Valid() || len(inv.Hashes) == 0 {
return errInvalidInvType
}
switch inv.Type {
case payload.TXType:
for _, hash := range inv.Hashes {
tx, _, err := s.chain.GetTransaction(hash)
if err == nil {
err = p.WriteMsg(NewMessage(s.Net, CMDTX, tx))
if err != nil {
return err
}
}
}
case payload.BlockType:
for _, hash := range inv.Hashes {
b, err := s.chain.GetBlock(hash)
if err == nil {
err = p.WriteMsg(NewMessage(s.Net, CMDBlock, b))
if err != nil {
return err
}
}
}
case payload.ConsensusType:
// TODO (#431)
}
return nil
}
// handleAddrCmd will process received addresses.
func (s *Server) handleAddrCmd(p Peer, addrs *payload.AddressList) error {
for _, a := range addrs.Addrs {
@ -357,6 +390,9 @@ func (s *Server) handleMessage(peer Peer, msg *Message) error {
case CMDGetAddr:
// it has no payload
return s.handleGetAddrCmd(peer)
case CMDGetData:
inv := msg.Payload.(*payload.Inventory)
return s.handleGetDataCmd(peer, inv)
case CMDHeaders:
headers := msg.Payload.(*payload.Headers)
go s.handleHeadersCmd(peer, headers)