Revert "Abi/dynamic types (#5)" (#6)

This reverts commit dabca31d79.
This commit is contained in:
Vedhavyas Singareddi 2018-11-07 11:59:32 +01:00 committed by GitHub
parent dabca31d79
commit fc490afef7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 53 deletions

View file

@ -42,7 +42,7 @@ func TestPack(t *testing.T) {
{ {
"uint8[]", "uint8[]",
[]uint8{1, 2}, []uint8{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"uint16", "uint16",
@ -52,7 +52,7 @@ func TestPack(t *testing.T) {
{ {
"uint16[]", "uint16[]",
[]uint16{1, 2}, []uint16{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"uint32", "uint32",
@ -62,7 +62,7 @@ func TestPack(t *testing.T) {
{ {
"uint32[]", "uint32[]",
[]uint32{1, 2}, []uint32{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"uint64", "uint64",
@ -72,7 +72,7 @@ func TestPack(t *testing.T) {
{ {
"uint64[]", "uint64[]",
[]uint64{1, 2}, []uint64{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"uint256", "uint256",
@ -82,7 +82,7 @@ func TestPack(t *testing.T) {
{ {
"uint256[]", "uint256[]",
[]*big.Int{big.NewInt(1), big.NewInt(2)}, []*big.Int{big.NewInt(1), big.NewInt(2)},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"int8", "int8",
@ -92,7 +92,7 @@ func TestPack(t *testing.T) {
{ {
"int8[]", "int8[]",
[]int8{1, 2}, []int8{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"int16", "int16",
@ -102,7 +102,7 @@ func TestPack(t *testing.T) {
{ {
"int16[]", "int16[]",
[]int16{1, 2}, []int16{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"int32", "int32",
@ -112,7 +112,7 @@ func TestPack(t *testing.T) {
{ {
"int32[]", "int32[]",
[]int32{1, 2}, []int32{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"int64", "int64",
@ -122,7 +122,7 @@ func TestPack(t *testing.T) {
{ {
"int64[]", "int64[]",
[]int64{1, 2}, []int64{1, 2},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"int256", "int256",
@ -132,7 +132,7 @@ func TestPack(t *testing.T) {
{ {
"int256[]", "int256[]",
[]*big.Int{big.NewInt(1), big.NewInt(2)}, []*big.Int{big.NewInt(1), big.NewInt(2)},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
}, },
{ {
"bytes1", "bytes1",
@ -308,12 +308,18 @@ func TestPack(t *testing.T) {
//web3.eth.abi.encodeParameter('address[]', ['0x0100000000000000000000000000000000000000','0x0200000000000000000000000000000000000000']); //web3.eth.abi.encodeParameter('address[]', ['0x0100000000000000000000000000000000000000','0x0200000000000000000000000000000000000000']);
"address[]", "address[]",
[]common.Address{{1}, {2}}, []common.Address{{1}, {2}},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000002" +
"0000000000000000000000000100000000000000000000000000000000000000" +
"0000000000000000000000000200000000000000000000000000000000000000"),
}, },
{ {
"bytes32[]", "bytes32[]",
[]common.Hash{{1}, {2}}, []common.Hash{{1}, {2}},
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + //offset: 32
"0000000000000000000000000000000000000000000000000000000000000002" + //len: 2
"0100000000000000000000000000000000000000000000000000000000000000" + // 1
"0200000000000000000000000000000000000000000000000000000000000000"), // 2
}, },
{ {
"function", "function",
@ -323,12 +329,15 @@ func TestPack(t *testing.T) {
{ {
"string", "string",
"foobar", "foobar",
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset: 32
"0000000000000000000000000000000000000000000000000000000000000006" + // len: 6
"666f6f6261720000000000000000000000000000000000000000000000000000"), // "foobar"
}, },
{ {
"string[]", "string[]",
[]string{"hello", "foobar"}, []string{"hello", "foobar"},
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset array 32
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0 "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0
"0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i=1 "0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i=1
"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5 "0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
@ -343,7 +352,8 @@ func TestPack(t *testing.T) {
// '0x0400000000000000000000000000000000000000000000000000000000000000','0x0500000000000000000000000000000000000000000000000000000000000000']]); // '0x0400000000000000000000000000000000000000000000000000000000000000','0x0500000000000000000000000000000000000000000000000000000000000000']]);
"bytes32[][]", "bytes32[][]",
[][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}},
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset array 32
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
"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

View file

@ -183,43 +183,23 @@ func (t Type) pack(v reflect.Value) ([]byte, error) {
return nil, err return nil, err
} }
switch t.T { if t.T == SliceTy || t.T == ArrayTy {
case SliceTy, ArrayTy: var packed []byte
var ret []byte
if t.requiresLengthPrefix() {
// append length
ret = append(ret, packNum(reflect.ValueOf(v.Len()))...)
}
// calculate offset if any
offset := 0
offsetReq := t.Elem.requiresLengthPrefix()
if offsetReq {
offset = getOffset(*t.Elem) * v.Len()
}
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
} }
packed = append(packed, val...)
if !offsetReq { }
ret = append(ret, val...) if t.T == SliceTy {
continue return packBytesSlice(packed, v.Len()), nil
} else if t.T == ArrayTy {
return packed, nil
} }
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 return packElement(t, v), nil
}
} }
// requireLengthPrefix returns whether the type requires any sort of length // requireLengthPrefix returns whether the type requires any sort of length
@ -227,12 +207,3 @@ func (t Type) pack(v reflect.Value) ([]byte, error) {
func (t Type) requiresLengthPrefix() bool { func (t Type) requiresLengthPrefix() bool {
return t.T == StringTy || t.T == BytesTy || t.T == SliceTy return t.T == StringTy || t.T == BytesTy || t.T == SliceTy
} }
// getOffset returns the offset to be added for t
func getOffset(t Type) int {
if t.T == ArrayTy {
return 32 * t.Size
}
return 32
}