Skip to content

Commit 2e2f093

Browse files
committed
Merge pull request bnb-chain#2167 from obscuren/abi-event-fixes
account/abi: implements event parsing
2 parents da645f0 + bddf8f7 commit 2e2f093

6 files changed

Lines changed: 174 additions & 31 deletions

File tree

‎accounts/abi/abi.go‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Executer func(datain []byte) []byte
3737
// packs data accordingly.
3838
type ABI struct {
3939
Methods map[string]Method
40+
Events map[string]Event
4041
}
4142

4243
// JSON returns a parsed ABI interface and error if it failed.
@@ -149,14 +150,37 @@ func (abi ABI) Call(executer Executer, name string, args ...interface{}) interfa
149150
}
150151

151152
func (abi *ABI) UnmarshalJSON(data []byte) error {
152-
var methods []Method
153-
if err := json.Unmarshal(data, &methods); err != nil {
153+
var fields []struct {
154+
Type string
155+
Name string
156+
Const bool
157+
Indexed bool
158+
Inputs []Argument
159+
Outputs []Argument
160+
}
161+
162+
if err := json.Unmarshal(data, &fields); err != nil {
154163
return err
155164
}
156165

157166
abi.Methods = make(map[string]Method)
158-
for _, method := range methods {
159-
abi.Methods[method.Name] = method
167+
abi.Events = make(map[string]Event)
168+
for _, field := range fields {
169+
switch field.Type {
170+
// empty defaults to function according to the abi spec
171+
case "function", "":
172+
abi.Methods[field.Name] = Method{
173+
Name: field.Name,
174+
Const: field.Const,
175+
Inputs: field.Inputs,
176+
Outputs: field.Outputs,
177+
}
178+
case "event":
179+
abi.Events[field.Name] = Event{
180+
Name: field.Name,
181+
Inputs: field.Inputs,
182+
}
183+
}
160184
}
161185

162186
return nil

‎accounts/abi/abi_test.go‎

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ import (
3131

3232
const jsondata = `
3333
[
34-
{ "name" : "balance", "const" : true },
35-
{ "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
34+
{ "type" : "function", "name" : "balance", "const" : true },
35+
{ "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
3636
]`
3737

3838
const jsondata2 = `
3939
[
40-
{ "name" : "balance", "const" : true },
41-
{ "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
42-
{ "name" : "test", "const" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
43-
{ "name" : "string", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
44-
{ "name" : "bool", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
45-
{ "name" : "address", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
46-
{ "name" : "string32", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string32" } ] },
47-
{ "name" : "uint64[2]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
48-
{ "name" : "uint64[]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
49-
{ "name" : "foo", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
50-
{ "name" : "bar", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
51-
{ "name" : "slice", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
52-
{ "name" : "slice256", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }
40+
{ "type" : "function", "name" : "balance", "const" : true },
41+
{ "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
42+
{ "type" : "function", "name" : "test", "const" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
43+
{ "type" : "function", "name" : "string", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
44+
{ "type" : "function", "name" : "bool", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
45+
{ "type" : "function", "name" : "address", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
46+
{ "type" : "function", "name" : "string32", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string32" } ] },
47+
{ "type" : "function", "name" : "uint64[2]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
48+
{ "type" : "function", "name" : "uint64[]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
49+
{ "type" : "function", "name" : "foo", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
50+
{ "type" : "function", "name" : "bar", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
51+
{ "type" : "function", "name" : "slice", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
52+
{ "type" : "function", "name" : "slice256", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }
5353
]`
5454

5555
func TestType(t *testing.T) {
@@ -96,7 +96,7 @@ func TestReader(t *testing.T) {
9696
},
9797
"send": Method{
9898
"send", false, []Argument{
99-
Argument{"amount", Uint256},
99+
Argument{"amount", Uint256, false},
100100
}, nil,
101101
},
102102
},
@@ -238,7 +238,7 @@ func TestTestAddress(t *testing.T) {
238238
func TestMethodSignature(t *testing.T) {
239239
String, _ := NewType("string")
240240
String32, _ := NewType("string32")
241-
m := Method{"foo", false, []Argument{Argument{"bar", String32}, Argument{"baz", String}}, nil}
241+
m := Method{"foo", false, []Argument{Argument{"bar", String32, false}, Argument{"baz", String, false}}, nil}
242242
exp := "foo(string32,string)"
243243
if m.Sig() != exp {
244244
t.Error("signature mismatch", exp, "!=", m.Sig())
@@ -250,7 +250,7 @@ func TestMethodSignature(t *testing.T) {
250250
}
251251

252252
uintt, _ := NewType("uint")
253-
m = Method{"foo", false, []Argument{Argument{"bar", uintt}}, nil}
253+
m = Method{"foo", false, []Argument{Argument{"bar", uintt, false}}, nil}
254254
exp = "foo(uint256)"
255255
if m.Sig() != exp {
256256
t.Error("signature mismatch", exp, "!=", m.Sig())
@@ -367,8 +367,8 @@ func ExampleJSON() {
367367

368368
func TestBytes(t *testing.T) {
369369
const definition = `[
370-
{ "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] },
371-
{ "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
370+
{ "type" : "function", "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] },
371+
{ "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
372372
]`
373373

374374
abi, err := JSON(strings.NewReader(definition))
@@ -396,10 +396,8 @@ func TestBytes(t *testing.T) {
396396

397397
func TestReturn(t *testing.T) {
398398
const definition = `[
399-
{ "name" : "balance", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "hash" } ] },
400-
{ "name" : "name", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "address" } ] }
401-
402-
]`
399+
{ "type" : "function", "name" : "balance", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "hash" } ] },
400+
{ "type" : "function", "name" : "name", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "address" } ] }]`
403401

404402
abi, err := JSON(strings.NewReader(definition))
405403
if err != nil {
@@ -424,3 +422,39 @@ func TestReturn(t *testing.T) {
424422
t.Errorf("expected type common.Address, got %T", r)
425423
}
426424
}
425+
426+
func TestDefaultFunctionParsing(t *testing.T) {
427+
const definition = `[{ "name" : "balance" }]`
428+
429+
abi, err := JSON(strings.NewReader(definition))
430+
if err != nil {
431+
t.Fatal(err)
432+
}
433+
434+
if _, ok := abi.Methods["balance"]; !ok {
435+
t.Error("expected 'balance' to be present")
436+
}
437+
}
438+
439+
func TestBareEvents(t *testing.T) {
440+
const definition = `[
441+
{ "type" : "event", "name" : "balance" },
442+
{ "type" : "event", "name" : "name" }]`
443+
444+
abi, err := JSON(strings.NewReader(definition))
445+
if err != nil {
446+
t.Fatal(err)
447+
}
448+
449+
if len(abi.Events) != 2 {
450+
t.Error("expected 2 events")
451+
}
452+
453+
if _, ok := abi.Events["balance"]; !ok {
454+
t.Error("expected 'balance' event to be present")
455+
}
456+
457+
if _, ok := abi.Events["name"]; !ok {
458+
t.Error("expected 'name' event to be present")
459+
}
460+
}

‎accounts/abi/argument.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
// Argument holds the name of the argument and the corresponding type.
2525
// Types are used when packing and testing arguments.
2626
type Argument struct {
27-
Name string
28-
Type Type
27+
Name string
28+
Type Type
29+
Indexed bool // indexed is only used by events
2930
}
3031

3132
func (a *Argument) UnmarshalJSON(data []byte) error {

‎accounts/abi/event.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package abi
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/crypto"
25+
)
26+
27+
// Event is an event potentially triggered by the EVM's LOG mechanism. The Event
28+
// holds type information (inputs) about the yielded output
29+
type Event struct {
30+
Name string
31+
Inputs []Argument
32+
}
33+
34+
// Id returns the canonical representation of the event's signature used by the
35+
// abi definition to identify event names and types.
36+
func (e Event) Id() common.Hash {
37+
types := make([]string, len(e.Inputs))
38+
i := 0
39+
for _, input := range e.Inputs {
40+
types[i] = input.Type.String()
41+
i++
42+
}
43+
return common.BytesToHash(crypto.Sha3([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
44+
}

‎accounts/abi/event_test.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package abi
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/ethereum/go-ethereum/common"
8+
"github.com/ethereum/go-ethereum/crypto"
9+
)
10+
11+
func TestEventId(t *testing.T) {
12+
var table = []struct {
13+
definition string
14+
expectations map[string]common.Hash
15+
}{
16+
{
17+
definition: `[
18+
{ "type" : "event", "name" : "balance", "inputs": [{ "name" : "in", "type": "uint" }] },
19+
{ "type" : "event", "name" : "check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
20+
]`,
21+
expectations: map[string]common.Hash{
22+
"balance": crypto.Sha3Hash([]byte("balance(uint256)")),
23+
"check": crypto.Sha3Hash([]byte("check(address,uint256)")),
24+
},
25+
},
26+
}
27+
28+
for _, test := range table {
29+
abi, err := JSON(strings.NewReader(test.definition))
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
for name, event := range abi.Events {
35+
if event.Id() != test.expectations[name] {
36+
t.Errorf("expected id to be %x, got %x", test.expectations[name], event.Id())
37+
}
38+
}
39+
}
40+
}

‎accounts/abi/type.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,5 @@ func (t Type) pack(v interface{}) ([]byte, error) {
218218
}
219219
}
220220

221-
return nil, fmt.Errorf("ABI: bad input given %T", value.Kind())
221+
return nil, fmt.Errorf("ABI: bad input given %v", value.Kind())
222222
}

0 commit comments

Comments
 (0)