Make MaxMessagePayload an exported constant.

ok @davecgh
This commit is contained in:
Josh Rickmar 2014-06-06 22:33:39 -05:00
parent ee46a0b108
commit 7dcb68275f
8 changed files with 20 additions and 24 deletions

View file

@ -426,9 +426,9 @@ func readVarString(r io.Reader, pver uint32) (string, error) {
// Prevent variable length strings that are larger than the maximum
// message size. It would be possible to cause memory exhaustion and
// panics without a sane upper bound on this count.
if count > maxMessagePayload {
if count > MaxMessagePayload {
str := fmt.Sprintf("variable length string is too long "+
"[count %d, max %d]", count, maxMessagePayload)
"[count %d, max %d]", count, MaxMessagePayload)
return "", messageError("readVarString", str)
}

View file

@ -16,10 +16,6 @@ import (
)
const (
// MaxMessagePayload makes the internal maxMessagePayload constant
// available to the test package.
MaxMessagePayload uint32 = maxMessagePayload
// MaxTxPerBlock makes the internal maxTxPerBlock constant available to
// the test package.
MaxTxPerBlock = maxTxPerBlock

View file

@ -20,9 +20,9 @@ const MessageHeaderSize = 24
// header. Shorter commands must be zero padded.
const commandSize = 12
// maxMessagePayload is the maximum bytes a message can be regardless of other
// MaxMessagePayload is the maximum bytes a message can be regardless of other
// individual limits imposed by messages themselves.
const maxMessagePayload = (1024 * 1024 * 32) // 32MB
const MaxMessagePayload = (1024 * 1024 * 32) // 32MB
// Commands used in bitcoin message headers which describe the type of message.
const (
@ -212,10 +212,10 @@ func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (in
lenp := len(payload)
// Enforce maximum overall message payload.
if lenp > maxMessagePayload {
if lenp > MaxMessagePayload {
str := fmt.Sprintf("message payload is too large - encoded "+
"%d bytes, but maximum message payload is %d bytes",
lenp, maxMessagePayload)
lenp, MaxMessagePayload)
return totalBytes, messageError("WriteMessage", str)
}
@ -285,10 +285,10 @@ func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []
totalBytes += n
// Enforce maximum message payload.
if hdr.length > maxMessagePayload {
if hdr.length > MaxMessagePayload {
str := fmt.Sprintf("message payload is too large - header "+
"indicates %d bytes, but max message payload is %d "+
"bytes.", hdr.length, maxMessagePayload)
"bytes.", hdr.length, MaxMessagePayload)
return totalBytes, nil, nil, messageError("ReadMessage", str)
}

View file

@ -197,7 +197,7 @@ func TestReadMessageWireErrors(t *testing.T) {
// Wire encoded bytes for a message that exceeds max overall message
// length.
mpl := btcwire.MaxMessagePayload
mpl := uint32(btcwire.MaxMessagePayload)
exceedMaxPayloadBytes := makeHeader(btcnet, "getaddr", mpl+1, 0)
// Wire encoded bytes for a command which is invalid utf-8.

View file

@ -76,8 +76,8 @@ const maxSignatureSize = 72
// maxAlertSize is the maximum size an alert.
//
// MessagePayload = VarInt(Alert) + Alert + VarInt(Signature) + Signature
// maxMessagePayload = maxAlertSize + max(VarInt) + maxSignatureSize + 1
const maxAlertSize = maxMessagePayload - maxSignatureSize - MaxVarIntPayload - 1
// MaxMessagePayload = maxAlertSize + max(VarInt) + maxSignatureSize + 1
const maxAlertSize = MaxMessagePayload - maxSignatureSize - MaxVarIntPayload - 1
// maxCountSetCancel is the maximum number of cancel IDs that could possibly
// fit into a maximum size alert.
@ -343,7 +343,7 @@ type MsgAlert struct {
func (msg *MsgAlert) BtcDecode(r io.Reader, pver uint32) error {
var err error
msg.SerializedPayload, err = readVarBytes(r, pver, maxMessagePayload,
msg.SerializedPayload, err = readVarBytes(r, pver, MaxMessagePayload,
"alert serialized payload")
if err != nil {
return err
@ -354,7 +354,7 @@ func (msg *MsgAlert) BtcDecode(r io.Reader, pver uint32) error {
msg.Payload = nil
}
msg.Signature, err = readVarBytes(r, pver, maxMessagePayload,
msg.Signature, err = readVarBytes(r, pver, MaxMessagePayload,
"alert signature")
if err != nil {
return err
@ -408,7 +408,7 @@ func (msg *MsgAlert) Command() string {
func (msg *MsgAlert) MaxPayloadLength(pver uint32) uint32 {
// Since this can vary depending on the message, make it the max
// size allowed.
return maxMessagePayload
return MaxMessagePayload
}
// NewMsgAlert returns a new bitcoin alert message that conforms to the Message

View file

@ -167,7 +167,7 @@ func (msg *MsgReject) MaxPayloadLength(pver uint32) uint32 {
// Unfortunately the bitcoin protocol does not enforce a sane
// limit on the length of the reason, so the max payload is the
// overall maximum message payload.
plen = maxMessagePayload
plen = MaxMessagePayload
}
return plen

View file

@ -76,7 +76,7 @@ func TestRejectLatest(t *testing.T) {
}
// Ensure max payload is expected value for latest protocol version.
wantPayload := btcwire.MaxMessagePayload
wantPayload := uint32(btcwire.MaxMessagePayload)
maxPayload := msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
t.Errorf("MaxPayloadLength: wrong max payload length for "+

View file

@ -39,7 +39,7 @@ const (
// maxTxInPerMessage is the maximum number of transactions inputs that
// a transaction which fits into a message could possibly have.
maxTxInPerMessage = (maxMessagePayload / minTxInPayload) + 1
maxTxInPerMessage = (MaxMessagePayload / minTxInPayload) + 1
// minTxOutPayload is the minimum payload size for a transaction output.
// Value 8 bytes + Varint for PkScript length 1 byte.
@ -47,7 +47,7 @@ const (
// maxTxOutPerMessage is the maximum number of transactions outputs that
// a transaction which fits into a message could possibly have.
maxTxOutPerMessage = (maxMessagePayload / minTxOutPayload) + 1
maxTxOutPerMessage = (MaxMessagePayload / minTxOutPayload) + 1
// minTxPayload is the minimum payload size for a transaction. Note
// that any realistically usable transaction must have at least one
@ -470,7 +470,7 @@ func readTxIn(r io.Reader, pver uint32, version uint32, ti *TxIn) error {
}
ti.PreviousOutpoint = op
ti.SignatureScript, err = readVarBytes(r, pver, maxMessagePayload,
ti.SignatureScript, err = readVarBytes(r, pver, MaxMessagePayload,
"transaction input signature script")
if err != nil {
return err
@ -519,7 +519,7 @@ func readTxOut(r io.Reader, pver uint32, version uint32, to *TxOut) error {
}
to.Value = int64(binary.LittleEndian.Uint64(buf[:]))
to.PkScript, err = readVarBytes(r, pver, maxMessagePayload,
to.PkScript, err = readVarBytes(r, pver, MaxMessagePayload,
"transaction output public key script")
if err != nil {
return err