github.com/luckypickle/go-ethereum-vet@v1.14.2/accounts/abi/reflect.go (about) 1 // Copyright 2016 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 abi 18 19 import ( 20 "fmt" 21 "reflect" 22 "strings" 23 ) 24 25 // indirect recursively dereferences the value until it either gets the value 26 // or finds a big.Int 27 func indirect(v reflect.Value) reflect.Value { 28 if v.Kind() == reflect.Ptr && v.Elem().Type() != derefbigT { 29 return indirect(v.Elem()) 30 } 31 return v 32 } 33 34 // reflectIntKind returns the reflect using the given size and 35 // unsignedness. 36 func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) { 37 switch size { 38 case 8: 39 if unsigned { 40 return reflect.Uint8, uint8T 41 } 42 return reflect.Int8, int8T 43 case 16: 44 if unsigned { 45 return reflect.Uint16, uint16T 46 } 47 return reflect.Int16, int16T 48 case 32: 49 if unsigned { 50 return reflect.Uint32, uint32T 51 } 52 return reflect.Int32, int32T 53 case 64: 54 if unsigned { 55 return reflect.Uint64, uint64T 56 } 57 return reflect.Int64, int64T 58 } 59 return reflect.Ptr, bigT 60 } 61 62 // mustArrayToBytesSlice creates a new byte slice with the exact same size as value 63 // and copies the bytes in value to the new slice. 64 func mustArrayToByteSlice(value reflect.Value) reflect.Value { 65 slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len()) 66 reflect.Copy(slice, value) 67 return slice 68 } 69 70 // set attempts to assign src to dst by either setting, copying or otherwise. 71 // 72 // set is a bit more lenient when it comes to assignment and doesn't force an as 73 // strict ruleset as bare `reflect` does. 74 func set(dst, src reflect.Value, output Argument) error { 75 dstType := dst.Type() 76 srcType := src.Type() 77 switch { 78 case dstType.AssignableTo(srcType): 79 dst.Set(src) 80 case dstType.Kind() == reflect.Interface: 81 dst.Set(src) 82 case dstType.Kind() == reflect.Ptr: 83 return set(dst.Elem(), src, output) 84 default: 85 return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type()) 86 } 87 return nil 88 } 89 90 // requireAssignable assures that `dest` is a pointer and it's not an interface. 91 func requireAssignable(dst, src reflect.Value) error { 92 if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface { 93 return fmt.Errorf("abi: cannot unmarshal %v into %v", src.Type(), dst.Type()) 94 } 95 return nil 96 } 97 98 // requireUnpackKind verifies preconditions for unpacking `args` into `kind` 99 func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind, 100 args Arguments) error { 101 102 switch k { 103 case reflect.Struct: 104 case reflect.Slice, reflect.Array: 105 if minLen := args.LengthNonIndexed(); v.Len() < minLen { 106 return fmt.Errorf("abi: insufficient number of elements in the list/array for unpack, want %d, got %d", 107 minLen, v.Len()) 108 } 109 default: 110 return fmt.Errorf("abi: cannot unmarshal tuple into %v", t) 111 } 112 return nil 113 } 114 115 // mapAbiToStringField maps abi to struct fields. 116 // first round: for each Exportable field that contains a `abi:""` tag 117 // and this field name exists in the arguments, pair them together. 118 // second round: for each argument field that has not been already linked, 119 // find what variable is expected to be mapped into, if it exists and has not been 120 // used, pair them. 121 func mapAbiToStructFields(args Arguments, value reflect.Value) (map[string]string, error) { 122 123 typ := value.Type() 124 125 abi2struct := make(map[string]string) 126 struct2abi := make(map[string]string) 127 128 // first round ~~~ 129 for i := 0; i < typ.NumField(); i++ { 130 structFieldName := typ.Field(i).Name 131 132 // skip private struct fields. 133 if structFieldName[:1] != strings.ToUpper(structFieldName[:1]) { 134 continue 135 } 136 137 // skip fields that have no abi:"" tag. 138 var ok bool 139 var tagName string 140 if tagName, ok = typ.Field(i).Tag.Lookup("abi"); !ok { 141 continue 142 } 143 144 // check if tag is empty. 145 if tagName == "" { 146 return nil, fmt.Errorf("struct: abi tag in '%s' is empty", structFieldName) 147 } 148 149 // check which argument field matches with the abi tag. 150 found := false 151 for _, abiField := range args.NonIndexed() { 152 if abiField.Name == tagName { 153 if abi2struct[abiField.Name] != "" { 154 return nil, fmt.Errorf("struct: abi tag in '%s' already mapped", structFieldName) 155 } 156 // pair them 157 abi2struct[abiField.Name] = structFieldName 158 struct2abi[structFieldName] = abiField.Name 159 found = true 160 } 161 } 162 163 // check if this tag has been mapped. 164 if !found { 165 return nil, fmt.Errorf("struct: abi tag '%s' defined but not found in abi", tagName) 166 } 167 168 } 169 170 // second round ~~~ 171 for _, arg := range args { 172 173 abiFieldName := arg.Name 174 structFieldName := capitalise(abiFieldName) 175 176 if structFieldName == "" { 177 return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct") 178 } 179 180 // this abi has already been paired, skip it... unless there exists another, yet unassigned 181 // struct field with the same field name. If so, raise an error: 182 // abi: [ { "name": "value" } ] 183 // struct { Value *big.Int , Value1 *big.Int `abi:"value"`} 184 if abi2struct[abiFieldName] != "" { 185 if abi2struct[abiFieldName] != structFieldName && 186 struct2abi[structFieldName] == "" && 187 value.FieldByName(structFieldName).IsValid() { 188 return nil, fmt.Errorf("abi: multiple variables maps to the same abi field '%s'", abiFieldName) 189 } 190 continue 191 } 192 193 // return an error if this struct field has already been paired. 194 if struct2abi[structFieldName] != "" { 195 return nil, fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", structFieldName) 196 } 197 198 if value.FieldByName(structFieldName).IsValid() { 199 // pair them 200 abi2struct[abiFieldName] = structFieldName 201 struct2abi[structFieldName] = abiFieldName 202 } else { 203 // not paired, but annotate as used, to detect cases like 204 // abi : [ { "name": "value" }, { "name": "_value" } ] 205 // struct { Value *big.Int } 206 struct2abi[structFieldName] = abiFieldName 207 } 208 209 } 210 211 return abi2struct, nil 212 }