github.com/MetalBlockchain/subnet-evm@v0.4.9/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 ) 38 39 // formatSliceString formats the reflection kind with the given slice size 40 // and returns a formatted string representation. 41 func formatSliceString(kind reflect.Kind, sliceSize int) string { 42 if sliceSize == -1 { 43 return fmt.Sprintf("[]%v", kind) 44 } 45 return fmt.Sprintf("[%d]%v", sliceSize, kind) 46 } 47 48 // sliceTypeCheck checks that the given slice can by assigned to the reflection 49 // type in t. 50 func sliceTypeCheck(t Type, val reflect.Value) error { 51 if val.Kind() != reflect.Slice && val.Kind() != reflect.Array { 52 return typeErr(formatSliceString(t.GetType().Kind(), t.Size), val.Type()) 53 } 54 55 if t.T == ArrayTy && val.Len() != t.Size { 56 return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len())) 57 } 58 59 if t.Elem.T == SliceTy || t.Elem.T == ArrayTy { 60 if val.Len() > 0 { 61 return sliceTypeCheck(*t.Elem, val.Index(0)) 62 } 63 } 64 65 if val.Type().Elem().Kind() != t.Elem.GetType().Kind() { 66 return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), val.Type()) 67 } 68 return nil 69 } 70 71 // typeCheck checks that the given reflection value can be assigned to the reflection 72 // type in t. 73 func typeCheck(t Type, value reflect.Value) error { 74 if t.T == SliceTy || t.T == ArrayTy { 75 return sliceTypeCheck(t, value) 76 } 77 78 // Check base type validity. Element types will be checked later on. 79 if t.GetType().Kind() != value.Kind() { 80 return typeErr(t.GetType().Kind(), value.Kind()) 81 } else if t.T == FixedBytesTy && t.Size != value.Len() { 82 return typeErr(t.GetType(), value.Type()) 83 } else { 84 return nil 85 } 86 } 87 88 // typeErr returns a formatted type casting error. 89 func typeErr(expected, got interface{}) error { 90 return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected) 91 }