From 536f6692159bb397a280d6960450d1d9fe26a783 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Fri, 17 Apr 2020 09:23:25 +0200 Subject: [PATCH] accounts/abi: improved readability in NewMethod, nitpicks --- accounts/abi/abi.go | 3 ++- accounts/abi/method.go | 33 ++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 7ed6794ef3..2050add018 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -140,7 +140,6 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { switch field.Type { case "constructor": abi.Constructor = NewMethod("", "", Constructor, field.StateMutability, field.Constant, field.Payable, field.Inputs, nil) - // empty defaults to function according to the abi spec case "function": name := abi.methodName(field.Name) abi.Methods[name] = NewMethod(name, field.Name, Function, field.StateMutability, field.Constant, field.Payable, field.Inputs, field.Outputs) @@ -164,6 +163,8 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { case "event": name := abi.eventName(field.Name) abi.Events[name] = NewEvent(name, field.Name, field.Anonymous, field.Inputs) + default: + return fmt.Errorf("abi: could not recognize type %v of field %v", field.Type, field.Name) } } return nil diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 72880b8ddb..37c7af65ea 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -92,23 +92,31 @@ type Method struct { // It also precomputes the sig representation and the string representation // of the method. func NewMethod(name string, rawName string, funType FunctionType, mutability string, isConst, isPayable bool, inputs Arguments, outputs Arguments) Method { - // inputs - inputNames := make([]string, len(inputs)) - types := make([]string, len(inputs)) + var ( + types = make([]string, len(inputs)) + inputNames = make([]string, len(inputs)) + outputNames = make([]string, len(outputs)) + ) for i, input := range inputs { inputNames[i] = fmt.Sprintf("%v %v", input.Type, input.Name) types[i] = input.Type.String() } - // outputs - outputNames := make([]string, len(outputs)) for i, output := range outputs { outputNames[i] = output.Type.String() if len(output.Name) > 0 { outputNames[i] += fmt.Sprintf(" %v", output.Name) } } - // calculate the signature. - sig := fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ",")) + // calculate the signature and method id. Note only function + // has meaningful signature and id. + var ( + sig string + id []byte + ) + if funType == Function { + sig = fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ",")) + id = crypto.Keccak256([]byte(sig))[:4] + } // Extract meaningful state mutability of solidity method. // If it's default value, never print it. state := mutability @@ -121,18 +129,14 @@ func NewMethod(name string, rawName string, funType FunctionType, mutability str identity := fmt.Sprintf("function %v", rawName) if funType == Fallback { identity = "fallback" - // The fallback function does not have any meaningful signature. - sig = "" } else if funType == Receive { identity = "receive" - // The receive function does not have any meaningful signature. - sig = "" + } else if funType == Constructor { + identity = "constructor" } - str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", ")) - id := crypto.Keccak256([]byte(sig))[:4] - method := Method{ + return Method{ Name: name, RawName: rawName, Type: funType, @@ -145,7 +149,6 @@ func NewMethod(name string, rawName string, funType FunctionType, mutability str Sig: sig, ID: id, } - return method } func (method Method) String() string {