error.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. package core
  17. import (
  18. "errors"
  19. "fmt"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. var (
  24. BlockNumberErr = errors.New("block number invalid")
  25. BlockFutureErr = errors.New("block time is in the future")
  26. BlockTSTooBigErr = errors.New("block time too big")
  27. BlockEqualTSErr = errors.New("block time stamp equal to previous")
  28. )
  29. // Parent error. In case a parent is unknown this error will be thrown
  30. // by the block manager
  31. type ParentErr struct {
  32. Message string
  33. }
  34. func (err *ParentErr) Error() string {
  35. return err.Message
  36. }
  37. func ParentError(hash common.Hash) error {
  38. return &ParentErr{Message: fmt.Sprintf("Block's parent unknown %x", hash)}
  39. }
  40. func IsParentErr(err error) bool {
  41. _, ok := err.(*ParentErr)
  42. return ok
  43. }
  44. type UncleErr struct {
  45. Message string
  46. }
  47. func (err *UncleErr) Error() string {
  48. return err.Message
  49. }
  50. func UncleError(format string, v ...interface{}) error {
  51. return &UncleErr{Message: fmt.Sprintf(format, v...)}
  52. }
  53. func IsUncleErr(err error) bool {
  54. _, ok := err.(*UncleErr)
  55. return ok
  56. }
  57. // Block validation error. If any validation fails, this error will be thrown
  58. type ValidationErr struct {
  59. Message string
  60. }
  61. func (err *ValidationErr) Error() string {
  62. return err.Message
  63. }
  64. func ValidationError(format string, v ...interface{}) *ValidationErr {
  65. return &ValidationErr{Message: fmt.Sprintf(format, v...)}
  66. }
  67. func IsValidationErr(err error) bool {
  68. _, ok := err.(*ValidationErr)
  69. return ok
  70. }
  71. type NonceErr struct {
  72. Message string
  73. Is, Exp uint64
  74. }
  75. func (err *NonceErr) Error() string {
  76. return err.Message
  77. }
  78. func NonceError(is, exp uint64) *NonceErr {
  79. return &NonceErr{Message: fmt.Sprintf("Transaction w/ invalid nonce. tx=%d state=%d)", is, exp), Is: is, Exp: exp}
  80. }
  81. func IsNonceErr(err error) bool {
  82. _, ok := err.(*NonceErr)
  83. return ok
  84. }
  85. // BlockNonceErr indicates that a block's nonce is invalid.
  86. type BlockNonceErr struct {
  87. Number *big.Int
  88. Hash common.Hash
  89. Nonce uint64
  90. }
  91. func (err *BlockNonceErr) Error() string {
  92. return fmt.Sprintf("nonce for #%d [%x…] is invalid (got %d)", err.Number, err.Hash, err.Nonce)
  93. }
  94. // IsBlockNonceErr returns true for invalid block nonce errors.
  95. func IsBlockNonceErr(err error) bool {
  96. _, ok := err.(*BlockNonceErr)
  97. return ok
  98. }
  99. type InvalidTxErr struct {
  100. Message string
  101. }
  102. func (err *InvalidTxErr) Error() string {
  103. return err.Message
  104. }
  105. func InvalidTxError(err error) *InvalidTxErr {
  106. return &InvalidTxErr{fmt.Sprintf("%v", err)}
  107. }
  108. func IsInvalidTxErr(err error) bool {
  109. _, ok := err.(*InvalidTxErr)
  110. return ok
  111. }
  112. type TDError struct {
  113. a, b *big.Int
  114. }
  115. func (self *TDError) Error() string {
  116. return fmt.Sprintf("incoming chain has a lower or equal TD (%v <= %v)", self.a, self.b)
  117. }
  118. func IsTDError(e error) bool {
  119. _, ok := e.(*TDError)
  120. return ok
  121. }
  122. type KnownBlockError struct {
  123. number *big.Int
  124. hash common.Hash
  125. }
  126. func (self *KnownBlockError) Error() string {
  127. return fmt.Sprintf("block %v already known (%x)", self.number, self.hash[0:4])
  128. }
  129. func IsKnownBlockErr(e error) bool {
  130. _, ok := e.(*KnownBlockError)
  131. return ok
  132. }
  133. type ValueTransferError struct {
  134. message string
  135. }
  136. func ValueTransferErr(str string, v ...interface{}) *ValueTransferError {
  137. return &ValueTransferError{fmt.Sprintf(str, v...)}
  138. }
  139. func (self *ValueTransferError) Error() string {
  140. return self.message
  141. }
  142. func IsValueTransferErr(e error) bool {
  143. _, ok := e.(*ValueTransferError)
  144. return ok
  145. }
  146. type BadHashError common.Hash
  147. func (h BadHashError) Error() string {
  148. return fmt.Sprintf("Found known bad hash in chain %x", h[:])
  149. }
  150. func IsBadHashError(err error) bool {
  151. _, ok := err.(BadHashError)
  152. return ok
  153. }
  154. type GasLimitErr struct {
  155. Have, Want *big.Int
  156. }
  157. func IsGasLimitErr(err error) bool {
  158. _, ok := err.(*GasLimitErr)
  159. return ok
  160. }
  161. func (err *GasLimitErr) Error() string {
  162. return fmt.Sprintf("GasLimit reached. Have %d gas, transaction requires %d", err.Have, err.Want)
  163. }