dcrd/internal/mining/error.go
Dave Collins 6ea800f516
mining: Move to internal.
This makes the mining package an internal package such that it will no
longer be an exported module.  The only place its functionality is
really needed is for the internal implementation of dcrd itself and thus
having an unnecessary exported module significantly increases the
maintenance burden.

This is part of the overall effort to reduce the total number of modules
and eventually get to the point it will be possible to follow semver for
the root module.

It is worth noting that there are a few constants, which will be listed
below, that were exported from this module that callers might have
previously relied upon.  However, due to previous discussions about the
goal of removing the module, a code search has revealed that there are
no known callers that still rely on them.

The aforementioned constants are:

- UnminedHeight
- MinHighPriority

Overview of the major changes:

- Move the following files from mining -> internal/mining:
  - README.md
  - cpuminer.go
  - doc.go
  - miningerror.go -> error.go
  - log.go
  - mining.go
  - mining_test.go
  - policy.go
  - policy_test.go
- Remove mining/go.mod and mining/go.sum
- Make the README.md and doc.go files match the new reality
- Update all import paths in the repository accordingly
- Remove the dependency from the root go.mod
- Run go mod tidy on all modules
2020-07-20 18:07:59 -05:00

98 lines
3.3 KiB
Go

// Copyright (c) 2015-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package mining
import (
"fmt"
)
// MiningErrorCode identifies a kind of error.
type MiningErrorCode int
// These constants are used to identify a specific RuleError.
const (
// ErrNotEnoughVoters indicates that there were not enough voters to
// build a block on top of HEAD.
ErrNotEnoughVoters MiningErrorCode = iota
// ErrFailedToGetGeneration specifies that the current generation for
// a block could not be obtained from blockchain.
ErrFailedToGetGeneration
// ErrGetStakeDifficulty indicates that the current top block of the
// blockchain could not be obtained.
ErrGetTopBlock
// ErrGettingDifficulty indicates that there was an error getting the
// PoW difficulty.
ErrGettingDifficulty
// ErrTransactionAppend indicates there was a problem adding a msgtx
// to a msgblock.
ErrTransactionAppend
// ErrCheckConnectBlock indicates that a newly created block template
// failed blockchain.CheckConnectBlock.
ErrCheckConnectBlock
// ErrFraudProofIndex indicates that there was an error finding the index
// for a fraud proof.
ErrFraudProofIndex
// ErrFetchTxStore indicates a transaction store failed to fetch.
ErrFetchTxStore
// ErrCalcCommitmentRoot indicates that creating the header commitments and
// calculating the associated commitment root for a newly created block
// template failed.
ErrCalcCommitmentRoot
)
// Map of MiningErrorCode values back to their constant names for pretty printing.
var miningErrorCodeStrings = map[MiningErrorCode]string{
ErrNotEnoughVoters: "ErrNotEnoughVoters",
ErrFailedToGetGeneration: "ErrFailedToGetGeneration",
ErrGetTopBlock: "ErrGetTopBlock",
ErrGettingDifficulty: "ErrGettingDifficulty",
ErrTransactionAppend: "ErrTransactionAppend",
ErrCheckConnectBlock: "ErrCheckConnectBlock",
ErrFraudProofIndex: "ErrFraudProofIndex",
ErrFetchTxStore: "ErrFetchTxStore",
ErrCalcCommitmentRoot: "ErrCalcCommitmentRoot",
}
// String returns the MiningErrorCode as a human-readable name.
func (e MiningErrorCode) String() string {
if s := miningErrorCodeStrings[e]; s != "" {
return s
}
return fmt.Sprintf("Unknown MiningErrorCode (%d)", int(e))
}
// MiningRuleError 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 MiningErrorCode field to
// ascertain the specific reason for the rule violation.
type MiningRuleError struct {
ErrorCode MiningErrorCode // 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 MiningRuleError) Error() string {
return e.Description
}
// GetCode satisfies the error interface and prints human-readable errors.
func (e MiningRuleError) GetCode() MiningErrorCode {
return e.ErrorCode
}
// miningRuleError creates an RuleError given a set of arguments.
func miningRuleError(c MiningErrorCode, desc string) MiningRuleError {
return MiningRuleError{ErrorCode: c, Description: desc}
}