github.com/codingfuture/orig-energi3@v0.8.4/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  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  // LogForStorage is a wrapper around a Log that flattens and parses the entire content of
    98  // a log including non-consensus fields.
    99  type LogForStorage Log
   100  
   101  // EncodeRLP implements rlp.Encoder.
   102  func (l *LogForStorage) EncodeRLP(w io.Writer) error {
   103  	return rlp.Encode(w, rlpStorageLog{
   104  		Address:     l.Address,
   105  		Topics:      l.Topics,
   106  		Data:        l.Data,
   107  		BlockNumber: l.BlockNumber,
   108  		TxHash:      l.TxHash,
   109  		TxIndex:     l.TxIndex,
   110  		BlockHash:   l.BlockHash,
   111  		Index:       l.Index,
   112  	})
   113  }
   114  
   115  // DecodeRLP implements rlp.Decoder.
   116  func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
   117  	var dec rlpStorageLog
   118  	err := s.Decode(&dec)
   119  	if err == nil {
   120  		*l = LogForStorage{
   121  			Address:     dec.Address,
   122  			Topics:      dec.Topics,
   123  			Data:        dec.Data,
   124  			BlockNumber: dec.BlockNumber,
   125  			TxHash:      dec.TxHash,
   126  			TxIndex:     dec.TxIndex,
   127  			BlockHash:   dec.BlockHash,
   128  			Index:       dec.Index,
   129  		}
   130  	}
   131  	return err
   132  }