2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2014 The btcsuite developers
|
2014-07-09 05:01:13 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-30 23:25:42 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2014-07-09 05:01:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RuleError identifies a rule violation. It is used to indicate that
|
|
|
|
// processing of a transaction failed due to one of the many validation
|
|
|
|
// rules. The caller can use type assertions to determine if a failure was
|
|
|
|
// specifically due to a rule violation and use the Err field to access the
|
2015-01-30 23:25:42 +01:00
|
|
|
// underlying error, which will be either a TxRuleError or a
|
|
|
|
// blockchain.RuleError.
|
2014-07-09 05:01:13 +02:00
|
|
|
type RuleError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error satisfies the error interface and prints human-readable errors.
|
|
|
|
func (e RuleError) Error() string {
|
|
|
|
if e.Err == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxRuleError identifies a rule violation. It is used to indicate that
|
|
|
|
// processing of a transaction failed due to one of the many validation
|
|
|
|
// rules. The caller can use type assertions to determine if a failure was
|
|
|
|
// specifically due to a rule violation and access the ErrorCode field to
|
|
|
|
// ascertain the specific reason for the rule violation.
|
|
|
|
type TxRuleError struct {
|
2015-02-05 22:16:39 +01:00
|
|
|
RejectCode wire.RejectCode // The code to send with reject messages
|
|
|
|
Description string // Human readable description of the issue
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error satisfies the error interface and prints human-readable errors.
|
|
|
|
func (e TxRuleError) Error() string {
|
|
|
|
return e.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
// txRuleError creates an underlying TxRuleError with the given a set of
|
|
|
|
// arguments and returns a RuleError that encapsulates it.
|
2015-02-05 22:16:39 +01:00
|
|
|
func txRuleError(c wire.RejectCode, desc string) RuleError {
|
2014-07-09 05:01:13 +02:00
|
|
|
return RuleError{
|
|
|
|
Err: TxRuleError{RejectCode: c, Description: desc},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chainRuleError returns a RuleError that encapsulates the given
|
2015-01-30 23:25:42 +01:00
|
|
|
// blockchain.RuleError.
|
|
|
|
func chainRuleError(chainErr blockchain.RuleError) RuleError {
|
2014-07-09 05:01:13 +02:00
|
|
|
return RuleError{
|
|
|
|
Err: chainErr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractRejectCode attempts to return a relevant reject code for a given error
|
|
|
|
// by examining the error for known types. It will return true if a code
|
|
|
|
// was successfully extracted.
|
2015-02-05 22:16:39 +01:00
|
|
|
func extractRejectCode(err error) (wire.RejectCode, bool) {
|
2014-07-09 05:01:13 +02:00
|
|
|
// Pull the underlying error out of a RuleError.
|
|
|
|
if rerr, ok := err.(RuleError); ok {
|
|
|
|
err = rerr.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.RuleError:
|
2014-07-09 05:01:13 +02:00
|
|
|
// Convert the chain error to a reject code.
|
2015-02-05 22:16:39 +01:00
|
|
|
var code wire.RejectCode
|
2014-07-09 05:01:13 +02:00
|
|
|
switch err.ErrorCode {
|
|
|
|
// Rejected due to duplicate.
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.ErrDuplicateBlock:
|
2014-07-09 05:01:13 +02:00
|
|
|
fallthrough
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.ErrDoubleSpend:
|
2015-02-05 22:16:39 +01:00
|
|
|
code = wire.RejectDuplicate
|
2014-07-09 05:01:13 +02:00
|
|
|
|
|
|
|
// Rejected due to obsolete version.
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.ErrBlockVersionTooOld:
|
2015-02-05 22:16:39 +01:00
|
|
|
code = wire.RejectObsolete
|
2014-07-09 05:01:13 +02:00
|
|
|
|
|
|
|
// Rejected due to checkpoint.
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.ErrCheckpointTimeTooOld:
|
2014-07-09 05:01:13 +02:00
|
|
|
fallthrough
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.ErrDifficultyTooLow:
|
2014-07-09 05:01:13 +02:00
|
|
|
fallthrough
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.ErrBadCheckpoint:
|
2014-07-09 05:01:13 +02:00
|
|
|
fallthrough
|
2015-01-30 23:25:42 +01:00
|
|
|
case blockchain.ErrForkTooOld:
|
2015-02-05 22:16:39 +01:00
|
|
|
code = wire.RejectCheckpoint
|
2014-07-09 05:01:13 +02:00
|
|
|
|
|
|
|
// Everything else is due to the block or transaction being invalid.
|
|
|
|
default:
|
2015-02-05 22:16:39 +01:00
|
|
|
code = wire.RejectInvalid
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return code, true
|
|
|
|
|
|
|
|
case TxRuleError:
|
|
|
|
return err.RejectCode, true
|
|
|
|
|
|
|
|
case nil:
|
2015-02-05 22:16:39 +01:00
|
|
|
return wire.RejectInvalid, false
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
return wire.RejectInvalid, false
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// errToRejectErr examines the underlying type of the error and returns a reject
|
2015-02-05 22:16:39 +01:00
|
|
|
// code and string appropriate to be sent in a wire.MsgReject message.
|
|
|
|
func errToRejectErr(err error) (wire.RejectCode, string) {
|
2014-07-09 05:01:13 +02:00
|
|
|
// Return the reject code along with the error text if it can be
|
|
|
|
// extracted from the error.
|
|
|
|
rejectCode, found := extractRejectCode(err)
|
|
|
|
if found {
|
|
|
|
return rejectCode, err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a generic rejected string if there is no error. This really
|
|
|
|
// should not happen unless the code elsewhere is not setting an error
|
|
|
|
// as it should be, but it's best to be safe and simply return a generic
|
2016-02-25 18:17:12 +01:00
|
|
|
// string rather than allowing the following code that dereferences the
|
2014-07-09 05:01:13 +02:00
|
|
|
// err to panic.
|
|
|
|
if err == nil {
|
2015-02-05 22:16:39 +01:00
|
|
|
return wire.RejectInvalid, "rejected"
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// When the underlying error is not one of the above cases, just return
|
2015-02-05 22:16:39 +01:00
|
|
|
// wire.RejectInvalid with a generic rejected string plus the error
|
2014-07-09 05:01:13 +02:00
|
|
|
// text.
|
2015-02-05 22:16:39 +01:00
|
|
|
return wire.RejectInvalid, "rejected: " + err.Error()
|
2014-07-09 05:01:13 +02:00
|
|
|
}
|