value.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package common
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/big"
  6. "reflect"
  7. "strconv"
  8. )
  9. // Data values are returned by the rlp decoder. The data values represents
  10. // one item within the rlp data structure. It's responsible for all the casting
  11. // It always returns something valid
  12. type Value struct {
  13. Val interface{}
  14. kind reflect.Value
  15. }
  16. func (val *Value) String() string {
  17. return fmt.Sprintf("%x", val.Val)
  18. }
  19. func NewValue(val interface{}) *Value {
  20. t := val
  21. if v, ok := val.(*Value); ok {
  22. t = v.Val
  23. }
  24. return &Value{Val: t}
  25. }
  26. func (val *Value) Type() reflect.Kind {
  27. return reflect.TypeOf(val.Val).Kind()
  28. }
  29. func (val *Value) IsNil() bool {
  30. return val.Val == nil
  31. }
  32. func (val *Value) Len() int {
  33. //return val.kind.Len()
  34. if data, ok := val.Val.([]interface{}); ok {
  35. return len(data)
  36. }
  37. return len(val.Bytes())
  38. }
  39. func (val *Value) Raw() interface{} {
  40. return val.Val
  41. }
  42. func (val *Value) Interface() interface{} {
  43. return val.Val
  44. }
  45. func (val *Value) Uint() uint64 {
  46. if Val, ok := val.Val.(uint8); ok {
  47. return uint64(Val)
  48. } else if Val, ok := val.Val.(uint16); ok {
  49. return uint64(Val)
  50. } else if Val, ok := val.Val.(uint32); ok {
  51. return uint64(Val)
  52. } else if Val, ok := val.Val.(uint64); ok {
  53. return Val
  54. } else if Val, ok := val.Val.(float32); ok {
  55. return uint64(Val)
  56. } else if Val, ok := val.Val.(float64); ok {
  57. return uint64(Val)
  58. } else if Val, ok := val.Val.(int); ok {
  59. return uint64(Val)
  60. } else if Val, ok := val.Val.(uint); ok {
  61. return uint64(Val)
  62. } else if Val, ok := val.Val.([]byte); ok {
  63. return new(big.Int).SetBytes(Val).Uint64()
  64. } else if Val, ok := val.Val.(*big.Int); ok {
  65. return Val.Uint64()
  66. }
  67. return 0
  68. }
  69. func (val *Value) Int() int64 {
  70. if Val, ok := val.Val.(int8); ok {
  71. return int64(Val)
  72. } else if Val, ok := val.Val.(int16); ok {
  73. return int64(Val)
  74. } else if Val, ok := val.Val.(int32); ok {
  75. return int64(Val)
  76. } else if Val, ok := val.Val.(int64); ok {
  77. return Val
  78. } else if Val, ok := val.Val.(int); ok {
  79. return int64(Val)
  80. } else if Val, ok := val.Val.(float32); ok {
  81. return int64(Val)
  82. } else if Val, ok := val.Val.(float64); ok {
  83. return int64(Val)
  84. } else if Val, ok := val.Val.([]byte); ok {
  85. return new(big.Int).SetBytes(Val).Int64()
  86. } else if Val, ok := val.Val.(*big.Int); ok {
  87. return Val.Int64()
  88. } else if Val, ok := val.Val.(string); ok {
  89. n, _ := strconv.Atoi(Val)
  90. return int64(n)
  91. }
  92. return 0
  93. }
  94. func (val *Value) Byte() byte {
  95. if Val, ok := val.Val.(byte); ok {
  96. return Val
  97. }
  98. return 0x0
  99. }
  100. func (val *Value) BigInt() *big.Int {
  101. if a, ok := val.Val.([]byte); ok {
  102. b := new(big.Int).SetBytes(a)
  103. return b
  104. } else if a, ok := val.Val.(*big.Int); ok {
  105. return a
  106. } else if a, ok := val.Val.(string); ok {
  107. return Big(a)
  108. } else {
  109. return big.NewInt(int64(val.Uint()))
  110. }
  111. return big.NewInt(0)
  112. }
  113. func (val *Value) Str() string {
  114. if a, ok := val.Val.([]byte); ok {
  115. return string(a)
  116. } else if a, ok := val.Val.(string); ok {
  117. return a
  118. } else if a, ok := val.Val.(byte); ok {
  119. return string(a)
  120. }
  121. return ""
  122. }
  123. func (val *Value) Bytes() []byte {
  124. if a, ok := val.Val.([]byte); ok {
  125. return a
  126. } else if s, ok := val.Val.(byte); ok {
  127. return []byte{s}
  128. } else if s, ok := val.Val.(string); ok {
  129. return []byte(s)
  130. } else if s, ok := val.Val.(*big.Int); ok {
  131. return s.Bytes()
  132. } else {
  133. return big.NewInt(val.Int()).Bytes()
  134. }
  135. return []byte{}
  136. }
  137. func (val *Value) Err() error {
  138. if err, ok := val.Val.(error); ok {
  139. return err
  140. }
  141. return nil
  142. }
  143. func (val *Value) Slice() []interface{} {
  144. if d, ok := val.Val.([]interface{}); ok {
  145. return d
  146. }
  147. return []interface{}{}
  148. }
  149. func (val *Value) SliceFrom(from int) *Value {
  150. slice := val.Slice()
  151. return NewValue(slice[from:])
  152. }
  153. func (val *Value) SliceTo(to int) *Value {
  154. slice := val.Slice()
  155. return NewValue(slice[:to])
  156. }
  157. func (val *Value) SliceFromTo(from, to int) *Value {
  158. slice := val.Slice()
  159. return NewValue(slice[from:to])
  160. }
  161. // TODO More type checking methods
  162. func (val *Value) IsSlice() bool {
  163. return val.Type() == reflect.Slice
  164. }
  165. func (val *Value) IsStr() bool {
  166. return val.Type() == reflect.String
  167. }
  168. func (self *Value) IsErr() bool {
  169. _, ok := self.Val.(error)
  170. return ok
  171. }
  172. // Special list checking function. Something is considered
  173. // a list if it's of type []interface{}. The list is usually
  174. // used in conjunction with rlp decoded streams.
  175. func (val *Value) IsList() bool {
  176. _, ok := val.Val.([]interface{})
  177. return ok
  178. }
  179. func (val *Value) IsEmpty() bool {
  180. return val.Val == nil || ((val.IsSlice() || val.IsStr()) && val.Len() == 0)
  181. }
  182. // Threat the value as a slice
  183. func (val *Value) Get(idx int) *Value {
  184. if d, ok := val.Val.([]interface{}); ok {
  185. // Guard for oob
  186. if len(d) <= idx {
  187. return NewValue(nil)
  188. }
  189. if idx < 0 {
  190. return NewValue(nil)
  191. }
  192. return NewValue(d[idx])
  193. }
  194. // If this wasn't a slice you probably shouldn't be using this function
  195. return NewValue(nil)
  196. }
  197. func (self *Value) Copy() *Value {
  198. switch val := self.Val.(type) {
  199. case *big.Int:
  200. return NewValue(new(big.Int).Set(val))
  201. case []byte:
  202. return NewValue(CopyBytes(val))
  203. default:
  204. return NewValue(self.Val)
  205. }
  206. return nil
  207. }
  208. func (val *Value) Cmp(o *Value) bool {
  209. return reflect.DeepEqual(val.Val, o.Val)
  210. }
  211. func (self *Value) DeepCmp(o *Value) bool {
  212. return bytes.Compare(self.Bytes(), o.Bytes()) == 0
  213. }
  214. func (val *Value) Encode() []byte {
  215. return Encode(val.Val)
  216. }
  217. // Assume that the data we have is encoded
  218. func (self *Value) Decode() {
  219. v, _ := Decode(self.Bytes(), 0)
  220. self.Val = v
  221. //self.Val = DecodeWithReader(bytes.NewBuffer(self.Bytes()))
  222. }
  223. func NewValueFromBytes(data []byte) *Value {
  224. if len(data) != 0 {
  225. value := NewValue(data)
  226. value.Decode()
  227. return value
  228. }
  229. return NewValue(nil)
  230. }
  231. // Value setters
  232. func NewSliceValue(s interface{}) *Value {
  233. list := EmptyValue()
  234. if s != nil {
  235. if slice, ok := s.([]interface{}); ok {
  236. for _, val := range slice {
  237. list.Append(val)
  238. }
  239. } else if slice, ok := s.([]string); ok {
  240. for _, val := range slice {
  241. list.Append(val)
  242. }
  243. }
  244. }
  245. return list
  246. }
  247. func EmptyValue() *Value {
  248. return NewValue([]interface{}{})
  249. }
  250. func (val *Value) AppendList() *Value {
  251. list := EmptyValue()
  252. val.Val = append(val.Slice(), list)
  253. return list
  254. }
  255. func (val *Value) Append(v interface{}) *Value {
  256. val.Val = append(val.Slice(), v)
  257. return val
  258. }
  259. const (
  260. valOpAdd = iota
  261. valOpDiv
  262. valOpMul
  263. valOpPow
  264. valOpSub
  265. )
  266. // Math stuff
  267. func (self *Value) doOp(op int, other interface{}) *Value {
  268. left := self.BigInt()
  269. right := NewValue(other).BigInt()
  270. switch op {
  271. case valOpAdd:
  272. self.Val = left.Add(left, right)
  273. case valOpDiv:
  274. self.Val = left.Div(left, right)
  275. case valOpMul:
  276. self.Val = left.Mul(left, right)
  277. case valOpPow:
  278. self.Val = left.Exp(left, right, Big0)
  279. case valOpSub:
  280. self.Val = left.Sub(left, right)
  281. }
  282. return self
  283. }
  284. func (self *Value) Add(other interface{}) *Value {
  285. return self.doOp(valOpAdd, other)
  286. }
  287. func (self *Value) Sub(other interface{}) *Value {
  288. return self.doOp(valOpSub, other)
  289. }
  290. func (self *Value) Div(other interface{}) *Value {
  291. return self.doOp(valOpDiv, other)
  292. }
  293. func (self *Value) Mul(other interface{}) *Value {
  294. return self.doOp(valOpMul, other)
  295. }
  296. func (self *Value) Pow(other interface{}) *Value {
  297. return self.doOp(valOpPow, other)
  298. }
  299. type ValueIterator struct {
  300. value *Value
  301. currentValue *Value
  302. idx int
  303. }
  304. func (val *Value) NewIterator() *ValueIterator {
  305. return &ValueIterator{value: val}
  306. }
  307. func (it *ValueIterator) Len() int {
  308. return it.value.Len()
  309. }
  310. func (it *ValueIterator) Next() bool {
  311. if it.idx >= it.value.Len() {
  312. return false
  313. }
  314. it.currentValue = it.value.Get(it.idx)
  315. it.idx++
  316. return true
  317. }
  318. func (it *ValueIterator) Value() *Value {
  319. return it.currentValue
  320. }
  321. func (it *ValueIterator) Idx() int {
  322. return it.idx - 1
  323. }