utils.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2015 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. package rpc
  17. import (
  18. "crypto/rand"
  19. "encoding/hex"
  20. "errors"
  21. "math/big"
  22. "reflect"
  23. "unicode"
  24. "unicode/utf8"
  25. "golang.org/x/net/context"
  26. )
  27. // Is this an exported - upper case - name?
  28. func isExported(name string) bool {
  29. rune, _ := utf8.DecodeRuneInString(name)
  30. return unicode.IsUpper(rune)
  31. }
  32. // Is this type exported or a builtin?
  33. func isExportedOrBuiltinType(t reflect.Type) bool {
  34. for t.Kind() == reflect.Ptr {
  35. t = t.Elem()
  36. }
  37. // PkgPath will be non-empty even for an exported type,
  38. // so we need to check the type name as well.
  39. return isExported(t.Name()) || t.PkgPath() == ""
  40. }
  41. var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
  42. // isContextType returns an indication if the given t is of context.Context or *context.Context type
  43. func isContextType(t reflect.Type) bool {
  44. for t.Kind() == reflect.Ptr {
  45. t = t.Elem()
  46. }
  47. return t == contextType
  48. }
  49. var errorType = reflect.TypeOf((*error)(nil)).Elem()
  50. // Implements this type the error interface
  51. func isErrorType(t reflect.Type) bool {
  52. for t.Kind() == reflect.Ptr {
  53. t = t.Elem()
  54. }
  55. return t.Implements(errorType)
  56. }
  57. var subscriptionType = reflect.TypeOf((*Subscription)(nil)).Elem()
  58. // isSubscriptionType returns an indication if the given t is of Subscription or *Subscription type
  59. func isSubscriptionType(t reflect.Type) bool {
  60. for t.Kind() == reflect.Ptr {
  61. t = t.Elem()
  62. }
  63. return t == subscriptionType
  64. }
  65. // isPubSub tests whether the given method has as as first argument a context.Context
  66. // and returns the pair (Subscription, error)
  67. func isPubSub(methodType reflect.Type) bool {
  68. // numIn(0) is the receiver type
  69. if methodType.NumIn() < 2 || methodType.NumOut() != 2 {
  70. return false
  71. }
  72. return isContextType(methodType.In(1)) &&
  73. isSubscriptionType(methodType.Out(0)) &&
  74. isErrorType(methodType.Out(1))
  75. }
  76. // formatName will convert to first character to lower case
  77. func formatName(name string) string {
  78. ret := []rune(name)
  79. if len(ret) > 0 {
  80. ret[0] = unicode.ToLower(ret[0])
  81. }
  82. return string(ret)
  83. }
  84. var bigIntType = reflect.TypeOf((*big.Int)(nil)).Elem()
  85. // Indication if this type should be serialized in hex
  86. func isHexNum(t reflect.Type) bool {
  87. if t == nil {
  88. return false
  89. }
  90. for t.Kind() == reflect.Ptr {
  91. t = t.Elem()
  92. }
  93. return t == bigIntType
  94. }
  95. var blockNumberType = reflect.TypeOf((*BlockNumber)(nil)).Elem()
  96. // Indication if the given block is a BlockNumber
  97. func isBlockNumber(t reflect.Type) bool {
  98. if t == nil {
  99. return false
  100. }
  101. for t.Kind() == reflect.Ptr {
  102. t = t.Elem()
  103. }
  104. return t == blockNumberType
  105. }
  106. // suitableCallbacks iterates over the methods of the given type. It will determine if a method satisfies the criteria
  107. // for a RPC callback or a subscription callback and adds it to the collection of callbacks or subscriptions. See server
  108. // documentation for a summary of these criteria.
  109. func suitableCallbacks(rcvr reflect.Value, typ reflect.Type) (callbacks, subscriptions) {
  110. callbacks := make(callbacks)
  111. subscriptions := make(subscriptions)
  112. METHODS:
  113. for m := 0; m < typ.NumMethod(); m++ {
  114. method := typ.Method(m)
  115. mtype := method.Type
  116. mname := formatName(method.Name)
  117. if method.PkgPath != "" { // method must be exported
  118. continue
  119. }
  120. var h callback
  121. h.isSubscribe = isPubSub(mtype)
  122. h.rcvr = rcvr
  123. h.method = method
  124. h.errPos = -1
  125. firstArg := 1
  126. numIn := mtype.NumIn()
  127. if numIn >= 2 && mtype.In(1) == contextType {
  128. h.hasCtx = true
  129. firstArg = 2
  130. }
  131. if h.isSubscribe {
  132. h.argTypes = make([]reflect.Type, numIn-firstArg) // skip rcvr type
  133. for i := firstArg; i < numIn; i++ {
  134. argType := mtype.In(i)
  135. if isExportedOrBuiltinType(argType) {
  136. h.argTypes[i-firstArg] = argType
  137. } else {
  138. continue METHODS
  139. }
  140. }
  141. subscriptions[mname] = &h
  142. continue METHODS
  143. }
  144. // determine method arguments, ignore first arg since it's the receiver type
  145. // Arguments must be exported or builtin types
  146. h.argTypes = make([]reflect.Type, numIn-firstArg)
  147. for i := firstArg; i < numIn; i++ {
  148. argType := mtype.In(i)
  149. if !isExportedOrBuiltinType(argType) {
  150. continue METHODS
  151. }
  152. h.argTypes[i-firstArg] = argType
  153. }
  154. // check that all returned values are exported or builtin types
  155. for i := 0; i < mtype.NumOut(); i++ {
  156. if !isExportedOrBuiltinType(mtype.Out(i)) {
  157. continue METHODS
  158. }
  159. }
  160. // when a method returns an error it must be the last returned value
  161. h.errPos = -1
  162. for i := 0; i < mtype.NumOut(); i++ {
  163. if isErrorType(mtype.Out(i)) {
  164. h.errPos = i
  165. break
  166. }
  167. }
  168. if h.errPos >= 0 && h.errPos != mtype.NumOut()-1 {
  169. continue METHODS
  170. }
  171. switch mtype.NumOut() {
  172. case 0, 1:
  173. break
  174. case 2:
  175. if h.errPos == -1 { // method must one return value and 1 error
  176. continue METHODS
  177. }
  178. break
  179. default:
  180. continue METHODS
  181. }
  182. callbacks[mname] = &h
  183. }
  184. return callbacks, subscriptions
  185. }
  186. func newSubscriptionID() (string, error) {
  187. var subid [16]byte
  188. n, _ := rand.Read(subid[:])
  189. if n != 16 {
  190. return "", errors.New("Unable to generate subscription id")
  191. }
  192. return "0x" + hex.EncodeToString(subid[:]), nil
  193. }