dcrd/blockchain/stake/error.go
Dave Collins b6d426241d blockchain: Rework to use new db interface.
This commit is the first stage of several that are planned to convert
the blockchain package into a concurrent safe package that will
ultimately allow support for multi-peer download and concurrent chain
processing.  The goal is to update btcd proper after each step so it can
take advantage of the enhancements as they are developed.

In addition to the aforementioned benefit, this staged approach has been
chosen since it is absolutely critical to maintain consensus.
Separating the changes into several stages makes it easier for reviewers
to logically follow what is happening and therefore helps prevent
consensus bugs.  Naturally there are significant automated tests to help
prevent consensus issues as well.

The main focus of this stage is to convert the blockchain package to use
the new database interface and implement the chain-related functionality
which it no longer handles.  It also aims to improve efficiency in
various areas by making use of the new database and chain capabilities.

The following is an overview of the chain changes:

- Update to use the new database interface
- Add chain-related functionality that the old database used to handle
  - Main chain structure and state
  - Transaction spend tracking
- Implement a new pruned unspent transaction output (utxo) set
  - Provides efficient direct access to the unspent transaction outputs
  - Uses a domain specific compression algorithm that understands the
    standard transaction scripts in order to significantly compress them
  - Removes reliance on the transaction index and paves the way toward
    eventually enabling block pruning
- Modify the New function to accept a Config struct instead of
  inidividual parameters
- Replace the old TxStore type with a new UtxoViewpoint type that makes
  use of the new pruned utxo set
- Convert code to treat the new UtxoViewpoint as a rolling view that is
  used between connects and disconnects to improve efficiency
- Make best chain state always set when the chain instance is created
  - Remove now unnecessary logic for dealing with unset best state
- Make all exported functions concurrent safe
  - Currently using a single chain state lock as it provides a straight
    forward and easy to review path forward however this can be improved
    with more fine grained locking
- Optimize various cases where full blocks were being loaded when only
  the header is needed to help reduce the I/O load
- Add the ability for callers to get a snapshot of the current best
  chain stats in a concurrent safe fashion
  - Does not block callers while new blocks are being processed
- Make error messages that reference transaction outputs consistently
  use <transaction hash>:<output index>
- Introduce a new AssertError type an convert internal consistency
  checks to use it
- Update tests and examples to reflect the changes
- Add a full suite of tests to ensure correct functionality of the new
  code

The following is an overview of the btcd changes:

- Update to use the new database and chain interfaces
- Temporarily remove all code related to the transaction index
- Temporarily remove all code related to the address index
- Convert all code that uses transaction stores to use the new utxo
  view
- Rework several calls that required the block manager for safe
  concurrency to use the chain package directly now that it is
  concurrent safe
- Change all calls to obtain the best hash to use the new best state
  snapshot capability from the chain package
- Remove workaround for limits on fetching height ranges since the new
  database interface no longer imposes them
- Correct the gettxout RPC handler to return the best chain hash as
  opposed the hash the txout was found in
- Optimize various RPC handlers:
  - Change several of the RPC handlers to use the new chain snapshot
    capability to avoid needlessly loading data
  - Update several handlers to use new functionality to avoid accessing
    the block manager so they are able to return the data without
    blocking when the server is busy processing blocks
  - Update non-verbose getblock to avoid deserialization and
    serialization overhead
  - Update getblockheader to request the block height directly from
    chain and only load the header
  - Update getdifficulty to use the new cached data from chain
  - Update getmininginfo to use the new cached data from chain
  - Update non-verbose getrawtransaction to avoid deserialization and
    serialization overhead
  - Update gettxout to use the new utxo store versus loading
    full transactions using the transaction index

The following is an overview of the utility changes:
- Update addblock to use the new database and chain interfaces
- Update findcheckpoint to use the new database and chain interfaces
- Remove the dropafter utility which is no longer supported

NOTE: The transaction index and address index will be reimplemented in
another commit.
2016-08-18 15:42:18 -04:00

220 lines
7.7 KiB
Go

// Copyright (c) 2014 Conformal Systems LLC.
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package stake
import (
"fmt"
)
// ErrorCode identifies a kind of error.
type ErrorCode int
// These constants are used to identify a specific RuleError.
const (
// ErrSStxTooManyInputs indicates that a given SStx contains too many
// inputs.
ErrSStxTooManyInputs = iota
// ErrSStxTooManyOutputs indicates that a given SStx contains too many
// outputs.
ErrSStxTooManyOutputs
// ErrSStxNoOutputs indicates that a given SStx has no outputs.
ErrSStxNoOutputs
// ErrSStxInvalidInput indicates that an invalid output has been used as
// an input for a SStx; only non-SStx tagged outputs may be used to
// purchase stake tickets.
// TODO: Add this into validate
// Ensure that all inputs are not tagged SStx outputs of some sort,
// along with checks to make sure they exist and are available.
ErrSStxInvalidInputs
// ErrSStxInvalidOutput indicates that the output for an SStx tx is
// invalid; in particular, either the output was not tagged SStx or the
// OP_RETURNs were missing or contained invalid addresses.
ErrSStxInvalidOutputs
// ErrSStxInOutProportions indicates the the number of inputs in an SStx
// was not equal to the number of output minus one.
ErrSStxInOutProportions
// ErrSStxBadCommitAmount indicates that a ticket tried to commit 0 or
// a negative value as the commitment amount.
ErrSStxBadCommitAmount
// ErrSStxBadChangeAmts indicates that the change amount for some SStx
// was invalid, for instance spending more than its inputs.
ErrSStxBadChangeAmts
// ErrSStxVerifyCalcAmts indicates that passed calculated amounts failed
// to conform to the amounts found in the ticket.
ErrSStxVerifyCalcAmts
// ErrSSGenWrongNumInputs indicates that a given SSGen tx contains an
// invalid number of inputs.
ErrSSGenWrongNumInputs
// ErrSSGenTooManyOutputs indicates that a given SSGen tx contains too
// many outputs.
ErrSSGenTooManyOutputs
// ErrSSGenNoOutputs indicates that a given SSGen has no outputs.
ErrSSGenNoOutputs
// ErrSSGenWrongIndex indicates that a given SSGen sstx input was not
// using the correct index.
ErrSSGenWrongIndex
// ErrSSGenWrongTxTree indicates that a given SSGen tx input was not found in
// the stake tx tree.
ErrSSGenWrongTxTree
// ErrSSGenNoStakebase indicates that the SSGen tx did not contain a
// valid StakeBase in the zeroeth position of inputs.
ErrSSGenNoStakebase
// ErrSSGenNoReference indicates that there is no reference OP_RETURN
// included as the first output.
ErrSSGenNoReference
// ErrSSGenNoReference indicates that the OP_RETURN included as the
// first output was corrupted in some way.
ErrSSGenBadReference
// ErrSSGenNoVotePush indicates that there is no vote bits OP_RETURN
// included as the second output.
ErrSSGenNoVotePush
// ErrSSGenBadVotePush indicates that the OP_RETURN included as the
// second output was corrupted in some way.
ErrSSGenBadVotePush
// ErrSSGenBadGenOuts indicates that the something was wrong with the
// stake generation outputs that were present after the first two
// OP_RETURN pushes in an SSGen tx.
ErrSSGenBadGenOuts
// ErrSSRtxWrongNumInputs indicates that a given SSRtx contains an
// invalid number of inputs.
ErrSSRtxWrongNumInputs
// ErrSSRtxTooManyOutputs indicates that a given SSRtx contains too many
// outputs.
ErrSSRtxTooManyOutputs
// ErrSSRtxNoOutputs indicates that a given SSRtx has no outputs.
ErrSSRtxNoOutputs
// ErrSSRtxWrongTxTree indicates that a given SSRtx input was not found in
// the stake tx tree.
ErrSSRtxWrongTxTree
// ErrSSRtxBadGenOuts indicates that there was a non-SSRtx tagged output
// present in an SSRtx.
ErrSSRtxBadOuts
// ErrVerSStxAmts indicates there was an error verifying the calculated
// SStx out amounts and the actual SStx out amounts.
ErrVerSStxAmts
// ErrVerifyInput indicates that there was an error in verification
// function input.
ErrVerifyInput
// ErrVerifyOutType indicates that there was a non-equivalence in the
// output type.
ErrVerifyOutType
// ErrVerifyTooMuchFees indicates that a transaction's output gave
// too much in fees after taking into accounts the limits imposed
// by the SStx output's version field.
ErrVerifyTooMuchFees
// ErrVerifySpendTooMuch indicates that a transaction's output spent more
// than it was allowed to spend based on the calculated subsidy or return
// for a vote or revocation.
ErrVerifySpendTooMuch
// ErrVerifyOutputAmt indicates that for a vote/revocation spend output,
// the rule was given that it must exactly match the calculated maximum,
// however the amount in the output did not (e.g. it gave fees).
ErrVerifyOutputAmt
// ErrVerifyOutPkhs indicates that the recipient of the P2PKH or P2SH
// script was different from that indicated in the SStx input.
ErrVerifyOutPkhs
)
// Map of ErrorCode values back to their constant names for pretty printing.
var errorCodeStrings = map[ErrorCode]string{
ErrSStxTooManyInputs: "ErrSStxTooManyInputs",
ErrSStxTooManyOutputs: "ErrSStxTooManyOutputs",
ErrSStxNoOutputs: "ErrSStxNoOutputs",
ErrSStxInvalidInputs: "ErrSStxInvalidInputs",
ErrSStxInvalidOutputs: "ErrSStxInvalidOutputs",
ErrSStxInOutProportions: "ErrSStxInOutProportions",
ErrSStxBadCommitAmount: "ErrSStxBadCommitAmount",
ErrSStxBadChangeAmts: "ErrSStxBadChangeAmts",
ErrSStxVerifyCalcAmts: "ErrSStxVerifyCalcAmts",
ErrSSGenWrongNumInputs: "ErrSSGenWrongNumInputs",
ErrSSGenTooManyOutputs: "ErrSSGenTooManyOutputs",
ErrSSGenNoOutputs: "ErrSSGenNoOutputs",
ErrSSGenWrongIndex: "ErrSSGenWrongIndex",
ErrSSGenWrongTxTree: "ErrSSGenWrongTxTree",
ErrSSGenNoStakebase: "ErrSSGenNoStakebase",
ErrSSGenNoReference: "ErrSSGenNoReference",
ErrSSGenBadReference: "ErrSSGenBadReference",
ErrSSGenNoVotePush: "ErrSSGenNoVotePush",
ErrSSGenBadVotePush: "ErrSSGenBadVotePush",
ErrSSGenBadGenOuts: "ErrSSGenBadGenOuts",
ErrSSRtxWrongNumInputs: "ErrSSRtxWrongNumInputs",
ErrSSRtxTooManyOutputs: "ErrSSRtxTooManyOutputs",
ErrSSRtxNoOutputs: "ErrSSRtxNoOutputs",
ErrSSRtxWrongTxTree: "ErrSSRtxWrongTxTree",
ErrSSRtxBadOuts: "ErrSSRtxBadOuts",
ErrVerSStxAmts: "ErrVerSStxAmts",
ErrVerifyInput: "ErrVerifyInput",
ErrVerifyOutType: "ErrVerifyOutType",
ErrVerifyTooMuchFees: "ErrVerifyTooMuchFees",
ErrVerifySpendTooMuch: "ErrVerifySpendTooMuch",
ErrVerifyOutputAmt: "ErrVerifyOutputAmt",
ErrVerifyOutPkhs: "ErrVerifyOutPkhs",
}
// String returns the ErrorCode as a human-readable name.
func (e ErrorCode) String() string {
if s := errorCodeStrings[e]; s != "" {
return s
}
return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
}
// RuleError identifies a rule violation. It is used to indicate that
// processing of a block or 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 RuleError struct {
ErrorCode ErrorCode // Describes the kind of error
Description string // Human readable description of the issue
}
// Error satisfies the error interface and prints human-readable errors.
func (e RuleError) Error() string {
return e.Description
}
// GetCode satisfies the error interface and prints human-readable errors.
func (e RuleError) GetCode() ErrorCode {
return e.ErrorCode
}
// stakeRuleError creates an RuleError given a set of arguments.
func stakeRuleError(c ErrorCode, desc string) RuleError {
return RuleError{ErrorCode: c, Description: desc}
}