diff --git a/pkg/network/message.go b/pkg/network/message.go index d336bd5ad..502ebbaed 100644 --- a/pkg/network/message.go +++ b/pkg/network/message.go @@ -146,9 +146,9 @@ func (m *Message) decodePayload() error { case CMDConsensus: p = consensus.NewPayload(m.Network) case CMDGetBlocks: - fallthrough - case CMDGetHeaders: p = &payload.GetBlocks{} + case CMDGetHeaders: + fallthrough case CMDGetBlockByIndex: p = &payload.GetBlockByIndex{} case CMDHeaders: diff --git a/pkg/network/server.go b/pkg/network/server.go index e681f335d..cba5b4976 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -633,18 +633,17 @@ func (s *Server) handleGetBlockByIndexCmd(p Peer, gbd *payload.GetBlockByIndex) } // handleGetHeadersCmd processes the getheaders request. -func (s *Server) handleGetHeadersCmd(p Peer, gh *payload.GetBlocks) error { - count := gh.Count - if gh.Count < 0 || gh.Count > payload.MaxHashesCount { - count = payload.MaxHashesCount +func (s *Server) handleGetHeadersCmd(p Peer, gh *payload.GetBlockByIndex) error { + if gh.IndexStart > s.chain.HeaderHeight() { + return nil } - start, err := s.chain.GetHeader(gh.HashStart) - if err != nil { - return err + count := gh.Count + if gh.Count < 0 || gh.Count > payload.MaxHeadersAllowed { + count = payload.MaxHeadersAllowed } resp := payload.Headers{} - resp.Hdrs = make([]*block.Header, 0, payload.MaxHeadersAllowed) - for i := start.Index + 1; i < start.Index+uint32(count); i++ { + resp.Hdrs = make([]*block.Header, 0, count) + for i := gh.IndexStart; i < gh.IndexStart+uint32(count); i++ { hash := s.chain.GetHeaderHash(int(i)) if hash.Equals(util.Uint256{}) { break @@ -709,9 +708,9 @@ func (s *Server) handleGetAddrCmd(p Peer) error { } // requestHeaders sends a getheaders message to the peer. -// The peer will respond with headers op to a count of 500. +// The peer will respond with headers op to a count of 2000. func (s *Server) requestHeaders(p Peer) error { - payload := payload.NewGetBlocks(s.chain.CurrentHeaderHash(), -1) + payload := payload.NewGetBlockByIndex(s.chain.HeaderHeight(), -1) return p.EnqueueP2PMessage(NewMessage(CMDGetHeaders, payload)) } @@ -767,7 +766,7 @@ func (s *Server) handleMessage(peer Peer, msg *Message) error { inv := msg.Payload.(*payload.Inventory) return s.handleGetDataCmd(peer, inv) case CMDGetHeaders: - gh := msg.Payload.(*payload.GetBlocks) + gh := msg.Payload.(*payload.GetBlockByIndex) return s.handleGetHeadersCmd(peer, gh) case CMDHeaders: headers := msg.Payload.(*payload.Headers) diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index 6c783f53f..dddd19a77 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -149,7 +149,7 @@ func TestRequestHeaders(t *testing.T) { p = newLocalPeer(t, s) ) p.messageHandler = func(t *testing.T, msg *Message) { - assert.IsType(t, &payload.GetBlocks{}, msg.Payload) + assert.IsType(t, &payload.GetBlockByIndex{}, msg.Payload) assert.Equal(t, CMDGetHeaders, msg.Command) } s.requestHeaders(p)