accounts/abi: testing that need to be repositioned once architecting is finished

Signed-off-by: RJ Catalano <rj@monax.io>
This commit is contained in:
RJ Catalano 2017-06-07 15:51:45 -05:00
parent 5ef1e97b7c
commit a5be068035
No known key found for this signature in database
GPG key ID: D4AB109D9B5D6386
3 changed files with 83 additions and 1 deletions

View file

@ -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")

View file

@ -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.
}*/

View file

@ -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.")
}
}
}*/