Skip to content

Commit 00a73fb

Browse files
aaronbuchwalds1na
andauthored
accounts/abi/bind: handle UnpackLog with zero topics (ethereum#26920)
Adds error handling for the case that UnpackLog or UnpackLogIntoMap is called with a log that has zero topics. --------- Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
1 parent b92d0ea commit 00a73fb

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

accounts/abi/bind/base.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import (
3434

3535
const basefeeWiggleMultiplier = 2
3636

37+
var (
38+
errNoEventSignature = errors.New("no event signature")
39+
errEventSignatureMismatch = errors.New("event signature mismatch")
40+
)
41+
3742
// SignerFn is a signer function callback when a contract requires a method to
3843
// sign the transaction before submission.
3944
type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)
@@ -488,8 +493,12 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter
488493

489494
// UnpackLog unpacks a retrieved log into the provided output structure.
490495
func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error {
496+
// Anonymous events are not supported.
497+
if len(log.Topics) == 0 {
498+
return errNoEventSignature
499+
}
491500
if log.Topics[0] != c.abi.Events[event].ID {
492-
return fmt.Errorf("event signature mismatch")
501+
return errEventSignatureMismatch
493502
}
494503
if len(log.Data) > 0 {
495504
if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil {
@@ -507,8 +516,12 @@ func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log)
507516

508517
// UnpackLogIntoMap unpacks a retrieved log into the provided map.
509518
func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error {
519+
// Anonymous events are not supported.
520+
if len(log.Topics) == 0 {
521+
return errNoEventSignature
522+
}
510523
if log.Topics[0] != c.abi.Events[event].ID {
511-
return fmt.Errorf("event signature mismatch")
524+
return errEventSignatureMismatch
512525
}
513526
if len(log.Data) > 0 {
514527
if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil {

accounts/abi/bind/base_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@ func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
186186
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
187187
}
188188

189+
func TestUnpackAnonymousLogIntoMap(t *testing.T) {
190+
mockLog := newMockLog(nil, common.HexToHash("0x0"))
191+
192+
abiString := `[{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"received","type":"event"}]`
193+
parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
194+
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
195+
196+
var received map[string]interface{}
197+
err := bc.UnpackLogIntoMap(received, "received", mockLog)
198+
if err == nil {
199+
t.Error("unpacking anonymous event is not supported")
200+
}
201+
if err.Error() != "no event signature" {
202+
t.Errorf("expected error 'no event signature', got '%s'", err)
203+
}
204+
}
205+
189206
func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
190207
sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"})
191208
if err != nil {

0 commit comments

Comments
 (0)