gitlab.com/yannislg/go-pulse@v0.0.0-20210722055913-a3e24e95638d/accounts/abi/bind/topics.go (about) 1 // Copyright 2018 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 bind 18 19 import ( 20 "encoding/binary" 21 "errors" 22 "fmt" 23 "math/big" 24 "reflect" 25 26 "github.com/ethereum/go-ethereum/accounts/abi" 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/crypto" 29 ) 30 31 // makeTopics converts a filter query argument list into a filter topic set. 32 func makeTopics(query ...[]interface{}) ([][]common.Hash, error) { 33 topics := make([][]common.Hash, len(query)) 34 for i, filter := range query { 35 for _, rule := range filter { 36 var topic common.Hash 37 38 // Try to generate the topic based on simple types 39 switch rule := rule.(type) { 40 case common.Hash: 41 copy(topic[:], rule[:]) 42 case common.Address: 43 copy(topic[common.HashLength-common.AddressLength:], rule[:]) 44 case *big.Int: 45 blob := rule.Bytes() 46 copy(topic[common.HashLength-len(blob):], blob) 47 case bool: 48 if rule { 49 topic[common.HashLength-1] = 1 50 } 51 case int8: 52 copy(topic[:], genIntType(int64(rule), 1)) 53 case int16: 54 copy(topic[:], genIntType(int64(rule), 2)) 55 case int32: 56 copy(topic[:], genIntType(int64(rule), 4)) 57 case int64: 58 copy(topic[:], genIntType(rule, 8)) 59 case uint8: 60 blob := new(big.Int).SetUint64(uint64(rule)).Bytes() 61 copy(topic[common.HashLength-len(blob):], blob) 62 case uint16: 63 blob := new(big.Int).SetUint64(uint64(rule)).Bytes() 64 copy(topic[common.HashLength-len(blob):], blob) 65 case uint32: 66 blob := new(big.Int).SetUint64(uint64(rule)).Bytes() 67 copy(topic[common.HashLength-len(blob):], blob) 68 case uint64: 69 blob := new(big.Int).SetUint64(rule).Bytes() 70 copy(topic[common.HashLength-len(blob):], blob) 71 case string: 72 hash := crypto.Keccak256Hash([]byte(rule)) 73 copy(topic[:], hash[:]) 74 case []byte: 75 hash := crypto.Keccak256Hash(rule) 76 copy(topic[:], hash[:]) 77 78 default: 79 // todo(rjl493456442) according solidity documentation, indexed event 80 // parameters that are not value types i.e. arrays and structs are not 81 // stored directly but instead a keccak256-hash of an encoding is stored. 82 // 83 // We only convert stringS and bytes to hash, still need to deal with 84 // array(both fixed-size and dynamic-size) and struct. 85 86 // Attempt to generate the topic from funky types 87 val := reflect.ValueOf(rule) 88 switch { 89 // static byte array 90 case val.Kind() == reflect.Array && reflect.TypeOf(rule).Elem().Kind() == reflect.Uint8: 91 reflect.Copy(reflect.ValueOf(topic[:val.Len()]), val) 92 default: 93 return nil, fmt.Errorf("unsupported indexed type: %T", rule) 94 } 95 } 96 topics[i] = append(topics[i], topic) 97 } 98 } 99 return topics, nil 100 } 101 102 func genIntType(rule int64, size uint) []byte { 103 var topic [common.HashLength]byte 104 if rule < 0 { 105 // if a rule is negative, we need to put it into two's complement. 106 // extended to common.Hashlength bytes. 107 topic = [common.HashLength]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} 108 } 109 for i := uint(0); i < size; i++ { 110 topic[common.HashLength-i-1] = byte(rule >> (i * 8)) 111 } 112 return topic[:] 113 } 114 115 // parseTopics converts the indexed topic fields into actual log field values. 116 func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) error { 117 return parseTopicWithSetter(fields, topics, 118 func(arg abi.Argument, reconstr interface{}) { 119 field := reflect.ValueOf(out).Elem().FieldByName(capitalise(arg.Name)) 120 field.Set(reflect.ValueOf(reconstr)) 121 }) 122 } 123 124 // parseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs 125 func parseTopicsIntoMap(out map[string]interface{}, fields abi.Arguments, topics []common.Hash) error { 126 return parseTopicWithSetter(fields, topics, 127 func(arg abi.Argument, reconstr interface{}) { 128 out[arg.Name] = reconstr 129 }) 130 } 131 132 // parseTopicWithSetter converts the indexed topic field-value pairs and stores them using the 133 // provided set function. 134 // 135 // Note, dynamic types cannot be reconstructed since they get mapped to Keccak256 136 // hashes as the topic value! 137 func parseTopicWithSetter(fields abi.Arguments, topics []common.Hash, setter func(abi.Argument, interface{})) error { 138 // Sanity check that the fields and topics match up 139 if len(fields) != len(topics) { 140 return errors.New("topic/field count mismatch") 141 } 142 // Iterate over all the fields and reconstruct them from topics 143 for i, arg := range fields { 144 if !arg.Indexed { 145 return errors.New("non-indexed field in topic reconstruction") 146 } 147 var reconstr interface{} 148 switch arg.Type.T { 149 case abi.TupleTy: 150 return errors.New("tuple type in topic reconstruction") 151 case abi.StringTy, abi.BytesTy, abi.SliceTy, abi.ArrayTy: 152 // Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash 153 // whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash 154 reconstr = topics[i] 155 case abi.FunctionTy: 156 if garbage := binary.BigEndian.Uint64(topics[i][0:8]); garbage != 0 { 157 return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[i].Bytes()) 158 } 159 var tmp [24]byte 160 copy(tmp[:], topics[i][8:32]) 161 reconstr = tmp 162 default: 163 var err error 164 reconstr, err = abi.ToGoType(0, arg.Type, topics[i].Bytes()) 165 if err != nil { 166 return err 167 } 168 } 169 // Use the setter function to store the value 170 setter(arg, reconstr) 171 } 172 173 return nil 174 }