Add new reject command.

This commit adds the new reject protocol message added to recent versions
of the reference implementation.  This message is intended to be used in
response to messages from a remote peer when it is rejected for some
reason such as blocks being rejected due to not conforming to the chain
rules, transactions double spending inputs, and version messages sent
after they're already sent.

This is work toward issue #9.
This commit is contained in:
Dave Collins 2013-11-11 23:12:07 -06:00
parent f8ec476691
commit c2a1444a88
4 changed files with 210 additions and 0 deletions

View file

@ -139,6 +139,15 @@ func readElement(r io.Reader, element interface{}) error {
} }
*e = BloomUpdateType(b[0]) *e = BloomUpdateType(b[0])
return nil return nil
case *RejectCode:
b := scratch[0:1]
_, err := io.ReadFull(r, b)
if err != nil {
return err
}
*e = RejectCode(b[0])
return nil
} }
// Fall back to the slower binary.Read if a fast path was not available // Fall back to the slower binary.Read if a fast path was not available
@ -280,6 +289,15 @@ func writeElement(w io.Writer, element interface{}) error {
return err return err
} }
return nil return nil
case RejectCode:
b := scratch[0:1]
b[0] = uint8(e)
_, err := w.Write(b)
if err != nil {
return err
}
return nil
} }
// Fall back to the slower binary.Write if a fast path was not available // Fall back to the slower binary.Write if a fast path was not available

View file

@ -46,6 +46,7 @@ const (
cmdFilterClear = "filterclear" cmdFilterClear = "filterclear"
cmdFilterLoad = "filterload" cmdFilterLoad = "filterload"
cmdMerkleBlock = "merkleblock" cmdMerkleBlock = "merkleblock"
cmdReject = "reject"
) )
// Message is an interface that describes a bitcoin message. A type that // Message is an interface that describes a bitcoin message. A type that
@ -124,6 +125,9 @@ func makeEmptyMessage(command string) (Message, error) {
case cmdMerkleBlock: case cmdMerkleBlock:
msg = &MsgMerkleBlock{} msg = &MsgMerkleBlock{}
case cmdReject:
msg = &MsgReject{}
default: default:
return nil, fmt.Errorf("unhandled command [%s]", command) return nil, fmt.Errorf("unhandled command [%s]", command)
} }

184
msgreject.go Normal file
View file

@ -0,0 +1,184 @@
// Copyright (c) 2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcwire
import (
"fmt"
"io"
)
// 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 bitcoin 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 ShaHash
}
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgReject) BtcDecode(r io.Reader, pver uint32) error {
if pver < RejectVersion {
str := fmt.Sprintf("reject message invalid for protocol "+
"version %d", pver)
return messageError("MsgReject.BtcDecode", str)
}
// Command that was rejected.
cmd, err := readVarString(r, pver)
if 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
}
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 bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgReject) BtcEncode(w io.Writer, pver uint32) error {
if pver < RejectVersion {
str := fmt.Sprintf("reject message invalid for protocol "+
"version %d", pver)
return messageError("MsgReject.BtcEncode", str)
}
// 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 {
plen := uint32(0)
// The reject message did not exist before protocol version
// RejectVersion.
if pver >= RejectVersion {
// 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
}
return plen
}
// NewMsgReject returns a new bitcoin 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,
}
}

View file

@ -45,6 +45,10 @@ const (
// bloom filtering related messages and extended the version message // bloom filtering related messages and extended the version message
// with a relay flag (pver >= BIP0037Version). // with a relay flag (pver >= BIP0037Version).
BIP0037Version uint32 = 70001 BIP0037Version uint32 = 70001
// RejectVersion is the protocol version which added a new reject
// message.
RejectVersion uint32 = 70002
) )
// ServiceFlag identifies services supported by a bitcoin peer. // ServiceFlag identifies services supported by a bitcoin peer.