accounts: fix unpack event error(#17477)

This commit is contained in:
bai 2018-08-22 10:50:35 +08:00
parent b2c644ffb5
commit cd96cd7850
4 changed files with 49 additions and 61 deletions

View file

@ -277,14 +277,48 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
return ret, nil return ret, nil
} }
// capitalise makes the first character of a string upper case, also removing any // Capitalise makes a camel-case string which starts with an upper case character.
// prefixing underscores from the variable names. func Capitalise(input string) string {
func capitalise(input string) string {
for len(input) > 0 && input[0] == '_' { for len(input) > 0 && input[0] == '_' {
input = input[1:] input = input[1:]
} }
if len(input) == 0 { if len(input) == 0 {
return "" return ""
} }
return strings.ToUpper(input[:1]) + input[1:] return toCamelCase(strings.ToUpper(input[:1]) + input[1:])
}
// Decapitalise makes a camel-case string which starts with a lower case character.
func Decapitalise(input string) string {
for len(input) > 0 && input[0] == '_' {
input = input[1:]
}
if len(input) == 0 {
return ""
}
return toCamelCase(strings.ToLower(input[:1]) + input[1:])
}
// toCamelCase converts an under-score string to a camel-case string
func toCamelCase(input string) string {
toupper := false
result := ""
for k, v := range input {
switch {
case k == 0:
result = strings.ToUpper(string(input[0]))
case toupper:
result += strings.ToUpper(string(v))
toupper = false
case v == '_':
toupper = true
default:
result += string(v)
}
}
return result
} }

View file

@ -85,7 +85,7 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string, lang La
copy(normalized.Outputs, original.Outputs) copy(normalized.Outputs, original.Outputs)
for j, output := range normalized.Outputs { for j, output := range normalized.Outputs {
if output.Name != "" { if output.Name != "" {
normalized.Outputs[j].Name = capitalise(output.Name) normalized.Outputs[j].Name = abi.Capitalise(output.Name)
} }
} }
// Append the methods to the call or transact lists // Append the methods to the call or transact lists
@ -118,7 +118,7 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string, lang La
events[original.Name] = &tmplEvent{Original: original, Normalized: normalized} events[original.Name] = &tmplEvent{Original: original, Normalized: normalized}
} }
contracts[types[i]] = &tmplContract{ contracts[types[i]] = &tmplContract{
Type: capitalise(types[i]), Type: abi.Capitalise(types[i]),
InputABI: strings.Replace(strippedABI, "\"", "\\\"", -1), InputABI: strings.Replace(strippedABI, "\"", "\\\"", -1),
InputBin: strings.TrimSpace(bytecodes[i]), InputBin: strings.TrimSpace(bytecodes[i]),
Constructor: evmABI.Constructor, Constructor: evmABI.Constructor,
@ -138,8 +138,8 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string, lang La
"bindtype": bindType[lang], "bindtype": bindType[lang],
"bindtopictype": bindTopicType[lang], "bindtopictype": bindTopicType[lang],
"namedtype": namedType[lang], "namedtype": namedType[lang],
"capitalise": capitalise, "capitalise": abi.Capitalise,
"decapitalise": decapitalise, "decapitalise": abi.Decapitalise,
} }
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource[lang])) tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource[lang]))
if err := tmpl.Execute(buffer, data); err != nil { if err := tmpl.Execute(buffer, data); err != nil {
@ -368,9 +368,9 @@ func namedTypeJava(javaKind string, solKind abi.Type) string {
switch parts[2] { switch parts[2] {
case "8", "16", "32", "64": case "8", "16", "32", "64":
if parts[3] == "" { if parts[3] == "" {
return capitalise(fmt.Sprintf("%sint%s", parts[1], parts[2])) return abi.Capitalise(fmt.Sprintf("%sint%s", parts[1], parts[2]))
} }
return capitalise(fmt.Sprintf("%sint%ss", parts[1], parts[2])) return abi.Capitalise(fmt.Sprintf("%sint%ss", parts[1], parts[2]))
default: default:
return javaKind return javaKind
@ -381,54 +381,8 @@ func namedTypeJava(javaKind string, solKind abi.Type) string {
// methodNormalizer is a name transformer that modifies Solidity method names to // methodNormalizer is a name transformer that modifies Solidity method names to
// conform to target language naming concentions. // conform to target language naming concentions.
var methodNormalizer = map[Lang]func(string) string{ var methodNormalizer = map[Lang]func(string) string{
LangGo: capitalise, LangGo: abi.Capitalise,
LangJava: decapitalise, LangJava: abi.Decapitalise,
}
// capitalise makes a camel-case string which starts with an upper case character.
func capitalise(input string) string {
for len(input) > 0 && input[0] == '_' {
input = input[1:]
}
if len(input) == 0 {
return ""
}
return toCamelCase(strings.ToUpper(input[:1]) + input[1:])
}
// decapitalise makes a camel-case string which starts with a lower case character.
func decapitalise(input string) string {
for len(input) > 0 && input[0] == '_' {
input = input[1:]
}
if len(input) == 0 {
return ""
}
return toCamelCase(strings.ToLower(input[:1]) + input[1:])
}
// toCamelCase converts an under-score string to a camel-case string
func toCamelCase(input string) string {
toupper := false
result := ""
for k, v := range input {
switch {
case k == 0:
result = strings.ToUpper(string(input[0]))
case toupper:
result += strings.ToUpper(string(v))
toupper = false
case v == '_':
toupper = true
default:
result += string(v)
}
}
return result
} }
// structured checks whether a list of ABI data types has enough information to // structured checks whether a list of ABI data types has enough information to
@ -445,7 +399,7 @@ func structured(args abi.Arguments) bool {
} }
// If the field name is empty when normalized or collides (var, Var, _var, _Var), // If the field name is empty when normalized or collides (var, Var, _var, _Var),
// we can't organize into a struct // we can't organize into a struct
field := capitalise(out.Name) field := abi.Capitalise(out.Name)
if field == "" || exists[field] { if field == "" || exists[field] {
return false return false
} }

View file

@ -117,7 +117,7 @@ func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) er
if !arg.Indexed { if !arg.Indexed {
return errors.New("non-indexed field in topic reconstruction") return errors.New("non-indexed field in topic reconstruction")
} }
field := reflect.ValueOf(out).Elem().FieldByName(capitalise(arg.Name)) field := reflect.ValueOf(out).Elem().FieldByName(abi.Capitalise(arg.Name))
// Try to parse the topic back into the fields based on primitive types // Try to parse the topic back into the fields based on primitive types
switch field.Kind() { switch field.Kind() {

View file

@ -171,7 +171,7 @@ func mapAbiToStructFields(args Arguments, value reflect.Value) (map[string]strin
for _, arg := range args { for _, arg := range args {
abiFieldName := arg.Name abiFieldName := arg.Name
structFieldName := capitalise(abiFieldName) structFieldName := Capitalise(abiFieldName)
if structFieldName == "" { if structFieldName == "" {
return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct") return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct")