github.com/amazechain/amc@v0.1.3/common/block/log.go (about) 1 // Copyright 2022 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package block 18 19 import ( 20 "fmt" 21 "github.com/amazechain/amc/api/protocol/types_pb" 22 "github.com/amazechain/amc/common/types" 23 24 "github.com/amazechain/amc/utils" 25 "github.com/holiman/uint256" 26 "google.golang.org/protobuf/proto" 27 ) 28 29 type Log struct { 30 // Consensus fields: 31 // address of the contract that generated the event 32 Address types.Address `json:"address" gencodec:"required"` 33 // list of topics provided by the contract. 34 Topics []types.Hash `json:"topics" gencodec:"required"` 35 // supplied by the contract, usually ABI-encoded 36 Data []byte `json:"data" gencodec:"required"` 37 38 // Derived fields. These fields are filled in by the node 39 // but not secured by consensus. 40 // block in which the transaction was included 41 BlockNumber *uint256.Int `json:"blockNumber"` 42 // hash of the transaction 43 TxHash types.Hash `json:"transactionHash" gencodec:"required"` 44 // index of the transaction in the block 45 TxIndex uint `json:"transactionIndex" gencodec:"required"` 46 // hash of the block in which the transaction was included 47 BlockHash types.Hash `json:"blockHash"` 48 // index of the log in the receipt 49 Index uint `json:"logIndex" gencodec:"required"` 50 51 // The Removed field is true if this log was reverted due to a chain reorganisation. 52 // You must pay attention to this field if you receive logs through a filter query. 53 Removed bool `json:"removed"` 54 } 55 56 func (l *Log) ToProtoMessage() proto.Message { 57 return &types_pb.Log{ 58 Address: utils.ConvertAddressToH160(l.Address), 59 Topics: utils.ConvertHashesToH256(l.Topics), 60 Data: l.Data, 61 BlockNumber: utils.ConvertUint256IntToH256(l.BlockNumber), 62 TxHash: utils.ConvertHashToH256(l.TxHash), 63 TxIndex: uint64(l.TxIndex), 64 BlockHash: utils.ConvertHashToH256(l.BlockHash), 65 Index: uint64(l.Index), 66 Removed: l.Removed, 67 } 68 } 69 70 func (l *Log) FromProtoMessage(message proto.Message) error { 71 var ( 72 pLog *types_pb.Log 73 ok bool 74 ) 75 76 if pLog, ok = message.(*types_pb.Log); !ok { 77 return fmt.Errorf("type conversion failure") 78 } 79 80 l.Address = utils.ConvertH160toAddress(pLog.Address) 81 l.Topics = utils.H256sToHashes(pLog.Topics) 82 l.Data = pLog.Data 83 l.BlockNumber = utils.ConvertH256ToUint256Int(pLog.BlockNumber) 84 l.TxHash = utils.ConvertH256ToHash(pLog.TxHash) 85 l.TxIndex = uint(pLog.TxIndex) 86 l.BlockHash = utils.ConvertH256ToHash(pLog.BlockHash) 87 l.Index = uint(pLog.Index) 88 l.Removed = pLog.Removed 89 90 return nil 91 } 92 93 type Logs []*Log 94 95 func (l *Logs) Marshal() ([]byte, error) { 96 pb := new(types_pb.Logs) 97 for _, log := range *l { 98 pb.Logs = append(pb.Logs, log.ToProtoMessage().(*types_pb.Log)) 99 } 100 101 return proto.Marshal(pb) 102 } 103 104 func (l *Logs) Unmarshal(data []byte) error { 105 pb := new(types_pb.Logs) 106 if err := proto.Unmarshal(data, pb); nil != err { 107 return err 108 } 109 110 body := make([]*Log, len(pb.Logs)) 111 for i, p := range pb.Logs { 112 if err := body[i].FromProtoMessage(p); nil != err { 113 return err 114 } 115 } 116 *l = body 117 return nil 118 }