From ad6e7a377d0542f17bc770e72d8d17829359d1a6 Mon Sep 17 00:00:00 2001 From: tzapu Date: Fri, 19 Oct 2018 01:02:22 +0300 Subject: [PATCH 1/6] argument type and name were reversed --- accounts/abi/method.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 5831057652b6..07b124a4dfa9 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -56,7 +56,7 @@ func (method Method) Sig() string { func (method Method) String() string { inputs := make([]string, len(method.Inputs)) for i, input := range method.Inputs { - inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type) + inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name) } outputs := make([]string, len(method.Outputs)) for i, output := range method.Outputs { From 7f597fa5d8a8c43db50ea3a8ab8ff873fd0f29b3 Mon Sep 17 00:00:00 2001 From: Alex T Date: Fri, 19 Oct 2018 17:19:17 +0300 Subject: [PATCH 2/6] reverse output --- accounts/abi/method.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 07b124a4dfa9..2d8d3d658992 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -60,10 +60,10 @@ func (method Method) String() string { } outputs := make([]string, len(method.Outputs)) for i, output := range method.Outputs { + outputs[i] = output.Type.String() if len(output.Name) > 0 { - outputs[i] = fmt.Sprintf("%v ", output.Name) + outputs[i] += fmt.Sprintf(" %v", output.Name) } - outputs[i] += output.Type.String() } constant := "" if method.Const { From 080deb0c93699087131486bc00104372f187b54d Mon Sep 17 00:00:00 2001 From: Alex T Date: Fri, 19 Oct 2018 20:26:02 +0300 Subject: [PATCH 3/6] added some tests --- accounts/abi/method_test.go | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 accounts/abi/method_test.go diff --git a/accounts/abi/method_test.go b/accounts/abi/method_test.go new file mode 100644 index 000000000000..e39b0dc8f1ff --- /dev/null +++ b/accounts/abi/method_test.go @@ -0,0 +1,63 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package abi + +import ( + "strings" + "testing" +) + +const methoddata = ` +[ + { "type" : "function", "name" : "balance", "constant" : true }, + { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }, + { "type" : "function", "name" : "transfer", "constant" : false, "inputs" : [ { "name" : "from", "type" : "address" }, { "name" : "to", "type" : "address" }, { "name" : "value", "type" : "uint256" } ], "outputs" : [ { "name" : "success", "type" : "bool" } ] } +]` + + +func TestMethodString(t *testing.T) { + var table = []struct { + method string + expectation string + }{ + { + method: "balance", + expectation:"function balance() constant returns()", + }, + { + method: "send", + expectation:"function send(uint256 amount) returns()", + }, + { + method: "transfer", + expectation:"function transfer(address from, address to, uint256 value) returns(bool success)", + }, + + } + + abi, err := JSON(strings.NewReader(methoddata)) + if err != nil { + t.Fatal(err) + } + + for _, test := range table { + got := abi.Methods[test.method].String() + if got != test.expectation { + t.Errorf("expected string to be %s, got %s", test.expectation, got) + } + } +} From 1e72a3008cc0e36341bf6b95b2466c3d79356d43 Mon Sep 17 00:00:00 2001 From: Alex T Date: Wed, 24 Oct 2018 09:35:14 +0300 Subject: [PATCH 4/6] event string was incorect --- accounts/abi/event.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounts/abi/event.go b/accounts/abi/event.go index a3f6be97323e..f5f97e47b7c7 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -41,7 +41,7 @@ func (e Event) String() string { inputs[i] = fmt.Sprintf("%v indexed %v", input.Name, input.Type) } } - return fmt.Sprintf("e %v(%v)", e.Name, strings.Join(inputs, ", ")) + return fmt.Sprintf("event %v(%v)", e.Name, strings.Join(inputs, ", ")) } // Id returns the canonical representation of the event's signature used by the From 5a59cc0eea2c04b88de7527b63cbe1027a3e0dc4 Mon Sep 17 00:00:00 2001 From: Alex T Date: Wed, 24 Oct 2018 11:32:28 +0300 Subject: [PATCH 5/6] reversed params for events, added tests --- accounts/abi/event.go | 4 ++-- accounts/abi/event_test.go | 41 ++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/accounts/abi/event.go b/accounts/abi/event.go index f5f97e47b7c7..9392c1990c3e 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -36,9 +36,9 @@ type Event struct { func (e Event) String() string { inputs := make([]string, len(e.Inputs)) for i, input := range e.Inputs { - inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type) + inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name) if input.Indexed { - inputs[i] = fmt.Sprintf("%v indexed %v", input.Name, input.Type) + inputs[i] = fmt.Sprintf("%v indexed %v", input.Type, input.Name) } } return fmt.Sprintf("event %v(%v)", e.Name, strings.Join(inputs, ", ")) diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index 3bfdd7c0abd5..5c999fc63abe 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -87,12 +87,12 @@ func TestEventId(t *testing.T) { }{ { definition: `[ - { "type" : "event", "name" : "balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, - { "type" : "event", "name" : "check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] } + { "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, + { "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] } ]`, expectations: map[string]common.Hash{ - "balance": crypto.Keccak256Hash([]byte("balance(uint256)")), - "check": crypto.Keccak256Hash([]byte("check(address,uint256)")), + "Balance": crypto.Keccak256Hash([]byte("Balance(uint256)")), + "Check": crypto.Keccak256Hash([]byte("Check(address,uint256)")), }, }, } @@ -111,6 +111,39 @@ func TestEventId(t *testing.T) { } } +func TestEventString(t *testing.T) { + var table = []struct { + definition string + expectations map[string]string + }{ + { + definition: `[ + { "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, + { "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }, + { "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] } + ]`, + expectations: map[string]string{ + "Balance": "event Balance(uint256 in)", + "Check": "event Check(address t, uint256 b)", + "Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)", + }, + }, + } + + for _, test := range table { + abi, err := JSON(strings.NewReader(test.definition)) + if err != nil { + t.Fatal(err) + } + + for name, event := range abi.Events { + if event.String() != test.expectations[name] { + t.Errorf("expected string to be %s, got %s", test.expectations[name], event.String()) + } + } + } +} + // TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array. func TestEventMultiValueWithArrayUnpack(t *testing.T) { definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` From 212b107860dfc0fed2420b14e03ac96acb6d65dc Mon Sep 17 00:00:00 2001 From: Alex T Date: Tue, 6 Nov 2018 10:47:17 +0200 Subject: [PATCH 6/6] linting --- accounts/abi/event_test.go | 4 ++-- accounts/abi/method_test.go | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index 5c999fc63abe..e735cceb8884 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -123,8 +123,8 @@ func TestEventString(t *testing.T) { { "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] } ]`, expectations: map[string]string{ - "Balance": "event Balance(uint256 in)", - "Check": "event Check(address t, uint256 b)", + "Balance": "event Balance(uint256 in)", + "Check": "event Check(address t, uint256 b)", "Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)", }, }, diff --git a/accounts/abi/method_test.go b/accounts/abi/method_test.go index e39b0dc8f1ff..a98f1cd31f7d 100644 --- a/accounts/abi/method_test.go +++ b/accounts/abi/method_test.go @@ -28,25 +28,23 @@ const methoddata = ` { "type" : "function", "name" : "transfer", "constant" : false, "inputs" : [ { "name" : "from", "type" : "address" }, { "name" : "to", "type" : "address" }, { "name" : "value", "type" : "uint256" } ], "outputs" : [ { "name" : "success", "type" : "bool" } ] } ]` - func TestMethodString(t *testing.T) { var table = []struct { - method string + method string expectation string }{ { - method: "balance", - expectation:"function balance() constant returns()", + method: "balance", + expectation: "function balance() constant returns()", }, { - method: "send", - expectation:"function send(uint256 amount) returns()", + method: "send", + expectation: "function send(uint256 amount) returns()", }, { - method: "transfer", - expectation:"function transfer(address from, address to, uint256 value) returns(bool success)", + method: "transfer", + expectation: "function transfer(address from, address to, uint256 value) returns(bool success)", }, - } abi, err := JSON(strings.NewReader(methoddata)) @@ -56,8 +54,8 @@ func TestMethodString(t *testing.T) { for _, test := range table { got := abi.Methods[test.method].String() - if got != test.expectation { - t.Errorf("expected string to be %s, got %s", test.expectation, got) - } + if got != test.expectation { + t.Errorf("expected string to be %s, got %s", test.expectation, got) + } } }