util.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package rpc
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "math/big"
  19. "reflect"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/state"
  24. "github.com/ethereum/go-ethereum/xeth"
  25. )
  26. var rpclogger = logger.NewLogger("RPC")
  27. // Unmarshal state is a helper method which has the ability to decode messsages
  28. // that use the `defaultBlock` (https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter)
  29. // For example a `call`: [{to: "0x....", data:"0x..."}, "latest"]. The first argument is the transaction
  30. // message and the second one refers to the block height (or state) to which to apply this `call`.
  31. func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error) {
  32. var data []json.RawMessage
  33. if err = json.Unmarshal(b, &data); err != nil && len(data) == 0 {
  34. return NewDecodeParamError(err.Error())
  35. }
  36. // Hrm... Occurs when no params
  37. if len(data) == 0 {
  38. return NewDecodeParamError("No data")
  39. }
  40. // Number index determines the index in the array for a possible block number
  41. numberIndex := 0
  42. value := reflect.ValueOf(iface)
  43. rvalue := reflect.Indirect(value)
  44. switch rvalue.Kind() {
  45. case reflect.Slice:
  46. // This is a bit of a cheat, but `data` is expected to be larger than 2 if iface is a slice
  47. if number != nil {
  48. numberIndex = len(data) - 1
  49. } else {
  50. numberIndex = len(data)
  51. }
  52. slice := reflect.MakeSlice(rvalue.Type(), numberIndex, numberIndex)
  53. for i, raw := range data[0:numberIndex] {
  54. v := slice.Index(i).Interface()
  55. if err = json.Unmarshal(raw, &v); err != nil {
  56. fmt.Println(err, v)
  57. return err
  58. }
  59. slice.Index(i).Set(reflect.ValueOf(v))
  60. }
  61. reflect.Indirect(rvalue).Set(slice) //value.Set(slice)
  62. case reflect.Struct:
  63. fallthrough
  64. default:
  65. if err = json.Unmarshal(data[0], iface); err != nil {
  66. return NewDecodeParamError(err.Error())
  67. }
  68. numberIndex = 1
  69. }
  70. // <0 index means out of bound for block number
  71. if numberIndex >= 0 && len(data) > numberIndex {
  72. if err = blockNumber(data[numberIndex], number); err != nil {
  73. return NewDecodeParamError(err.Error())
  74. }
  75. }
  76. return nil
  77. }
  78. func i2hex(n int) string {
  79. return common.ToHex(big.NewInt(int64(n)).Bytes())
  80. }
  81. type RpcServer interface {
  82. Start()
  83. Stop()
  84. }
  85. type Log struct {
  86. Address string `json:"address"`
  87. Topic []string `json:"topic"`
  88. Data string `json:"data"`
  89. Number uint64 `json:"number"`
  90. }
  91. func toLogs(logs state.Logs) (ls []Log) {
  92. ls = make([]Log, len(logs))
  93. for i, log := range logs {
  94. var l Log
  95. l.Topic = make([]string, len(log.Topics()))
  96. l.Address = log.Address().Hex()
  97. l.Data = common.ToHex(log.Data())
  98. l.Number = log.Number()
  99. for j, topic := range log.Topics() {
  100. l.Topic[j] = topic.Hex()
  101. }
  102. ls[i] = l
  103. }
  104. return
  105. }
  106. type whisperFilter struct {
  107. messages []xeth.WhisperMessage
  108. timeout time.Time
  109. id int
  110. }
  111. func (w *whisperFilter) add(msgs ...xeth.WhisperMessage) {
  112. w.messages = append(w.messages, msgs...)
  113. }
  114. func (w *whisperFilter) get() []xeth.WhisperMessage {
  115. w.timeout = time.Now()
  116. tmp := w.messages
  117. w.messages = nil
  118. return tmp
  119. }
  120. type logFilter struct {
  121. logs state.Logs
  122. timeout time.Time
  123. id int
  124. }
  125. func (l *logFilter) add(logs ...state.Log) {
  126. l.logs = append(l.logs, logs...)
  127. }
  128. func (l *logFilter) get() state.Logs {
  129. l.timeout = time.Now()
  130. tmp := l.logs
  131. l.logs = nil
  132. return tmp
  133. }