github.com/aerth/aquachain@v1.4.1/core/types/log.go (about)

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