error.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 NonceErr struct {
  54. Message string
  55. Is, Exp uint64
  56. }
  57. func (err *NonceErr) Error() string {
  58. return err.Message
  59. }
  60. func NonceError(is, exp uint64) *NonceErr {
  61. return &NonceErr{Message: fmt.Sprintf("Nonce err. Is %d, expected %d", is, exp), Is: is, Exp: exp}
  62. }
  63. func IsNonceErr(err error) bool {
  64. _, ok := err.(*NonceErr)
  65. return ok
  66. }
  67. type OutOfGasErr struct {
  68. Message string
  69. }
  70. func OutOfGasError() *OutOfGasErr {
  71. return &OutOfGasErr{Message: "Out of gas"}
  72. }
  73. func (self *OutOfGasErr) Error() string {
  74. return self.Message
  75. }
  76. func IsOutOfGasErr(err error) bool {
  77. _, ok := err.(*OutOfGasErr)
  78. return ok
  79. }
  80. type TDError struct {
  81. a, b *big.Int
  82. }
  83. func (self *TDError) Error() string {
  84. return fmt.Sprintf("incoming chain has a lower or equal TD (%v <= %v)", self.a, self.b)
  85. }
  86. func IsTDError(e error) bool {
  87. _, ok := e.(*TDError)
  88. return ok
  89. }
  90. type KnownBlockError struct {
  91. number *big.Int
  92. hash []byte
  93. }
  94. func (self *KnownBlockError) Error() string {
  95. return fmt.Sprintf("block %v already known (%x)", self.number, self.hash[0:4])
  96. }
  97. func IsKnownBlockErr(e error) bool {
  98. _, ok := e.(*KnownBlockError)
  99. return ok
  100. }