Skip to content

Commit f0f594d

Browse files
committed
accounts/abi: Deduplicate code in unpacker
1 parent 1ede683 commit f0f594d

4 files changed

Lines changed: 33 additions & 53 deletions

File tree

‎accounts/abi/abi.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
136136

137137
// MethodById looks up a method by the 4-byte id
138138
// returns nil if none found
139-
func (abi *ABI) MethodById(sigdata []byte) (*Method, error){
139+
func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
140140
for _, method := range abi.Methods {
141141
if bytes.Equal(method.Id(), sigdata[:4]) {
142142
return &method, nil

‎accounts/abi/abi_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ func TestABI_MethodById(t *testing.T) {
689689
}
690690
for name, m := range abi.Methods {
691691
a := fmt.Sprintf("%v", m)
692-
m2,err := abi.MethodById(m.Id())
692+
m2, err := abi.MethodById(m.Id())
693693
if err != nil {
694694
t.Fatal(err)
695695
}

‎accounts/abi/argument.go‎

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ func (arguments Arguments) LengthNonIndexed() int {
6767
return out
6868
}
6969

70-
func (arguments Arguments) NonIndexed() Arguments{
70+
func (arguments Arguments) NonIndexed() Arguments {
7171
var ret []Argument
72-
for _,arg := range arguments{
73-
if !arg.Indexed{
72+
for _, arg := range arguments {
73+
if !arg.Indexed {
7474
ret = append(ret, arg)
7575
}
7676
}
@@ -84,21 +84,27 @@ func (arguments Arguments) isTuple() bool {
8484

8585
// Unpack performs the operation hexdata -> Go format
8686
func (arguments Arguments) Unpack(v interface{}, data []byte) error {
87-
if arguments.isTuple() {
88-
return arguments.unpackTuple(v, data)
89-
}
90-
return arguments.unpackAtomic(v, data)
91-
}
9287

93-
func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
9488
// make sure the passed value is arguments pointer
95-
valueOf := reflect.ValueOf(v)
96-
if reflect.Ptr != valueOf.Kind() {
89+
if reflect.Ptr != reflect.ValueOf(v).Kind() {
9790
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
9891
}
9992

93+
marshalledValues, err := arguments.UnpackValues(data)
94+
if err != nil {
95+
return err
96+
}
97+
98+
if arguments.isTuple() {
99+
return arguments.unpackTuple(v, marshalledValues)
100+
}
101+
return arguments.unpackAtomic(v, marshalledValues)
102+
}
103+
104+
func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interface{}) error {
105+
100106
var (
101-
value = valueOf.Elem()
107+
value = reflect.ValueOf(v).Elem()
102108
typ = value.Type()
103109
kind = value.Kind()
104110
)
@@ -120,25 +126,9 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
120126
exists[field] = true
121127
}
122128
}
123-
// `i` counts the nonindexed arguments.
124-
// `j` counts the number of complex types.
125-
// both `i` and `j` are used to to correctly compute `data` offset.
126-
127-
j := 0
128129
for i, arg := range arguments.NonIndexed() {
129130

130-
marshalledValue, err := toGoType((i+j)*32, arg.Type, output)
131-
if err != nil {
132-
return err
133-
}
134-
135-
if arg.Type.T == ArrayTy {
136-
// combined index ('i' + 'j') need to be adjusted only by size of array, thus
137-
// we need to decrement 'j' because 'i' was incremented
138-
j += arg.Type.Size - 1
139-
}
140-
141-
reflectValue := reflect.ValueOf(marshalledValue)
131+
reflectValue := reflect.ValueOf(marshalledValues[i])
142132

143133
switch kind {
144134
case reflect.Struct:
@@ -171,37 +161,29 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
171161
}
172162

173163
// unpackAtomic unpacks ( hexdata -> go ) a single value
174-
func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
175-
// make sure the passed value is arguments pointer
176-
valueOf := reflect.ValueOf(v)
177-
if reflect.Ptr != valueOf.Kind() {
178-
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
179-
}
180-
arg := arguments[0]
181-
if arg.Indexed {
182-
return fmt.Errorf("abi: attempting to unpack indexed variable into element.")
183-
}
164+
func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues []interface{}) error {
184165

185-
value := valueOf.Elem()
186-
marshalledValue, err := toGoType(0, arg.Type, output)
187-
if err != nil {
188-
return err
166+
if len(marshalledValues) != 1 {
167+
return fmt.Errorf("abi: wrong length, expected single value, got %d", len(marshalledValues))
189168
}
190-
return set(value, reflect.ValueOf(marshalledValue), arg)
169+
170+
elem := reflect.ValueOf(v).Elem()
171+
reflectValue := reflect.ValueOf(marshalledValues[0])
172+
return set(elem, reflectValue, arguments.NonIndexed()[0])
191173
}
192174

193175
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
194176
// without supplying a struct to unpack into. Instead, this method returns a list containing the
195177
// values. An atomic argument will be a list with one element.
196-
func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error){
178+
func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
197179

198-
retval := make([]interface{},0,arguments.LengthNonIndexed())
180+
retval := make([]interface{}, 0, arguments.LengthNonIndexed())
199181

200182
virtualArgs := 0
201183

202-
for index,arg:= range arguments.NonIndexed(){
184+
for index, arg := range arguments.NonIndexed() {
203185

204-
marshalledValue, err := toGoType((index + virtualArgs) * 32, arg.Type, data)
186+
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
205187

206188
if arg.Type.T == ArrayTy {
207189
//If we have a static array, like [3]uint256, these are coded as
@@ -212,7 +194,7 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error){
212194
virtualArgs += arg.Type.Size - 1
213195
}
214196

215-
if err != nil{
197+
if err != nil {
216198
return nil, err
217199
}
218200
retval = append(retval, marshalledValue)
@@ -226,7 +208,6 @@ func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
226208
return arguments.Pack(args...)
227209
}
228210

229-
230211
// Pack performs the operation Go format -> Hexdata
231212
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
232213
// Make sure arguments match up and pack them

‎accounts/abi/unpackv2_test.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func TestUnpackV2(t *testing.T) {
5757
}
5858
}
5959

60-
6160
func TestMultiReturnWithArrayV2(t *testing.T) {
6261
const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
6362
abi, err := JSON(strings.NewReader(definition))

0 commit comments

Comments
 (0)