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