diff --git a/common.go b/common.go index 42204ec9..8e48152f 100644 --- a/common.go +++ b/common.go @@ -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) } diff --git a/internal_test.go b/internal_test.go index 7b59992c..14437201 100644 --- a/internal_test.go +++ b/internal_test.go @@ -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 diff --git a/message.go b/message.go index fafe2155..d5b51ea0 100644 --- a/message.go +++ b/message.go @@ -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) } diff --git a/message_test.go b/message_test.go index 5d709c15..036ff0fa 100644 --- a/message_test.go +++ b/message_test.go @@ -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. diff --git a/msgalert.go b/msgalert.go index d1026c2a..6b95fb49 100644 --- a/msgalert.go +++ b/msgalert.go @@ -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 diff --git a/msgreject.go b/msgreject.go index 14856cfb..c8d33fff 100644 --- a/msgreject.go +++ b/msgreject.go @@ -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 diff --git a/msgreject_test.go b/msgreject_test.go index 19c5317c..1efde9c7 100644 --- a/msgreject_test.go +++ b/msgreject_test.go @@ -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 "+ diff --git a/msgtx.go b/msgtx.go index fc93faa4..0d6714e9 100644 --- a/msgtx.go +++ b/msgtx.go @@ -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