interface.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2016 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. // Contains perverted wrappers to allow crossing over empty interfaces.
  17. package geth
  18. import (
  19. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. // Interface represents a wrapped version of Go's interface{}, with the capacity
  24. // to store arbitrary data types.
  25. //
  26. // Since it's impossible to get the arbitrary-ness converted between Go and mobile
  27. // platforms, we're using explicit getters and setters for the conversions. There
  28. // is of course no point in enumerating everything, just enough to support the
  29. // contract bindins requiring client side generated code.
  30. type Interface struct {
  31. object interface{}
  32. }
  33. // NewInterface creates a new empty interface that can be used to pass around
  34. // generic types.
  35. func NewInterface() *Interface {
  36. return new(Interface)
  37. }
  38. func (i *Interface) SetBool(b bool) { i.object = &b }
  39. func (i *Interface) SetBools(bs *Bools) { i.object = &bs.bools }
  40. func (i *Interface) SetString(str string) { i.object = &str }
  41. func (i *Interface) SetStrings(strs *Strings) { i.object = &strs.strs }
  42. func (i *Interface) SetBinary(binary []byte) { b := common.CopyBytes(binary); i.object = &b }
  43. func (i *Interface) SetBinaries(binaries *Binaries) { i.object = &binaries.binaries }
  44. func (i *Interface) SetAddress(address *Address) { i.object = &address.address }
  45. func (i *Interface) SetAddresses(addrs *Addresses) { i.object = &addrs.addresses }
  46. func (i *Interface) SetHash(hash *Hash) { i.object = &hash.hash }
  47. func (i *Interface) SetHashes(hashes *Hashes) { i.object = &hashes.hashes }
  48. func (i *Interface) SetInt8(n int8) { i.object = &n }
  49. func (i *Interface) SetInt16(n int16) { i.object = &n }
  50. func (i *Interface) SetInt32(n int32) { i.object = &n }
  51. func (i *Interface) SetInt64(n int64) { i.object = &n }
  52. func (i *Interface) SetInt8s(bigints *BigInts) {
  53. ints := make([]int8, 0, bigints.Size())
  54. for _, bi := range bigints.bigints {
  55. ints = append(ints, int8(bi.Int64()))
  56. }
  57. i.object = &ints
  58. }
  59. func (i *Interface) SetInt16s(bigints *BigInts) {
  60. ints := make([]int16, 0, bigints.Size())
  61. for _, bi := range bigints.bigints {
  62. ints = append(ints, int16(bi.Int64()))
  63. }
  64. i.object = &ints
  65. }
  66. func (i *Interface) SetInt32s(bigints *BigInts) {
  67. ints := make([]int32, 0, bigints.Size())
  68. for _, bi := range bigints.bigints {
  69. ints = append(ints, int32(bi.Int64()))
  70. }
  71. i.object = &ints
  72. }
  73. func (i *Interface) SetInt64s(bigints *BigInts) {
  74. ints := make([]int64, 0, bigints.Size())
  75. for _, bi := range bigints.bigints {
  76. ints = append(ints, bi.Int64())
  77. }
  78. i.object = &ints
  79. }
  80. func (i *Interface) SetUint8(bigint *BigInt) { n := uint8(bigint.bigint.Uint64()); i.object = &n }
  81. func (i *Interface) SetUint16(bigint *BigInt) { n := uint16(bigint.bigint.Uint64()); i.object = &n }
  82. func (i *Interface) SetUint32(bigint *BigInt) { n := uint32(bigint.bigint.Uint64()); i.object = &n }
  83. func (i *Interface) SetUint64(bigint *BigInt) { n := bigint.bigint.Uint64(); i.object = &n }
  84. func (i *Interface) SetUint8s(bigints *BigInts) {
  85. ints := make([]uint8, 0, bigints.Size())
  86. for _, bi := range bigints.bigints {
  87. ints = append(ints, uint8(bi.Uint64()))
  88. }
  89. i.object = &ints
  90. }
  91. func (i *Interface) SetUint16s(bigints *BigInts) {
  92. ints := make([]uint16, 0, bigints.Size())
  93. for _, bi := range bigints.bigints {
  94. ints = append(ints, uint16(bi.Uint64()))
  95. }
  96. i.object = &ints
  97. }
  98. func (i *Interface) SetUint32s(bigints *BigInts) {
  99. ints := make([]uint32, 0, bigints.Size())
  100. for _, bi := range bigints.bigints {
  101. ints = append(ints, uint32(bi.Uint64()))
  102. }
  103. i.object = &ints
  104. }
  105. func (i *Interface) SetUint64s(bigints *BigInts) {
  106. ints := make([]uint64, 0, bigints.Size())
  107. for _, bi := range bigints.bigints {
  108. ints = append(ints, bi.Uint64())
  109. }
  110. i.object = &ints
  111. }
  112. func (i *Interface) SetBigInt(bigint *BigInt) { i.object = &bigint.bigint }
  113. func (i *Interface) SetBigInts(bigints *BigInts) { i.object = &bigints.bigints }
  114. func (i *Interface) SetDefaultBool() { i.object = new(bool) }
  115. func (i *Interface) SetDefaultBools() { i.object = new([]bool) }
  116. func (i *Interface) SetDefaultString() { i.object = new(string) }
  117. func (i *Interface) SetDefaultStrings() { i.object = new([]string) }
  118. func (i *Interface) SetDefaultBinary() { i.object = new([]byte) }
  119. func (i *Interface) SetDefaultBinaries() { i.object = new([][]byte) }
  120. func (i *Interface) SetDefaultAddress() { i.object = new(common.Address) }
  121. func (i *Interface) SetDefaultAddresses() { i.object = new([]common.Address) }
  122. func (i *Interface) SetDefaultHash() { i.object = new(common.Hash) }
  123. func (i *Interface) SetDefaultHashes() { i.object = new([]common.Hash) }
  124. func (i *Interface) SetDefaultInt8() { i.object = new(int8) }
  125. func (i *Interface) SetDefaultInt8s() { i.object = new([]int8) }
  126. func (i *Interface) SetDefaultInt16() { i.object = new(int16) }
  127. func (i *Interface) SetDefaultInt16s() { i.object = new([]int16) }
  128. func (i *Interface) SetDefaultInt32() { i.object = new(int32) }
  129. func (i *Interface) SetDefaultInt32s() { i.object = new([]int32) }
  130. func (i *Interface) SetDefaultInt64() { i.object = new(int64) }
  131. func (i *Interface) SetDefaultInt64s() { i.object = new([]int64) }
  132. func (i *Interface) SetDefaultUint8() { i.object = new(uint8) }
  133. func (i *Interface) SetDefaultUint8s() { i.object = new([]uint8) }
  134. func (i *Interface) SetDefaultUint16() { i.object = new(uint16) }
  135. func (i *Interface) SetDefaultUint16s() { i.object = new([]uint16) }
  136. func (i *Interface) SetDefaultUint32() { i.object = new(uint32) }
  137. func (i *Interface) SetDefaultUint32s() { i.object = new([]uint32) }
  138. func (i *Interface) SetDefaultUint64() { i.object = new(uint64) }
  139. func (i *Interface) SetDefaultUint64s() { i.object = new([]uint64) }
  140. func (i *Interface) SetDefaultBigInt() { i.object = new(*big.Int) }
  141. func (i *Interface) SetDefaultBigInts() { i.object = new([]*big.Int) }
  142. func (i *Interface) GetBool() bool { return *i.object.(*bool) }
  143. func (i *Interface) GetBools() *Bools { return &Bools{*i.object.(*[]bool)} }
  144. func (i *Interface) GetString() string { return *i.object.(*string) }
  145. func (i *Interface) GetStrings() *Strings { return &Strings{*i.object.(*[]string)} }
  146. func (i *Interface) GetBinary() []byte { return *i.object.(*[]byte) }
  147. func (i *Interface) GetBinaries() *Binaries { return &Binaries{*i.object.(*[][]byte)} }
  148. func (i *Interface) GetAddress() *Address { return &Address{*i.object.(*common.Address)} }
  149. func (i *Interface) GetAddresses() *Addresses { return &Addresses{*i.object.(*[]common.Address)} }
  150. func (i *Interface) GetHash() *Hash { return &Hash{*i.object.(*common.Hash)} }
  151. func (i *Interface) GetHashes() *Hashes { return &Hashes{*i.object.(*[]common.Hash)} }
  152. func (i *Interface) GetInt8() int8 { return *i.object.(*int8) }
  153. func (i *Interface) GetInt16() int16 { return *i.object.(*int16) }
  154. func (i *Interface) GetInt32() int32 { return *i.object.(*int32) }
  155. func (i *Interface) GetInt64() int64 { return *i.object.(*int64) }
  156. func (i *Interface) GetInt8s() *BigInts {
  157. val := i.object.(*[]int8)
  158. bigints := NewBigInts(len(*val))
  159. for i, v := range *val {
  160. bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
  161. }
  162. return bigints
  163. }
  164. func (i *Interface) GetInt16s() *BigInts {
  165. val := i.object.(*[]int16)
  166. bigints := NewBigInts(len(*val))
  167. for i, v := range *val {
  168. bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
  169. }
  170. return bigints
  171. }
  172. func (i *Interface) GetInt32s() *BigInts {
  173. val := i.object.(*[]int32)
  174. bigints := NewBigInts(len(*val))
  175. for i, v := range *val {
  176. bigints.Set(i, &BigInt{new(big.Int).SetInt64(int64(v))})
  177. }
  178. return bigints
  179. }
  180. func (i *Interface) GetInt64s() *BigInts {
  181. val := i.object.(*[]int64)
  182. bigints := NewBigInts(len(*val))
  183. for i, v := range *val {
  184. bigints.Set(i, &BigInt{new(big.Int).SetInt64(v)})
  185. }
  186. return bigints
  187. }
  188. func (i *Interface) GetUint8() *BigInt {
  189. return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint8)))}
  190. }
  191. func (i *Interface) GetUint16() *BigInt {
  192. return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint16)))}
  193. }
  194. func (i *Interface) GetUint32() *BigInt {
  195. return &BigInt{new(big.Int).SetUint64(uint64(*i.object.(*uint32)))}
  196. }
  197. func (i *Interface) GetUint64() *BigInt {
  198. return &BigInt{new(big.Int).SetUint64(*i.object.(*uint64))}
  199. }
  200. func (i *Interface) GetUint8s() *BigInts {
  201. val := i.object.(*[]uint8)
  202. bigints := NewBigInts(len(*val))
  203. for i, v := range *val {
  204. bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
  205. }
  206. return bigints
  207. }
  208. func (i *Interface) GetUint16s() *BigInts {
  209. val := i.object.(*[]uint16)
  210. bigints := NewBigInts(len(*val))
  211. for i, v := range *val {
  212. bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
  213. }
  214. return bigints
  215. }
  216. func (i *Interface) GetUint32s() *BigInts {
  217. val := i.object.(*[]uint32)
  218. bigints := NewBigInts(len(*val))
  219. for i, v := range *val {
  220. bigints.Set(i, &BigInt{new(big.Int).SetUint64(uint64(v))})
  221. }
  222. return bigints
  223. }
  224. func (i *Interface) GetUint64s() *BigInts {
  225. val := i.object.(*[]uint64)
  226. bigints := NewBigInts(len(*val))
  227. for i, v := range *val {
  228. bigints.Set(i, &BigInt{new(big.Int).SetUint64(v)})
  229. }
  230. return bigints
  231. }
  232. func (i *Interface) GetBigInt() *BigInt { return &BigInt{*i.object.(**big.Int)} }
  233. func (i *Interface) GetBigInts() *BigInts { return &BigInts{*i.object.(*[]*big.Int)} }
  234. // Interfaces is a slices of wrapped generic objects.
  235. type Interfaces struct {
  236. objects []interface{}
  237. }
  238. // NewInterfaces creates a slice of uninitialized interfaces.
  239. func NewInterfaces(size int) *Interfaces {
  240. return &Interfaces{objects: make([]interface{}, size)}
  241. }
  242. // Size returns the number of interfaces in the slice.
  243. func (i *Interfaces) Size() int {
  244. return len(i.objects)
  245. }
  246. // Get returns the bigint at the given index from the slice.
  247. // Notably the returned value can be changed without affecting the
  248. // interfaces itself.
  249. func (i *Interfaces) Get(index int) (iface *Interface, _ error) {
  250. if index < 0 || index >= len(i.objects) {
  251. return nil, errors.New("index out of bounds")
  252. }
  253. return &Interface{object: i.objects[index]}, nil
  254. }
  255. // Set sets the big int at the given index in the slice.
  256. func (i *Interfaces) Set(index int, object *Interface) error {
  257. if index < 0 || index >= len(i.objects) {
  258. return errors.New("index out of bounds")
  259. }
  260. i.objects[index] = object.object
  261. return nil
  262. }