dcrd/wire/msgreject.go
Dave Collins c1722eb7b2
wire: Improve error handling.
This updates the error handling in the wire package to be more
consistent with the rest of the code base, tightens some of the error
handling in message parsing to harden the protocol, and adds tests to
ensure the error implementations works correctly with the standard
library errors.Is and errors.As functions introduced in Go 1.13.

In particular, the error code semantics have been changed so the first
error starts at 0 instead of treating 0 is a sentinel "no error" value
because it goes against Go best practices which designed the errors
specifically to avoid the use of sentinel values.  Further, the use of 0
as a sentinel means that there are two different ways to indicate "no
error", namely nil and an error code of zero.  Thus callers have to know
that they need to check for both conditions which is incredibly error
prone.  In fact, there is already at least one instance in the code base
that has that mistake in the tests.

Finally, while here, this hardens the protocol by enforcing more strict
parsing requirements lower in the software stack instead of relying on
upper layers to take care of it.
2020-05-01 20:24:15 -05:00

206 lines
5.8 KiB
Go

// Copyright (c) 2014-2016 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"fmt"
"io"
"github.com/decred/dcrd/chaincfg/chainhash"
)
// RejectCode represents a numeric value by which a remote peer indicates
// why a message was rejected.
type RejectCode uint8
// These constants define the various supported reject codes.
const (
RejectMalformed RejectCode = 0x01
RejectInvalid RejectCode = 0x10
RejectObsolete RejectCode = 0x11
RejectDuplicate RejectCode = 0x12
RejectNonstandard RejectCode = 0x40
RejectDust RejectCode = 0x41
RejectInsufficientFee RejectCode = 0x42
RejectCheckpoint RejectCode = 0x43
)
// Map of reject codes back strings for pretty printing.
var rejectCodeStrings = map[RejectCode]string{
RejectMalformed: "REJECT_MALFORMED",
RejectInvalid: "REJECT_INVALID",
RejectObsolete: "REJECT_OBSOLETE",
RejectDuplicate: "REJECT_DUPLICATE",
RejectNonstandard: "REJECT_NONSTANDARD",
RejectDust: "REJECT_DUST",
RejectInsufficientFee: "REJECT_INSUFFICIENTFEE",
RejectCheckpoint: "REJECT_CHECKPOINT",
}
// String returns the RejectCode in human-readable form.
func (code RejectCode) String() string {
if s, ok := rejectCodeStrings[code]; ok {
return s
}
return fmt.Sprintf("Unknown RejectCode (%d)", uint8(code))
}
// MsgReject implements the Message interface and represents a Decred reject
// message.
//
// This message was not added until protocol version RejectVersion.
type MsgReject struct {
// Cmd is the command for the message which was rejected such as
// as CmdBlock or CmdTx. This can be obtained from the Command function
// of a Message.
Cmd string
// RejectCode is a code indicating why the command was rejected. It
// is encoded as a uint8 on the wire.
Code RejectCode
// Reason is a human-readable string with specific details (over and
// above the reject code) about why the command was rejected.
Reason string
// Hash identifies a specific block or transaction that was rejected
// and therefore only applies the MsgBlock and MsgTx messages.
Hash chainhash.Hash
}
// validateRejectCommand ensures the provided reject command conforms to the
// results imposed by the protocol.
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 nil
}
// validateRejectReason ensures the provided reject reason conforms to the
// results imposed by the protocol.
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 nil
}
// BtcDecode decodes r using the Decred protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgReject) BtcDecode(r io.Reader, pver uint32) error {
// Command that was rejected.
cmd, err := ReadVarString(r, pver)
if err != nil {
return err
}
if err := validateRejectCommand(cmd); err != nil {
return err
}
msg.Cmd = cmd
// Code indicating why the command was rejected.
err = readElement(r, &msg.Code)
if err != nil {
return err
}
// Human readable string with specific details (over and above the
// reject code above) about why the command was rejected.
reason, err := ReadVarString(r, pver)
if err != nil {
return err
}
if err := validateRejectReason(reason); err != nil {
return err
}
msg.Reason = reason
// CmdBlock and CmdTx messages have an additional hash field that
// identifies the specific block or transaction.
if msg.Cmd == CmdBlock || msg.Cmd == CmdTx {
err := readElement(r, &msg.Hash)
if err != nil {
return err
}
}
return nil
}
// BtcEncode encodes the receiver to w using the Decred protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgReject) BtcEncode(w io.Writer, pver uint32) error {
if err := validateRejectCommand(msg.Cmd); err != nil {
return err
}
if err := validateRejectReason(msg.Reason); err != nil {
return err
}
// Command that was rejected.
err := WriteVarString(w, pver, msg.Cmd)
if err != nil {
return err
}
// Code indicating why the command was rejected.
err = writeElement(w, msg.Code)
if err != nil {
return err
}
// Human readable string with specific details (over and above the
// reject code above) about why the command was rejected.
err = WriteVarString(w, pver, msg.Reason)
if err != nil {
return err
}
// CmdBlock and CmdTx messages have an additional hash field that
// identifies the specific block or transaction.
if msg.Cmd == CmdBlock || msg.Cmd == CmdTx {
err := writeElement(w, &msg.Hash)
if err != nil {
return err
}
}
return nil
}
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgReject) Command() string {
return CmdReject
}
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgReject) MaxPayloadLength(pver uint32) uint32 {
// Unfortunately the Decred protocol does not enforce a sane
// limit on the length of the reason, so the max payload is the
// overall maximum message payload.
return uint32(MaxMessagePayload)
}
// NewMsgReject returns a new Decred reject message that conforms to the
// Message interface. See MsgReject for details.
func NewMsgReject(command string, code RejectCode, reason string) *MsgReject {
return &MsgReject{
Cmd: command,
Code: code,
Reason: reason,
}
}