github.com/ava-labs/subnet-evm@v0.6.4/accounts/abi/error_handling.go (about)

     1  // (c) 2019-2021, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2016 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package abi
    28  
    29  import (
    30  	"errors"
    31  	"fmt"
    32  	"reflect"
    33  )
    34  
    35  var (
    36  	errBadBool   = errors.New("abi: improperly encoded boolean value")
    37  	errBadUint8  = errors.New("abi: improperly encoded uint8 value")
    38  	errBadUint16 = errors.New("abi: improperly encoded uint16 value")
    39  	errBadUint32 = errors.New("abi: improperly encoded uint32 value")
    40  	errBadUint64 = errors.New("abi: improperly encoded uint64 value")
    41  	errBadInt8   = errors.New("abi: improperly encoded int8 value")
    42  	errBadInt16  = errors.New("abi: improperly encoded int16 value")
    43  	errBadInt32  = errors.New("abi: improperly encoded int32 value")
    44  	errBadInt64  = errors.New("abi: improperly encoded int64 value")
    45  )
    46  
    47  // formatSliceString formats the reflection kind with the given slice size
    48  // and returns a formatted string representation.
    49  func formatSliceString(kind reflect.Kind, sliceSize int) string {
    50  	if sliceSize == -1 {
    51  		return fmt.Sprintf("[]%v", kind)
    52  	}
    53  	return fmt.Sprintf("[%d]%v", sliceSize, kind)
    54  }
    55  
    56  // sliceTypeCheck checks that the given slice can by assigned to the reflection
    57  // type in t.
    58  func sliceTypeCheck(t Type, val reflect.Value) error {
    59  	if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
    60  		return typeErr(formatSliceString(t.GetType().Kind(), t.Size), val.Type())
    61  	}
    62  
    63  	if t.T == ArrayTy && val.Len() != t.Size {
    64  		return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
    65  	}
    66  
    67  	if t.Elem.T == SliceTy || t.Elem.T == ArrayTy {
    68  		if val.Len() > 0 {
    69  			return sliceTypeCheck(*t.Elem, val.Index(0))
    70  		}
    71  	}
    72  
    73  	if val.Type().Elem().Kind() != t.Elem.GetType().Kind() {
    74  		return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), val.Type())
    75  	}
    76  	return nil
    77  }
    78  
    79  // typeCheck checks that the given reflection value can be assigned to the reflection
    80  // type in t.
    81  func typeCheck(t Type, value reflect.Value) error {
    82  	if t.T == SliceTy || t.T == ArrayTy {
    83  		return sliceTypeCheck(t, value)
    84  	}
    85  
    86  	// Check base type validity. Element types will be checked later on.
    87  	if t.GetType().Kind() != value.Kind() {
    88  		return typeErr(t.GetType().Kind(), value.Kind())
    89  	} else if t.T == FixedBytesTy && t.Size != value.Len() {
    90  		return typeErr(t.GetType(), value.Type())
    91  	} else {
    92  		return nil
    93  	}
    94  }
    95  
    96  // typeErr returns a formatted type casting error.
    97  func typeErr(expected, got interface{}) error {
    98  	return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected)
    99  }