github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/go-ethereum/core/types/log.go (about)

     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  
    17  package types
    18  
    19  import (
    20  	"io"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/common/hexutil"
    24  	"github.com/ethereum/go-ethereum/rlp"
    25  )
    26  
    27  //go:generate gencodec -type Log -field-override logMarshaling -out gen_log_json.go
    28  
    29  // Log represents a contract log event. These events are generated by the LOG opcode and
    30  // stored/indexed by the node.
    31  type Log struct {
    32  	// Consensus fields:
    33  	// address of the contract that generated the event
    34  	Address common.Address `json:"address" gencodec:"required"`
    35  	// list of topics provided by the contract.
    36  	Topics []common.Hash `json:"topics" gencodec:"required"`
    37  	// supplied by the contract, usually ABI-encoded
    38  	Data []byte `json:"data" gencodec:"required"`
    39  
    40  	// Derived fields. These fields are filled in by the node
    41  	// but not secured by consensus.
    42  	// block in which the transaction was included
    43  	BlockNumber uint64 `json:"blockNumber"`
    44  	// hash of the transaction
    45  	TxHash common.Hash `json:"transactionHash" gencodec:"required"`
    46  	// index of the transaction in the block
    47  	TxIndex uint `json:"transactionIndex" gencodec:"required"`
    48  	// hash of the block in which the transaction was included
    49  	BlockHash common.Hash `json:"blockHash"`
    50  	// index of the log in the block
    51  	Index uint `json:"logIndex" gencodec:"required"`
    52  
    53  	// The Removed field is true if this log was reverted due to a chain reorganisation.
    54  	// You must pay attention to this field if you receive logs through a filter query.
    55  	Removed bool `json:"removed"`
    56  }
    57  
    58  type logMarshaling struct {
    59  	Data        hexutil.Bytes
    60  	BlockNumber hexutil.Uint64
    61  	TxIndex     hexutil.Uint
    62  	Index       hexutil.Uint
    63  }
    64  
    65  type rlpLog struct {
    66  	Address common.Address
    67  	Topics  []common.Hash
    68  	Data    []byte
    69  }
    70  
    71  // rlpStorageLog is the storage encoding of a log.
    72  type rlpStorageLog rlpLog
    73  
    74  // legacyRlpStorageLog is the previous storage encoding of a log including some redundant fields.
    75  type legacyRlpStorageLog struct {
    76  	Address     common.Address
    77  	Topics      []common.Hash
    78  	Data        []byte
    79  	BlockNumber uint64
    80  	TxHash      common.Hash
    81  	TxIndex     uint
    82  	BlockHash   common.Hash
    83  	Index       uint
    84  }
    85  
    86  // EncodeRLP implements rlp.Encoder.
    87  func (l *Log) EncodeRLP(w io.Writer) error {
    88  	return rlp.Encode(w, rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data})
    89  }
    90  
    91  // DecodeRLP implements rlp.Decoder.
    92  func (l *Log) DecodeRLP(s *rlp.Stream) error {
    93  	var dec rlpLog
    94  	err := s.Decode(&dec)
    95  	if err == nil {
    96  		l.Address, l.Topics, l.Data = dec.Address, dec.Topics, dec.Data
    97  	}
    98  	return err
    99  }
   100  
   101  // LogForStorage is a wrapper around a Log that flattens and parses the entire content of
   102  // a log including non-consensus fields.
   103  type LogForStorage Log
   104  
   105  // EncodeRLP implements rlp.Encoder.
   106  func (l *LogForStorage) EncodeRLP(w io.Writer) error {
   107  	return rlp.Encode(w, rlpStorageLog{
   108  		Address: l.Address,
   109  		Topics:  l.Topics,
   110  		Data:    l.Data,
   111  	})
   112  }
   113  
   114  // DecodeRLP implements rlp.Decoder.
   115  //
   116  // Note some redundant fields(e.g. block number, tx hash etc) will be assembled later.
   117  func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
   118  	blob, err := s.Raw()
   119  	if err != nil {
   120  		return err
   121  	}
   122  	var dec rlpStorageLog
   123  	err = rlp.DecodeBytes(blob, &dec)
   124  	if err == nil {
   125  		*l = LogForStorage{
   126  			Address: dec.Address,
   127  			Topics:  dec.Topics,
   128  			Data:    dec.Data,
   129  		}
   130  	} else {
   131  		// Try to decode log with previous definition.
   132  		var dec legacyRlpStorageLog
   133  		err = rlp.DecodeBytes(blob, &dec)
   134  		if err == nil {
   135  			*l = LogForStorage{
   136  				Address: dec.Address,
   137  				Topics:  dec.Topics,
   138  				Data:    dec.Data,
   139  			}
   140  		}
   141  	}
   142  	return err
   143  }