github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/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  	"errors"
    21  	"fmt"
    22  	"math/big"
    23  	"reflect"
    24  	"strings"
    25  )
    26  
    27  // ConvertType converts an interface of a runtime type into a interface of the
    28  // given type
    29  // e.g. turn
    30  // var fields []reflect.StructField
    31  // fields = append(fields, reflect.StructField{
    32  // 		Name: "X",
    33  //		Type: reflect.TypeOf(new(big.Int)),
    34  //		Tag:  reflect.StructTag("json:\"" + "x" + "\""),
    35  // }
    36  // into
    37  // type TupleT struct { X *big.Int }
    38  func ConvertType(in interface{}, proto interface{}) interface{} {
    39  	protoType := reflect.TypeOf(proto)
    40  	if reflect.TypeOf(in).ConvertibleTo(protoType) {
    41  		return reflect.ValueOf(in).Convert(protoType).Interface()
    42  	}
    43  	// Use set as a last ditch effort
    44  	if err := set(reflect.ValueOf(proto), reflect.ValueOf(in)); err != nil {
    45  		panic(err)
    46  	}
    47  	return proto
    48  }
    49  
    50  // indirect recursively dereferences the value until it either gets the value
    51  // or finds a big.Int
    52  func indirect(v reflect.Value) reflect.Value {
    53  	if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeOf(big.Int{}) {
    54  		return indirect(v.Elem())
    55  	}
    56  	return v
    57  }
    58  
    59  // reflectIntType returns the reflect using the given size and
    60  // unsignedness.
    61  func reflectIntType(unsigned bool, size int) reflect.Type {
    62  	if unsigned {
    63  		switch size {
    64  		case 8:
    65  			return reflect.TypeOf(uint8(0))
    66  		case 16:
    67  			return reflect.TypeOf(uint16(0))
    68  		case 32:
    69  			return reflect.TypeOf(uint32(0))
    70  		case 64:
    71  			return reflect.TypeOf(uint64(0))
    72  		}
    73  	}
    74  	switch size {
    75  	case 8:
    76  		return reflect.TypeOf(int8(0))
    77  	case 16:
    78  		return reflect.TypeOf(int16(0))
    79  	case 32:
    80  		return reflect.TypeOf(int32(0))
    81  	case 64:
    82  		return reflect.TypeOf(int64(0))
    83  	}
    84  	return reflect.TypeOf(&big.Int{})
    85  }
    86  
    87  // mustArrayToByteSlice creates a new byte slice with the exact same size as value
    88  // and copies the bytes in value to the new slice.
    89  func mustArrayToByteSlice(value reflect.Value) reflect.Value {
    90  	slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len())
    91  	reflect.Copy(slice, value)
    92  	return slice
    93  }
    94  
    95  // set attempts to assign src to dst by either setting, copying or otherwise.
    96  //
    97  // set is a bit more lenient when it comes to assignment and doesn't force an as
    98  // strict ruleset as bare `reflect` does.
    99  func set(dst, src reflect.Value) error {
   100  	dstType, srcType := dst.Type(), src.Type()
   101  	switch {
   102  	case dstType.Kind() == reflect.Interface && dst.Elem().IsValid():
   103  		return set(dst.Elem(), src)
   104  	case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeOf(big.Int{}):
   105  		return set(dst.Elem(), src)
   106  	case srcType.AssignableTo(dstType) && dst.CanSet():
   107  		dst.Set(src)
   108  	case dstType.Kind() == reflect.Slice && srcType.Kind() == reflect.Slice && dst.CanSet():
   109  		return setSlice(dst, src)
   110  	case dstType.Kind() == reflect.Array:
   111  		return setArray(dst, src)
   112  	case dstType.Kind() == reflect.Struct:
   113  		return setStruct(dst, src)
   114  	default:
   115  		return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
   116  	}
   117  	return nil
   118  }
   119  
   120  // setSlice attempts to assign src to dst when slices are not assignable by default
   121  // e.g. src: [][]byte -> dst: [][15]byte
   122  // setSlice ignores if we cannot copy all of src' elements.
   123  func setSlice(dst, src reflect.Value) error {
   124  	slice := reflect.MakeSlice(dst.Type(), src.Len(), src.Len())
   125  	for i := 0; i < src.Len(); i++ {
   126  		if src.Index(i).Kind() == reflect.Struct {
   127  			if err := set(slice.Index(i), src.Index(i)); err != nil {
   128  				return err
   129  			}
   130  		} else {
   131  			// e.g. [][32]uint8 to []common.Hash
   132  			if err := set(slice.Index(i), src.Index(i)); err != nil {
   133  				return err
   134  			}
   135  		}
   136  	}
   137  	if dst.CanSet() {
   138  		dst.Set(slice)
   139  		return nil
   140  	}
   141  	return errors.New("Cannot set slice, destination not settable")
   142  }
   143  
   144  func setArray(dst, src reflect.Value) error {
   145  	if src.Kind() == reflect.Ptr {
   146  		return set(dst, indirect(src))
   147  	}
   148  	array := reflect.New(dst.Type()).Elem()
   149  	min := src.Len()
   150  	if src.Len() > dst.Len() {
   151  		min = dst.Len()
   152  	}
   153  	for i := 0; i < min; i++ {
   154  		if err := set(array.Index(i), src.Index(i)); err != nil {
   155  			return err
   156  		}
   157  	}
   158  	if dst.CanSet() {
   159  		dst.Set(array)
   160  		return nil
   161  	}
   162  	return errors.New("Cannot set array, destination not settable")
   163  }
   164  
   165  func setStruct(dst, src reflect.Value) error {
   166  	for i := 0; i < src.NumField(); i++ {
   167  		srcField := src.Field(i)
   168  		dstField := dst.Field(i)
   169  		if !dstField.IsValid() || !srcField.IsValid() {
   170  			return fmt.Errorf("Could not find src field: %v value: %v in destination", srcField.Type().Name(), srcField)
   171  		}
   172  		if err := set(dstField, srcField); err != nil {
   173  			return err
   174  		}
   175  	}
   176  	return nil
   177  }
   178  
   179  // mapArgNamesToStructFields maps a slice of argument names to struct fields.
   180  // first round: for each Exportable field that contains a `abi:""` tag
   181  //   and this field name exists in the given argument name list, pair them together.
   182  // second round: for each argument name that has not been already linked,
   183  //   find what variable is expected to be mapped into, if it exists and has not been
   184  //   used, pair them.
   185  // Note this function assumes the given value is a struct value.
   186  func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[string]string, error) {
   187  	typ := value.Type()
   188  
   189  	abi2struct := make(map[string]string)
   190  	struct2abi := make(map[string]string)
   191  
   192  	// first round ~~~
   193  	for i := 0; i < typ.NumField(); i++ {
   194  		structFieldName := typ.Field(i).Name
   195  
   196  		// skip private struct fields.
   197  		if structFieldName[:1] != strings.ToUpper(structFieldName[:1]) {
   198  			continue
   199  		}
   200  		// skip fields that have no abi:"" tag.
   201  		tagName, ok := typ.Field(i).Tag.Lookup("abi")
   202  		if !ok {
   203  			continue
   204  		}
   205  		// check if tag is empty.
   206  		if tagName == "" {
   207  			return nil, fmt.Errorf("struct: abi tag in '%s' is empty", structFieldName)
   208  		}
   209  		// check which argument field matches with the abi tag.
   210  		found := false
   211  		for _, arg := range argNames {
   212  			if arg == tagName {
   213  				if abi2struct[arg] != "" {
   214  					return nil, fmt.Errorf("struct: abi tag in '%s' already mapped", structFieldName)
   215  				}
   216  				// pair them
   217  				abi2struct[arg] = structFieldName
   218  				struct2abi[structFieldName] = arg
   219  				found = true
   220  			}
   221  		}
   222  		// check if this tag has been mapped.
   223  		if !found {
   224  			return nil, fmt.Errorf("struct: abi tag '%s' defined but not found in abi", tagName)
   225  		}
   226  	}
   227  
   228  	// second round ~~~
   229  	for _, argName := range argNames {
   230  
   231  		structFieldName := ToCamelCase(argName)
   232  
   233  		if structFieldName == "" {
   234  			return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct")
   235  		}
   236  
   237  		// this abi has already been paired, skip it... unless there exists another, yet unassigned
   238  		// struct field with the same field name. If so, raise an error:
   239  		//    abi: [ { "name": "value" } ]
   240  		//    struct { Value  *big.Int , Value1 *big.Int `abi:"value"`}
   241  		if abi2struct[argName] != "" {
   242  			if abi2struct[argName] != structFieldName &&
   243  				struct2abi[structFieldName] == "" &&
   244  				value.FieldByName(structFieldName).IsValid() {
   245  				return nil, fmt.Errorf("abi: multiple variables maps to the same abi field '%s'", argName)
   246  			}
   247  			continue
   248  		}
   249  
   250  		// return an error if this struct field has already been paired.
   251  		if struct2abi[structFieldName] != "" {
   252  			return nil, fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", structFieldName)
   253  		}
   254  
   255  		if value.FieldByName(structFieldName).IsValid() {
   256  			// pair them
   257  			abi2struct[argName] = structFieldName
   258  			struct2abi[structFieldName] = argName
   259  		} else {
   260  			// not paired, but annotate as used, to detect cases like
   261  			//   abi : [ { "name": "value" }, { "name": "_value" } ]
   262  			//   struct { Value *big.Int }
   263  			struct2abi[structFieldName] = argName
   264  		}
   265  	}
   266  	return abi2struct, nil
   267  }