github.com/ethereum/go-ethereum@v1.14.3/accounts/abi/error_handling.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  	"reflect"
    23  )
    24  
    25  var (
    26  	errBadBool   = errors.New("abi: improperly encoded boolean value")
    27  	errBadUint8  = errors.New("abi: improperly encoded uint8 value")
    28  	errBadUint16 = errors.New("abi: improperly encoded uint16 value")
    29  	errBadUint32 = errors.New("abi: improperly encoded uint32 value")
    30  	errBadUint64 = errors.New("abi: improperly encoded uint64 value")
    31  	errBadInt8   = errors.New("abi: improperly encoded int8 value")
    32  	errBadInt16  = errors.New("abi: improperly encoded int16 value")
    33  	errBadInt32  = errors.New("abi: improperly encoded int32 value")
    34  	errBadInt64  = errors.New("abi: improperly encoded int64 value")
    35  )
    36  
    37  // formatSliceString formats the reflection kind with the given slice size
    38  // and returns a formatted string representation.
    39  func formatSliceString(kind reflect.Kind, sliceSize int) string {
    40  	if sliceSize == -1 {
    41  		return fmt.Sprintf("[]%v", kind)
    42  	}
    43  	return fmt.Sprintf("[%d]%v", sliceSize, kind)
    44  }
    45  
    46  // sliceTypeCheck checks that the given slice can by assigned to the reflection
    47  // type in t.
    48  func sliceTypeCheck(t Type, val reflect.Value) error {
    49  	if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
    50  		return typeErr(formatSliceString(t.GetType().Kind(), t.Size), val.Type())
    51  	}
    52  
    53  	if t.T == ArrayTy && val.Len() != t.Size {
    54  		return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
    55  	}
    56  
    57  	if t.Elem.T == SliceTy || t.Elem.T == ArrayTy {
    58  		if val.Len() > 0 {
    59  			return sliceTypeCheck(*t.Elem, val.Index(0))
    60  		}
    61  	}
    62  
    63  	if val.Type().Elem().Kind() != t.Elem.GetType().Kind() {
    64  		return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), val.Type())
    65  	}
    66  	return nil
    67  }
    68  
    69  // typeCheck checks that the given reflection value can be assigned to the reflection
    70  // type in t.
    71  func typeCheck(t Type, value reflect.Value) error {
    72  	if t.T == SliceTy || t.T == ArrayTy {
    73  		return sliceTypeCheck(t, value)
    74  	}
    75  
    76  	// Check base type validity. Element types will be checked later on.
    77  	if t.GetType().Kind() != value.Kind() {
    78  		return typeErr(t.GetType().Kind(), value.Kind())
    79  	} else if t.T == FixedBytesTy && t.Size != value.Len() {
    80  		return typeErr(t.GetType(), value.Type())
    81  	} else {
    82  		return nil
    83  	}
    84  }
    85  
    86  // typeErr returns a formatted type casting error.
    87  func typeErr(expected, got interface{}) error {
    88  	return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected)
    89  }