github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/core/vm/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 vm
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/ethereumproject/go-ethereum/common"
    25  	"github.com/ethereumproject/go-ethereum/rlp"
    26  )
    27  
    28  type Log struct {
    29  	// Consensus fields
    30  	Address common.Address
    31  	Topics  []common.Hash
    32  	Data    []byte
    33  
    34  	// Derived fields (don't reorder!)
    35  	BlockNumber uint64
    36  	TxHash      common.Hash
    37  	TxIndex     uint
    38  	BlockHash   common.Hash
    39  	Index       uint
    40  }
    41  
    42  func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
    43  	return &Log{Address: address, Topics: topics, Data: data, BlockNumber: number}
    44  }
    45  
    46  func (l *Log) EncodeRLP(w io.Writer) error {
    47  	return rlp.Encode(w, []interface{}{l.Address, l.Topics, l.Data})
    48  }
    49  
    50  func (l *Log) DecodeRLP(s *rlp.Stream) error {
    51  	var log struct {
    52  		Address common.Address
    53  		Topics  []common.Hash
    54  		Data    []byte
    55  	}
    56  	if err := s.Decode(&log); err != nil {
    57  		return err
    58  	}
    59  	l.Address, l.Topics, l.Data = log.Address, log.Topics, log.Data
    60  	return nil
    61  }
    62  
    63  func (l *Log) String() string {
    64  	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)
    65  }
    66  
    67  func (r *Log) MarshalJSON() ([]byte, error) {
    68  	fields := map[string]interface{}{
    69  		"address":          r.Address,
    70  		"data":             fmt.Sprintf("%#x", r.Data),
    71  		"blockNumber":      fmt.Sprintf("%#x", r.BlockNumber),
    72  		"logIndex":         fmt.Sprintf("%#x", r.Index),
    73  		"blockHash":        r.BlockHash,
    74  		"transactionHash":  r.TxHash,
    75  		"transactionIndex": fmt.Sprintf("%#x", r.TxIndex),
    76  		"topics":           r.Topics,
    77  	}
    78  
    79  	return json.Marshal(fields)
    80  }
    81  
    82  type Logs []*Log
    83  
    84  // LogForStorage is a wrapper around a Log that flattens and parses the entire
    85  // content of a log, as opposed to only the consensus fields originally (by hiding
    86  // the rlp interface methods).
    87  type LogForStorage Log