error.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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. BlockEqualTSErr = errors.New("block time stamp equal to previous")
  27. )
  28. // Parent error. In case a parent is unknown this error will be thrown
  29. // by the block manager
  30. type ParentErr struct {
  31. Message string
  32. }
  33. func (err *ParentErr) Error() string {
  34. return err.Message
  35. }
  36. func ParentError(hash common.Hash) error {
  37. return &ParentErr{Message: fmt.Sprintf("Block's parent unknown %x", hash)}
  38. }
  39. func IsParentErr(err error) bool {
  40. _, ok := err.(*ParentErr)
  41. return ok
  42. }
  43. type UncleErr struct {
  44. Message string
  45. }
  46. func (err *UncleErr) Error() string {
  47. return err.Message
  48. }
  49. func UncleError(format string, v ...interface{}) error {
  50. return &UncleErr{Message: fmt.Sprintf(format, v...)}
  51. }
  52. func IsUncleErr(err error) bool {
  53. _, ok := err.(*UncleErr)
  54. return ok
  55. }
  56. // Block validation error. If any validation fails, this error will be thrown
  57. type ValidationErr struct {
  58. Message string
  59. }
  60. func (err *ValidationErr) Error() string {
  61. return err.Message
  62. }
  63. func ValidationError(format string, v ...interface{}) *ValidationErr {
  64. return &ValidationErr{Message: fmt.Sprintf(format, v...)}
  65. }
  66. func IsValidationErr(err error) bool {
  67. _, ok := err.(*ValidationErr)
  68. return ok
  69. }
  70. type NonceErr struct {
  71. Message string
  72. Is, Exp uint64
  73. }
  74. func (err *NonceErr) Error() string {
  75. return err.Message
  76. }
  77. func NonceError(is, exp uint64) *NonceErr {
  78. return &NonceErr{Message: fmt.Sprintf("Transaction w/ invalid nonce. tx=%d state=%d)", is, exp), Is: is, Exp: exp}
  79. }
  80. func IsNonceErr(err error) bool {
  81. _, ok := err.(*NonceErr)
  82. return ok
  83. }
  84. // BlockNonceErr indicates that a block's nonce is invalid.
  85. type BlockNonceErr struct {
  86. Number *big.Int
  87. Hash common.Hash
  88. Nonce uint64
  89. }
  90. func (err *BlockNonceErr) Error() string {
  91. return fmt.Sprintf("block %d (%v) nonce is invalid (got %d)", err.Number, err.Hash, err.Nonce)
  92. }
  93. // IsBlockNonceErr returns true for invalid block nonce errors.
  94. func IsBlockNonceErr(err error) bool {
  95. _, ok := err.(*BlockNonceErr)
  96. return ok
  97. }
  98. type InvalidTxErr struct {
  99. Message string
  100. }
  101. func (err *InvalidTxErr) Error() string {
  102. return err.Message
  103. }
  104. func InvalidTxError(err error) *InvalidTxErr {
  105. return &InvalidTxErr{fmt.Sprintf("%v", err)}
  106. }
  107. func IsInvalidTxErr(err error) bool {
  108. _, ok := err.(*InvalidTxErr)
  109. return ok
  110. }
  111. type TDError struct {
  112. a, b *big.Int
  113. }
  114. func (self *TDError) Error() string {
  115. return fmt.Sprintf("incoming chain has a lower or equal TD (%v <= %v)", self.a, self.b)
  116. }
  117. func IsTDError(e error) bool {
  118. _, ok := e.(*TDError)
  119. return ok
  120. }
  121. type KnownBlockError struct {
  122. number *big.Int
  123. hash common.Hash
  124. }
  125. func (self *KnownBlockError) Error() string {
  126. return fmt.Sprintf("block %v already known (%x)", self.number, self.hash[0:4])
  127. }
  128. func IsKnownBlockErr(e error) bool {
  129. _, ok := e.(*KnownBlockError)
  130. return ok
  131. }
  132. type ValueTransferError struct {
  133. message string
  134. }
  135. func ValueTransferErr(str string, v ...interface{}) *ValueTransferError {
  136. return &ValueTransferError{fmt.Sprintf(str, v...)}
  137. }
  138. func (self *ValueTransferError) Error() string {
  139. return self.message
  140. }
  141. func IsValueTransferErr(e error) bool {
  142. _, ok := e.(*ValueTransferError)
  143. return ok
  144. }