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