github.com/klaytn/klaytn@v1.10.2/blockchain/types/log.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/types/log.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package types
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  
    27  	"github.com/klaytn/klaytn/common"
    28  	"github.com/klaytn/klaytn/common/hexutil"
    29  	"github.com/klaytn/klaytn/rlp"
    30  )
    31  
    32  //go:generate gencodec -type Log -field-override logMarshaling -out gen_log_json.go
    33  
    34  // Log represents a contract log event. These events are generated by the LOG opcode and
    35  // stored/indexed by the node.
    36  type Log struct {
    37  	// Consensus fields:
    38  	// address of the contract that generated the event
    39  	Address common.Address `json:"address" gencodec:"required"`
    40  	// list of topics provided by the contract.
    41  	Topics []common.Hash `json:"topics" gencodec:"required"`
    42  	// supplied by the contract, usually ABI-encoded
    43  	Data []byte `json:"data" gencodec:"required"`
    44  
    45  	// Derived fields. These fields are filled in by the node
    46  	// but not secured by consensus.
    47  	// block in which the transaction was included
    48  	BlockNumber uint64 `json:"blockNumber"`
    49  	// hash of the transaction
    50  	TxHash common.Hash `json:"transactionHash" gencodec:"required"`
    51  	// index of the transaction in the block
    52  	TxIndex uint `json:"transactionIndex" gencodec:"required"`
    53  	// hash of the block in which the transaction was included
    54  	BlockHash common.Hash `json:"blockHash"`
    55  	// index of the log in the receipt
    56  	Index uint `json:"logIndex" gencodec:"required"`
    57  
    58  	// The Removed field is true if this log was reverted due to a chain reorganisation.
    59  	// You must pay attention to this field if you receive logs through a filter query.
    60  	Removed bool `json:"removed"`
    61  }
    62  
    63  type logMarshaling struct {
    64  	Data        hexutil.Bytes
    65  	BlockNumber hexutil.Uint64
    66  	TxIndex     hexutil.Uint
    67  	Index       hexutil.Uint
    68  }
    69  
    70  type rlpLog struct {
    71  	Address common.Address
    72  	Topics  []common.Hash
    73  	Data    []byte
    74  }
    75  
    76  type rlpStorageLog struct {
    77  	Address     common.Address
    78  	Topics      []common.Hash
    79  	Data        []byte
    80  	BlockNumber uint64
    81  	TxHash      common.Hash
    82  	TxIndex     uint
    83  	BlockHash   common.Hash
    84  	Index       uint
    85  }
    86  
    87  // EncodeRLP implements rlp.Encoder.
    88  func (l *Log) EncodeRLP(w io.Writer) error {
    89  	return rlp.Encode(w, rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data})
    90  }
    91  
    92  // DecodeRLP implements rlp.Decoder.
    93  func (l *Log) DecodeRLP(s *rlp.Stream) error {
    94  	var dec rlpLog
    95  	err := s.Decode(&dec)
    96  	if err == nil {
    97  		l.Address, l.Topics, l.Data = dec.Address, dec.Topics, dec.Data
    98  	}
    99  	return err
   100  }
   101  
   102  func (l *Log) String() string {
   103  	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)
   104  }
   105  
   106  // LogForStorage is a wrapper around a Log that flattens and parses the entire content of
   107  // a log including non-consensus fields.
   108  type LogForStorage Log
   109  
   110  // EncodeRLP implements rlp.Encoder.
   111  func (l *LogForStorage) EncodeRLP(w io.Writer) error {
   112  	return rlp.Encode(w, rlpStorageLog{
   113  		Address:     l.Address,
   114  		Topics:      l.Topics,
   115  		Data:        l.Data,
   116  		BlockNumber: l.BlockNumber,
   117  		TxHash:      l.TxHash,
   118  		TxIndex:     l.TxIndex,
   119  		BlockHash:   l.BlockHash,
   120  		Index:       l.Index,
   121  	})
   122  }
   123  
   124  // DecodeRLP implements rlp.Decoder.
   125  func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
   126  	var dec rlpStorageLog
   127  	err := s.Decode(&dec)
   128  	if err == nil {
   129  		*l = LogForStorage{
   130  			Address:     dec.Address,
   131  			Topics:      dec.Topics,
   132  			Data:        dec.Data,
   133  			BlockNumber: dec.BlockNumber,
   134  			TxHash:      dec.TxHash,
   135  			TxIndex:     dec.TxIndex,
   136  			BlockHash:   dec.BlockHash,
   137  			Index:       dec.Index,
   138  		}
   139  	}
   140  	return err
   141  }