accounts/abi: refactored methodName and eventName

This commit moves the complicated logic of how we assign method names
and event names if they already exist into their own functions for
better readability.
This commit is contained in:
Marius van der Wijden 2020-04-02 14:25:54 +02:00
parent f4b170a088
commit 07f5bc448e

View file

@ -142,12 +142,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
abi.Constructor = NewMethod("", "", field.StateMutability, field.Constant, field.Payable, false, false, field.Inputs, nil) abi.Constructor = NewMethod("", "", field.StateMutability, field.Constant, field.Payable, false, false, field.Inputs, nil)
// empty defaults to function according to the abi spec // empty defaults to function according to the abi spec
case "function", "": case "function", "":
name := field.Name name := abi.methodName(field.Name)
_, ok := abi.Methods[name]
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", field.Name, idx)
_, ok = abi.Methods[name]
}
isConst := field.Constant || field.StateMutability == "pure" || field.StateMutability == "view" isConst := field.Constant || field.StateMutability == "pure" || field.StateMutability == "view"
abi.Methods[name] = NewMethod(name, field.Name, field.StateMutability, isConst, field.Payable, false, false, field.Inputs, field.Outputs) abi.Methods[name] = NewMethod(name, field.Name, field.StateMutability, isConst, field.Payable, false, false, field.Inputs, field.Outputs)
case "fallback": case "fallback":
@ -168,23 +163,46 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
} }
abi.Receive = NewMethod("", "", "payable", field.Constant, field.Payable, false, true, nil, nil) abi.Receive = NewMethod("", "", "payable", field.Constant, field.Payable, false, true, nil, nil)
case "event": case "event":
name := field.Name
_, ok := abi.Events[name]
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", field.Name, idx)
_, ok = abi.Events[name]
}
abi.Events[name] = Event{ abi.Events[name] = Event{
Name: name, Name: name,
RawName: field.Name, RawName: field.Name,
Anonymous: field.Anonymous, Anonymous: field.Anonymous,
Inputs: field.Inputs, Inputs: field.Inputs,
} }
name := abi.eventName(field.Name)
} }
} }
return nil return nil
} }
// methodName returns the next available name for a given function.
//
// e.g. if the abi contains Methods send, send1
// methodName would return send2 for input send.
func (abi *ABI) methodName(rawName string) string {
name := rawName
_, ok := abi.Methods[name]
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", rawName, idx)
_, ok = abi.Methods[name]
}
return name
}
// eventName returns the next available name for a given event.
//
// e.g. if the abi contains events received, received1
// eventName would return received2 for input received.
func (abi *ABI) eventName(rawName string) string {
name := rawName
_, ok := abi.Events[name]
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", rawName, idx)
_, ok = abi.Events[name]
}
return name
}
// MethodById looks up a method by the 4-byte id // MethodById looks up a method by the 4-byte id
// returns nil if none found // returns nil if none found
func (abi *ABI) MethodById(sigdata []byte) (*Method, error) { func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {