network: allow to use -1 to specify GetBlockByIndex.Count

This commit is contained in:
Anna Shaleva 2020-07-31 14:17:14 +03:00
parent 0b856033b0
commit 0b0591fc34
2 changed files with 10 additions and 6 deletions

View file

@ -9,11 +9,11 @@ import (
// GetBlockByIndex payload // GetBlockByIndex payload
type GetBlockByIndex struct { type GetBlockByIndex struct {
IndexStart uint32 IndexStart uint32
Count uint16 Count int16
} }
// NewGetBlockByIndex returns GetBlockByIndex payload with specified start index and count // NewGetBlockByIndex returns GetBlockByIndex payload with specified start index and count
func NewGetBlockByIndex(indexStart uint32, count uint16) *GetBlockByIndex { func NewGetBlockByIndex(indexStart uint32, count int16) *GetBlockByIndex {
return &GetBlockByIndex{ return &GetBlockByIndex{
IndexStart: indexStart, IndexStart: indexStart,
Count: count, Count: count,
@ -23,8 +23,8 @@ func NewGetBlockByIndex(indexStart uint32, count uint16) *GetBlockByIndex {
// DecodeBinary implements Serializable interface. // DecodeBinary implements Serializable interface.
func (d *GetBlockByIndex) DecodeBinary(br *io.BinReader) { func (d *GetBlockByIndex) DecodeBinary(br *io.BinReader) {
d.IndexStart = br.ReadU32LE() d.IndexStart = br.ReadU32LE()
d.Count = br.ReadU16LE() d.Count = int16(br.ReadU16LE())
if d.Count == 0 || d.Count > MaxHeadersAllowed { if d.Count < -1 || d.Count == 0 || d.Count > MaxHeadersAllowed {
br.Err = errors.New("invalid block count") br.Err = errors.New("invalid block count")
} }
} }
@ -32,5 +32,5 @@ func (d *GetBlockByIndex) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface. // EncodeBinary implements Serializable interface.
func (d *GetBlockByIndex) EncodeBinary(bw *io.BinWriter) { func (d *GetBlockByIndex) EncodeBinary(bw *io.BinWriter) {
bw.WriteU32LE(d.IndexStart) bw.WriteU32LE(d.IndexStart)
bw.WriteU16LE(d.Count) bw.WriteU16LE(uint16(d.Count))
} }

View file

@ -611,7 +611,11 @@ func (s *Server) handleGetBlocksCmd(p Peer, gb *payload.GetBlocks) error {
// handleGetBlockByIndexCmd processes the getblockbyindex request. // handleGetBlockByIndexCmd processes the getblockbyindex request.
func (s *Server) handleGetBlockByIndexCmd(p Peer, gbd *payload.GetBlockByIndex) error { func (s *Server) handleGetBlockByIndexCmd(p Peer, gbd *payload.GetBlockByIndex) error {
for i := gbd.IndexStart; i < gbd.IndexStart+uint32(gbd.Count); i++ { count := gbd.Count
if gbd.Count < 0 || gbd.Count > payload.MaxHashesCount {
count = payload.MaxHashesCount
}
for i := gbd.IndexStart; i < gbd.IndexStart+uint32(count); i++ {
b, err := s.chain.GetBlock(s.chain.GetHeaderHash(int(i))) b, err := s.chain.GetBlock(s.chain.GetHeaderHash(int(i)))
if err != nil { if err != nil {
return err return err