github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/core/error.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  var (
    28  	BlockNumberErr   = errors.New("block number invalid")
    29  	BlockFutureErr   = errors.New("block time is in the future")
    30  	BlockTSTooBigErr = errors.New("block time too big")
    31  	BlockEqualTSErr  = errors.New("block time stamp equal to previous")
    32  )
    33  
    34  // Parent error. In case a parent is unknown this error will be thrown
    35  // by the block manager
    36  type ParentErr struct {
    37  	Message string
    38  }
    39  
    40  func (err *ParentErr) Error() string {
    41  	return err.Message
    42  }
    43  
    44  func ParentError(hash common.Hash) error {
    45  	return &ParentErr{Message: fmt.Sprintf("Block's parent unknown %x", hash)}
    46  }
    47  
    48  func IsParentErr(err error) bool {
    49  	_, ok := err.(*ParentErr)
    50  	return ok
    51  }
    52  
    53  type UncleErr struct {
    54  	Message string
    55  }
    56  
    57  func (err *UncleErr) Error() string {
    58  	return err.Message
    59  }
    60  
    61  func UncleError(format string, v ...interface{}) error {
    62  	return &UncleErr{Message: fmt.Sprintf(format, v...)}
    63  }
    64  
    65  func IsUncleErr(err error) bool {
    66  	_, ok := err.(*UncleErr)
    67  	return ok
    68  }
    69  
    70  // Block validation error. If any validation fails, this error will be thrown
    71  type ValidationErr struct {
    72  	Message string
    73  }
    74  
    75  func (err *ValidationErr) Error() string {
    76  	return err.Message
    77  }
    78  
    79  func ValidationError(format string, v ...interface{}) *ValidationErr {
    80  	return &ValidationErr{Message: fmt.Sprintf(format, v...)}
    81  }
    82  
    83  func IsValidationErr(err error) bool {
    84  	_, ok := err.(*ValidationErr)
    85  	return ok
    86  }
    87  
    88  type NonceErr struct {
    89  	Message string
    90  	Is, Exp uint64
    91  }
    92  
    93  func (err *NonceErr) Error() string {
    94  	return err.Message
    95  }
    96  
    97  func NonceError(is, exp uint64) *NonceErr {
    98  	return &NonceErr{Message: fmt.Sprintf("Transaction w/ invalid nonce. tx=%d  state=%d)", is, exp), Is: is, Exp: exp}
    99  }
   100  
   101  func IsNonceErr(err error) bool {
   102  	_, ok := err.(*NonceErr)
   103  	return ok
   104  }
   105  
   106  // BlockNonceErr indicates that a block's nonce is invalid.
   107  type BlockNonceErr struct {
   108  	Number *big.Int
   109  	Hash   common.Hash
   110  	Nonce  uint64
   111  }
   112  
   113  func (err *BlockNonceErr) Error() string {
   114  	return fmt.Sprintf("nonce for #%d [%x…] is invalid (got %d)", err.Number, err.Hash, err.Nonce)
   115  }
   116  
   117  // IsBlockNonceErr returns true for invalid block nonce errors.
   118  func IsBlockNonceErr(err error) bool {
   119  	_, ok := err.(*BlockNonceErr)
   120  	return ok
   121  }
   122  
   123  type InvalidTxErr struct {
   124  	Message string
   125  }
   126  
   127  func (err *InvalidTxErr) Error() string {
   128  	return err.Message
   129  }
   130  
   131  func InvalidTxError(err error) *InvalidTxErr {
   132  	return &InvalidTxErr{fmt.Sprintf("%v", err)}
   133  }
   134  
   135  func IsInvalidTxErr(err error) bool {
   136  	_, ok := err.(*InvalidTxErr)
   137  	return ok
   138  }
   139  
   140  type TDError struct {
   141  	a, b *big.Int
   142  }
   143  
   144  func (self *TDError) Error() string {
   145  	return fmt.Sprintf("incoming chain has a lower or equal TD (%v <= %v)", self.a, self.b)
   146  }
   147  func IsTDError(e error) bool {
   148  	_, ok := e.(*TDError)
   149  	return ok
   150  }
   151  
   152  type KnownBlockError struct {
   153  	number *big.Int
   154  	hash   common.Hash
   155  }
   156  
   157  func (self *KnownBlockError) Error() string {
   158  	return fmt.Sprintf("block %v already known (%x)", self.number, self.hash[0:4])
   159  }
   160  func IsKnownBlockErr(e error) bool {
   161  	_, ok := e.(*KnownBlockError)
   162  	return ok
   163  }
   164  
   165  type ValueTransferError struct {
   166  	message string
   167  }
   168  
   169  func ValueTransferErr(str string, v ...interface{}) *ValueTransferError {
   170  	return &ValueTransferError{fmt.Sprintf(str, v...)}
   171  }
   172  
   173  func (self *ValueTransferError) Error() string {
   174  	return self.message
   175  }
   176  func IsValueTransferErr(e error) bool {
   177  	_, ok := e.(*ValueTransferError)
   178  	return ok
   179  }
   180  
   181  type BadHashError common.Hash
   182  
   183  func (h BadHashError) Error() string {
   184  	return fmt.Sprintf("Found known bad hash in chain %x", h[:])
   185  }
   186  
   187  func IsBadHashError(err error) bool {
   188  	_, ok := err.(BadHashError)
   189  	return ok
   190  }
   191  
   192  type GasLimitErr struct {
   193  	Have, Want *big.Int
   194  }
   195  
   196  func IsGasLimitErr(err error) bool {
   197  	_, ok := err.(*GasLimitErr)
   198  	return ok
   199  }
   200  
   201  func (err *GasLimitErr) Error() string {
   202  	return fmt.Sprintf("GasLimit reached. Have %d gas, transaction requires %d", err.Have, err.Want)
   203  }