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 { for _, abiArg := range abiArgs {
inputOffset += getOffset(abiArg.Type) inputOffset += getOffset(abiArg.Type)
} }
var ret []byte var ret []byte
for i, a := range args { for i, a := range args {
input := abiArgs[i] input := abiArgs[i]
@ -258,10 +257,8 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
if offsetRequired(input.Type) { if offsetRequired(input.Type) {
// set the offset // set the offset
ret = append(ret, packNum(reflect.ValueOf(inputOffset))...) ret = append(ret, packNum(reflect.ValueOf(inputOffset))...)
// calculate next offset // calculate next offset
inputOffset += len(packed) inputOffset += len(packed)
// append to variable input // append to variable input
variableInput = append(variableInput, packed...) variableInput = append(variableInput, packed...)
} else { } else {

View file

@ -338,12 +338,12 @@ func TestPack(t *testing.T) {
{ {
"string[2]", "string[2]",
[]string{"hello", "foobar"}, []string{"hello", "foobar"},
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset to hello common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset to i = 0
"0000000000000000000000000000000000000000000000000000000000000080" + // offset to foobar "0000000000000000000000000000000000000000000000000000000000000080" + // offset to i = 1
"0000000000000000000000000000000000000000000000000000000000000005" + // length of hello "0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
"68656c6c6f000000000000000000000000000000000000000000000000000000" + // encoded foobar "68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
"0000000000000000000000000000000000000000000000000000000000000006" + // length of foobar "0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
"666f6f6261720000000000000000000000000000000000000000000000000000"), // encoded foobar "666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1]
}, },
{ {
"bytes32[][]", "bytes32[][]",
@ -352,13 +352,12 @@ func TestPack(t *testing.T) {
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0] "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
"0200000000000000000000000000000000000000000000000000000000000000" + // array[1] "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
"0300000000000000000000000000000000000000000000000000000000000000" + // array[0] "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1] "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
"0500000000000000000000000000000000000000000000000000000000000000"), // array[2] "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
}, },
{ {
@ -367,25 +366,23 @@ func TestPack(t *testing.T) {
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1 "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0] "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
"0200000000000000000000000000000000000000000000000000000000000000" + // array[1] "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
"0300000000000000000000000000000000000000000000000000000000000000" + // array[0] "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1] "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
"0500000000000000000000000000000000000000000000000000000000000000"), // array[2] "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
}, },
{ {
"bytes32[3][2]", "bytes32[3][2]",
[][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}, [][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}},
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + // array[0] common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
"0200000000000000000000000000000000000000000000000000000000000000" + // array[1] "0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
"0300000000000000000000000000000000000000000000000000000000000000" + // array[2] "0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2]
"0300000000000000000000000000000000000000000000000000000000000000" + // array[0] "0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1] "0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
"0500000000000000000000000000000000000000000000000000000000000000"), // array[2] "0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
}, },
} { } {
typ, err := NewType(test.typ) typ, err := NewType(test.typ)

View file

@ -198,24 +198,20 @@ func (t Type) pack(v reflect.Value) ([]byte, error) {
if offsetReq { if offsetReq {
offset = getOffset(*t.Elem) * v.Len() offset = getOffset(*t.Elem) * v.Len()
} }
var tail []byte var tail []byte
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
val, err := t.Elem.pack(v.Index(i)) val, err := t.Elem.pack(v.Index(i))
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !offsetReq { if !offsetReq {
ret = append(ret, val...) ret = append(ret, val...)
continue continue
} }
ret = append(ret, packNum(reflect.ValueOf(offset))...) ret = append(ret, packNum(reflect.ValueOf(offset))...)
offset += len(val) offset += len(val)
tail = append(tail, val...) tail = append(tail, val...)
} }
return append(ret, tail...), nil return append(ret, tail...), nil
default: default:
return packElement(t, v), nil return packElement(t, v), nil
@ -232,11 +228,7 @@ func (t Type) requiresLengthPrefix() bool {
func offsetRequired(t Type) bool { func offsetRequired(t Type) bool {
// dynamic types // dynamic types
// array is also a dynamic type if the array type is dynamic // 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 t.T == StringTy || t.T == BytesTy || t.T == SliceTy || (t.T == ArrayTy && offsetRequired(*t.Elem))
return true
}
return false
} }
// getOffset returns the offset to be added for t // 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) { if t.T == ArrayTy && !offsetRequired(*t.Elem) {
return 32 * t.Size return 32 * t.Size
} }
return 32 return 32
} }