error.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package core
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. )
  7. var (
  8. BlockNumberErr = errors.New("block number invalid")
  9. BlockFutureErr = errors.New("block time is in the future")
  10. )
  11. // Parent error. In case a parent is unknown this error will be thrown
  12. // by the block manager
  13. type ParentErr struct {
  14. Message string
  15. }
  16. func (err *ParentErr) Error() string {
  17. return err.Message
  18. }
  19. func ParentError(hash []byte) error {
  20. return &ParentErr{Message: fmt.Sprintf("Block's parent unkown %x", hash)}
  21. }
  22. func IsParentErr(err error) bool {
  23. _, ok := err.(*ParentErr)
  24. return ok
  25. }
  26. type UncleErr struct {
  27. Message string
  28. }
  29. func (err *UncleErr) Error() string {
  30. return err.Message
  31. }
  32. func UncleError(str string) error {
  33. return &UncleErr{Message: str}
  34. }
  35. func IsUncleErr(err error) bool {
  36. _, ok := err.(*UncleErr)
  37. return ok
  38. }
  39. // Block validation error. If any validation fails, this error will be thrown
  40. type ValidationErr struct {
  41. Message string
  42. }
  43. func (err *ValidationErr) Error() string {
  44. return err.Message
  45. }
  46. func ValidationError(format string, v ...interface{}) *ValidationErr {
  47. return &ValidationErr{Message: fmt.Sprintf(format, v...)}
  48. }
  49. func IsValidationErr(err error) bool {
  50. _, ok := err.(*ValidationErr)
  51. return ok
  52. }
  53. type GasLimitErr struct {
  54. Message string
  55. Is, Max *big.Int
  56. }
  57. func IsGasLimitErr(err error) bool {
  58. _, ok := err.(*GasLimitErr)
  59. return ok
  60. }
  61. func (err *GasLimitErr) Error() string {
  62. return err.Message
  63. }
  64. func GasLimitError(is, max *big.Int) *GasLimitErr {
  65. return &GasLimitErr{Message: fmt.Sprintf("GasLimit error. Max %s, transaction would take it to %s", max, is), Is: is, Max: max}
  66. }
  67. type NonceErr struct {
  68. Message string
  69. Is, Exp uint64
  70. }
  71. func (err *NonceErr) Error() string {
  72. return err.Message
  73. }
  74. func NonceError(is, exp uint64) *NonceErr {
  75. return &NonceErr{Message: fmt.Sprintf("Nonce err. Is %d, expected %d", is, exp), Is: is, Exp: exp}
  76. }
  77. func IsNonceErr(err error) bool {
  78. _, ok := err.(*NonceErr)
  79. return ok
  80. }
  81. type OutOfGasErr struct {
  82. Message string
  83. }
  84. func OutOfGasError() *OutOfGasErr {
  85. return &OutOfGasErr{Message: "Out of gas"}
  86. }
  87. func (self *OutOfGasErr) Error() string {
  88. return self.Message
  89. }
  90. func IsOutOfGasErr(err error) bool {
  91. _, ok := err.(*OutOfGasErr)
  92. return ok
  93. }
  94. type TDError struct {
  95. a, b *big.Int
  96. }
  97. func (self *TDError) Error() string {
  98. return fmt.Sprintf("incoming chain has a lower or equal TD (%v <= %v)", self.a, self.b)
  99. }
  100. func IsTDError(e error) bool {
  101. _, ok := e.(*TDError)
  102. return ok
  103. }
  104. type KnownBlockError struct {
  105. number *big.Int
  106. hash []byte
  107. }
  108. func (self *KnownBlockError) Error() string {
  109. return fmt.Sprintf("block %v already known (%x)", self.number, self.hash[0:4])
  110. }
  111. func IsKnownBlockErr(e error) bool {
  112. _, ok := e.(*KnownBlockError)
  113. return ok
  114. }