log.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "errors"
  20. "fmt"
  21. "io"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. var errMissingLogFields = errors.New("missing required JSON log fields")
  26. // Log represents a contract log event. These events are generated by the LOG
  27. // opcode and stored/indexed by the node.
  28. type Log struct {
  29. // Consensus fields.
  30. Address common.Address // address of the contract that generated the event
  31. Topics []common.Hash // list of topics provided by the contract.
  32. Data []byte // supplied by the contract, usually ABI-encoded
  33. // Derived fields (don't reorder!).
  34. BlockNumber uint64 // block in which the transaction was included
  35. TxHash common.Hash // hash of the transaction
  36. TxIndex uint // index of the transaction in the block
  37. BlockHash common.Hash // hash of the block in which the transaction was included
  38. Index uint // index of the log in the receipt
  39. }
  40. type jsonLog struct {
  41. Address *common.Address `json:"address"`
  42. Topics *[]common.Hash `json:"topics"`
  43. Data string `json:"data"`
  44. BlockNumber string `json:"blockNumber"`
  45. TxIndex string `json:"transactionIndex"`
  46. TxHash *common.Hash `json:"transactionHash"`
  47. BlockHash *common.Hash `json:"blockHash"`
  48. Index string `json:"logIndex"`
  49. }
  50. func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
  51. return &Log{Address: address, Topics: topics, Data: data, BlockNumber: number}
  52. }
  53. func (l *Log) EncodeRLP(w io.Writer) error {
  54. return rlp.Encode(w, []interface{}{l.Address, l.Topics, l.Data})
  55. }
  56. func (l *Log) DecodeRLP(s *rlp.Stream) error {
  57. var log struct {
  58. Address common.Address
  59. Topics []common.Hash
  60. Data []byte
  61. }
  62. if err := s.Decode(&log); err != nil {
  63. return err
  64. }
  65. l.Address, l.Topics, l.Data = log.Address, log.Topics, log.Data
  66. return nil
  67. }
  68. func (l *Log) String() string {
  69. 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)
  70. }
  71. // MarshalJSON implements json.Marshaler.
  72. func (r *Log) MarshalJSON() ([]byte, error) {
  73. return json.Marshal(&jsonLog{
  74. Address: &r.Address,
  75. Topics: &r.Topics,
  76. Data: fmt.Sprintf("0x%x", r.Data),
  77. BlockNumber: fmt.Sprintf("0x%x", r.BlockNumber),
  78. TxIndex: fmt.Sprintf("0x%x", r.TxIndex),
  79. TxHash: &r.TxHash,
  80. BlockHash: &r.BlockHash,
  81. Index: fmt.Sprintf("0x%x", r.Index),
  82. })
  83. }
  84. // UnmarshalJSON implements json.Umarshaler.
  85. func (r *Log) UnmarshalJSON(input []byte) error {
  86. var dec jsonLog
  87. if err := json.Unmarshal(input, &dec); err != nil {
  88. return err
  89. }
  90. if dec.Address == nil || dec.Topics == nil || dec.Data == "" || dec.BlockNumber == "" ||
  91. dec.TxIndex == "" || dec.TxHash == nil || dec.BlockHash == nil || dec.Index == "" {
  92. return errMissingLogFields
  93. }
  94. declog := Log{
  95. Address: *dec.Address,
  96. Topics: *dec.Topics,
  97. TxHash: *dec.TxHash,
  98. BlockHash: *dec.BlockHash,
  99. }
  100. if _, err := fmt.Sscanf(dec.Data, "0x%x", &declog.Data); err != nil {
  101. return fmt.Errorf("invalid hex log data")
  102. }
  103. if _, err := fmt.Sscanf(dec.BlockNumber, "0x%x", &declog.BlockNumber); err != nil {
  104. return fmt.Errorf("invalid hex log block number")
  105. }
  106. if _, err := fmt.Sscanf(dec.TxIndex, "0x%x", &declog.TxIndex); err != nil {
  107. return fmt.Errorf("invalid hex log tx index")
  108. }
  109. if _, err := fmt.Sscanf(dec.Index, "0x%x", &declog.Index); err != nil {
  110. return fmt.Errorf("invalid hex log index")
  111. }
  112. *r = declog
  113. return nil
  114. }
  115. type Logs []*Log
  116. // LogForStorage is a wrapper around a Log that flattens and parses the entire
  117. // content of a log, as opposed to only the consensus fields originally (by hiding
  118. // the rlp interface methods).
  119. type LogForStorage Log