mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 23:33:37 +00:00
network: allow to GetHeaders by index
Use GetBlockByIndex payload for GetHeaders command instead of GetBlocks payload.
This commit is contained in:
parent
7b1c305000
commit
f6f3863e0e
3 changed files with 14 additions and 15 deletions
|
@ -146,9 +146,9 @@ func (m *Message) decodePayload() error {
|
||||||
case CMDConsensus:
|
case CMDConsensus:
|
||||||
p = consensus.NewPayload(m.Network)
|
p = consensus.NewPayload(m.Network)
|
||||||
case CMDGetBlocks:
|
case CMDGetBlocks:
|
||||||
fallthrough
|
|
||||||
case CMDGetHeaders:
|
|
||||||
p = &payload.GetBlocks{}
|
p = &payload.GetBlocks{}
|
||||||
|
case CMDGetHeaders:
|
||||||
|
fallthrough
|
||||||
case CMDGetBlockByIndex:
|
case CMDGetBlockByIndex:
|
||||||
p = &payload.GetBlockByIndex{}
|
p = &payload.GetBlockByIndex{}
|
||||||
case CMDHeaders:
|
case CMDHeaders:
|
||||||
|
|
|
@ -633,18 +633,17 @@ func (s *Server) handleGetBlockByIndexCmd(p Peer, gbd *payload.GetBlockByIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleGetHeadersCmd processes the getheaders request.
|
// handleGetHeadersCmd processes the getheaders request.
|
||||||
func (s *Server) handleGetHeadersCmd(p Peer, gh *payload.GetBlocks) error {
|
func (s *Server) handleGetHeadersCmd(p Peer, gh *payload.GetBlockByIndex) error {
|
||||||
count := gh.Count
|
if gh.IndexStart > s.chain.HeaderHeight() {
|
||||||
if gh.Count < 0 || gh.Count > payload.MaxHashesCount {
|
return nil
|
||||||
count = payload.MaxHashesCount
|
|
||||||
}
|
}
|
||||||
start, err := s.chain.GetHeader(gh.HashStart)
|
count := gh.Count
|
||||||
if err != nil {
|
if gh.Count < 0 || gh.Count > payload.MaxHeadersAllowed {
|
||||||
return err
|
count = payload.MaxHeadersAllowed
|
||||||
}
|
}
|
||||||
resp := payload.Headers{}
|
resp := payload.Headers{}
|
||||||
resp.Hdrs = make([]*block.Header, 0, payload.MaxHeadersAllowed)
|
resp.Hdrs = make([]*block.Header, 0, count)
|
||||||
for i := start.Index + 1; i < start.Index+uint32(count); i++ {
|
for i := gh.IndexStart; i < gh.IndexStart+uint32(count); i++ {
|
||||||
hash := s.chain.GetHeaderHash(int(i))
|
hash := s.chain.GetHeaderHash(int(i))
|
||||||
if hash.Equals(util.Uint256{}) {
|
if hash.Equals(util.Uint256{}) {
|
||||||
break
|
break
|
||||||
|
@ -709,9 +708,9 @@ func (s *Server) handleGetAddrCmd(p Peer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// requestHeaders sends a getheaders message to the peer.
|
// 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 {
|
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))
|
return p.EnqueueP2PMessage(NewMessage(CMDGetHeaders, payload))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -767,7 +766,7 @@ func (s *Server) handleMessage(peer Peer, msg *Message) error {
|
||||||
inv := msg.Payload.(*payload.Inventory)
|
inv := msg.Payload.(*payload.Inventory)
|
||||||
return s.handleGetDataCmd(peer, inv)
|
return s.handleGetDataCmd(peer, inv)
|
||||||
case CMDGetHeaders:
|
case CMDGetHeaders:
|
||||||
gh := msg.Payload.(*payload.GetBlocks)
|
gh := msg.Payload.(*payload.GetBlockByIndex)
|
||||||
return s.handleGetHeadersCmd(peer, gh)
|
return s.handleGetHeadersCmd(peer, gh)
|
||||||
case CMDHeaders:
|
case CMDHeaders:
|
||||||
headers := msg.Payload.(*payload.Headers)
|
headers := msg.Payload.(*payload.Headers)
|
||||||
|
|
|
@ -149,7 +149,7 @@ func TestRequestHeaders(t *testing.T) {
|
||||||
p = newLocalPeer(t, s)
|
p = newLocalPeer(t, s)
|
||||||
)
|
)
|
||||||
p.messageHandler = func(t *testing.T, msg *Message) {
|
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)
|
assert.Equal(t, CMDGetHeaders, msg.Command)
|
||||||
}
|
}
|
||||||
s.requestHeaders(p)
|
s.requestHeaders(p)
|
||||||
|
|
Loading…
Reference in a new issue