Skip to content

Commit

Permalink
network.pasl: getHeaders request support
Browse files Browse the repository at this point in the history
  • Loading branch information
xiphon committed Feb 18, 2020
1 parent d1f68a8 commit 9a7cff7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
33 changes: 32 additions & 1 deletion network/pasl/connection.go
Expand Up @@ -255,7 +255,38 @@ func (this *PascalConnection) onMessageRequest(request *requestResponse, payload

func (this *PascalConnection) onGetHeadersRequest(request *requestResponse, payload []byte) ([]byte, error) {
utils.Tracef("[P2P %s] %s", this.logPrefix, request.GetType())
return nil, nil

var packet packetGetBlocksRequest
if err := utils.Deserialize(&packet, bytes.NewBuffer(payload)); err != nil {
return nil, err
}

if packet.FromIndex > packet.ToIndex {
packet.ToIndex, packet.FromIndex = packet.FromIndex, packet.ToIndex
}

total := packet.ToIndex - packet.FromIndex
if total > defaults.NetworkBlocksPerRequest {
total = defaults.NetworkBlocksPerRequest
packet.ToIndex = packet.FromIndex + total
}

serialized := make([]safebox.SerializedBlockHeader, 0, total)
for index := packet.FromIndex; index <= packet.ToIndex; index++ {
if block, err := this.blockchain.GetBlock(index); err == nil {
serialized = append(serialized, this.blockchain.SerializeBlockHeader(block, false, false))
} else {
utils.Tracef("[P2P %s] Failed to get block header %d: %v", this.logPrefix, index, err)
break
}
}

out := utils.Serialize(packetGetHeadersResponse{
BlockHeaders: serialized,
})
request.result.setError(success)

return out, nil
}

func (this *PascalConnection) onNewBlockNotification(request *requestResponse, payload []byte) ([]byte, error) {
Expand Down
4 changes: 4 additions & 0 deletions network/pasl/packets.go
Expand Up @@ -33,6 +33,10 @@ type packetGetBlocksResponse struct {
Blocks []safebox.SerializedBlock
}

type packetGetHeadersResponse struct {
BlockHeaders []safebox.SerializedBlockHeader
}

type packetError struct {
Message string
}
Expand Down

0 comments on commit 9a7cff7

Please sign in to comment.