github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/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/ethw3/go-ethereuma/common" 23 "github.com/ethw3/go-ethereuma/common/hexutil" 24 "github.com/ethw3/go-ethereuma/rlp" 25 ) 26 27 //go:generate go run github.com/fjl/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"` 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"` 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 //go:generate go run ../../rlp/rlpgen -type rlpLog -out gen_log_rlp.go 66 67 type rlpLog struct { 68 Address common.Address 69 Topics []common.Hash 70 Data []byte 71 } 72 73 // legacyRlpStorageLog is the previous storage encoding of a log including some redundant fields. 74 type legacyRlpStorageLog struct { 75 Address common.Address 76 Topics []common.Hash 77 Data []byte 78 BlockNumber uint64 79 TxHash common.Hash 80 TxIndex uint 81 BlockHash common.Hash 82 Index uint 83 } 84 85 // EncodeRLP implements rlp.Encoder. 86 func (l *Log) EncodeRLP(w io.Writer) error { 87 rl := rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data} 88 return rlp.Encode(w, &rl) 89 } 90 91 // DecodeRLP implements rlp.Decoder. 92 func (l *Log) DecodeRLP(s *rlp.Stream) error { 93 var dec rlpLog 94 err := s.Decode(&dec) 95 if err == nil { 96 l.Address, l.Topics, l.Data = dec.Address, dec.Topics, dec.Data 97 } 98 return err 99 } 100 101 // LogForStorage is a wrapper around a Log that handles 102 // backward compatibility with prior storage formats. 103 type LogForStorage Log 104 105 // EncodeRLP implements rlp.Encoder. 106 func (l *LogForStorage) EncodeRLP(w io.Writer) error { 107 rl := rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data} 108 return rlp.Encode(w, &rl) 109 } 110 111 // DecodeRLP implements rlp.Decoder. 112 // 113 // Note some redundant fields(e.g. block number, tx hash etc) will be assembled later. 114 func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error { 115 blob, err := s.Raw() 116 if err != nil { 117 return err 118 } 119 var dec rlpLog 120 err = rlp.DecodeBytes(blob, &dec) 121 if err == nil { 122 *l = LogForStorage{ 123 Address: dec.Address, 124 Topics: dec.Topics, 125 Data: dec.Data, 126 } 127 } else { 128 // Try to decode log with previous definition. 129 var dec legacyRlpStorageLog 130 err = rlp.DecodeBytes(blob, &dec) 131 if err == nil { 132 *l = LogForStorage{ 133 Address: dec.Address, 134 Topics: dec.Topics, 135 Data: dec.Data, 136 } 137 } 138 } 139 return err 140 }