diff options
author | Martin Holst Swende <martin@swende.se> | 2017-12-23 03:59:41 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-23 03:59:41 +0800 |
commit | 9d187f02389ba12493112c7feb15a83f44e3a3ff (patch) | |
tree | c98e69e41f2412d8ccf6ebec886ed2110be866bb /accounts/abi/abi.go | |
parent | 5f8888e11606296c9582496974c0f6b96a882146 (diff) | |
parent | c095c87e117785ba5467487336215f86a958409b (diff) | |
download | go-tangerine-9d187f02389ba12493112c7feb15a83f44e3a3ff.tar go-tangerine-9d187f02389ba12493112c7feb15a83f44e3a3ff.tar.gz go-tangerine-9d187f02389ba12493112c7feb15a83f44e3a3ff.tar.bz2 go-tangerine-9d187f02389ba12493112c7feb15a83f44e3a3ff.tar.lz go-tangerine-9d187f02389ba12493112c7feb15a83f44e3a3ff.tar.xz go-tangerine-9d187f02389ba12493112c7feb15a83f44e3a3ff.tar.zst go-tangerine-9d187f02389ba12493112c7feb15a83f44e3a3ff.zip |
Merge pull request #15731 from holiman/revamp_abi
accounts/abi refactor
Diffstat (limited to 'accounts/abi/abi.go')
-rw-r--r-- | accounts/abi/abi.go | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 02b4fa472..cbcf4ca92 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -17,6 +17,7 @@ package abi import ( + "bytes" "encoding/json" "fmt" "io" @@ -50,25 +51,25 @@ func JSON(reader io.Reader) (ABI, error) { // methods string signature. (signature = baz(uint32,string32)) func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { // Fetch the ABI of the requested method - var method Method - if name == "" { - method = abi.Constructor - } else { - m, exist := abi.Methods[name] - if !exist { - return nil, fmt.Errorf("method '%s' not found", name) + // constructor + arguments, err := abi.Constructor.Inputs.Pack(args...) + if err != nil { + return nil, err } - method = m + return arguments, nil + + } + method, exist := abi.Methods[name] + if !exist { + return nil, fmt.Errorf("method '%s' not found", name) } - arguments, err := method.pack(args...) + + arguments, err := method.Inputs.Pack(args...) if err != nil { return nil, err } // Pack up the method ID too if not a constructor and return - if name == "" { - return arguments, nil - } return append(method.Id(), arguments...), nil } @@ -77,28 +78,20 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) { if len(output) == 0 { return fmt.Errorf("abi: unmarshalling empty output") } - // since there can't be naming collisions with contracts and events, // we need to decide whether we're calling a method or an event - var unpack unpacker if method, ok := abi.Methods[name]; ok { if len(output)%32 != 0 { return fmt.Errorf("abi: improperly formatted output") } - unpack = method + return method.Outputs.Unpack(v, output) } else if event, ok := abi.Events[name]; ok { - unpack = event - } else { - return fmt.Errorf("abi: could not locate named method or event.") - } - - // requires a struct to unpack into for a tuple return... - if unpack.isTupleReturn() { - return unpack.tupleUnpack(v, output) + return event.Inputs.Unpack(v, output) } - return unpack.singleUnpack(v, output) + return fmt.Errorf("abi: could not locate named method or event") } +// UnmarshalJSON implements json.Unmarshaler interface func (abi *ABI) UnmarshalJSON(data []byte) error { var fields []struct { Type string @@ -141,3 +134,14 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { return nil } + +// MethodById looks up a method by the 4-byte id +// returns nil if none found +func (abi *ABI) MethodById(sigdata []byte) *Method { + for _, method := range abi.Methods { + if bytes.Equal(method.Id(), sigdata[:4]) { + return &method + } + } + return nil +} |