error.go 2.6 KB

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