log.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2014 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 vm
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "io"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. type Log struct {
  25. // Consensus fields
  26. Address common.Address
  27. Topics []common.Hash
  28. Data []byte
  29. // Derived fields (don't reorder!)
  30. BlockNumber uint64
  31. TxHash common.Hash
  32. TxIndex uint
  33. BlockHash common.Hash
  34. Index uint
  35. }
  36. func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
  37. return &Log{Address: address, Topics: topics, Data: data, BlockNumber: number}
  38. }
  39. func (l *Log) EncodeRLP(w io.Writer) error {
  40. return rlp.Encode(w, []interface{}{l.Address, l.Topics, l.Data})
  41. }
  42. func (l *Log) DecodeRLP(s *rlp.Stream) error {
  43. var log struct {
  44. Address common.Address
  45. Topics []common.Hash
  46. Data []byte
  47. }
  48. if err := s.Decode(&log); err != nil {
  49. return err
  50. }
  51. l.Address, l.Topics, l.Data = log.Address, log.Topics, log.Data
  52. return nil
  53. }
  54. func (l *Log) String() string {
  55. return fmt.Sprintf(`log: %x %x %x %x %d %x %d`, l.Address, l.Topics, l.Data, l.TxHash, l.TxIndex, l.BlockHash, l.Index)
  56. }
  57. func (r *Log) MarshalJSON() ([]byte, error) {
  58. fields := map[string]interface{}{
  59. "address": r.Address,
  60. "data": fmt.Sprintf("%#x", r.Data),
  61. "blockNumber": fmt.Sprintf("%#x", r.BlockNumber),
  62. "logIndex": fmt.Sprintf("%#x", r.Index),
  63. "blockHash": r.BlockHash,
  64. "transactionHash": r.TxHash,
  65. "transactionIndex": fmt.Sprintf("%#x", r.TxIndex),
  66. "topics": r.Topics,
  67. }
  68. return json.Marshal(fields)
  69. }
  70. type Logs []*Log
  71. // LogForStorage is a wrapper around a Log that flattens and parses the entire
  72. // content of a log, as opposed to only the consensus fields originally (by hiding
  73. // the rlp interface methods).
  74. type LogForStorage Log