From a5be068035e92f1f316329e0517ecfb1e5e83c70 Mon Sep 17 00:00:00 2001 From: RJ Catalano Date: Wed, 7 Jun 2017 15:51:45 -0500 Subject: [PATCH] accounts/abi: testing that need to be repositioned once architecting is finished Signed-off-by: RJ Catalano --- accounts/abi/abi.go | 14 ++++++++- accounts/abi/event.go | 7 +++++ accounts/abi/event_test.go | 63 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 7eadb664b4..93a7280349 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -283,7 +283,19 @@ var ( // Unpack output in v according to the abi specification func (abi ABI) Unpack(v interface{}, name string, output []byte) error { - var method = abi.Methods[name] + var isEvent bool + + // first look for name in the methods map, otherwise look in the events map, since there will + // be a name collision for one ABI if there are two of the same names. + // If we can't find it, error. + method, ok := abi.Methods[name] + if !ok { + method, ok = abi.Events[name] + if !ok { + return fmt.Errorf("abi: could not find requested method or event %v", name) + } + isEvent = true + } if len(output) == 0 { return fmt.Errorf("abi: unmarshalling empty output") diff --git a/accounts/abi/event.go b/accounts/abi/event.go index 51ab842418..97648062cc 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -44,3 +44,10 @@ func (e Event) Id() common.Hash { } return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ","))))) } + +/*func (e Event) UnpackLog(v interface{}, data []byte) error { + // if not indexed, should be able to unpack normal log + + // if it is indexed, we will not be able to get back the value, rather we will only be able to + // search for it. +}*/ diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index b5054a0329..89e8ae3aff 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -54,3 +54,66 @@ func TestEventId(t *testing.T) { } } } + +/*func TestEventUnpacking(t *testing.T) { + + var isErrCorrect = func(expectedErr string, receivedError error) bool { + if expectedErr != receivedError.Error() { + t.Errorf("Expected error %v, receieved error %v", expectedErr, receivedError) + } + return true + } + + for i, test := range []struct { + definition string //abi definition + data []byte //log data gotten from the event + expectedOutput interface{} // the expected output + err string // empty or error if expected + }{ + { + `[{"anonymous":false,"inputs":[{"indexed":false,"name":"a","type":"uint256"},{"indexed":true,"name":"b","type":"uint256"},{"indexed":false,"name":"c","type":"uint256"}],"name":"testEvent","type":"event"}]`, + append(pad([]byte{1}, 32, true), pad([]byte{3}, 32, true)...), + []interface{}{*big.Int.SetUInt64(int(1)), *big.Int{3}}, + "", + }, + { + `[{"anonymous":false,"inputs":[{"indexed":false,"name":"a","type":"int256"},{"indexed":true,"name":"b","type":"int256"},{"indexed":false,"name":"c","type":"int256"}],"name":"testEvent","type":"event"}]`, + append(pad([]byte{1}, 32, true), pad([]byte{3}, 32, true)...), + []interface{}{*big.Int{1}, *big.Int{3}}, + "", + }, + { + `[{"anonymous":false,"inputs":[{"indexed":false,"name":"a","type":"bool"},{"indexed":false,"name":"b","type":"int256"},{"indexed":false,"name":"c","type":"uint256"}],"name":"testEvent","type":"event"}]`, + append(pad([]byte{0}, 32, true), append(pad([]byte{1}, 32, true), pad([]byte{3}, 32, true)...)...), + []interface{}{false, *big.Int{1}, *big.Int{3}}, + "", + }, + { + `[{"anonymous":false,"inputs":[{"indexed":false,"name":"a","type":"string"},{"indexed":false,"name":"b","type":"string"},{"indexed":false,"name":"c","type":"string"}],"name":"testEvent","type":"event"}]`, + common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000568656c6c6f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076675636b696e67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005776f726c64000000000000000000000000000000000000000000000000000000"), + []interface{}{"bring", "me", "ether"}, + "", + }, + { + `[{"anonymous":false,"inputs":[{"indexed":true,"name":"a","type":"string"},{"indexed":true,"name":"b","type":"string"},{"indexed":true,"name":"c","type":"string"}],"name":"testEvent","type":"event"}]`, + []byte(nil), + []interface{}{}, + "", + }, + } { + abi, err := JSON(strings.NewReader(test.definition)) + if err != nil { + t.Fatal(err) + } + + var v interface{} + if err = abi.Events["method"].UnpackLog(&v, data); err == nil { + if !reflect.DeepEqual(test.expectedOutput, v) { + t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp) + } + } else { + t.Errorf("abi: Unexpected error %v during event unpacking.") + } + + } +}*/