added better formatting (#8)

This commit is contained in:
Manuel Polzhofer 2018-11-15 17:38:19 +01:00 committed by Vedhavyas Singareddi
parent b65b3d2512
commit 9810e35a66
3 changed files with 29 additions and 44 deletions

View file

@ -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 {

View file

@ -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)

View file

@ -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
}