github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/accounts/abi/reflect.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2016 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package abi 26 27 import ( 28 "fmt" 29 "reflect" 30 "strings" 31 ) 32 33 //间接递归地取消对值的引用,直到它得到值为止 34 //或者找到一个大的.int 35 func indirect(v reflect.Value) reflect.Value { 36 if v.Kind() == reflect.Ptr && v.Elem().Type() != derefbigT { 37 return indirect(v.Elem()) 38 } 39 return v 40 } 41 42 //ReflectIntKind返回使用给定大小和 43 //不拘一格 44 func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) { 45 switch size { 46 case 8: 47 if unsigned { 48 return reflect.Uint8, uint8T 49 } 50 return reflect.Int8, int8T 51 case 16: 52 if unsigned { 53 return reflect.Uint16, uint16T 54 } 55 return reflect.Int16, int16T 56 case 32: 57 if unsigned { 58 return reflect.Uint32, uint32T 59 } 60 return reflect.Int32, int32T 61 case 64: 62 if unsigned { 63 return reflect.Uint64, uint64T 64 } 65 return reflect.Int64, int64T 66 } 67 return reflect.Ptr, bigT 68 } 69 70 //mustArrayToBytesSlice创建与值大小完全相同的新字节片 71 //并将值中的字节复制到新切片。 72 func mustArrayToByteSlice(value reflect.Value) reflect.Value { 73 slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len()) 74 reflect.Copy(slice, value) 75 return slice 76 } 77 78 //设置尝试通过设置、复制或其他方式将SRC分配给DST。 79 // 80 //当涉及到任务时,set要宽松一点,而不是强制 81 //严格的规则集为bare`reflect`的规则集。 82 func set(dst, src reflect.Value, output Argument) error { 83 dstType := dst.Type() 84 srcType := src.Type() 85 switch { 86 case dstType.AssignableTo(srcType): 87 dst.Set(src) 88 case dstType.Kind() == reflect.Interface: 89 dst.Set(src) 90 case dstType.Kind() == reflect.Ptr: 91 return set(dst.Elem(), src, output) 92 default: 93 return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type()) 94 } 95 return nil 96 } 97 98 //RequiresSignable确保“dest”是指针,而不是接口。 99 func requireAssignable(dst, src reflect.Value) error { 100 if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface { 101 return fmt.Errorf("abi: cannot unmarshal %v into %v", src.Type(), dst.Type()) 102 } 103 return nil 104 } 105 106 //RequireUnpackKind验证将“args”解包为“kind”的前提条件 107 func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind, 108 args Arguments) error { 109 110 switch k { 111 case reflect.Struct: 112 case reflect.Slice, reflect.Array: 113 if minLen := args.LengthNonIndexed(); v.Len() < minLen { 114 return fmt.Errorf("abi: insufficient number of elements in the list/array for unpack, want %d, got %d", 115 minLen, v.Len()) 116 } 117 default: 118 return fmt.Errorf("abi: cannot unmarshal tuple into %v", t) 119 } 120 return nil 121 } 122 123 //mapabitoStringField将abi映射到结构字段。 124 //第一轮:对于每个包含“abi:”标记的可导出字段 125 //这个字段名存在于参数中,将它们配对在一起。 126 //第二轮:对于每个尚未链接的参数字段, 127 //如果变量存在且尚未映射,则查找该变量应映射到的对象。 128 //用过,配对。 129 func mapAbiToStructFields(args Arguments, value reflect.Value) (map[string]string, error) { 130 131 typ := value.Type() 132 133 abi2struct := make(map[string]string) 134 struct2abi := make(map[string]string) 135 136 //第一轮~~~ 137 for i := 0; i < typ.NumField(); i++ { 138 structFieldName := typ.Field(i).Name 139 140 //跳过私有结构字段。 141 if structFieldName[:1] != strings.ToUpper(structFieldName[:1]) { 142 continue 143 } 144 145 //跳过没有abi:“”标记的字段。 146 var ok bool 147 var tagName string 148 if tagName, ok = typ.Field(i).Tag.Lookup("abi"); !ok { 149 continue 150 } 151 152 //检查标签是否为空。 153 if tagName == "" { 154 return nil, fmt.Errorf("struct: abi tag in '%s' is empty", structFieldName) 155 } 156 157 //检查哪个参数字段与ABI标记匹配。 158 found := false 159 for _, abiField := range args.NonIndexed() { 160 if abiField.Name == tagName { 161 if abi2struct[abiField.Name] != "" { 162 return nil, fmt.Errorf("struct: abi tag in '%s' already mapped", structFieldName) 163 } 164 //配对他们 165 abi2struct[abiField.Name] = structFieldName 166 struct2abi[structFieldName] = abiField.Name 167 found = true 168 } 169 } 170 171 //检查此标记是否已映射。 172 if !found { 173 return nil, fmt.Errorf("struct: abi tag '%s' defined but not found in abi", tagName) 174 } 175 176 } 177 178 //第二轮~~~ 179 for _, arg := range args { 180 181 abiFieldName := arg.Name 182 structFieldName := capitalise(abiFieldName) 183 184 if structFieldName == "" { 185 return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct") 186 } 187 188 //这个ABI已经配对了,跳过它…除非存在另一个尚未分配的 189 //具有相同字段名的结构字段。如果是,则引发错误: 190 //ABI:[“name”:“value”] 191 //结构value*big.int,value1*big.int`abi:“value”` 192 if abi2struct[abiFieldName] != "" { 193 if abi2struct[abiFieldName] != structFieldName && 194 struct2abi[structFieldName] == "" && 195 value.FieldByName(structFieldName).IsValid() { 196 return nil, fmt.Errorf("abi: multiple variables maps to the same abi field '%s'", abiFieldName) 197 } 198 continue 199 } 200 201 //如果此结构字段已配对,则返回错误。 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 //配对他们 208 abi2struct[abiFieldName] = structFieldName 209 struct2abi[structFieldName] = abiFieldName 210 } else { 211 //不是成对的,而是按使用进行注释,以检测 212 //ABI:[“name”:“value”,“name”:“value”] 213 //结构值*big.int 214 struct2abi[structFieldName] = abiFieldName 215 } 216 217 } 218 219 return abi2struct, nil 220 }