log.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/common/hexutil"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. var errMissingLogFields = errors.New("missing required JSON log fields")
  27. // Log represents a contract log event. These events are generated by the LOG opcode and
  28. // stored/indexed by the node.
  29. type Log struct {
  30. // Consensus fields.
  31. Address common.Address // address of the contract that generated the event
  32. Topics []common.Hash // list of topics provided by the contract.
  33. Data []byte // supplied by the contract, usually ABI-encoded
  34. // Derived fields. These fields are filled in by the node
  35. // but not secured by consensus.
  36. BlockNumber uint64 // block in which the transaction was included
  37. TxHash common.Hash // hash of the transaction
  38. TxIndex uint // index of the transaction in the block
  39. BlockHash common.Hash // hash of the block in which the transaction was included
  40. Index uint // index of the log in the receipt
  41. // The Removed field is true if this log was reverted due to a chain reorganisation.
  42. // You must pay attention to this field if you receive logs through a filter query.
  43. Removed bool
  44. }
  45. type rlpLog struct {
  46. Address common.Address
  47. Topics []common.Hash
  48. Data []byte
  49. }
  50. type rlpStorageLog struct {
  51. Address common.Address
  52. Topics []common.Hash
  53. Data []byte
  54. BlockNumber uint64
  55. TxHash common.Hash
  56. TxIndex uint
  57. BlockHash common.Hash
  58. Index uint
  59. }
  60. type jsonLog struct {
  61. Address *common.Address `json:"address"`
  62. Topics *[]common.Hash `json:"topics"`
  63. Data *hexutil.Bytes `json:"data"`
  64. BlockNumber *hexutil.Uint64 `json:"blockNumber"`
  65. TxIndex *hexutil.Uint `json:"transactionIndex"`
  66. TxHash *common.Hash `json:"transactionHash"`
  67. BlockHash *common.Hash `json:"blockHash"`
  68. Index *hexutil.Uint `json:"logIndex"`
  69. Removed bool `json:"removed"`
  70. }
  71. func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
  72. return &Log{Address: address, Topics: topics, Data: data, BlockNumber: number}
  73. }
  74. // EncodeRLP implements rlp.Encoder.
  75. func (l *Log) EncodeRLP(w io.Writer) error {
  76. return rlp.Encode(w, rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data})
  77. }
  78. // DecodeRLP implements rlp.Decoder.
  79. func (l *Log) DecodeRLP(s *rlp.Stream) error {
  80. var dec rlpLog
  81. err := s.Decode(&dec)
  82. if err == nil {
  83. l.Address, l.Topics, l.Data = dec.Address, dec.Topics, dec.Data
  84. }
  85. return err
  86. }
  87. func (l *Log) String() string {
  88. 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)
  89. }
  90. // MarshalJSON implements json.Marshaler.
  91. func (l *Log) MarshalJSON() ([]byte, error) {
  92. jslog := &jsonLog{
  93. Address: &l.Address,
  94. Topics: &l.Topics,
  95. Data: (*hexutil.Bytes)(&l.Data),
  96. TxIndex: (*hexutil.Uint)(&l.TxIndex),
  97. TxHash: &l.TxHash,
  98. Index: (*hexutil.Uint)(&l.Index),
  99. Removed: l.Removed,
  100. }
  101. // Set block information for mined logs.
  102. if (l.BlockHash != common.Hash{}) {
  103. jslog.BlockHash = &l.BlockHash
  104. jslog.BlockNumber = (*hexutil.Uint64)(&l.BlockNumber)
  105. }
  106. return json.Marshal(jslog)
  107. }
  108. // UnmarshalJSON implements json.Umarshaler.
  109. func (l *Log) UnmarshalJSON(input []byte) error {
  110. var dec jsonLog
  111. if err := json.Unmarshal(input, &dec); err != nil {
  112. return err
  113. }
  114. if dec.Address == nil || dec.Topics == nil || dec.Data == nil ||
  115. dec.TxIndex == nil || dec.TxHash == nil || dec.Index == nil {
  116. return errMissingLogFields
  117. }
  118. declog := Log{
  119. Address: *dec.Address,
  120. Topics: *dec.Topics,
  121. Data: *dec.Data,
  122. TxHash: *dec.TxHash,
  123. TxIndex: uint(*dec.TxIndex),
  124. Index: uint(*dec.Index),
  125. Removed: dec.Removed,
  126. }
  127. // Block information may be missing if the log is received through
  128. // the pending log filter, so it's handled specially here.
  129. if dec.BlockHash != nil && dec.BlockNumber != nil {
  130. declog.BlockHash = *dec.BlockHash
  131. declog.BlockNumber = uint64(*dec.BlockNumber)
  132. }
  133. *l = declog
  134. return nil
  135. }
  136. type Logs []*Log
  137. // LogForStorage is a wrapper around a Log that flattens and parses the entire content of
  138. // a log including non-consensus fields.
  139. type LogForStorage Log
  140. // EncodeRLP implements rlp.Encoder.
  141. func (l *LogForStorage) EncodeRLP(w io.Writer) error {
  142. return rlp.Encode(w, rlpStorageLog{
  143. Address: l.Address,
  144. Topics: l.Topics,
  145. Data: l.Data,
  146. BlockNumber: l.BlockNumber,
  147. TxHash: l.TxHash,
  148. TxIndex: l.TxIndex,
  149. BlockHash: l.BlockHash,
  150. Index: l.Index,
  151. })
  152. }
  153. // DecodeRLP implements rlp.Decoder.
  154. func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
  155. var dec rlpStorageLog
  156. err := s.Decode(&dec)
  157. if err == nil {
  158. *l = LogForStorage{
  159. Address: dec.Address,
  160. Topics: dec.Topics,
  161. Data: dec.Data,
  162. BlockNumber: dec.BlockNumber,
  163. TxHash: dec.TxHash,
  164. TxIndex: dec.TxIndex,
  165. BlockHash: dec.BlockHash,
  166. Index: dec.Index,
  167. }
  168. }
  169. return err
  170. }