github.com/palcoin-project/palcd@v1.0.0/blockchain/error.go (about)

     1  // Copyright (c) 2014-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package blockchain
     6  
     7  import (
     8  	"fmt"
     9  )
    10  
    11  // DeploymentError identifies an error that indicates a deployment ID was
    12  // specified that does not exist.
    13  type DeploymentError uint32
    14  
    15  // Error returns the assertion error as a human-readable string and satisfies
    16  // the error interface.
    17  func (e DeploymentError) Error() string {
    18  	return fmt.Sprintf("deployment ID %d does not exist", uint32(e))
    19  }
    20  
    21  // AssertError identifies an error that indicates an internal code consistency
    22  // issue and should be treated as a critical and unrecoverable error.
    23  type AssertError string
    24  
    25  // Error returns the assertion error as a human-readable string and satisfies
    26  // the error interface.
    27  func (e AssertError) Error() string {
    28  	return "assertion failed: " + string(e)
    29  }
    30  
    31  // ErrorCode identifies a kind of error.
    32  type ErrorCode int
    33  
    34  // These constants are used to identify a specific RuleError.
    35  const (
    36  	// ErrDuplicateBlock indicates a block with the same hash already
    37  	// exists.
    38  	ErrDuplicateBlock ErrorCode = iota
    39  
    40  	// ErrBlockTooBig indicates the serialized block size exceeds the
    41  	// maximum allowed size.
    42  	ErrBlockTooBig
    43  
    44  	// ErrBlockWeightTooHigh indicates that the block's computed weight
    45  	// metric exceeds the maximum allowed value.
    46  	ErrBlockWeightTooHigh
    47  
    48  	// ErrBlockVersionTooOld indicates the block version is too old and is
    49  	// no longer accepted since the majority of the network has upgraded
    50  	// to a newer version.
    51  	ErrBlockVersionTooOld
    52  
    53  	// ErrInvalidTime indicates the time in the passed block has a precision
    54  	// that is more than one second.  The chain consensus rules require
    55  	// timestamps to have a maximum precision of one second.
    56  	ErrInvalidTime
    57  
    58  	// ErrTimeTooOld indicates the time is either before the median time of
    59  	// the last several blocks per the chain consensus rules or prior to the
    60  	// most recent checkpoint.
    61  	ErrTimeTooOld
    62  
    63  	// ErrTimeTooNew indicates the time is too far in the future as compared
    64  	// the current time.
    65  	ErrTimeTooNew
    66  
    67  	// ErrDifficultyTooLow indicates the difficulty for the block is lower
    68  	// than the difficulty required by the most recent checkpoint.
    69  	ErrDifficultyTooLow
    70  
    71  	// ErrUnexpectedDifficulty indicates specified bits do not align with
    72  	// the expected value either because it doesn't match the calculated
    73  	// valued based on difficulty regarted rules or it is out of the valid
    74  	// range.
    75  	ErrUnexpectedDifficulty
    76  
    77  	// ErrHighHash indicates the block does not hash to a value which is
    78  	// lower than the required target difficultly.
    79  	ErrHighHash
    80  
    81  	// ErrBadMerkleRoot indicates the calculated merkle root does not match
    82  	// the expected value.
    83  	ErrBadMerkleRoot
    84  
    85  	// ErrBadCheckpoint indicates a block that is expected to be at a
    86  	// checkpoint height does not match the expected one.
    87  	ErrBadCheckpoint
    88  
    89  	// ErrForkTooOld indicates a block is attempting to fork the block chain
    90  	// before the most recent checkpoint.
    91  	ErrForkTooOld
    92  
    93  	// ErrCheckpointTimeTooOld indicates a block has a timestamp before the
    94  	// most recent checkpoint.
    95  	ErrCheckpointTimeTooOld
    96  
    97  	// ErrNoTransactions indicates the block does not have a least one
    98  	// transaction.  A valid block must have at least the coinbase
    99  	// transaction.
   100  	ErrNoTransactions
   101  
   102  	// ErrNoTxInputs indicates a transaction does not have any inputs.  A
   103  	// valid transaction must have at least one input.
   104  	ErrNoTxInputs
   105  
   106  	// ErrNoTxOutputs indicates a transaction does not have any outputs.  A
   107  	// valid transaction must have at least one output.
   108  	ErrNoTxOutputs
   109  
   110  	// ErrTxTooBig indicates a transaction exceeds the maximum allowed size
   111  	// when serialized.
   112  	ErrTxTooBig
   113  
   114  	// ErrBadTxOutValue indicates an output value for a transaction is
   115  	// invalid in some way such as being out of range.
   116  	ErrBadTxOutValue
   117  
   118  	// ErrDuplicateTxInputs indicates a transaction references the same
   119  	// input more than once.
   120  	ErrDuplicateTxInputs
   121  
   122  	// ErrBadTxInput indicates a transaction input is invalid in some way
   123  	// such as referencing a previous transaction outpoint which is out of
   124  	// range or not referencing one at all.
   125  	ErrBadTxInput
   126  
   127  	// ErrMissingTxOut indicates a transaction output referenced by an input
   128  	// either does not exist or has already been spent.
   129  	ErrMissingTxOut
   130  
   131  	// ErrUnfinalizedTx indicates a transaction has not been finalized.
   132  	// A valid block may only contain finalized transactions.
   133  	ErrUnfinalizedTx
   134  
   135  	// ErrDuplicateTx indicates a block contains an identical transaction
   136  	// (or at least two transactions which hash to the same value).  A
   137  	// valid block may only contain unique transactions.
   138  	ErrDuplicateTx
   139  
   140  	// ErrOverwriteTx indicates a block contains a transaction that has
   141  	// the same hash as a previous transaction which has not been fully
   142  	// spent.
   143  	ErrOverwriteTx
   144  
   145  	// ErrImmatureSpend indicates a transaction is attempting to spend a
   146  	// coinbase that has not yet reached the required maturity.
   147  	ErrImmatureSpend
   148  
   149  	// ErrSpendTooHigh indicates a transaction is attempting to spend more
   150  	// value than the sum of all of its inputs.
   151  	ErrSpendTooHigh
   152  
   153  	// ErrBadFees indicates the total fees for a block are invalid due to
   154  	// exceeding the maximum possible value.
   155  	ErrBadFees
   156  
   157  	// ErrTooManySigOps indicates the total number of signature operations
   158  	// for a transaction or block exceed the maximum allowed limits.
   159  	ErrTooManySigOps
   160  
   161  	// ErrFirstTxNotCoinbase indicates the first transaction in a block
   162  	// is not a coinbase transaction.
   163  	ErrFirstTxNotCoinbase
   164  
   165  	// ErrMultipleCoinbases indicates a block contains more than one
   166  	// coinbase transaction.
   167  	ErrMultipleCoinbases
   168  
   169  	// ErrBadCoinbaseScriptLen indicates the length of the signature script
   170  	// for a coinbase transaction is not within the valid range.
   171  	ErrBadCoinbaseScriptLen
   172  
   173  	// ErrBadCoinbaseValue indicates the amount of a coinbase value does
   174  	// not match the expected value of the subsidy plus the sum of all fees.
   175  	ErrBadCoinbaseValue
   176  
   177  	// ErrMissingCoinbaseHeight indicates the coinbase transaction for a
   178  	// block does not start with the serialized block block height as
   179  	// required for version 2 and higher blocks.
   180  	ErrMissingCoinbaseHeight
   181  
   182  	// ErrBadCoinbaseHeight indicates the serialized block height in the
   183  	// coinbase transaction for version 2 and higher blocks does not match
   184  	// the expected value.
   185  	ErrBadCoinbaseHeight
   186  
   187  	// ErrScriptMalformed indicates a transaction script is malformed in
   188  	// some way.  For example, it might be longer than the maximum allowed
   189  	// length or fail to parse.
   190  	ErrScriptMalformed
   191  
   192  	// ErrScriptValidation indicates the result of executing transaction
   193  	// script failed.  The error covers any failure when executing scripts
   194  	// such signature verification failures and execution past the end of
   195  	// the stack.
   196  	ErrScriptValidation
   197  
   198  	// ErrUnexpectedWitness indicates that a block includes transactions
   199  	// with witness data, but doesn't also have a witness commitment within
   200  	// the coinbase transaction.
   201  	ErrUnexpectedWitness
   202  
   203  	// ErrInvalidWitnessCommitment indicates that a block's witness
   204  	// commitment is not well formed.
   205  	ErrInvalidWitnessCommitment
   206  
   207  	// ErrWitnessCommitmentMismatch indicates that the witness commitment
   208  	// included in the block's coinbase transaction doesn't match the
   209  	// manually computed witness commitment.
   210  	ErrWitnessCommitmentMismatch
   211  
   212  	// ErrPreviousBlockUnknown indicates that the previous block is not known.
   213  	ErrPreviousBlockUnknown
   214  
   215  	// ErrInvalidAncestorBlock indicates that an ancestor of this block has
   216  	// already failed validation.
   217  	ErrInvalidAncestorBlock
   218  
   219  	// ErrPrevBlockNotBest indicates that the block's previous block is not the
   220  	// current chain tip. This is not a block validation rule, but is required
   221  	// for block proposals submitted via getblocktemplate RPC.
   222  	ErrPrevBlockNotBest
   223  )
   224  
   225  // Map of ErrorCode values back to their constant names for pretty printing.
   226  var errorCodeStrings = map[ErrorCode]string{
   227  	ErrDuplicateBlock:            "ErrDuplicateBlock",
   228  	ErrBlockTooBig:               "ErrBlockTooBig",
   229  	ErrBlockVersionTooOld:        "ErrBlockVersionTooOld",
   230  	ErrBlockWeightTooHigh:        "ErrBlockWeightTooHigh",
   231  	ErrInvalidTime:               "ErrInvalidTime",
   232  	ErrTimeTooOld:                "ErrTimeTooOld",
   233  	ErrTimeTooNew:                "ErrTimeTooNew",
   234  	ErrDifficultyTooLow:          "ErrDifficultyTooLow",
   235  	ErrUnexpectedDifficulty:      "ErrUnexpectedDifficulty",
   236  	ErrHighHash:                  "ErrHighHash",
   237  	ErrBadMerkleRoot:             "ErrBadMerkleRoot",
   238  	ErrBadCheckpoint:             "ErrBadCheckpoint",
   239  	ErrForkTooOld:                "ErrForkTooOld",
   240  	ErrCheckpointTimeTooOld:      "ErrCheckpointTimeTooOld",
   241  	ErrNoTransactions:            "ErrNoTransactions",
   242  	ErrNoTxInputs:                "ErrNoTxInputs",
   243  	ErrNoTxOutputs:               "ErrNoTxOutputs",
   244  	ErrTxTooBig:                  "ErrTxTooBig",
   245  	ErrBadTxOutValue:             "ErrBadTxOutValue",
   246  	ErrDuplicateTxInputs:         "ErrDuplicateTxInputs",
   247  	ErrBadTxInput:                "ErrBadTxInput",
   248  	ErrMissingTxOut:              "ErrMissingTxOut",
   249  	ErrUnfinalizedTx:             "ErrUnfinalizedTx",
   250  	ErrDuplicateTx:               "ErrDuplicateTx",
   251  	ErrOverwriteTx:               "ErrOverwriteTx",
   252  	ErrImmatureSpend:             "ErrImmatureSpend",
   253  	ErrSpendTooHigh:              "ErrSpendTooHigh",
   254  	ErrBadFees:                   "ErrBadFees",
   255  	ErrTooManySigOps:             "ErrTooManySigOps",
   256  	ErrFirstTxNotCoinbase:        "ErrFirstTxNotCoinbase",
   257  	ErrMultipleCoinbases:         "ErrMultipleCoinbases",
   258  	ErrBadCoinbaseScriptLen:      "ErrBadCoinbaseScriptLen",
   259  	ErrBadCoinbaseValue:          "ErrBadCoinbaseValue",
   260  	ErrMissingCoinbaseHeight:     "ErrMissingCoinbaseHeight",
   261  	ErrBadCoinbaseHeight:         "ErrBadCoinbaseHeight",
   262  	ErrScriptMalformed:           "ErrScriptMalformed",
   263  	ErrScriptValidation:          "ErrScriptValidation",
   264  	ErrUnexpectedWitness:         "ErrUnexpectedWitness",
   265  	ErrInvalidWitnessCommitment:  "ErrInvalidWitnessCommitment",
   266  	ErrWitnessCommitmentMismatch: "ErrWitnessCommitmentMismatch",
   267  	ErrPreviousBlockUnknown:      "ErrPreviousBlockUnknown",
   268  	ErrInvalidAncestorBlock:      "ErrInvalidAncestorBlock",
   269  	ErrPrevBlockNotBest:          "ErrPrevBlockNotBest",
   270  }
   271  
   272  // String returns the ErrorCode as a human-readable name.
   273  func (e ErrorCode) String() string {
   274  	if s := errorCodeStrings[e]; s != "" {
   275  		return s
   276  	}
   277  	return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
   278  }
   279  
   280  // RuleError identifies a rule violation.  It is used to indicate that
   281  // processing of a block or transaction failed due to one of the many validation
   282  // rules.  The caller can use type assertions to determine if a failure was
   283  // specifically due to a rule violation and access the ErrorCode field to
   284  // ascertain the specific reason for the rule violation.
   285  type RuleError struct {
   286  	ErrorCode   ErrorCode // Describes the kind of error
   287  	Description string    // Human readable description of the issue
   288  }
   289  
   290  // Error satisfies the error interface and prints human-readable errors.
   291  func (e RuleError) Error() string {
   292  	return e.Description
   293  }
   294  
   295  // ruleError creates an RuleError given a set of arguments.
   296  func ruleError(c ErrorCode, desc string) RuleError {
   297  	return RuleError{ErrorCode: c, Description: desc}
   298  }