log.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package state
  2. import (
  3. "fmt"
  4. "github.com/ethereum/go-ethereum/common"
  5. )
  6. type Log interface {
  7. common.RlpEncodable
  8. Address() []byte
  9. Topics() [][]byte
  10. Data() []byte
  11. Number() uint64
  12. }
  13. type StateLog struct {
  14. address []byte
  15. topics [][]byte
  16. data []byte
  17. number uint64
  18. }
  19. func NewLog(address []byte, topics [][]byte, data []byte, number uint64) *StateLog {
  20. return &StateLog{address, topics, data, number}
  21. }
  22. func (self *StateLog) Address() []byte {
  23. return self.address
  24. }
  25. func (self *StateLog) Topics() [][]byte {
  26. return self.topics
  27. }
  28. func (self *StateLog) Data() []byte {
  29. return self.data
  30. }
  31. func (self *StateLog) Number() uint64 {
  32. return self.number
  33. }
  34. func NewLogFromValue(decoder *common.Value) *StateLog {
  35. log := &StateLog{
  36. address: decoder.Get(0).Bytes(),
  37. data: decoder.Get(2).Bytes(),
  38. }
  39. it := decoder.Get(1).NewIterator()
  40. for it.Next() {
  41. log.topics = append(log.topics, it.Value().Bytes())
  42. }
  43. return log
  44. }
  45. func (self *StateLog) RlpData() interface{} {
  46. return []interface{}{self.address, common.ByteSliceToInterface(self.topics), self.data}
  47. }
  48. func (self *StateLog) String() string {
  49. return fmt.Sprintf(`log: %x %x %x`, self.address, self.topics, self.data)
  50. }
  51. type Logs []Log
  52. func (self Logs) RlpData() interface{} {
  53. data := make([]interface{}, len(self))
  54. for i, log := range self {
  55. data[i] = log.RlpData()
  56. }
  57. return data
  58. }
  59. func (self Logs) String() (ret string) {
  60. for _, log := range self {
  61. ret += fmt.Sprintf("%v", log)
  62. }
  63. return "[" + ret + "]"
  64. }