2013-05-08 21:31:00 +02:00
|
|
|
// Copyright (c) 2013 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MsgGetData implements the Message interface and represents a bitcoin
|
|
|
|
// getdata message. It is used to request data such as blocks and transactions
|
|
|
|
// from another peer. It should be used in response to the inv (MsgInv) message
|
|
|
|
// to request the actual data referenced by each inventory vector the receiving
|
|
|
|
// peer doesn't already have. Each message is limited to a maximum number of
|
|
|
|
// inventory vectors, which is currently 50,000. As a result, multiple messages
|
|
|
|
// must be used to request larger amounts of data.
|
|
|
|
//
|
|
|
|
// Use the AddInvVect function to build up the list of inventory vectors when
|
|
|
|
// sending a getdata message to another peer.
|
|
|
|
type MsgGetData struct {
|
|
|
|
InvList []*InvVect
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddInvVect adds an inventory vector to the message.
|
|
|
|
func (msg *MsgGetData) AddInvVect(iv *InvVect) error {
|
|
|
|
if len(msg.InvList)+1 > MaxInvPerMsg {
|
2013-05-10 20:04:20 +02:00
|
|
|
str := fmt.Sprintf("too many invvect in message [max %v]",
|
|
|
|
MaxInvPerMsg)
|
2013-05-10 20:37:55 +02:00
|
|
|
return messageError("MsgGetData.AddInvVect", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.InvList = append(msg.InvList, iv)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
|
|
// This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgGetData) BtcDecode(r io.Reader, pver uint32) error {
|
|
|
|
count, err := readVarInt(r, pver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit to max inventory vectors per message.
|
|
|
|
if count > MaxInvPerMsg {
|
2013-05-10 20:04:20 +02:00
|
|
|
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
|
|
return messageError("MsgGetData.BtcDecode", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 20:57:01 +02:00
|
|
|
msg.InvList = make([]*InvVect, 0, count)
|
2013-05-08 21:31:00 +02:00
|
|
|
for i := uint64(0); i < count; i++ {
|
|
|
|
iv := InvVect{}
|
|
|
|
err := readInvVect(r, pver, &iv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.AddInvVect(&iv)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
|
|
// This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgGetData) BtcEncode(w io.Writer, pver uint32) error {
|
|
|
|
// Limit to max inventory vectors per message.
|
|
|
|
count := len(msg.InvList)
|
|
|
|
if count > MaxInvPerMsg {
|
2013-05-10 20:04:20 +02:00
|
|
|
str := fmt.Sprintf("too many invvect in message [%v]", count)
|
|
|
|
return messageError("MsgGetData.BtcEncode", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := writeVarInt(w, pver, uint64(count))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, iv := range msg.InvList {
|
|
|
|
err := writeInvVect(w, pver, iv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
|
|
// of the Message interface implementation.
|
|
|
|
func (msg *MsgGetData) Command() string {
|
|
|
|
return cmdGetData
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
|
|
// receiver. This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgGetData) MaxPayloadLength(pver uint32) uint32 {
|
|
|
|
// Num inventory vectors (varInt) + max allowed inventory vectors.
|
|
|
|
return maxVarIntPayload + (MaxInvPerMsg * maxInvVectPayload)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgGetData returns a new bitcoin getdata message that conforms to the
|
|
|
|
// Message interface. See MsgGetData for details.
|
|
|
|
func NewMsgGetData() *MsgGetData {
|
2013-09-25 20:57:01 +02:00
|
|
|
return &MsgGetData{
|
|
|
|
InvList: make([]*InvVect, 0, defaultInvListAlloc),
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|