diff --git a/pkg/network/server.go b/pkg/network/server.go index eec400c9a..b164a3e79 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -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)