Make MaxMessagePayload an exported constant.
ok @davecgh
This commit is contained in:
parent
ee46a0b108
commit
7dcb68275f
8 changed files with 20 additions and 24 deletions
|
@ -426,9 +426,9 @@ func readVarString(r io.Reader, pver uint32) (string, error) {
|
||||||
// Prevent variable length strings that are larger than the maximum
|
// Prevent variable length strings that are larger than the maximum
|
||||||
// message size. It would be possible to cause memory exhaustion and
|
// message size. It would be possible to cause memory exhaustion and
|
||||||
// panics without a sane upper bound on this count.
|
// panics without a sane upper bound on this count.
|
||||||
if count > maxMessagePayload {
|
if count > MaxMessagePayload {
|
||||||
str := fmt.Sprintf("variable length string is too long "+
|
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)
|
return "", messageError("readVarString", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// MaxMessagePayload makes the internal maxMessagePayload constant
|
|
||||||
// available to the test package.
|
|
||||||
MaxMessagePayload uint32 = maxMessagePayload
|
|
||||||
|
|
||||||
// MaxTxPerBlock makes the internal maxTxPerBlock constant available to
|
// MaxTxPerBlock makes the internal maxTxPerBlock constant available to
|
||||||
// the test package.
|
// the test package.
|
||||||
MaxTxPerBlock = maxTxPerBlock
|
MaxTxPerBlock = maxTxPerBlock
|
||||||
|
|
12
message.go
12
message.go
|
@ -20,9 +20,9 @@ const MessageHeaderSize = 24
|
||||||
// header. Shorter commands must be zero padded.
|
// header. Shorter commands must be zero padded.
|
||||||
const commandSize = 12
|
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.
|
// 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.
|
// Commands used in bitcoin message headers which describe the type of message.
|
||||||
const (
|
const (
|
||||||
|
@ -212,10 +212,10 @@ func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (in
|
||||||
lenp := len(payload)
|
lenp := len(payload)
|
||||||
|
|
||||||
// Enforce maximum overall message payload.
|
// Enforce maximum overall message payload.
|
||||||
if lenp > maxMessagePayload {
|
if lenp > MaxMessagePayload {
|
||||||
str := fmt.Sprintf("message payload is too large - encoded "+
|
str := fmt.Sprintf("message payload is too large - encoded "+
|
||||||
"%d bytes, but maximum message payload is %d bytes",
|
"%d bytes, but maximum message payload is %d bytes",
|
||||||
lenp, maxMessagePayload)
|
lenp, MaxMessagePayload)
|
||||||
return totalBytes, messageError("WriteMessage", str)
|
return totalBytes, messageError("WriteMessage", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,10 +285,10 @@ func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []
|
||||||
totalBytes += n
|
totalBytes += n
|
||||||
|
|
||||||
// Enforce maximum message payload.
|
// Enforce maximum message payload.
|
||||||
if hdr.length > maxMessagePayload {
|
if hdr.length > MaxMessagePayload {
|
||||||
str := fmt.Sprintf("message payload is too large - header "+
|
str := fmt.Sprintf("message payload is too large - header "+
|
||||||
"indicates %d bytes, but max message payload is %d "+
|
"indicates %d bytes, but max message payload is %d "+
|
||||||
"bytes.", hdr.length, maxMessagePayload)
|
"bytes.", hdr.length, MaxMessagePayload)
|
||||||
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,7 +197,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||||
|
|
||||||
// Wire encoded bytes for a message that exceeds max overall message
|
// Wire encoded bytes for a message that exceeds max overall message
|
||||||
// length.
|
// length.
|
||||||
mpl := btcwire.MaxMessagePayload
|
mpl := uint32(btcwire.MaxMessagePayload)
|
||||||
exceedMaxPayloadBytes := makeHeader(btcnet, "getaddr", mpl+1, 0)
|
exceedMaxPayloadBytes := makeHeader(btcnet, "getaddr", mpl+1, 0)
|
||||||
|
|
||||||
// Wire encoded bytes for a command which is invalid utf-8.
|
// Wire encoded bytes for a command which is invalid utf-8.
|
||||||
|
|
10
msgalert.go
10
msgalert.go
|
@ -76,8 +76,8 @@ const maxSignatureSize = 72
|
||||||
// maxAlertSize is the maximum size an alert.
|
// maxAlertSize is the maximum size an alert.
|
||||||
//
|
//
|
||||||
// MessagePayload = VarInt(Alert) + Alert + VarInt(Signature) + Signature
|
// MessagePayload = VarInt(Alert) + Alert + VarInt(Signature) + Signature
|
||||||
// maxMessagePayload = maxAlertSize + max(VarInt) + maxSignatureSize + 1
|
// MaxMessagePayload = maxAlertSize + max(VarInt) + maxSignatureSize + 1
|
||||||
const maxAlertSize = maxMessagePayload - maxSignatureSize - MaxVarIntPayload - 1
|
const maxAlertSize = MaxMessagePayload - maxSignatureSize - MaxVarIntPayload - 1
|
||||||
|
|
||||||
// maxCountSetCancel is the maximum number of cancel IDs that could possibly
|
// maxCountSetCancel is the maximum number of cancel IDs that could possibly
|
||||||
// fit into a maximum size alert.
|
// fit into a maximum size alert.
|
||||||
|
@ -343,7 +343,7 @@ type MsgAlert struct {
|
||||||
func (msg *MsgAlert) BtcDecode(r io.Reader, pver uint32) error {
|
func (msg *MsgAlert) BtcDecode(r io.Reader, pver uint32) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
msg.SerializedPayload, err = readVarBytes(r, pver, maxMessagePayload,
|
msg.SerializedPayload, err = readVarBytes(r, pver, MaxMessagePayload,
|
||||||
"alert serialized payload")
|
"alert serialized payload")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -354,7 +354,7 @@ func (msg *MsgAlert) BtcDecode(r io.Reader, pver uint32) error {
|
||||||
msg.Payload = nil
|
msg.Payload = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Signature, err = readVarBytes(r, pver, maxMessagePayload,
|
msg.Signature, err = readVarBytes(r, pver, MaxMessagePayload,
|
||||||
"alert signature")
|
"alert signature")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -408,7 +408,7 @@ func (msg *MsgAlert) Command() string {
|
||||||
func (msg *MsgAlert) MaxPayloadLength(pver uint32) uint32 {
|
func (msg *MsgAlert) MaxPayloadLength(pver uint32) uint32 {
|
||||||
// Since this can vary depending on the message, make it the max
|
// Since this can vary depending on the message, make it the max
|
||||||
// size allowed.
|
// size allowed.
|
||||||
return maxMessagePayload
|
return MaxMessagePayload
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMsgAlert returns a new bitcoin alert message that conforms to the Message
|
// NewMsgAlert returns a new bitcoin alert message that conforms to the Message
|
||||||
|
|
|
@ -167,7 +167,7 @@ func (msg *MsgReject) MaxPayloadLength(pver uint32) uint32 {
|
||||||
// Unfortunately the bitcoin protocol does not enforce a sane
|
// Unfortunately the bitcoin protocol does not enforce a sane
|
||||||
// limit on the length of the reason, so the max payload is the
|
// limit on the length of the reason, so the max payload is the
|
||||||
// overall maximum message payload.
|
// overall maximum message payload.
|
||||||
plen = maxMessagePayload
|
plen = MaxMessagePayload
|
||||||
}
|
}
|
||||||
|
|
||||||
return plen
|
return plen
|
||||||
|
|
|
@ -76,7 +76,7 @@ func TestRejectLatest(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure max payload is expected value for latest protocol version.
|
// Ensure max payload is expected value for latest protocol version.
|
||||||
wantPayload := btcwire.MaxMessagePayload
|
wantPayload := uint32(btcwire.MaxMessagePayload)
|
||||||
maxPayload := msg.MaxPayloadLength(pver)
|
maxPayload := msg.MaxPayloadLength(pver)
|
||||||
if maxPayload != wantPayload {
|
if maxPayload != wantPayload {
|
||||||
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
||||||
|
|
8
msgtx.go
8
msgtx.go
|
@ -39,7 +39,7 @@ const (
|
||||||
|
|
||||||
// maxTxInPerMessage is the maximum number of transactions inputs that
|
// maxTxInPerMessage is the maximum number of transactions inputs that
|
||||||
// a transaction which fits into a message could possibly have.
|
// 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.
|
// minTxOutPayload is the minimum payload size for a transaction output.
|
||||||
// Value 8 bytes + Varint for PkScript length 1 byte.
|
// Value 8 bytes + Varint for PkScript length 1 byte.
|
||||||
|
@ -47,7 +47,7 @@ const (
|
||||||
|
|
||||||
// maxTxOutPerMessage is the maximum number of transactions outputs that
|
// maxTxOutPerMessage is the maximum number of transactions outputs that
|
||||||
// a transaction which fits into a message could possibly have.
|
// 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
|
// minTxPayload is the minimum payload size for a transaction. Note
|
||||||
// that any realistically usable transaction must have at least one
|
// 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.PreviousOutpoint = op
|
||||||
|
|
||||||
ti.SignatureScript, err = readVarBytes(r, pver, maxMessagePayload,
|
ti.SignatureScript, err = readVarBytes(r, pver, MaxMessagePayload,
|
||||||
"transaction input signature script")
|
"transaction input signature script")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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.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")
|
"transaction output public key script")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in a new issue