OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / blockchain / error.go
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         // ErrTooManyTransactions indicates the block has more transactions than
103         // are allowed.
104         ErrTooManyTransactions
105
106         // ErrNoTxInputs indicates a transaction does not have any inputs.  A
107         // valid transaction must have at least one input.
108         ErrNoTxInputs
109
110         // ErrNoTxOutputs indicates a transaction does not have any outputs.  A
111         // valid transaction must have at least one output.
112         ErrNoTxOutputs
113
114         // ErrTxTooBig indicates a transaction exceeds the maximum allowed size
115         // when serialized.
116         ErrTxTooBig
117
118         // ErrBadTxOutValue indicates an output value for a transaction is
119         // invalid in some way such as being out of range.
120         ErrBadTxOutValue
121
122         // ErrDuplicateTxInputs indicates a transaction references the same
123         // input more than once.
124         ErrDuplicateTxInputs
125
126         // ErrBadTxInput indicates a transaction input is invalid in some way
127         // such as referencing a previous transaction outpoint which is out of
128         // range or not referencing one at all.
129         ErrBadTxInput
130
131         // ErrMissingTxOut indicates a transaction output referenced by an input
132         // either does not exist or has already been spent.
133         ErrMissingTxOut
134
135         // ErrUnfinalizedTx indicates a transaction has not been finalized.
136         // A valid block may only contain finalized transactions.
137         ErrUnfinalizedTx
138
139         // ErrDuplicateTx indicates a block contains an identical transaction
140         // (or at least two transactions which hash to the same value).  A
141         // valid block may only contain unique transactions.
142         ErrDuplicateTx
143
144         // ErrOverwriteTx indicates a block contains a transaction that has
145         // the same hash as a previous transaction which has not been fully
146         // spent.
147         ErrOverwriteTx
148
149         // ErrImmatureSpend indicates a transaction is attempting to spend a
150         // coinbase that has not yet reached the required maturity.
151         ErrImmatureSpend
152
153         // ErrSpendTooHigh indicates a transaction is attempting to spend more
154         // value than the sum of all of its inputs.
155         ErrSpendTooHigh
156
157         // ErrBadFees indicates the total fees for a block are invalid due to
158         // exceeding the maximum possible value.
159         ErrBadFees
160
161         // ErrTooManySigOps indicates the total number of signature operations
162         // for a transaction or block exceed the maximum allowed limits.
163         ErrTooManySigOps
164
165         // ErrFirstTxNotCoinbase indicates the first transaction in a block
166         // is not a coinbase transaction.
167         ErrFirstTxNotCoinbase
168
169         // ErrMultipleCoinbases indicates a block contains more than one
170         // coinbase transaction.
171         ErrMultipleCoinbases
172
173         // ErrBadCoinbaseScriptLen indicates the length of the signature script
174         // for a coinbase transaction is not within the valid range.
175         ErrBadCoinbaseScriptLen
176
177         // ErrBadCoinbaseValue indicates the amount of a coinbase value does
178         // not match the expected value of the subsidy plus the sum of all fees.
179         ErrBadCoinbaseValue
180
181         // ErrMissingCoinbaseHeight indicates the coinbase transaction for a
182         // block does not start with the serialized block block height as
183         // required for version 2 and higher blocks.
184         ErrMissingCoinbaseHeight
185
186         // ErrBadCoinbaseHeight indicates the serialized block height in the
187         // coinbase transaction for version 2 and higher blocks does not match
188         // the expected value.
189         ErrBadCoinbaseHeight
190
191         // ErrScriptMalformed indicates a transaction script is malformed in
192         // some way.  For example, it might be longer than the maximum allowed
193         // length or fail to parse.
194         ErrScriptMalformed
195
196         // ErrScriptValidation indicates the result of executing transaction
197         // script failed.  The error covers any failure when executing scripts
198         // such signature verification failures and execution past the end of
199         // the stack.
200         ErrScriptValidation
201
202         // ErrUnexpectedWitness indicates that a block includes transactions
203         // with witness data, but doesn't also have a witness commitment within
204         // the coinbase transaction.
205         ErrUnexpectedWitness
206
207         // ErrInvalidWitnessCommitment indicates that a block's witness
208         // commitment is not well formed.
209         ErrInvalidWitnessCommitment
210
211         // ErrWitnessCommitmentMismatch indicates that the witness commitment
212         // included in the block's coinbase transaction doesn't match the
213         // manually computed witness commitment.
214         ErrWitnessCommitmentMismatch
215 )
216
217 // Map of ErrorCode values back to their constant names for pretty printing.
218 var errorCodeStrings = map[ErrorCode]string{
219         ErrDuplicateBlock:            "ErrDuplicateBlock",
220         ErrBlockTooBig:               "ErrBlockTooBig",
221         ErrBlockVersionTooOld:        "ErrBlockVersionTooOld",
222         ErrBlockWeightTooHigh:        "ErrBlockWeightTooHigh",
223         ErrInvalidTime:               "ErrInvalidTime",
224         ErrTimeTooOld:                "ErrTimeTooOld",
225         ErrTimeTooNew:                "ErrTimeTooNew",
226         ErrDifficultyTooLow:          "ErrDifficultyTooLow",
227         ErrUnexpectedDifficulty:      "ErrUnexpectedDifficulty",
228         ErrHighHash:                  "ErrHighHash",
229         ErrBadMerkleRoot:             "ErrBadMerkleRoot",
230         ErrBadCheckpoint:             "ErrBadCheckpoint",
231         ErrForkTooOld:                "ErrForkTooOld",
232         ErrCheckpointTimeTooOld:      "ErrCheckpointTimeTooOld",
233         ErrNoTransactions:            "ErrNoTransactions",
234         ErrTooManyTransactions:       "ErrTooManyTransactions",
235         ErrNoTxInputs:                "ErrNoTxInputs",
236         ErrNoTxOutputs:               "ErrNoTxOutputs",
237         ErrTxTooBig:                  "ErrTxTooBig",
238         ErrBadTxOutValue:             "ErrBadTxOutValue",
239         ErrDuplicateTxInputs:         "ErrDuplicateTxInputs",
240         ErrBadTxInput:                "ErrBadTxInput",
241         ErrMissingTxOut:              "ErrMissingTxOut",
242         ErrUnfinalizedTx:             "ErrUnfinalizedTx",
243         ErrDuplicateTx:               "ErrDuplicateTx",
244         ErrOverwriteTx:               "ErrOverwriteTx",
245         ErrImmatureSpend:             "ErrImmatureSpend",
246         ErrSpendTooHigh:              "ErrSpendTooHigh",
247         ErrBadFees:                   "ErrBadFees",
248         ErrTooManySigOps:             "ErrTooManySigOps",
249         ErrFirstTxNotCoinbase:        "ErrFirstTxNotCoinbase",
250         ErrMultipleCoinbases:         "ErrMultipleCoinbases",
251         ErrBadCoinbaseScriptLen:      "ErrBadCoinbaseScriptLen",
252         ErrBadCoinbaseValue:          "ErrBadCoinbaseValue",
253         ErrMissingCoinbaseHeight:     "ErrMissingCoinbaseHeight",
254         ErrBadCoinbaseHeight:         "ErrBadCoinbaseHeight",
255         ErrScriptMalformed:           "ErrScriptMalformed",
256         ErrScriptValidation:          "ErrScriptValidation",
257         ErrUnexpectedWitness:         "ErrUnexpectedWitness",
258         ErrInvalidWitnessCommitment:  "ErrInvalidWitnessCommitment",
259         ErrWitnessCommitmentMismatch: "ErrWitnessCommitmentMismatch",
260 }
261
262 // String returns the ErrorCode as a human-readable name.
263 func (e ErrorCode) String() string {
264         if s := errorCodeStrings[e]; s != "" {
265                 return s
266         }
267         return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
268 }
269
270 // RuleError identifies a rule violation.  It is used to indicate that
271 // processing of a block or transaction failed due to one of the many validation
272 // rules.  The caller can use type assertions to determine if a failure was
273 // specifically due to a rule violation and access the ErrorCode field to
274 // ascertain the specific reason for the rule violation.
275 type RuleError struct {
276         ErrorCode   ErrorCode // Describes the kind of error
277         Description string    // Human readable description of the issue
278 }
279
280 // Error satisfies the error interface and prints human-readable errors.
281 func (e RuleError) Error() string {
282         return e.Description
283 }
284
285 // ruleError creates an RuleError given a set of arguments.
286 func ruleError(c ErrorCode, desc string) RuleError {
287         return RuleError{ErrorCode: c, Description: desc}
288 }