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