Skip to content

Commit

Permalink
multi: update wire error types.
Browse files Browse the repository at this point in the history
This updates the wire error types to leverage go
1.13 errors.Is/As functionality as well as confirm to
the error infrastructure best practices.
  • Loading branch information
dnldd committed Jul 16, 2020
1 parent 5616620 commit 5b338c4
Show file tree
Hide file tree
Showing 35 changed files with 205 additions and 304 deletions.
2 changes: 1 addition & 1 deletion blockchain/fullblocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions blockchain/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 0 additions & 2 deletions blockchain/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
10 changes: 5 additions & 5 deletions wire/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions wire/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion wire/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 5b338c4

Please sign in to comment.