github.com/beyonderyue/gochain@v2.2.26+incompatible/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/gochain-io/gochain/common"
    23  	"github.com/gochain-io/gochain/common/hexutil"
    24  	"github.com/gochain-io/gochain/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  type rlpStorageLog struct {
    72  	Address     common.Address
    73  	Topics      []common.Hash
    74  	Data        []byte
    75  	BlockNumber uint64
    76  	TxHash      common.Hash
    77  	TxIndex     uint
    78  	BlockHash   common.Hash
    79  	Index       uint
    80  }
    81  
    82  // EncodeRLP implements rlp.Encoder.
    83  func (l *Log) EncodeRLP(w io.Writer) error {
    84  	return rlp.Encode(w, rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data})
    85  }
    86  
    87  // DecodeRLP implements rlp.Decoder.
    88  func (l *Log) DecodeRLP(s *rlp.Stream) error {
    89  	var dec rlpLog
    90  	err := s.Decode(&dec)
    91  	if err == nil {
    92  		l.Address, l.Topics, l.Data = dec.Address, dec.Topics, dec.Data
    93  	}
    94  	return err
    95  }
    96  
    97  // LogsForStorage RLP encodes as []*LogForStorage.
    98  type LogsForStorage []*Log
    99  
   100  func (l LogsForStorage) EncodeRLPElem(i int, w io.Writer) error {
   101  	return rlp.Encode(w, (*LogForStorage)(l[i]))
   102  }
   103  
   104  func (l *LogsForStorage) DecodeRLPElem(s *rlp.Stream) error {
   105  	var log LogForStorage
   106  	if err := s.Decode(&log); err != nil {
   107  		return err
   108  	}
   109  	*l = append(*l, (*Log)(&log))
   110  	return nil
   111  }
   112  
   113  // LogForStorage is a wrapper around a Log that flattens and parses the entire content of
   114  // a log including non-consensus fields.
   115  type LogForStorage Log
   116  
   117  // EncodeRLP implements rlp.Encoder.
   118  func (l *LogForStorage) EncodeRLP(w io.Writer) error {
   119  	return rlp.Encode(w, rlpStorageLog{
   120  		Address:     l.Address,
   121  		Topics:      l.Topics,
   122  		Data:        l.Data,
   123  		BlockNumber: l.BlockNumber,
   124  		TxHash:      l.TxHash,
   125  		TxIndex:     l.TxIndex,
   126  		BlockHash:   l.BlockHash,
   127  		Index:       l.Index,
   128  	})
   129  }
   130  
   131  // DecodeRLP implements rlp.Decoder.
   132  func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
   133  	var dec rlpStorageLog
   134  	err := s.Decode(&dec)
   135  	if err == nil {
   136  		*l = LogForStorage{
   137  			Address:     dec.Address,
   138  			Topics:      dec.Topics,
   139  			Data:        dec.Data,
   140  			BlockNumber: dec.BlockNumber,
   141  			TxHash:      dec.TxHash,
   142  			TxIndex:     dec.TxIndex,
   143  			BlockHash:   dec.BlockHash,
   144  			Index:       dec.Index,
   145  		}
   146  	}
   147  	return err
   148  }