value.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 common
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io"
  21. "math/big"
  22. "reflect"
  23. "strconv"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. // Value can hold values of certain basic types and provides ways to
  27. // convert between types without bothering to check whether the
  28. // conversion is actually meaningful.
  29. //
  30. // It currently supports the following types:
  31. //
  32. // - int{,8,16,32,64}
  33. // - uint{,8,16,32,64}
  34. // - *big.Int
  35. // - []byte, string
  36. // - []interface{}
  37. //
  38. // Value is useful whenever you feel that Go's types limit your
  39. // ability to express yourself. In these situations, use Value and
  40. // forget about this strong typing nonsense.
  41. type Value struct{ Val interface{} }
  42. func (val *Value) String() string {
  43. return fmt.Sprintf("%x", val.Val)
  44. }
  45. func NewValue(val interface{}) *Value {
  46. t := val
  47. if v, ok := val.(*Value); ok {
  48. t = v.Val
  49. }
  50. return &Value{Val: t}
  51. }
  52. func (val *Value) Type() reflect.Kind {
  53. return reflect.TypeOf(val.Val).Kind()
  54. }
  55. func (val *Value) IsNil() bool {
  56. return val.Val == nil
  57. }
  58. func (val *Value) Len() int {
  59. if data, ok := val.Val.([]interface{}); ok {
  60. return len(data)
  61. }
  62. return len(val.Bytes())
  63. }
  64. func (val *Value) Uint() uint64 {
  65. if Val, ok := val.Val.(uint8); ok {
  66. return uint64(Val)
  67. } else if Val, ok := val.Val.(uint16); ok {
  68. return uint64(Val)
  69. } else if Val, ok := val.Val.(uint32); ok {
  70. return uint64(Val)
  71. } else if Val, ok := val.Val.(uint64); ok {
  72. return Val
  73. } else if Val, ok := val.Val.(float32); ok {
  74. return uint64(Val)
  75. } else if Val, ok := val.Val.(float64); ok {
  76. return uint64(Val)
  77. } else if Val, ok := val.Val.(int); ok {
  78. return uint64(Val)
  79. } else if Val, ok := val.Val.(uint); ok {
  80. return uint64(Val)
  81. } else if Val, ok := val.Val.([]byte); ok {
  82. return new(big.Int).SetBytes(Val).Uint64()
  83. } else if Val, ok := val.Val.(*big.Int); ok {
  84. return Val.Uint64()
  85. }
  86. return 0
  87. }
  88. func (val *Value) Int() int64 {
  89. if Val, ok := val.Val.(int8); ok {
  90. return int64(Val)
  91. } else if Val, ok := val.Val.(int16); ok {
  92. return int64(Val)
  93. } else if Val, ok := val.Val.(int32); ok {
  94. return int64(Val)
  95. } else if Val, ok := val.Val.(int64); ok {
  96. return Val
  97. } else if Val, ok := val.Val.(int); ok {
  98. return int64(Val)
  99. } else if Val, ok := val.Val.(float32); ok {
  100. return int64(Val)
  101. } else if Val, ok := val.Val.(float64); ok {
  102. return int64(Val)
  103. } else if Val, ok := val.Val.([]byte); ok {
  104. return new(big.Int).SetBytes(Val).Int64()
  105. } else if Val, ok := val.Val.(*big.Int); ok {
  106. return Val.Int64()
  107. } else if Val, ok := val.Val.(string); ok {
  108. n, _ := strconv.Atoi(Val)
  109. return int64(n)
  110. }
  111. return 0
  112. }
  113. func (val *Value) Byte() byte {
  114. if Val, ok := val.Val.(byte); ok {
  115. return Val
  116. }
  117. return 0x0
  118. }
  119. func (val *Value) BigInt() *big.Int {
  120. if a, ok := val.Val.([]byte); ok {
  121. b := new(big.Int).SetBytes(a)
  122. return b
  123. } else if a, ok := val.Val.(*big.Int); ok {
  124. return a
  125. } else if a, ok := val.Val.(string); ok {
  126. return Big(a)
  127. } else {
  128. return big.NewInt(int64(val.Uint()))
  129. }
  130. return big.NewInt(0)
  131. }
  132. func (val *Value) Str() string {
  133. if a, ok := val.Val.([]byte); ok {
  134. return string(a)
  135. } else if a, ok := val.Val.(string); ok {
  136. return a
  137. } else if a, ok := val.Val.(byte); ok {
  138. return string(a)
  139. }
  140. return ""
  141. }
  142. func (val *Value) Bytes() []byte {
  143. if a, ok := val.Val.([]byte); ok {
  144. return a
  145. } else if s, ok := val.Val.(byte); ok {
  146. return []byte{s}
  147. } else if s, ok := val.Val.(string); ok {
  148. return []byte(s)
  149. } else if s, ok := val.Val.(*big.Int); ok {
  150. return s.Bytes()
  151. } else {
  152. return big.NewInt(val.Int()).Bytes()
  153. }
  154. return []byte{}
  155. }
  156. func (val *Value) Err() error {
  157. if err, ok := val.Val.(error); ok {
  158. return err
  159. }
  160. return nil
  161. }
  162. func (val *Value) Slice() []interface{} {
  163. if d, ok := val.Val.([]interface{}); ok {
  164. return d
  165. }
  166. return []interface{}{}
  167. }
  168. func (val *Value) SliceFrom(from int) *Value {
  169. slice := val.Slice()
  170. return NewValue(slice[from:])
  171. }
  172. func (val *Value) SliceTo(to int) *Value {
  173. slice := val.Slice()
  174. return NewValue(slice[:to])
  175. }
  176. func (val *Value) SliceFromTo(from, to int) *Value {
  177. slice := val.Slice()
  178. return NewValue(slice[from:to])
  179. }
  180. // TODO More type checking methods
  181. func (val *Value) IsSlice() bool {
  182. return val.Type() == reflect.Slice
  183. }
  184. func (val *Value) IsStr() bool {
  185. return val.Type() == reflect.String
  186. }
  187. func (self *Value) IsErr() bool {
  188. _, ok := self.Val.(error)
  189. return ok
  190. }
  191. // Special list checking function. Something is considered
  192. // a list if it's of type []interface{}. The list is usually
  193. // used in conjunction with rlp decoded streams.
  194. func (val *Value) IsList() bool {
  195. _, ok := val.Val.([]interface{})
  196. return ok
  197. }
  198. func (val *Value) IsEmpty() bool {
  199. return val.Val == nil || ((val.IsSlice() || val.IsStr()) && val.Len() == 0)
  200. }
  201. // Threat the value as a slice
  202. func (val *Value) Get(idx int) *Value {
  203. if d, ok := val.Val.([]interface{}); ok {
  204. // Guard for oob
  205. if len(d) <= idx {
  206. return NewValue(nil)
  207. }
  208. if idx < 0 {
  209. return NewValue(nil)
  210. }
  211. return NewValue(d[idx])
  212. }
  213. // If this wasn't a slice you probably shouldn't be using this function
  214. return NewValue(nil)
  215. }
  216. func (self *Value) Copy() *Value {
  217. switch val := self.Val.(type) {
  218. case *big.Int:
  219. return NewValue(new(big.Int).Set(val))
  220. case []byte:
  221. return NewValue(CopyBytes(val))
  222. default:
  223. return NewValue(self.Val)
  224. }
  225. return nil
  226. }
  227. func (val *Value) Cmp(o *Value) bool {
  228. return reflect.DeepEqual(val.Val, o.Val)
  229. }
  230. func (self *Value) DeepCmp(o *Value) bool {
  231. return bytes.Compare(self.Bytes(), o.Bytes()) == 0
  232. }
  233. func (self *Value) DecodeRLP(s *rlp.Stream) error {
  234. var v interface{}
  235. if err := s.Decode(&v); err != nil {
  236. return err
  237. }
  238. self.Val = v
  239. return nil
  240. }
  241. func (self *Value) EncodeRLP(w io.Writer) error {
  242. if self == nil {
  243. w.Write(rlp.EmptyList)
  244. return nil
  245. } else {
  246. return rlp.Encode(w, self.Val)
  247. }
  248. }
  249. // NewValueFromBytes decodes RLP data.
  250. // The contained value will be nil if data contains invalid RLP.
  251. func NewValueFromBytes(data []byte) *Value {
  252. v := new(Value)
  253. if len(data) != 0 {
  254. if err := rlp.DecodeBytes(data, v); err != nil {
  255. v.Val = nil
  256. }
  257. }
  258. return v
  259. }
  260. // Value setters
  261. func NewSliceValue(s interface{}) *Value {
  262. list := EmptyValue()
  263. if s != nil {
  264. if slice, ok := s.([]interface{}); ok {
  265. for _, val := range slice {
  266. list.Append(val)
  267. }
  268. } else if slice, ok := s.([]string); ok {
  269. for _, val := range slice {
  270. list.Append(val)
  271. }
  272. }
  273. }
  274. return list
  275. }
  276. func EmptyValue() *Value {
  277. return NewValue([]interface{}{})
  278. }
  279. func (val *Value) AppendList() *Value {
  280. list := EmptyValue()
  281. val.Val = append(val.Slice(), list)
  282. return list
  283. }
  284. func (val *Value) Append(v interface{}) *Value {
  285. val.Val = append(val.Slice(), v)
  286. return val
  287. }
  288. const (
  289. valOpAdd = iota
  290. valOpDiv
  291. valOpMul
  292. valOpPow
  293. valOpSub
  294. )
  295. // Math stuff
  296. func (self *Value) doOp(op int, other interface{}) *Value {
  297. left := self.BigInt()
  298. right := NewValue(other).BigInt()
  299. switch op {
  300. case valOpAdd:
  301. self.Val = left.Add(left, right)
  302. case valOpDiv:
  303. self.Val = left.Div(left, right)
  304. case valOpMul:
  305. self.Val = left.Mul(left, right)
  306. case valOpPow:
  307. self.Val = left.Exp(left, right, Big0)
  308. case valOpSub:
  309. self.Val = left.Sub(left, right)
  310. }
  311. return self
  312. }
  313. func (self *Value) Add(other interface{}) *Value {
  314. return self.doOp(valOpAdd, other)
  315. }
  316. func (self *Value) Sub(other interface{}) *Value {
  317. return self.doOp(valOpSub, other)
  318. }
  319. func (self *Value) Div(other interface{}) *Value {
  320. return self.doOp(valOpDiv, other)
  321. }
  322. func (self *Value) Mul(other interface{}) *Value {
  323. return self.doOp(valOpMul, other)
  324. }
  325. func (self *Value) Pow(other interface{}) *Value {
  326. return self.doOp(valOpPow, other)
  327. }
  328. type ValueIterator struct {
  329. value *Value
  330. currentValue *Value
  331. idx int
  332. }
  333. func (val *Value) NewIterator() *ValueIterator {
  334. return &ValueIterator{value: val}
  335. }
  336. func (it *ValueIterator) Len() int {
  337. return it.value.Len()
  338. }
  339. func (it *ValueIterator) Next() bool {
  340. if it.idx >= it.value.Len() {
  341. return false
  342. }
  343. it.currentValue = it.value.Get(it.idx)
  344. it.idx++
  345. return true
  346. }
  347. func (it *ValueIterator) Value() *Value {
  348. return it.currentValue
  349. }
  350. func (it *ValueIterator) Idx() int {
  351. return it.idx - 1
  352. }