github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/accounts/abi/reflect.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-dubxcoin library. 3 // 4 // The go-dubxcoin 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-dubxcoin 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-dubxcoin 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) error { 75 dstType, srcType := dst.Type(), src.Type() 76 switch { 77 case dstType.Kind() == reflect.Interface: 78 return set(dst.Elem(), src) 79 case dstType.Kind() == reflect.Ptr && dstType.Elem() != derefbigT: 80 return set(dst.Elem(), src) 81 case srcType.AssignableTo(dstType) && dst.CanSet(): 82 dst.Set(src) 83 case dstType.Kind() == reflect.Slice && srcType.Kind() == reflect.Slice: 84 return setSlice(dst, src) 85 default: 86 return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type()) 87 } 88 return nil 89 } 90 91 // setSlice attempts to assign src to dst when slices are not assignable by default 92 // e.g. src: [][]byte -> dst: [][15]byte 93 func setSlice(dst, src reflect.Value) error { 94 slice := reflect.MakeSlice(dst.Type(), src.Len(), src.Len()) 95 for i := 0; i < src.Len(); i++ { 96 v := src.Index(i) 97 reflect.Copy(slice.Index(i), v) 98 } 99 100 dst.Set(slice) 101 return nil 102 } 103 104 // requireAssignable assures that `dest` is a pointer and it's not an interface. 105 func requireAssignable(dst, src reflect.Value) error { 106 if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface { 107 return fmt.Errorf("abi: cannot unmarshal %v into %v", src.Type(), dst.Type()) 108 } 109 return nil 110 } 111 112 // requireUnpackKind verifies preconditions for unpacking `args` into `kind` 113 func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind, 114 args Arguments) error { 115 116 switch k { 117 case reflect.Struct: 118 case reflect.Slice, reflect.Array: 119 if minLen := args.LengthNonIndexed(); v.Len() < minLen { 120 return fmt.Errorf("abi: insufficient number of elements in the list/array for unpack, want %d, got %d", 121 minLen, v.Len()) 122 } 123 default: 124 return fmt.Errorf("abi: cannot unmarshal tuple into %v", t) 125 } 126 return nil 127 } 128 129 // mapArgNamesToStructFields maps a slice of argument names to struct fields. 130 // first round: for each Exportable field that contains a `abi:""` tag 131 // and this field name exists in the given argument name list, pair them together. 132 // second round: for each argument name that has not been already linked, 133 // find what variable is expected to be mapped into, if it exists and has not been 134 // used, pair them. 135 // Note this function assumes the given value is a struct value. 136 func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[string]string, error) { 137 typ := value.Type() 138 139 abi2struct := make(map[string]string) 140 struct2abi := make(map[string]string) 141 142 // first round ~~~ 143 for i := 0; i < typ.NumField(); i++ { 144 structFieldName := typ.Field(i).Name 145 146 // skip private struct fields. 147 if structFieldName[:1] != strings.ToUpper(structFieldName[:1]) { 148 continue 149 } 150 // skip fields that have no abi:"" tag. 151 var ok bool 152 var tagName string 153 if tagName, ok = typ.Field(i).Tag.Lookup("abi"); !ok { 154 continue 155 } 156 // check if tag is empty. 157 if tagName == "" { 158 return nil, fmt.Errorf("struct: abi tag in '%s' is empty", structFieldName) 159 } 160 // check which argument field matches with the abi tag. 161 found := false 162 for _, arg := range argNames { 163 if arg == tagName { 164 if abi2struct[arg] != "" { 165 return nil, fmt.Errorf("struct: abi tag in '%s' already mapped", structFieldName) 166 } 167 // pair them 168 abi2struct[arg] = structFieldName 169 struct2abi[structFieldName] = arg 170 found = true 171 } 172 } 173 // check if this tag has been mapped. 174 if !found { 175 return nil, fmt.Errorf("struct: abi tag '%s' defined but not found in abi", tagName) 176 } 177 } 178 179 // second round ~~~ 180 for _, argName := range argNames { 181 182 structFieldName := ToCamelCase(argName) 183 184 if structFieldName == "" { 185 return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct") 186 } 187 188 // this abi has already been paired, skip it... unless there exists another, yet unassigned 189 // struct field with the same field name. If so, raise an error: 190 // abi: [ { "name": "value" } ] 191 // struct { Value *big.Int , Value1 *big.Int `abi:"value"`} 192 if abi2struct[argName] != "" { 193 if abi2struct[argName] != structFieldName && 194 struct2abi[structFieldName] == "" && 195 value.FieldByName(structFieldName).IsValid() { 196 return nil, fmt.Errorf("abi: multiple variables maps to the same abi field '%s'", argName) 197 } 198 continue 199 } 200 201 // return an error if this struct field has already been paired. 202 if struct2abi[structFieldName] != "" { 203 return nil, fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", structFieldName) 204 } 205 206 if value.FieldByName(structFieldName).IsValid() { 207 // pair them 208 abi2struct[argName] = structFieldName 209 struct2abi[structFieldName] = argName 210 } else { 211 // not paired, but annotate as used, to detect cases like 212 // abi : [ { "name": "value" }, { "name": "_value" } ] 213 // struct { Value *big.Int } 214 struct2abi[structFieldName] = argName 215 } 216 } 217 return abi2struct, nil 218 }