util.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "time"
  17. "github.com/ethereum/go-ethereum/common"
  18. "github.com/ethereum/go-ethereum/logger"
  19. "github.com/ethereum/go-ethereum/state"
  20. "github.com/ethereum/go-ethereum/xeth"
  21. )
  22. var rpclogger = logger.NewLogger("RPC")
  23. type Log struct {
  24. Address string `json:"address"`
  25. Topic []string `json:"topic"`
  26. Data string `json:"data"`
  27. Number uint64 `json:"number"`
  28. }
  29. func toLogs(logs state.Logs) (ls []Log) {
  30. ls = make([]Log, len(logs))
  31. for i, log := range logs {
  32. var l Log
  33. l.Topic = make([]string, len(log.Topics()))
  34. l.Address = common.ToHex(log.Address())
  35. l.Data = common.ToHex(log.Data())
  36. l.Number = log.Number()
  37. for j, topic := range log.Topics() {
  38. l.Topic[j] = common.ToHex(topic)
  39. }
  40. ls[i] = l
  41. }
  42. return
  43. }
  44. type whisperFilter struct {
  45. messages []xeth.WhisperMessage
  46. timeout time.Time
  47. id int
  48. }
  49. func (w *whisperFilter) add(msgs ...xeth.WhisperMessage) {
  50. w.messages = append(w.messages, msgs...)
  51. }
  52. func (w *whisperFilter) get() []xeth.WhisperMessage {
  53. w.timeout = time.Now()
  54. tmp := w.messages
  55. w.messages = nil
  56. return tmp
  57. }
  58. type logFilter struct {
  59. logs state.Logs
  60. timeout time.Time
  61. id int
  62. }
  63. func (l *logFilter) add(logs ...state.Log) {
  64. l.logs = append(l.logs, logs...)
  65. }
  66. func (l *logFilter) get() state.Logs {
  67. l.timeout = time.Now()
  68. tmp := l.logs
  69. l.logs = nil
  70. return tmp
  71. }