Skip to content

Commit a54526d

Browse files
tzapugzliudan
authored andcommitted
accounts/abi: argument type and name were reversed (ethereum#17947)
argument type and name were reversed
1 parent 614a0ee commit a54526d

File tree

4 files changed

+107
-13
lines changed

4 files changed

+107
-13
lines changed

accounts/abi/event.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ type Event struct {
3333
Inputs Arguments
3434
}
3535

36-
func (event Event) String() string {
37-
inputs := make([]string, len(event.Inputs))
38-
for i, input := range event.Inputs {
39-
inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type)
36+
func (e Event) String() string {
37+
inputs := make([]string, len(e.Inputs))
38+
for i, input := range e.Inputs {
39+
inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
4040
if input.Indexed {
41-
inputs[i] = fmt.Sprintf("%v indexed %v", input.Name, input.Type)
41+
inputs[i] = fmt.Sprintf("%v indexed %v", input.Type, input.Name)
4242
}
4343
}
44-
return fmt.Sprintf("event %v(%v)", event.Name, strings.Join(inputs, ", "))
44+
return fmt.Sprintf("event %v(%v)", e.Name, strings.Join(inputs, ", "))
4545
}
4646

4747
// Id returns the canonical representation of the event's signature used by the

accounts/abi/event_test.go

+37-4
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ func TestEventId(t *testing.T) {
7171
}{
7272
{
7373
definition: `[
74-
{ "type" : "event", "name" : "balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
75-
{ "type" : "event", "name" : "check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
74+
{ "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
75+
{ "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
7676
]`,
7777
expectations: map[string]common.Hash{
78-
"balance": crypto.Keccak256Hash([]byte("balance(uint256)")),
79-
"check": crypto.Keccak256Hash([]byte("check(address,uint256)")),
78+
"Balance": crypto.Keccak256Hash([]byte("Balance(uint256)")),
79+
"Check": crypto.Keccak256Hash([]byte("Check(address,uint256)")),
8080
},
8181
},
8282
}
@@ -95,6 +95,39 @@ func TestEventId(t *testing.T) {
9595
}
9696
}
9797

98+
func TestEventString(t *testing.T) {
99+
var table = []struct {
100+
definition string
101+
expectations map[string]string
102+
}{
103+
{
104+
definition: `[
105+
{ "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
106+
{ "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] },
107+
{ "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] }
108+
]`,
109+
expectations: map[string]string{
110+
"Balance": "event Balance(uint256 in)",
111+
"Check": "event Check(address t, uint256 b)",
112+
"Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)",
113+
},
114+
},
115+
}
116+
117+
for _, test := range table {
118+
abi, err := JSON(strings.NewReader(test.definition))
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
123+
for name, event := range abi.Events {
124+
if event.String() != test.expectations[name] {
125+
t.Errorf("expected string to be %s, got %s", test.expectations[name], event.String())
126+
}
127+
}
128+
}
129+
}
130+
98131
// TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array.
99132
func TestEventMultiValueWithArrayUnpack(t *testing.T) {
100133
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`

accounts/abi/method.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ func (method Method) Sig() string {
5656
func (method Method) String() string {
5757
inputs := make([]string, len(method.Inputs))
5858
for i, input := range method.Inputs {
59-
inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type)
59+
inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
6060
}
6161
outputs := make([]string, len(method.Outputs))
6262
for i, output := range method.Outputs {
63+
outputs[i] = output.Type.String()
6364
if len(output.Name) > 0 {
64-
outputs[i] = fmt.Sprintf("%v ", output.Name)
65+
outputs[i] += fmt.Sprintf(" %v", output.Name)
6566
}
66-
outputs[i] += output.Type.String()
6767
}
6868
constant := ""
6969
if method.Const {

accounts/abi/method_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
"strings"
21+
"testing"
22+
)
23+
24+
const methoddata = `
25+
[
26+
{ "type" : "function", "name" : "balance", "constant" : true },
27+
{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
28+
{ "type" : "function", "name" : "transfer", "constant" : false, "inputs" : [ { "name" : "from", "type" : "address" }, { "name" : "to", "type" : "address" }, { "name" : "value", "type" : "uint256" } ], "outputs" : [ { "name" : "success", "type" : "bool" } ] }
29+
]`
30+
31+
func TestMethodString(t *testing.T) {
32+
var table = []struct {
33+
method string
34+
expectation string
35+
}{
36+
{
37+
method: "balance",
38+
expectation: "function balance() constant returns()",
39+
},
40+
{
41+
method: "send",
42+
expectation: "function send(uint256 amount) returns()",
43+
},
44+
{
45+
method: "transfer",
46+
expectation: "function transfer(address from, address to, uint256 value) returns(bool success)",
47+
},
48+
}
49+
50+
abi, err := JSON(strings.NewReader(methoddata))
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
55+
for _, test := range table {
56+
got := abi.Methods[test.method].String()
57+
if got != test.expectation {
58+
t.Errorf("expected string to be %s, got %s", test.expectation, got)
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)