From aa2e04bc8614a9d74a8a34d132e10415d87a1966 Mon Sep 17 00:00:00 2001 From: Manuel Polzhofer Date: Thu, 15 Nov 2018 16:56:09 +0100 Subject: [PATCH 1/2] formatting --- accounts/abi/argument.go | 3 --- accounts/abi/type.go | 11 +---------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go index d91f32027858..b5e6cd185883 100644 --- a/accounts/abi/argument.go +++ b/accounts/abi/argument.go @@ -245,7 +245,6 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) { for _, abiArg := range abiArgs { inputOffset += getOffset(abiArg.Type) } - var ret []byte for i, a := range args { input := abiArgs[i] @@ -258,10 +257,8 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) { if offsetRequired(input.Type) { // set the offset ret = append(ret, packNum(reflect.ValueOf(inputOffset))...) - // calculate next offset inputOffset += len(packed) - // append to variable input variableInput = append(variableInput, packed...) } else { diff --git a/accounts/abi/type.go b/accounts/abi/type.go index ef775ce807dc..1a3fd1fe1e28 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -198,24 +198,20 @@ func (t Type) pack(v reflect.Value) ([]byte, error) { if offsetReq { offset = getOffset(*t.Elem) * v.Len() } - var tail []byte for i := 0; i < v.Len(); i++ { val, err := t.Elem.pack(v.Index(i)) if err != nil { return nil, err } - if !offsetReq { ret = append(ret, val...) continue } - ret = append(ret, packNum(reflect.ValueOf(offset))...) offset += len(val) tail = append(tail, val...) } - return append(ret, tail...), nil default: return packElement(t, v), nil @@ -232,11 +228,7 @@ func (t Type) requiresLengthPrefix() bool { func offsetRequired(t Type) bool { // dynamic types // array is also a dynamic type if the array type is dynamic - if t.T == StringTy || t.T == BytesTy || t.T == SliceTy || (t.T == ArrayTy && offsetRequired(*t.Elem)) { - return true - } - - return false + return t.T == StringTy || t.T == BytesTy || t.T == SliceTy || (t.T == ArrayTy && offsetRequired(*t.Elem)) } // getOffset returns the offset to be added for t @@ -246,6 +238,5 @@ func getOffset(t Type) int { if t.T == ArrayTy && !offsetRequired(*t.Elem) { return 32 * t.Size } - return 32 } From 4738b7af37ed32602b5715fa599d64c85e6fd5e5 Mon Sep 17 00:00:00 2001 From: Manuel Polzhofer Date: Thu, 15 Nov 2018 17:29:58 +0100 Subject: [PATCH 2/2] better comments --- accounts/abi/pack_test.go | 59 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/accounts/abi/pack_test.go b/accounts/abi/pack_test.go index 8491e3fe7337..ddd2b7362d5a 100644 --- a/accounts/abi/pack_test.go +++ b/accounts/abi/pack_test.go @@ -328,8 +328,8 @@ func TestPack(t *testing.T) { "string[]", []string{"hello", "foobar"}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 - "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0 - "0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i=1 + "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 + "0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i = 1 "0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5 "68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0] "0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6 @@ -338,54 +338,51 @@ func TestPack(t *testing.T) { { "string[2]", []string{"hello", "foobar"}, - common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset to hello - "0000000000000000000000000000000000000000000000000000000000000080" + // offset to foobar - "0000000000000000000000000000000000000000000000000000000000000005" + // length of hello - "68656c6c6f000000000000000000000000000000000000000000000000000000" + // encoded foobar - "0000000000000000000000000000000000000000000000000000000000000006" + // length of foobar - "666f6f6261720000000000000000000000000000000000000000000000000000"), // encoded foobar + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset to i = 0 + "0000000000000000000000000000000000000000000000000000000000000080" + // offset to i = 1 + "0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5 + "68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0] + "0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6 + "666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1] }, { "bytes32[][]", [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 - "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0 - "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i=1 + "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 + "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 - "0100000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0200000000000000000000000000000000000000000000000000000000000000" + // array[1] + "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0] + "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 - "0300000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0400000000000000000000000000000000000000000000000000000000000000" + // array[1] - "0500000000000000000000000000000000000000000000000000000000000000"), // array[2] - + "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0] + "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1] + "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2] }, { "bytes32[][2]", [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, - common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0 - "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i=1 + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 + "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 - "0100000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0200000000000000000000000000000000000000000000000000000000000000" + // array[1] + "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0] + "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 - "0300000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0400000000000000000000000000000000000000000000000000000000000000" + // array[1] - "0500000000000000000000000000000000000000000000000000000000000000"), // array[2] - + "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0] + "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1] + "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2] }, { "bytes32[3][2]", [][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}, - common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0200000000000000000000000000000000000000000000000000000000000000" + // array[1] - "0300000000000000000000000000000000000000000000000000000000000000" + // array[2] - "0300000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0400000000000000000000000000000000000000000000000000000000000000" + // array[1] - "0500000000000000000000000000000000000000000000000000000000000000"), // array[2] - + common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0] + "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1] + "0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2] + "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0] + "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1] + "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2] }, } { typ, err := NewType(test.typ)