bind: report error if event not not found by name when processing log

This commit is contained in:
Rader 2018-05-24 14:40:02 +08:00
parent 55b579e02c
commit 31c1e02921
2 changed files with 54 additions and 6 deletions

View file

@ -251,8 +251,14 @@ func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]int
if opts == nil { if opts == nil {
opts = new(FilterOpts) opts = new(FilterOpts)
} }
// Append the event selector to the query parameters and construct the topic set
query = append([][]interface{}{{c.abi.Events[name].Id()}}, query...) var e abi.Event
var exist bool
if e, exist = c.abi.Events[name]; !exist {
return nil, nil, fmt.Errorf("abi: event [%v] not found", name)
}
// Append the event selector to the query parameters and construct the topic seft
query = append([][]interface{}{{e.Id()}}, query...)
topics, err := makeTopics(query...) topics, err := makeTopics(query...)
if err != nil { if err != nil {
@ -300,8 +306,14 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter
if opts == nil { if opts == nil {
opts = new(WatchOpts) opts = new(WatchOpts)
} }
var e abi.Event
var exist bool
if e, exist = c.abi.Events[name]; !exist {
return nil, nil, fmt.Errorf("abi: event [%v] not found", name)
}
// Append the event selector to the query parameters and construct the topic set // Append the event selector to the query parameters and construct the topic set
query = append([][]interface{}{{c.abi.Events[name].Id()}}, query...) query = append([][]interface{}{{e.Id()}}, query...)
topics, err := makeTopics(query...) topics, err := makeTopics(query...)
if err != nil { if err != nil {
@ -325,14 +337,21 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter
} }
// UnpackLog unpacks a retrieved log into the provided output structure. // UnpackLog unpacks a retrieved log into the provided output structure.
func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error { func (c *BoundContract) UnpackLog(out interface{}, eventName string, log types.Log) error {
if len(log.Data) > 0 { if len(log.Data) > 0 {
if err := c.abi.Unpack(out, event, log.Data); err != nil { if err := c.abi.Unpack(out, eventName, log.Data); err != nil {
return err return err
} }
} }
var e abi.Event
var exist bool
if e, exist = c.abi.Events[eventName]; !exist {
return fmt.Errorf("abi: event [%v] not found", eventName)
}
var indexed abi.Arguments var indexed abi.Arguments
for _, arg := range c.abi.Events[event].Inputs { for _, arg := range e.Inputs {
if arg.Indexed { if arg.Indexed {
indexed = append(indexed, arg) indexed = append(indexed, arg)
} }

View file

@ -0,0 +1,29 @@
package bind
import (
"strings"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"
)
func TestUnkownEventName(t *testing.T) {
errorPrefix := "abi: event"
c := &BoundContract{}
c.abi.Events = make(map[string]abi.Event)
c.abi.Events["event1"] = abi.Event{}
if _, _, err := c.FilterLogs(nil, "event_not_exist"); err == nil || !strings.HasPrefix(err.Error(), errorPrefix) {
t.Fatal("should report error if event not found")
}
if _, _, err := c.WatchLogs(nil, "event_not_exist"); err == nil || !strings.HasPrefix(err.Error(), errorPrefix) {
t.Fatal("should report error if event not found")
}
var v interface{}
elog := types.Log{}
if err := c.UnpackLog(v, "event_not_exist", elog); err == nil || !strings.HasPrefix(err.Error(), errorPrefix) {
t.Fatal("should report error if event not found")
}
}