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