diff --git a/blockchain/fullblocks_test.go b/blockchain/fullblocks_test.go index a106971764..c757d275a2 100644 --- a/blockchain/fullblocks_test.go +++ b/blockchain/fullblocks_test.go @@ -220,7 +220,7 @@ func TestFullBlocks(t *testing.T) { // Ensure there is an error due to deserializing the block. var msgBlock wire.MsgBlock err := msgBlock.BtcDecode(bytes.NewReader(item.RawBlock), 0) - var werr *wire.MessageError + var werr wire.Error if !errors.As(err, &werr) { t.Fatalf("block %q (hash %s, height %d) should have "+ "failed to decode", item.Name, blockHash, diff --git a/blockchain/go.mod b/blockchain/go.mod index e9e553a31d..23257a7793 100644 --- a/blockchain/go.mod +++ b/blockchain/go.mod @@ -24,4 +24,5 @@ replace ( github.com/decred/dcrd/dcrec/secp256k1/v3 => ../dcrec/secp256k1 github.com/decred/dcrd/dcrutil/v3 => ../dcrutil github.com/decred/dcrd/txscript/v3 => ../txscript + github.com/decred/dcrd/wire => ../wire ) diff --git a/blockchain/go.sum b/blockchain/go.sum index 04dc6c61b1..a4c2c307c5 100644 --- a/blockchain/go.sum +++ b/blockchain/go.sum @@ -36,8 +36,6 @@ github.com/decred/dcrd/gcs/v2 v2.0.0 h1:nCc3q9iIwIpF0khTSiC7xYgojKoKnPrqrgVjboOB github.com/decred/dcrd/gcs/v2 v2.0.0/go.mod h1:3XjKcrtvB+r2ezhIsyNCLk6dRnXRJVyYmsd1P3SkU3o= github.com/decred/dcrd/txscript/v2 v2.1.0 h1:IKIpNm0lPmNQoaZ2zxZm1qMwfmLb/XXeahxXlfc+MrA= github.com/decred/dcrd/txscript/v2 v2.1.0/go.mod h1:XaJAVrZU4NWRx4UEzTiDAs86op1m8GRJLz24SDBKOi0= -github.com/decred/dcrd/wire v1.3.0 h1:X76I2/a8esUmxXmFpJpAvXEi014IA4twgwcOBeIS8lE= -github.com/decred/dcrd/wire v1.3.0/go.mod h1:fnKGlUY2IBuqnpxx5dYRU5Oiq392OBqAuVjRVSkIoXM= github.com/decred/slog v1.0.0 h1:Dl+W8O6/JH6n2xIFN2p3DNjCmjYwvrXsjlSJTQQ4MhE= github.com/decred/slog v1.0.0/go.mod h1:zR98rEZHSnbZ4WHZtO0iqmSZjDLKhkXfrPTZQKtAonQ= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= diff --git a/server.go b/server.go index b51c8521d2..b9c7907c69 100644 --- a/server.go +++ b/server.go @@ -1424,9 +1424,9 @@ func (sp *serverPeer) OnAddr(p *peer.Peer, msg *wire.MsgAddr) { // the bytes received by the server. func (sp *serverPeer) OnRead(p *peer.Peer, bytesRead int, msg wire.Message, err error) { // Ban peers sending messages that do not conform to the wire protocol. - var errCode wire.ErrorCode - if errors.As(err, &errCode) { - peerLog.Errorf("Unable to read wire message from %s: %v", sp, err) + var werr wire.Error + if errors.As(err, &werr) { + peerLog.Errorf("Unable to read wire message from %s: %v", sp, werr) sp.server.BanPeer(sp) sp.Disconnect() } diff --git a/wire/common.go b/wire/common.go index 3136890cd4..916ec62cbb 100644 --- a/wire/common.go +++ b/wire/common.go @@ -516,7 +516,7 @@ func ReadVarInt(r io.Reader, pver uint32) (uint64, error) { min := uint64(0x100000000) if rv < min { msg := fmt.Sprintf(nonCanonicalVarIntFormat, rv, discriminant, min) - return 0, messageError(op, ErrNonCanonicalVarInt, msg) + return 0, makeError(op, ErrNonCanonicalVarInt, msg) } case 0xfe: @@ -531,7 +531,7 @@ func ReadVarInt(r io.Reader, pver uint32) (uint64, error) { min := uint64(0x10000) if rv < min { msg := fmt.Sprintf(nonCanonicalVarIntFormat, rv, discriminant, min) - return 0, messageError(op, ErrNonCanonicalVarInt, msg) + return 0, makeError(op, ErrNonCanonicalVarInt, msg) } case 0xfd: @@ -546,7 +546,7 @@ func ReadVarInt(r io.Reader, pver uint32) (uint64, error) { min := uint64(0xfd) if rv < min { msg := fmt.Sprintf(nonCanonicalVarIntFormat, rv, discriminant, min) - return 0, messageError(op, ErrNonCanonicalVarInt, msg) + return 0, makeError(op, ErrNonCanonicalVarInt, msg) } default: @@ -628,7 +628,7 @@ func ReadVarString(r io.Reader, pver uint32) (string, error) { if count > MaxMessagePayload { msg := fmt.Sprintf("variable length string is too long "+ "[count %d, max %d]", count, MaxMessagePayload) - return "", messageError(op, ErrVarStringTooLong, msg) + return "", makeError(op, ErrVarStringTooLong, msg) } buf := make([]byte, count) @@ -672,7 +672,7 @@ func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32, if count > uint64(maxAllowed) { msg := fmt.Sprintf("%s is larger than the max allowed size "+ "[count %d, max %d]", fieldName, count, maxAllowed) - return nil, messageError(op, ErrVarBytesTooLong, msg) + return nil, makeError(op, ErrVarBytesTooLong, msg) } b := make([]byte, count) diff --git a/wire/common_test.go b/wire/common_test.go index cdac4cd8ca..0ff1861524 100644 --- a/wire/common_test.go +++ b/wire/common_test.go @@ -399,7 +399,7 @@ func TestVarIntNonCanonical(t *testing.T) { // Decode from wire format. rbuf := bytes.NewReader(test.in) val, err := ReadVarInt(rbuf, test.pver) - var merr *MessageError + var merr Error if !errors.As(err, &merr) { t.Errorf("ReadVarInt #%d (%s) unexpected error %v", i, test.name, err) @@ -560,9 +560,9 @@ func TestVarStringOverflowErrors(t *testing.T) { err error // Expected error }{ {[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, - pver, &MessageError{}}, + pver, Error{}}, {[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, - pver, &MessageError{}}, + pver, Error{}}, } t.Logf("Running %d tests", len(tests)) @@ -691,9 +691,9 @@ func TestVarBytesOverflowErrors(t *testing.T) { err error // Expected error }{ {[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, - pver, &MessageError{}}, + pver, Error{}}, {[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, - pver, &MessageError{}}, + pver, Error{}}, } t.Logf("Running %d tests", len(tests)) diff --git a/wire/doc.go b/wire/doc.go index 10be795943..93d7ffbaf3 100644 --- a/wire/doc.go +++ b/wire/doc.go @@ -144,7 +144,7 @@ Errors Errors returned by this package are either the raw errors provided by underlying calls to read/write from streams such as io.EOF, io.ErrUnexpectedEOF, and -io.ErrShortWrite, or of type wire.MessageError. This allows the caller to +io.ErrShortWrite, or of type wire.Error. This allows the caller to differentiate between general IO errors and malformed messages through type assertions. diff --git a/wire/error.go b/wire/error.go index 2fa3007a41..c2007d3bc7 100644 --- a/wire/error.go +++ b/wire/error.go @@ -5,231 +5,148 @@ package wire -import ( - "fmt" -) - -// ErrorCode describes a kind of message error. -type ErrorCode int +// ErrorKind identifies a kind of error. It has full support for errors.Is and +// errors.As, so the caller can directly check against an error kind when +// determining the reason for an error. +type ErrorKind string // These constants are used to identify a specific Error. const ( // ErrNonCanonicalVarInt is returned when a variable length integer is // not canonically encoded. - ErrNonCanonicalVarInt ErrorCode = iota + ErrNonCanonicalVarInt = ErrorKind("ErrNonCanonicalVarInt") // ErrVarStringTooLong is returned when a variable string exceeds the // maximum message size allowed. - ErrVarStringTooLong + ErrVarStringTooLong = ErrorKind("ErrVarStringTooLong") // ErrVarBytesTooLong is returned when a variable-length byte slice // exceeds the maximum message size allowed. - ErrVarBytesTooLong + ErrVarBytesTooLong = ErrorKind("ErrVarBytesTooLong") // ErrCmdTooLong is returned when a command exceeds the maximum command // size allowed. - ErrCmdTooLong + ErrCmdTooLong = ErrorKind("ErrCmdTooLong") // ErrPayloadTooLarge is returned when a payload exceeds the maximum // payload size allowed. - ErrPayloadTooLarge + ErrPayloadTooLarge = ErrorKind("ErrPayloadTooLarge") // ErrWrongNetwork is returned when a message intended for a different // network is received. - ErrWrongNetwork + ErrWrongNetwork = ErrorKind("ErrWrongNetwork") // ErrMalformedCmd is returned when a malformed command is received. - ErrMalformedCmd + ErrMalformedCmd = ErrorKind("ErrMalformedCmd") // ErrUnknownCmd is returned when an unknown command is received. - ErrUnknownCmd + ErrUnknownCmd = ErrorKind("ErrUnknownCmd") // ErrPayloadChecksum is returned when a message with an invalid checksum // is received. - ErrPayloadChecksum + ErrPayloadChecksum = ErrorKind("ErrPayloadChecksum") // ErrTooManyAddrs is returned when an address list exceeds the maximum // allowed. - ErrTooManyAddrs + ErrTooManyAddrs = ErrorKind("ErrTooManyAddrs") // ErrTooManyTxs is returned when a the number of transactions exceed the // maximum allowed. - ErrTooManyTxs + ErrTooManyTxs = ErrorKind("ErrTooManyTxs") // ErrMsgInvalidForPVer is returned when a message is invalid for // the expected protocol version. - ErrMsgInvalidForPVer + ErrMsgInvalidForPVer = ErrorKind("ErrMsgInvalidForPVer") // ErrFilterTooLarge is returned when a committed filter exceeds // the maximum size allowed. - ErrFilterTooLarge + ErrFilterTooLarge = ErrorKind("ErrFilterTooLarge") // ErrTooManyProofs is returned when the numeber of proof hashes // exceeds the maximum allowed. - ErrTooManyProofs + ErrTooManyProofs = ErrorKind("ErrTooManyProofs") // ErrTooManyFilterTypes is returned when the number of filter types // exceeds the maximum allowed. - ErrTooManyFilterTypes + ErrTooManyFilterTypes = ErrorKind("ErrTooManyFilterTypes") // ErrTooManyLocators is returned when the number of block locators exceed // the maximum allowed. - ErrTooManyLocators + ErrTooManyLocators = ErrorKind("ErrTooManyLocators") // ErrTooManyVectors is returned when the number of inventory vectors // exceed the maximum allowed. - ErrTooManyVectors + ErrTooManyVectors = ErrorKind("ErrTooManyVectors") // ErrTooManyHeaders is returned when the number of block headers exceed // the maximum allowed. - ErrTooManyHeaders + ErrTooManyHeaders = ErrorKind("ErrTooManyHeaders") // ErrHeaderContainsTxs is returned when a header's transactions // count is greater than zero. - ErrHeaderContainsTxs + ErrHeaderContainsTxs = ErrorKind("ErrHeaderContainsTxs") // ErrTooManyVotes is returned when the number of vote hashes exceed the // maximum allowed. - ErrTooManyVotes + ErrTooManyVotes = ErrorKind("ErrTooManyVotes") // ErrTooManyBlocks is returned when the number of block hashes exceed the // maximum allowed. - ErrTooManyBlocks + ErrTooManyBlocks = ErrorKind("ErrTooManyBlocks") // ErrMismatchedWitnessCount returned when a transaction has unequal witness // and prefix txin quantities. - ErrMismatchedWitnessCount + ErrMismatchedWitnessCount = ErrorKind("ErrMismatchedWitnessCount") // ErrUnknownTxType is returned when a transaction type is unknown. - ErrUnknownTxType + ErrUnknownTxType = ErrorKind("ErrUnknownTxType") // ErrReadInPrefixFromWitnessOnlyTx is returned when attempting to read a // transaction input prefix from a witness only transaction. - ErrReadInPrefixFromWitnessOnlyTx + ErrReadInPrefixFromWitnessOnlyTx = ErrorKind("ErrReadInPrefixFromWitnessOnlyTx") // ErrInvalidMsg is returned for an invalid message structure. - ErrInvalidMsg + ErrInvalidMsg = ErrorKind("ErrInvalidMsg") // ErrUserAgentTooLong is returned when the provided user agent exceeds // the maximum allowed. - ErrUserAgentTooLong + ErrUserAgentTooLong = ErrorKind("ErrUserAgentTooLong") // ErrTooManyFilterHeaders is returned when the number of committed filter // headers exceed the maximum allowed. - ErrTooManyFilterHeaders + ErrTooManyFilterHeaders = ErrorKind("ErrTooManyFilterHeaders") // ErrMalformedStrictString is returned when a string that has strict // formatting requirements does not conform to the requirements. - ErrMalformedStrictString + ErrMalformedStrictString = ErrorKind("ErrMalformedStrictString") ) -// Map of ErrorCode values back to their constant names for pretty printing. -var errorCodeStrings = map[ErrorCode]string{ - ErrNonCanonicalVarInt: "ErrNonCanonicalVarInt", - ErrVarStringTooLong: "ErrVarStringTooLong", - ErrVarBytesTooLong: "ErrVarBytesTooLong", - ErrCmdTooLong: "ErrCmdTooLong", - ErrPayloadTooLarge: "ErrPayloadTooLarge", - ErrWrongNetwork: "ErrWrongNetwork", - ErrMalformedCmd: "ErrMalformedCmd", - ErrUnknownCmd: "ErrUnknownCmd", - ErrPayloadChecksum: "ErrPayloadChecksum", - ErrTooManyAddrs: "ErrTooManyAddrs", - ErrTooManyTxs: "ErrTooManyTxs", - ErrMsgInvalidForPVer: "ErrMsgInvalidForPVer", - ErrFilterTooLarge: "ErrFilterTooLarge", - ErrTooManyProofs: "ErrTooManyProofs", - ErrTooManyFilterTypes: "ErrTooManyFilterTypes", - ErrTooManyLocators: "ErrTooManyLocators", - ErrTooManyVectors: "ErrTooManyVectors", - ErrTooManyHeaders: "ErrTooManyHeaders", - ErrHeaderContainsTxs: "ErrHeaderContainsTxs", - ErrTooManyVotes: "ErrTooManyVotes", - ErrTooManyBlocks: "ErrTooManyBlocks", - ErrMismatchedWitnessCount: "ErrMismatchedWitnessCount", - ErrUnknownTxType: "ErrUnknownTxType", - ErrReadInPrefixFromWitnessOnlyTx: "ErrReadInPrefixFromWitnessOnlyTx", - ErrInvalidMsg: "ErrInvalidMsg", - ErrUserAgentTooLong: "ErrUserAgentTooLong", - ErrTooManyFilterHeaders: "ErrTooManyFilterHeaders", - ErrMalformedStrictString: "ErrMalformedStrictString", -} - -// String returns the ErrorCode as a human-readable name. -func (e ErrorCode) String() string { - if s := errorCodeStrings[e]; s != "" { - return s - } - return fmt.Sprintf("Unknown ErrorCode (%d)", int(e)) -} - -// Error implements the error interface. -func (e ErrorCode) Error() string { - return e.String() -} - -// Is implements the interface to work with the standard library's errors.Is. -// -// It returns true in the following cases: -// - The target is a *MessageError and the error codes match -// - The target is an ErrorCode and it the error codes match -func (e ErrorCode) Is(target error) bool { - switch target := target.(type) { - case *MessageError: - return e == target.ErrorCode - - case ErrorCode: - return e == target - } - - return false +// Error satisfies the error interface and prints human-readable errors. +func (e ErrorKind) Error() string { + return string(e) } -// MessageError describes an issue with a message. -// An example of some potential issues are messages from the wrong decred -// network, invalid commands, mismatched checksums, and exceeding max payloads. -// -// This provides a mechanism for the caller to type assert the error to -// differentiate between general io errors such as io.EOF and issues that -// resulted from malformed messages. -type MessageError struct { - Func string // Function name - ErrorCode ErrorCode // Describes the kind of error - Description string // Human readable description of the issue +// Error identifies an error related to wire messages. It has +// full support for errors.Is and errors.As, so the caller can +// ascertain the specific reason for the error by checking the +// underlying error. +type Error struct { + Func string + Description string + Err error } // Error satisfies the error interface and prints human-readable errors. -func (m MessageError) Error() string { - if m.Func != "" { - return fmt.Sprintf("%v: %v", m.Func, m.Description) - } - return m.Description -} - -// messageError creates an Error given a set of arguments. -func messageError(Func string, c ErrorCode, desc string) *MessageError { - return &MessageError{Func: Func, ErrorCode: c, Description: desc} +func (e Error) Error() string { + return e.Description } -// Is implements the interface to work with the standard library's errors.Is. -// -// It returns true in the following cases: -// - The target is a *MessageError and the error codes match -// - The target is an ErrorCode and it the error codes match -func (m *MessageError) Is(target error) bool { - switch target := target.(type) { - case *MessageError: - return m.ErrorCode == target.ErrorCode - - case ErrorCode: - return target == m.ErrorCode - } - - return false +// Unwrap returns the underlying wrapped error. +func (e Error) Unwrap() error { + return e.Err } -// Unwrap returns the underlying wrapped error if it is not ErrOther. -// Unwrap returns the ErrorCode. Else, it returns nil. -func (m *MessageError) Unwrap() error { - return m.ErrorCode +// makeError creates an Error given a set of arguments. +func makeError(fn string, kind ErrorKind, desc string) Error { + return Error{Func: fn, Err: kind, Description: desc} } diff --git a/wire/error_test.go b/wire/error_test.go index be42797be9..40ef779593 100644 --- a/wire/error_test.go +++ b/wire/error_test.go @@ -10,13 +10,13 @@ import ( "testing" ) -// TestMessageErrorCodeStringer tests the stringized output for -// the ErrorCode type. -func TestMessageErrorCodeStringer(t *testing.T) { +// TestErrorKindStringer tests the stringized output for +// the ErrorKind type. +func TestErrorKindStringer(t *testing.T) { t.Parallel() tests := []struct { - in ErrorCode + in ErrorKind want string }{ {ErrNonCanonicalVarInt, "ErrNonCanonicalVarInt"}, @@ -47,39 +47,33 @@ func TestMessageErrorCodeStringer(t *testing.T) { {ErrUserAgentTooLong, "ErrUserAgentTooLong"}, {ErrTooManyFilterHeaders, "ErrTooManyFilterHeaders"}, {ErrMalformedStrictString, "ErrMalformedStrictString"}, - {0xffff, "Unknown ErrorCode (65535)"}, } - t.Logf("Running %d tests", len(tests)) for i, test := range tests { - result := test.in.String() + result := test.in.Error() if result != test.want { - t.Errorf("String #%d\n got: %s want: %s", i, result, + t.Errorf("%d: got: %s want: %s", i, result, test.want) continue } } } -// TestMessageError tests the error output for the MessageError type. -func TestMessageError(t *testing.T) { +// TestError tests the error output for the Error type. +func TestError(t *testing.T) { t.Parallel() tests := []struct { - in MessageError + in Error want string }{{ - MessageError{Description: "some error"}, + Error{Description: "some error"}, "some error", }, { - MessageError{Description: "human-readable error"}, + Error{Description: "human-readable error"}, "human-readable error", - }, { - MessageError{Func: "foo", Description: "something bad happened"}, - "foo: something bad happened", }} - t.Logf("Running %d tests", len(tests)) for i, test := range tests { result := test.in.Error() if result != test.want { @@ -89,7 +83,7 @@ func TestMessageError(t *testing.T) { } } -// TestErrorCodeIsAs ensures both ErrorCode and MessageError can be identified +// TestErrorKindIsAs ensures both ErrorKind and Error can be identified // as being a specific error code via errors.Is and unwrapped via errors.As. func TestErrorCodeIsAs(t *testing.T) { tests := []struct { @@ -97,7 +91,7 @@ func TestErrorCodeIsAs(t *testing.T) { err error target error wantMatch bool - wantAs ErrorCode + wantAs ErrorKind }{{ name: "ErrTooManyAddrs == ErrTooManyAddrs", err: ErrTooManyAddrs, @@ -105,21 +99,15 @@ func TestErrorCodeIsAs(t *testing.T) { wantMatch: true, wantAs: ErrTooManyAddrs, }, { - name: "MessageError.ErrTooManyAddrs == ErrTooManyAddrs", - err: messageError("", ErrTooManyAddrs, ""), + name: "Error.ErrTooManyAddrs == ErrTooManyAddrs", + err: makeError("", ErrTooManyAddrs, ""), target: ErrTooManyAddrs, wantMatch: true, wantAs: ErrTooManyAddrs, }, { - name: "ErrTooManyAddrs == MessageError.ErrTooManyAddrs", - err: ErrTooManyAddrs, - target: messageError("", ErrTooManyAddrs, ""), - wantMatch: true, - wantAs: ErrTooManyAddrs, - }, { - name: "MessageError.ErrTooManyAddrs == MessageError.ErrTooManyAddrs", - err: messageError("", ErrTooManyAddrs, ""), - target: messageError("", ErrTooManyAddrs, ""), + name: "Error.ErrTooManyAddrs == Error.ErrTooManyAddrs", + err: makeError("", ErrTooManyAddrs, ""), + target: makeError("", ErrTooManyAddrs, ""), wantMatch: true, wantAs: ErrTooManyAddrs, }, { @@ -129,21 +117,21 @@ func TestErrorCodeIsAs(t *testing.T) { wantMatch: false, wantAs: ErrTooManyTxs, }, { - name: "MessageError.ErrTooManyTxs != ErrTooManyAddrs", - err: messageError("", ErrTooManyTxs, ""), + name: "Error.ErrTooManyTxs != ErrTooManyAddrs", + err: makeError("", ErrTooManyTxs, ""), target: ErrTooManyAddrs, wantMatch: false, wantAs: ErrTooManyTxs, }, { - name: "ErrTooManyTxs != MessageError.ErrTooManyAddrs", + name: "ErrTooManyTxs != Error.ErrTooManyAddrs", err: ErrTooManyTxs, - target: messageError("", ErrTooManyAddrs, ""), + target: makeError("", ErrTooManyAddrs, ""), wantMatch: false, wantAs: ErrTooManyTxs, }, { - name: "MessageError.ErrTooManyTxs != MessageError.ErrTooManyAddrs", - err: messageError("", ErrTooManyTxs, ""), - target: messageError("", ErrTooManyAddrs, ""), + name: "Error.ErrTooManyTxs != Error.ErrTooManyAddrs", + err: makeError("", ErrTooManyTxs, ""), + target: makeError("", ErrTooManyAddrs, ""), wantMatch: false, wantAs: ErrTooManyTxs, }} @@ -157,16 +145,16 @@ func TestErrorCodeIsAs(t *testing.T) { continue } - // Ensure the underlying error code can be unwrapped and is the expected - // code. - var code ErrorCode - if !errors.As(test.err, &code) { + // Ensure the underlying error kind can be unwrapped and is the expected + // error. + var kind ErrorKind + if !errors.As(test.err, &kind) { t.Errorf("%s: unable to unwrap to error code", test.name) continue } - if code != test.wantAs { - t.Errorf("%s: unexpected unwrapped error code -- got %v, want %v", - test.name, code, test.wantAs) + if kind != test.wantAs { + t.Errorf("%s: unexpected unwrapped error kind -- got %v, want %v", + test.name, kind, test.wantAs) continue } } diff --git a/wire/fakemessage_test.go b/wire/fakemessage_test.go index 67f701d56d..1b8ed7eb6c 100644 --- a/wire/fakemessage_test.go +++ b/wire/fakemessage_test.go @@ -26,7 +26,7 @@ func (msg *fakeMessage) BtcDecode(r io.Reader, pver uint32) error { // Message interface. func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32) error { if msg.forceEncodeErr { - err := &MessageError{ + err := Error{ Func: "fakeMessage.BtcEncode", Description: "intentional error", } diff --git a/wire/message.go b/wire/message.go index 24935745cb..27d6ac9960 100644 --- a/wire/message.go +++ b/wire/message.go @@ -228,7 +228,7 @@ func WriteMessageN(w io.Writer, msg Message, pver uint32, dcrnet CurrencyNet) (i cmd := msg.Command() if len(cmd) > CommandSize { msg := fmt.Sprintf("command [%s] is too long [max %v]", cmd, CommandSize) - return totalBytes, messageError(op, ErrCmdTooLong, msg) + return totalBytes, makeError(op, ErrCmdTooLong, msg) } copy(command[:], []byte(cmd)) @@ -246,16 +246,16 @@ func WriteMessageN(w io.Writer, msg Message, pver uint32, dcrnet CurrencyNet) (i msg := fmt.Sprintf("message payload is too large - encoded "+ "%d bytes, but maximum message payload is %d bytes", lenp, MaxMessagePayload) - return totalBytes, messageError(op, ErrPayloadTooLarge, msg) + return totalBytes, makeError(op, ErrPayloadTooLarge, msg) } // Enforce maximum message payload based on the message type. mpl := msg.MaxPayloadLength(pver) if uint32(lenp) > mpl { - str := fmt.Sprintf("message payload is too large - encoded "+ + msg := fmt.Sprintf("message payload is too large - encoded "+ "%d bytes, but maximum message payload size for "+ "messages of type [%s] is %d.", lenp, cmd, mpl) - return totalBytes, messageError(op, ErrPayloadTooLarge, str) + return totalBytes, makeError(op, ErrPayloadTooLarge, msg) } // Create header for the message. @@ -313,14 +313,14 @@ func ReadMessageN(r io.Reader, pver uint32, dcrnet CurrencyNet) (int, Message, [ msg := fmt.Sprintf("message payload is too large - header "+ "indicates %d bytes, but max message payload is %d bytes.", hdr.length, MaxMessagePayload) - return totalBytes, nil, nil, messageError(op, ErrPayloadTooLarge, msg) + return totalBytes, nil, nil, makeError(op, ErrPayloadTooLarge, msg) } // Check for messages from the wrong Decred network. if hdr.magic != dcrnet { discardInput(r, hdr.length) msg := fmt.Sprintf("message from other network [%v]", hdr.magic) - return totalBytes, nil, nil, messageError(op, ErrWrongNetwork, msg) + return totalBytes, nil, nil, makeError(op, ErrWrongNetwork, msg) } // Check for malformed commands. @@ -328,14 +328,14 @@ func ReadMessageN(r io.Reader, pver uint32, dcrnet CurrencyNet) (int, Message, [ if !isStrictAscii(command) { discardInput(r, hdr.length) msg := fmt.Sprintf("invalid command %v", []byte(command)) - return totalBytes, nil, nil, messageError(op, ErrMalformedCmd, msg) + return totalBytes, nil, nil, makeError(op, ErrMalformedCmd, msg) } // Create struct of appropriate message type based on the command. msg, err := makeEmptyMessage(command) if err != nil { discardInput(r, hdr.length) - return totalBytes, nil, nil, messageError(op, ErrUnknownCmd, err.Error()) + return totalBytes, nil, nil, makeError(op, ErrUnknownCmd, err.Error()) } // Check for maximum length based on the message type as a malicious client @@ -347,7 +347,7 @@ func ReadMessageN(r io.Reader, pver uint32, dcrnet CurrencyNet) (int, Message, [ msg := fmt.Sprintf("payload exceeds max length - header "+ "indicates %v bytes, but max payload size for messages of "+ "type [%v] is %v.", hdr.length, command, mpl) - return totalBytes, nil, nil, messageError(op, ErrPayloadTooLarge, msg) + return totalBytes, nil, nil, makeError(op, ErrPayloadTooLarge, msg) } // Read payload. @@ -363,7 +363,7 @@ func ReadMessageN(r io.Reader, pver uint32, dcrnet CurrencyNet) (int, Message, [ if !bytes.Equal(checksum, hdr.checksum[:]) { msg := fmt.Sprintf("payload checksum failed - header indicates %v, "+ "but actual checksum is %v.", hdr.checksum, checksum) - return totalBytes, nil, nil, messageError(op, ErrPayloadChecksum, msg) + return totalBytes, nil, nil, makeError(op, ErrPayloadChecksum, msg) } // Unmarshal message. NOTE: This must be a *bytes.Buffer since the diff --git a/wire/message_test.go b/wire/message_test.go index bd98805fea..abdf70df20 100644 --- a/wire/message_test.go +++ b/wire/message_test.go @@ -246,7 +246,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(testNetBytes), - &MessageError{}, + Error{}, 24, }, @@ -256,7 +256,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(exceedMaxPayloadBytes), - &MessageError{}, + Error{}, 24, }, @@ -266,7 +266,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(badCommandBytes), - &MessageError{}, + Error{}, 24, }, @@ -276,7 +276,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(unsupportedCommandBytes), - &MessageError{}, + Error{}, 24, }, @@ -286,7 +286,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(exceedTypePayloadBytes), - &MessageError{}, + Error{}, 24, }, @@ -306,7 +306,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(badChecksumBytes), - &MessageError{}, + Error{}, 26, }, @@ -316,7 +316,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(badMessageBytes), - &MessageError{}, + Error{}, 25, }, @@ -326,7 +326,7 @@ func TestReadMessageWireErrors(t *testing.T) { pver, dcrnet, len(discardBytes), - &MessageError{}, + Error{}, 24, }, } @@ -348,9 +348,9 @@ func TestReadMessageWireErrors(t *testing.T) { "got %d, want %d", i, nr, test.bytes) } - // For errors which are not of type MessageError, check them for + // For errors which are not of type Error, check them for // equality. - var merr *MessageError + var merr Error if !errors.As(err, &merr) { if !errors.Is(err, test.readErr) { t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+ @@ -367,7 +367,7 @@ func TestReadMessageWireErrors(t *testing.T) { func TestWriteMessageWireErrors(t *testing.T) { pver := ProtocolVersion dcrnet := MainNet - wireErr := &MessageError{} + wireErr := Error{} // Fake message with a command that is too long. badCommandMsg := &fakeMessage{command: "somethingtoolong"} @@ -427,9 +427,9 @@ func TestWriteMessageWireErrors(t *testing.T) { "written - got %d, want %d", i, nw, test.bytes) } - // For errors which are not of type MessageError, check them for + // For errors which are not of type Error, check them for // equality. - var merr *MessageError + var merr Error if !errors.As(err, &merr) { if !errors.Is(err, test.err) { t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+ diff --git a/wire/msgaddr.go b/wire/msgaddr.go index 0418a03ed2..e683be1138 100644 --- a/wire/msgaddr.go +++ b/wire/msgaddr.go @@ -33,7 +33,7 @@ func (msg *MsgAddr) AddAddress(na *NetAddress) error { const op = "MsgAddr.AddAddress" if len(msg.AddrList)+1 > MaxAddrPerMsg { msg := fmt.Sprintf("too many addresses in message [max %v]", MaxAddrPerMsg) - return messageError(op, ErrTooManyAddrs, msg) + return makeError(op, ErrTooManyAddrs, msg) } msg.AddrList = append(msg.AddrList, na) @@ -69,7 +69,7 @@ func (msg *MsgAddr) BtcDecode(r io.Reader, pver uint32) error { if count > MaxAddrPerMsg { msg := fmt.Sprintf("too many addresses for message [count %v, max %v]", count, MaxAddrPerMsg) - return messageError(op, ErrTooManyAddrs, msg) + return makeError(op, ErrTooManyAddrs, msg) } addrList := make([]NetAddress, count) @@ -95,7 +95,7 @@ func (msg *MsgAddr) BtcEncode(w io.Writer, pver uint32) error { if count > MaxAddrPerMsg { msg := fmt.Sprintf("too many addresses for message [count %v, max %v]", count, MaxAddrPerMsg) - return messageError(op, ErrTooManyAddrs, msg) + return makeError(op, ErrTooManyAddrs, msg) } err := WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msgblock.go b/wire/msgblock.go index 5952452813..73348c8d37 100644 --- a/wire/msgblock.go +++ b/wire/msgblock.go @@ -102,7 +102,7 @@ func (msg *MsgBlock) BtcDecode(r io.Reader, pver uint32) error { if txCount > maxTxPerTree { msg := fmt.Sprintf("too many transactions to fit into a block "+ "[count %d, max %d]", txCount, maxTxPerTree) - return messageError(op, ErrTooManyTxs, msg) + return makeError(op, ErrTooManyTxs, msg) } msg.Transactions = make([]*MsgTx, 0, txCount) @@ -126,7 +126,7 @@ func (msg *MsgBlock) BtcDecode(r io.Reader, pver uint32) error { if stakeTxCount > maxTxPerTree { msg := fmt.Sprintf("too many stransactions to fit into a block "+ "[count %d, max %d]", stakeTxCount, maxTxPerTree) - return messageError(op, ErrTooManyTxs, msg) + return makeError(op, ErrTooManyTxs, msg) } msg.STransactions = make([]*MsgTx, 0, stakeTxCount) @@ -194,7 +194,7 @@ func (msg *MsgBlock) DeserializeTxLoc(r *bytes.Buffer) ([]TxLoc, []TxLoc, error) if txCount > maxTxPerTree { msg := fmt.Sprintf("too many transactions to fit into a block "+ "[count %d, max %d]", txCount, maxTxPerTree) - return nil, nil, messageError(op, ErrTooManyTxs, msg) + return nil, nil, makeError(op, ErrTooManyTxs, msg) } // Deserialize each transaction while keeping track of its location @@ -226,7 +226,7 @@ func (msg *MsgBlock) DeserializeTxLoc(r *bytes.Buffer) ([]TxLoc, []TxLoc, error) if stakeTxCount > maxTxPerTree { msg := fmt.Sprintf("too many transactions to fit into a stake tx tree "+ "[count %d, max %d]", stakeTxCount, maxTxPerTree) - return nil, nil, messageError(op, ErrTooManyTxs, msg) + return nil, nil, makeError(op, ErrTooManyTxs, msg) } // Deserialize each transaction while keeping track of its location diff --git a/wire/msgcfheaders.go b/wire/msgcfheaders.go index cbf40f176a..a4a508e147 100644 --- a/wire/msgcfheaders.go +++ b/wire/msgcfheaders.go @@ -40,7 +40,7 @@ func (msg *MsgCFHeaders) AddCFHeader(headerHash *chainhash.Hash) error { if len(msg.HeaderHashes)+1 > MaxCFHeadersPerMsg { msg := fmt.Sprintf("too many block headers in message [max %v]", MaxBlockHeadersPerMsg) - return messageError(op, ErrTooManyHeaders, msg) + return makeError(op, ErrTooManyHeaders, msg) } msg.HeaderHashes = append(msg.HeaderHashes, headerHash) @@ -53,7 +53,7 @@ func (msg *MsgCFHeaders) BtcDecode(r io.Reader, pver uint32) error { const op = "MsgCFHeaders.BtcDecode" if pver < NodeCFVersion { msg := fmt.Sprintf("cfheaders message invalid for protocol version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } // Read stop hash @@ -78,7 +78,7 @@ func (msg *MsgCFHeaders) BtcDecode(r io.Reader, pver uint32) error { if count > MaxCFHeadersPerMsg { msg := fmt.Sprintf("too many committed filter headers for message "+ "[count %v, max %v]", count, MaxBlockHeadersPerMsg) - return messageError(op, ErrTooManyFilterHeaders, msg) + return makeError(op, ErrTooManyFilterHeaders, msg) } // Create a contiguous slice of headers to deserialize into in order to @@ -103,7 +103,7 @@ func (msg *MsgCFHeaders) BtcEncode(w io.Writer, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("cfheaders message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } // Write stop hash @@ -123,7 +123,7 @@ func (msg *MsgCFHeaders) BtcEncode(w io.Writer, pver uint32) error { if count > MaxCFHeadersPerMsg { msg := fmt.Sprintf("too many committed filter headers for message "+ "[count %v, max %v]", count, MaxBlockHeadersPerMsg) - return messageError(op, ErrTooManyFilterHeaders, msg) + return makeError(op, ErrTooManyFilterHeaders, msg) } err = WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msgcfilter.go b/wire/msgcfilter.go index d54e2a4b72..18b89c9abe 100644 --- a/wire/msgcfilter.go +++ b/wire/msgcfilter.go @@ -35,7 +35,7 @@ func (msg *MsgCFilter) BtcDecode(r io.Reader, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("cfilter message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } // Read the hash of the filter's block @@ -62,14 +62,14 @@ func (msg *MsgCFilter) BtcEncode(w io.Writer, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("cfilter message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } size := len(msg.Data) if size > MaxCFilterDataSize { msg := fmt.Sprintf("cfilter size too large for message "+ "[size %v, max %v]", size, MaxCFilterDataSize) - return messageError(op, ErrFilterTooLarge, msg) + return makeError(op, ErrFilterTooLarge, msg) } err := writeElement(w, &msg.BlockHash) diff --git a/wire/msgcfilterv2.go b/wire/msgcfilterv2.go index 1a2e9fcbeb..67ee62bf3b 100644 --- a/wire/msgcfilterv2.go +++ b/wire/msgcfilterv2.go @@ -39,7 +39,7 @@ func (msg *MsgCFilterV2) BtcDecode(r io.Reader, pver uint32) error { if pver < CFilterV2Version { msg := fmt.Sprintf("%s message invalid for protocol version %d", msg.Command(), pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } err := readElement(r, &msg.BlockHash) @@ -65,7 +65,7 @@ func (msg *MsgCFilterV2) BtcDecode(r io.Reader, pver uint32) error { if count > MaxHeaderProofHashes { msg := fmt.Sprintf("too many proof hashes for message "+ "[count %v, max %v]", count, MaxHeaderProofHashes) - return messageError(op, ErrTooManyProofs, msg) + return makeError(op, ErrTooManyProofs, msg) } // Create a contiguous slice of hashes to deserialize into in order to @@ -88,21 +88,21 @@ func (msg *MsgCFilterV2) BtcEncode(w io.Writer, pver uint32) error { if pver < CFilterV2Version { msg := fmt.Sprintf("%s message invalid for protocol version %d", msg.Command(), pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } size := len(msg.Data) if size > MaxCFilterDataSize { msg := fmt.Sprintf("filter size too large for message "+ "[size %v, max %v]", size, MaxCFilterDataSize) - return messageError(op, ErrFilterTooLarge, msg) + return makeError(op, ErrFilterTooLarge, msg) } numHashes := len(msg.ProofHashes) if numHashes > MaxHeaderProofHashes { msg := fmt.Sprintf("too many proof hashes for message "+ "[count %v, max %v]", numHashes, MaxHeaderProofHashes) - return messageError(op, ErrTooManyProofs, msg) + return makeError(op, ErrTooManyProofs, msg) } err := writeElement(w, &msg.BlockHash) diff --git a/wire/msgcftypes.go b/wire/msgcftypes.go index 54d4ada8d7..24f2c69ec2 100644 --- a/wire/msgcftypes.go +++ b/wire/msgcftypes.go @@ -38,7 +38,7 @@ func (msg *MsgCFTypes) BtcDecode(r io.Reader, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("cftypes message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } // Read the number of filter types supported. The count may not exceed the @@ -51,7 +51,7 @@ func (msg *MsgCFTypes) BtcDecode(r io.Reader, pver uint32) error { if count > MaxFilterTypesPerMsg { msg := fmt.Sprintf("too many filter types for message "+ "[count %v, max %v]", count, MaxFilterTypesPerMsg) - return messageError(op, ErrTooManyFilterTypes, msg) + return makeError(op, ErrTooManyFilterTypes, msg) } // Read each filter type. @@ -73,13 +73,13 @@ func (msg *MsgCFTypes) BtcEncode(w io.Writer, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("cftypes message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } if len(msg.SupportedFilters) > MaxFilterTypesPerMsg { msg := fmt.Sprintf("too many filter types for message "+ "[count %v, max %v]", len(msg.SupportedFilters), MaxFilterTypesPerMsg) - return messageError(op, ErrTooManyFilterTypes, msg) + return makeError(op, ErrTooManyFilterTypes, msg) } // Write length of supported filters slice. diff --git a/wire/msgfeefilter.go b/wire/msgfeefilter.go index 963fa6a3a0..eac8692a20 100644 --- a/wire/msgfeefilter.go +++ b/wire/msgfeefilter.go @@ -27,7 +27,7 @@ func (msg *MsgFeeFilter) BtcDecode(r io.Reader, pver uint32) error { if pver < FeeFilterVersion { msg := fmt.Sprintf("feefilter message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return readElement(r, &msg.MinFee) @@ -40,7 +40,7 @@ func (msg *MsgFeeFilter) BtcEncode(w io.Writer, pver uint32) error { if pver < FeeFilterVersion { msg := fmt.Sprintf("feefilter message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return writeElement(w, msg.MinFee) diff --git a/wire/msggetblocks.go b/wire/msggetblocks.go index 9d59966f03..7ee8106b68 100644 --- a/wire/msggetblocks.go +++ b/wire/msggetblocks.go @@ -43,7 +43,7 @@ func (msg *MsgGetBlocks) AddBlockLocatorHash(hash *chainhash.Hash) error { if len(msg.BlockLocatorHashes)+1 > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message [max %v]", MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } msg.BlockLocatorHashes = append(msg.BlockLocatorHashes, hash) @@ -67,7 +67,7 @@ func (msg *MsgGetBlocks) BtcDecode(r io.Reader, pver uint32) error { if count > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message "+ "[count %v, max %v]", count, MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } // Create a contiguous slice of hashes to deserialize into in order to @@ -94,7 +94,7 @@ func (msg *MsgGetBlocks) BtcEncode(w io.Writer, pver uint32) error { if count > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message "+ "[count %v, max %v]", count, MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } err := writeElement(w, msg.ProtocolVersion) diff --git a/wire/msggetcfheaders.go b/wire/msggetcfheaders.go index 8f2a112672..e3576acba7 100644 --- a/wire/msggetcfheaders.go +++ b/wire/msggetcfheaders.go @@ -28,7 +28,7 @@ func (msg *MsgGetCFHeaders) AddBlockLocatorHash(hash *chainhash.Hash) error { if len(msg.BlockLocatorHashes)+1 > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message [max %v]", MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } msg.BlockLocatorHashes = append(msg.BlockLocatorHashes, hash) @@ -42,7 +42,7 @@ func (msg *MsgGetCFHeaders) BtcDecode(r io.Reader, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("getcfheaders message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } // Read num block locator hashes and limit to max. @@ -53,7 +53,7 @@ func (msg *MsgGetCFHeaders) BtcDecode(r io.Reader, pver uint32) error { if count > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message "+ "[count %v, max %v]", count, MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } // Create a contiguous slice of hashes to deserialize into in order to @@ -84,7 +84,7 @@ func (msg *MsgGetCFHeaders) BtcEncode(w io.Writer, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("getcfheaders message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } // Limit to max block locator hashes per message. @@ -92,7 +92,7 @@ func (msg *MsgGetCFHeaders) BtcEncode(w io.Writer, pver uint32) error { if count > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message "+ "[count %v, max %v]", count, MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } err := WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msggetcfilter.go b/wire/msggetcfilter.go index 5411f7a758..6d02271eda 100644 --- a/wire/msggetcfilter.go +++ b/wire/msggetcfilter.go @@ -27,7 +27,7 @@ func (msg *MsgGetCFilter) BtcDecode(r io.Reader, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("getcfilter message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } err := readElement(r, &msg.BlockHash) @@ -44,7 +44,7 @@ func (msg *MsgGetCFilter) BtcEncode(w io.Writer, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("getcfilter message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } err := writeElement(w, &msg.BlockHash) diff --git a/wire/msggetcfilterv2.go b/wire/msggetcfilterv2.go index b465e30552..a608fcdb9b 100644 --- a/wire/msggetcfilterv2.go +++ b/wire/msggetcfilterv2.go @@ -28,7 +28,7 @@ func (msg *MsgGetCFilterV2) BtcDecode(r io.Reader, pver uint32) error { if pver < CFilterV2Version { msg := fmt.Sprintf("%s message invalid for protocol version %d", msg.Command(), pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return readElement(r, &msg.BlockHash) @@ -41,7 +41,7 @@ func (msg *MsgGetCFilterV2) BtcEncode(w io.Writer, pver uint32) error { if pver < CFilterV2Version { msg := fmt.Sprintf("%s message invalid for protocol version %d", msg.Command(), pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return writeElement(w, &msg.BlockHash) diff --git a/wire/msggetcftypes.go b/wire/msggetcftypes.go index 2516b05cf5..f1fd17a404 100644 --- a/wire/msggetcftypes.go +++ b/wire/msggetcftypes.go @@ -21,7 +21,7 @@ func (msg *MsgGetCFTypes) BtcDecode(r io.Reader, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("getcftypes message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return nil @@ -34,7 +34,7 @@ func (msg *MsgGetCFTypes) BtcEncode(w io.Writer, pver uint32) error { if pver < NodeCFVersion { msg := fmt.Sprintf("getcftypes message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return nil diff --git a/wire/msggetdata.go b/wire/msggetdata.go index a83ee62f43..000ac88ede 100644 --- a/wire/msggetdata.go +++ b/wire/msggetdata.go @@ -29,7 +29,7 @@ func (msg *MsgGetData) AddInvVect(iv *InvVect) error { const op = "MsgGetData.AddInvVect" if len(msg.InvList)+1 > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [max %v]", MaxInvPerMsg) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } msg.InvList = append(msg.InvList, iv) @@ -48,7 +48,7 @@ func (msg *MsgGetData) BtcDecode(r io.Reader, pver uint32) error { // Limit to max inventory vectors per message. if count > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [%v]", count) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } // Create a contiguous slice of inventory vectors to deserialize into in @@ -76,7 +76,7 @@ func (msg *MsgGetData) BtcEncode(w io.Writer, pver uint32) error { count := len(msg.InvList) if count > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [%v]", count) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } err := WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msggetheaders.go b/wire/msggetheaders.go index 00da96d6a1..e8de7f6de8 100644 --- a/wire/msggetheaders.go +++ b/wire/msggetheaders.go @@ -40,7 +40,7 @@ func (msg *MsgGetHeaders) AddBlockLocatorHash(hash *chainhash.Hash) error { if len(msg.BlockLocatorHashes)+1 > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message [max %v]", MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } msg.BlockLocatorHashes = append(msg.BlockLocatorHashes, hash) @@ -64,7 +64,7 @@ func (msg *MsgGetHeaders) BtcDecode(r io.Reader, pver uint32) error { if count > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message "+ "[count %v, max %v]", count, MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } // Create a contiguous slice of hashes to deserialize into in order to @@ -93,7 +93,7 @@ func (msg *MsgGetHeaders) BtcEncode(w io.Writer, pver uint32) error { if count > MaxBlockLocatorsPerMsg { msg := fmt.Sprintf("too many block locator hashes for message "+ "[count %v, max %v]", count, MaxBlockLocatorsPerMsg) - return messageError(op, ErrTooManyLocators, msg) + return makeError(op, ErrTooManyLocators, msg) } err := writeElement(w, msg.ProtocolVersion) diff --git a/wire/msgheaders.go b/wire/msgheaders.go index 77463303d0..ee41baeb39 100644 --- a/wire/msgheaders.go +++ b/wire/msgheaders.go @@ -29,7 +29,7 @@ func (msg *MsgHeaders) AddBlockHeader(bh *BlockHeader) error { if len(msg.Headers)+1 > MaxBlockHeadersPerMsg { msg := fmt.Sprintf("too many block headers in message [max %v]", MaxBlockHeadersPerMsg) - return messageError(op, ErrTooManyHeaders, msg) + return makeError(op, ErrTooManyHeaders, msg) } msg.Headers = append(msg.Headers, bh) @@ -49,7 +49,7 @@ func (msg *MsgHeaders) BtcDecode(r io.Reader, pver uint32) error { if count > MaxBlockHeadersPerMsg { msg := fmt.Sprintf("too many block headers for message "+ "[count %v, max %v]", count, MaxBlockHeadersPerMsg) - return messageError(op, ErrTooManyHeaders, msg) + return makeError(op, ErrTooManyHeaders, msg) } // Create a contiguous slice of headers to deserialize into in order to @@ -72,7 +72,7 @@ func (msg *MsgHeaders) BtcDecode(r io.Reader, pver uint32) error { if txCount > 0 { msg := fmt.Sprintf("block headers may not contain transactions "+ "[count %v]", txCount) - return messageError(op, ErrHeaderContainsTxs, msg) + return makeError(op, ErrHeaderContainsTxs, msg) } msg.AddBlockHeader(bh) } @@ -90,7 +90,7 @@ func (msg *MsgHeaders) BtcEncode(w io.Writer, pver uint32) error { if count > MaxBlockHeadersPerMsg { msg := fmt.Sprintf("too many block headers for message "+ "[count %v, max %v]", count, MaxBlockHeadersPerMsg) - return messageError(op, ErrTooManyHeaders, msg) + return makeError(op, ErrTooManyHeaders, msg) } err := WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msginv.go b/wire/msginv.go index 2264e1e170..6f461b24a0 100644 --- a/wire/msginv.go +++ b/wire/msginv.go @@ -37,7 +37,7 @@ func (msg *MsgInv) AddInvVect(iv *InvVect) error { const op = "MsgInv.AddInvVect" if len(msg.InvList)+1 > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [max %v]", MaxInvPerMsg) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } msg.InvList = append(msg.InvList, iv) @@ -56,7 +56,7 @@ func (msg *MsgInv) BtcDecode(r io.Reader, pver uint32) error { // Limit to max inventory vectors per message. if count > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [%v]", count) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } // Create a contiguous slice of inventory vectors to deserialize into in @@ -84,7 +84,7 @@ func (msg *MsgInv) BtcEncode(w io.Writer, pver uint32) error { count := len(msg.InvList) if count > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [%v]", count) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } err := WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msgminingstate.go b/wire/msgminingstate.go index 0b28fa5648..a7bd270a64 100644 --- a/wire/msgminingstate.go +++ b/wire/msgminingstate.go @@ -37,7 +37,7 @@ func (msg *MsgMiningState) AddBlockHash(hash *chainhash.Hash) error { if len(msg.BlockHashes)+1 > MaxMSBlocksAtHeadPerMsg { msg := fmt.Sprintf("too many block hashes for message [max %v]", MaxMSBlocksAtHeadPerMsg) - return messageError(op, ErrTooManyHeaders, msg) + return makeError(op, ErrTooManyHeaders, msg) } msg.BlockHashes = append(msg.BlockHashes, hash) @@ -50,7 +50,7 @@ func (msg *MsgMiningState) AddVoteHash(hash *chainhash.Hash) error { if len(msg.VoteHashes)+1 > MaxMSVotesAtHeadPerMsg { msg := fmt.Sprintf("too many vote hashes for message [max %v]", MaxMSVotesAtHeadPerMsg) - return messageError(op, ErrTooManyVotes, msg) + return makeError(op, ErrTooManyVotes, msg) } msg.VoteHashes = append(msg.VoteHashes, hash) @@ -79,7 +79,7 @@ func (msg *MsgMiningState) BtcDecode(r io.Reader, pver uint32) error { if count > MaxMSBlocksAtHeadPerMsg { msg := fmt.Sprintf("too many block hashes for message "+ "[count %v, max %v]", count, MaxMSBlocksAtHeadPerMsg) - return messageError(op, ErrTooManyBlocks, msg) + return makeError(op, ErrTooManyBlocks, msg) } msg.BlockHashes = make([]*chainhash.Hash, 0, count) @@ -100,7 +100,7 @@ func (msg *MsgMiningState) BtcDecode(r io.Reader, pver uint32) error { if count > MaxMSVotesAtHeadPerMsg { msg := fmt.Sprintf("too many vote hashes for message "+ "[count %v, max %v]", count, MaxMSVotesAtHeadPerMsg) - return messageError(op, ErrTooManyVotes, msg) + return makeError(op, ErrTooManyVotes, msg) } msg.VoteHashes = make([]*chainhash.Hash, 0, count) @@ -138,7 +138,7 @@ func (msg *MsgMiningState) BtcEncode(w io.Writer, pver uint32) error { if count > MaxMSBlocksAtHeadPerMsg { msg := fmt.Sprintf("too many block hashes for message "+ "[count %v, max %v]", count, MaxMSBlocksAtHeadPerMsg) - return messageError(op, ErrTooManyBlocks, msg) + return makeError(op, ErrTooManyBlocks, msg) } err = WriteVarInt(w, pver, uint64(count)) @@ -158,7 +158,7 @@ func (msg *MsgMiningState) BtcEncode(w io.Writer, pver uint32) error { if count > MaxMSVotesAtHeadPerMsg { msg := fmt.Sprintf("too many vote hashes for message "+ "[count %v, max %v]", count, MaxMSVotesAtHeadPerMsg) - return messageError(op, ErrTooManyVotes, msg) + return makeError(op, ErrTooManyVotes, msg) } err = WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msgnotfound.go b/wire/msgnotfound.go index 289dff5e78..0089fb54af 100644 --- a/wire/msgnotfound.go +++ b/wire/msgnotfound.go @@ -27,7 +27,7 @@ func (msg *MsgNotFound) AddInvVect(iv *InvVect) error { if len(msg.InvList)+1 > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [max %v]", MaxInvPerMsg) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } msg.InvList = append(msg.InvList, iv) @@ -46,7 +46,7 @@ func (msg *MsgNotFound) BtcDecode(r io.Reader, pver uint32) error { // Limit to max inventory vectors per message. if count > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [%v]", count) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } // Create a contiguous slice of inventory vectors to deserialize into in @@ -73,7 +73,7 @@ func (msg *MsgNotFound) BtcEncode(w io.Writer, pver uint32) error { count := len(msg.InvList) if count > MaxInvPerMsg { msg := fmt.Sprintf("too many invvect in message [%v]", count) - return messageError(op, ErrTooManyVectors, msg) + return makeError(op, ErrTooManyVectors, msg) } err := WriteVarInt(w, pver, uint64(count)) diff --git a/wire/msgreject.go b/wire/msgreject.go index 0bdd3024b9..4149d9acc3 100644 --- a/wire/msgreject.go +++ b/wire/msgreject.go @@ -78,7 +78,7 @@ func validateRejectCommand(cmd string) error { const op = "MsgReject.validateRejectCommand" if !isStrictAscii(cmd) { msg := "reject command is not strict ASCII" - return messageError(op, ErrMalformedStrictString, msg) + return makeError(op, ErrMalformedStrictString, msg) } return nil @@ -90,7 +90,7 @@ func validateRejectReason(reason string) error { const op = "MsgReject.validateRejectReason" if !isStrictAscii(reason) { msg := "reject reason is not strict ASCII" - return messageError(op, ErrMalformedStrictString, msg) + return makeError(op, ErrMalformedStrictString, msg) } return nil diff --git a/wire/msgsendheaders.go b/wire/msgsendheaders.go index 7788491e22..33356e05ba 100644 --- a/wire/msgsendheaders.go +++ b/wire/msgsendheaders.go @@ -25,7 +25,7 @@ func (msg *MsgSendHeaders) BtcDecode(r io.Reader, pver uint32) error { if pver < SendHeadersVersion { msg := fmt.Sprintf("sendheaders message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return nil @@ -38,7 +38,7 @@ func (msg *MsgSendHeaders) BtcEncode(w io.Writer, pver uint32) error { if pver < SendHeadersVersion { msg := fmt.Sprintf("sendheaders message invalid for protocol "+ "version %d", pver) - return messageError(op, ErrMsgInvalidForPVer, msg) + return makeError(op, ErrMsgInvalidForPVer, msg) } return nil diff --git a/wire/msgtx.go b/wire/msgtx.go index f7a62b90b4..3a446e0b99 100644 --- a/wire/msgtx.go +++ b/wire/msgtx.go @@ -227,7 +227,7 @@ func readScript(r io.Reader, pver uint32, maxAllowed uint32, fieldName string) ( if count > uint64(maxAllowed) { msg := fmt.Sprintf("%s is larger than the max allowed size "+ "[count %d, max %d]", fieldName, count, maxAllowed) - return nil, messageError(op, ErrVarBytesTooLong, msg) + return nil, makeError(op, ErrVarBytesTooLong, msg) } b := scriptPool.Borrow(count) @@ -608,7 +608,7 @@ func (msg *MsgTx) decodePrefix(r io.Reader, pver uint32) (uint64, error) { if count > uint64(maxTxInPerMessage) { msg := fmt.Sprintf("too many input transactions to fit into max "+ "message size [count %d, max %d]", count, maxTxInPerMessage) - return 0, messageError(op, ErrTooManyTxs, msg) + return 0, makeError(op, ErrTooManyTxs, msg) } // TxIns. @@ -636,7 +636,7 @@ func (msg *MsgTx) decodePrefix(r io.Reader, pver uint32) (uint64, error) { if count > uint64(maxTxOutPerMessage) { msg := fmt.Sprintf("too many output transactions to fit into "+ "max message size [count %d, max %d]", count, maxTxOutPerMessage) - return 0, messageError(op, ErrTooManyTxs, msg) + return 0, makeError(op, ErrTooManyTxs, msg) } // TxOuts. @@ -687,7 +687,7 @@ func (msg *MsgTx) decodeWitness(r io.Reader, pver uint32, isFull bool) (uint64, if count > uint64(maxTxInPerMessage) { str := fmt.Sprintf("too many input transactions to fit into "+ "max message size [count %d, max %d]", count, maxTxInPerMessage) - return 0, messageError(op, ErrTooManyTxs, str) + return 0, makeError(op, ErrTooManyTxs, str) } txIns := make([]TxIn, count) @@ -719,7 +719,7 @@ func (msg *MsgTx) decodeWitness(r io.Reader, pver uint32, isFull bool) (uint64, if int(count) != len(msg.TxIn) { msg := fmt.Sprintf("non equal witness and prefix txin quantities "+ "(witness %v, prefix %v)", count, len(msg.TxIn)) - return 0, messageError(op, ErrMismatchedWitnessCount, msg) + return 0, makeError(op, ErrMismatchedWitnessCount, msg) } // Prevent more input transactions than could possibly fit into a @@ -728,7 +728,7 @@ func (msg *MsgTx) decodeWitness(r io.Reader, pver uint32, isFull bool) (uint64, if count > uint64(maxTxInPerMessage) { msg := fmt.Sprintf("too many input transactions to fit into "+ "max message size [count %d, max %d]", count, maxTxInPerMessage) - return 0, messageError(op, ErrTooManyTxs, msg) + return 0, makeError(op, ErrTooManyTxs, msg) } // Read in the witnesses, and copy them into the already generated @@ -827,7 +827,7 @@ func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error { totalScriptSizeOuts, txSerType) default: - return messageError(op, ErrUnknownTxType, "unsupported transaction type") + return makeError(op, ErrUnknownTxType, "unsupported transaction type") } return nil @@ -948,7 +948,7 @@ func (msg *MsgTx) BtcEncode(w io.Writer, pver uint32) error { } default: - return messageError("MsgTx.BtcEncode", ErrUnknownTxType, + return makeError("MsgTx.BtcEncode", ErrUnknownTxType, "unsupported transaction type") } @@ -1156,7 +1156,7 @@ func WriteOutPoint(w io.Writer, pver uint32, version uint16, op *OutPoint) error // (TxIn) in the transaction prefix. func readTxInPrefix(r io.Reader, pver uint32, serType TxSerializeType, version uint16, ti *TxIn) error { if serType == TxSerializeOnlyWitness { - return messageError("readTxInPrefix", ErrReadInPrefixFromWitnessOnlyTx, + return makeError("readTxInPrefix", ErrReadInPrefixFromWitnessOnlyTx, "tried to read a prefix input for a witness only tx") } diff --git a/wire/msgtx_test.go b/wire/msgtx_test.go index 919e1b978e..ca621f1572 100644 --- a/wire/msgtx_test.go +++ b/wire/msgtx_test.go @@ -719,7 +719,7 @@ func TestTxOverflowErrors(t *testing.T) { 0x01, 0x00, 0x00, 0x00, // Version 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for number of input transactions - }, pver, txVer, &MessageError{}, + }, pver, txVer, Error{}, }, // Transaction that claims to have ~uint64(0) outputs. [1] @@ -729,7 +729,7 @@ func TestTxOverflowErrors(t *testing.T) { 0x00, // Varint for number of input transactions 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for number of output transactions - }, pver, txVer, &MessageError{}, + }, pver, txVer, Error{}, }, // Transaction that has an input with a signature script that [2] @@ -777,7 +777,7 @@ func TestTxOverflowErrors(t *testing.T) { 0x00, 0x00, 0x00, 0x00, // Expiry 0x01, // Varint for number of input signature 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for sig script length (overflows) - }, pver, txVer, &MessageError{}, + }, pver, txVer, Error{}, }, // Transaction that has an output with a public key script [3] @@ -798,7 +798,7 @@ func TestTxOverflowErrors(t *testing.T) { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for length of public key script - }, pver, txVer, &MessageError{}, + }, pver, txVer, Error{}, }, } diff --git a/wire/msgversion.go b/wire/msgversion.go index 9b71959a35..571ecd23d4 100644 --- a/wire/msgversion.go +++ b/wire/msgversion.go @@ -82,7 +82,7 @@ func (msg *MsgVersion) BtcDecode(r io.Reader, pver uint32) error { const op = "MsgVersion.BtcDecode" buf, ok := r.(*bytes.Buffer) if !ok { - return messageError(op, ErrInvalidMsg, "reader is not a *bytes.Buffer") + return makeError(op, ErrInvalidMsg, "reader is not a *bytes.Buffer") } err := readElements(buf, &msg.ProtocolVersion, &msg.Services, @@ -257,12 +257,12 @@ func validateUserAgent(userAgent string) error { if len(userAgent) > MaxUserAgentLen { msg := fmt.Sprintf("user agent too long [len %v, max %v]", len(userAgent), MaxUserAgentLen) - return messageError(op, ErrUserAgentTooLong, msg) + return makeError(op, ErrUserAgentTooLong, msg) } if !isStrictAscii(userAgent) { msg := "user agent is not strict ASCII" - return messageError(op, ErrMalformedStrictString, msg) + return makeError(op, ErrMalformedStrictString, msg) } return nil