From af1cb62b03b05b8e86307e91eb2f077bd25edf11 Mon Sep 17 00:00:00 2001 From: Manuel Polzhofer Date: Tue, 6 Nov 2018 16:42:18 +0100 Subject: [PATCH 1/3] added tests from bytes32[][] and string[] --- accounts/abi/pack_test.go | 55 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/accounts/abi/pack_test.go b/accounts/abi/pack_test.go index 58a5b7a581e2..a8fb8644939c 100644 --- a/accounts/abi/pack_test.go +++ b/accounts/abi/pack_test.go @@ -18,6 +18,7 @@ package abi import ( "bytes" + "fmt" "math" "math/big" "reflect" @@ -312,7 +313,10 @@ func TestPack(t *testing.T) { { "bytes32[]", []common.Hash{{1}, {2}}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + //offset: 32 + "0000000000000000000000000000000000000000000000000000000000000002" + //len: 2 + "0100000000000000000000000000000000000000000000000000000000000000" + // 1 + "0200000000000000000000000000000000000000000000000000000000000000"), // 2 }, { "function", @@ -322,8 +326,43 @@ func TestPack(t *testing.T) { { "string", "foobar", - common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"+ // offset: 32 + "0000000000000000000000000000000000000000000000000000000000000006" +// len: 6 + "666f6f6261720000000000000000000000000000000000000000000000000000"),// "foobar" }, + { + "string[]", + []string{"hello","foobar"}, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset array 32 + "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 + "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 + "666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1] + }, + { + + //web3.eth.abi.encodeParameter('bytes32[][]', [['0x0100000000000000000000000000000000000000000000000000000000000000', + // '0x0200000000000000000000000000000000000000000000000000000000000000'],['0x0300000000000000000000000000000000000000000000000000000000000000', + // '0x0400000000000000000000000000000000000000000000000000000000000000','0x0500000000000000000000000000000000000000000000000000000000000000']]); + "bytes32[][]", + [][]common.Hash{{{1}, {2}},{{3}, {4},{5}}}, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset array 32 + "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 + "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0 + "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i=1 + "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 + "0100000000000000000000000000000000000000000000000000000000000000" + // array[0] + "0200000000000000000000000000000000000000000000000000000000000000" + // array[1] + "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 + "0300000000000000000000000000000000000000000000000000000000000000" + // array[0] + "0400000000000000000000000000000000000000000000000000000000000000" + // array[1] + "0500000000000000000000000000000000000000000000000000000000000000"), // array[2] + + }, + } { typ, err := NewType(test.typ) if err != nil { @@ -341,6 +380,18 @@ func TestPack(t *testing.T) { } } +func TestDummy(t *testing.T){ + fmt.Println(common.Hash{1}.String()) + fmt.Println(common.Hash{2}.String()) + fmt.Println(common.Hash{3}.String()) + fmt.Println(common.Hash{4}.String()) + fmt.Println(common.Hash{5}.String()) + + + + fmt.Println(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000")) +} + func TestMethodPack(t *testing.T) { abi, err := JSON(strings.NewReader(jsondata2)) if err != nil { From 90371345b022651af3ff3bbee2e3b5267a31bb2e Mon Sep 17 00:00:00 2001 From: Manuel Polzhofer Date: Tue, 6 Nov 2018 16:56:12 +0100 Subject: [PATCH 2/3] added offset to other types --- accounts/abi/pack_test.go | 42 ++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/accounts/abi/pack_test.go b/accounts/abi/pack_test.go index a8fb8644939c..b9dc714228b5 100644 --- a/accounts/abi/pack_test.go +++ b/accounts/abi/pack_test.go @@ -18,7 +18,6 @@ package abi import ( "bytes" - "fmt" "math" "math/big" "reflect" @@ -43,7 +42,8 @@ func TestPack(t *testing.T) { { "uint8[]", []uint8{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + }, { "uint16", @@ -53,7 +53,7 @@ func TestPack(t *testing.T) { { "uint16[]", []uint16{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "uint32", @@ -63,7 +63,7 @@ func TestPack(t *testing.T) { { "uint32[]", []uint32{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "uint64", @@ -73,7 +73,7 @@ func TestPack(t *testing.T) { { "uint64[]", []uint64{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "uint256", @@ -83,7 +83,7 @@ func TestPack(t *testing.T) { { "uint256[]", []*big.Int{big.NewInt(1), big.NewInt(2)}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "int8", @@ -93,7 +93,7 @@ func TestPack(t *testing.T) { { "int8[]", []int8{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "int16", @@ -103,7 +103,7 @@ func TestPack(t *testing.T) { { "int16[]", []int16{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "int32", @@ -113,7 +113,7 @@ func TestPack(t *testing.T) { { "int32[]", []int32{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "int64", @@ -123,7 +123,7 @@ func TestPack(t *testing.T) { { "int64[]", []int64{1, 2}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "int256", @@ -133,7 +133,7 @@ func TestPack(t *testing.T) { { "int256[]", []*big.Int{big.NewInt(1), big.NewInt(2)}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), }, { "bytes1", @@ -306,9 +306,13 @@ func TestPack(t *testing.T) { common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018"), }, { + //web3.eth.abi.encodeParameter('address[]', ['0x0100000000000000000000000000000000000000','0x0200000000000000000000000000000000000000']); "address[]", []common.Address{{1}, {2}}, - common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"), + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + + "0000000000000000000000000000000000000000000000000000000000000002" + + "0000000000000000000000000100000000000000000000000000000000000000" + + "0000000000000000000000000200000000000000000000000000000000000000"), }, { "bytes32[]", @@ -375,23 +379,11 @@ func TestPack(t *testing.T) { } if !bytes.Equal(output, test.output) { - t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output) + t.Errorf("input %d for typ: %v failed. Expected bytes: '%x' Got: '%x'", i,typ.String(), test.output, output) } } } -func TestDummy(t *testing.T){ - fmt.Println(common.Hash{1}.String()) - fmt.Println(common.Hash{2}.String()) - fmt.Println(common.Hash{3}.String()) - fmt.Println(common.Hash{4}.String()) - fmt.Println(common.Hash{5}.String()) - - - - fmt.Println(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000")) -} - func TestMethodPack(t *testing.T) { abi, err := JSON(strings.NewReader(jsondata2)) if err != nil { From 5de431bc9c07e76c45f1cc9227abda7a95f0c7f5 Mon Sep 17 00:00:00 2001 From: Manuel Polzhofer Date: Tue, 6 Nov 2018 16:57:07 +0100 Subject: [PATCH 3/3] formatting --- accounts/abi/pack_test.go | 60 +++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/accounts/abi/pack_test.go b/accounts/abi/pack_test.go index b9dc714228b5..5f802fce07a5 100644 --- a/accounts/abi/pack_test.go +++ b/accounts/abi/pack_test.go @@ -43,7 +43,6 @@ func TestPack(t *testing.T) { "uint8[]", []uint8{1, 2}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), - }, { "uint16", @@ -310,17 +309,17 @@ func TestPack(t *testing.T) { "address[]", []common.Address{{1}, {2}}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + - "0000000000000000000000000000000000000000000000000000000000000002" + - "0000000000000000000000000100000000000000000000000000000000000000" + - "0000000000000000000000000200000000000000000000000000000000000000"), + "0000000000000000000000000000000000000000000000000000000000000002" + + "0000000000000000000000000100000000000000000000000000000000000000" + + "0000000000000000000000000200000000000000000000000000000000000000"), }, { "bytes32[]", []common.Hash{{1}, {2}}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + //offset: 32 - "0000000000000000000000000000000000000000000000000000000000000002" + //len: 2 - "0100000000000000000000000000000000000000000000000000000000000000" + // 1 - "0200000000000000000000000000000000000000000000000000000000000000"), // 2 + "0000000000000000000000000000000000000000000000000000000000000002" + //len: 2 + "0100000000000000000000000000000000000000000000000000000000000000" + // 1 + "0200000000000000000000000000000000000000000000000000000000000000"), // 2 }, { "function", @@ -330,21 +329,21 @@ func TestPack(t *testing.T) { { "string", "foobar", - common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"+ // offset: 32 - "0000000000000000000000000000000000000000000000000000000000000006" +// len: 6 - "666f6f6261720000000000000000000000000000000000000000000000000000"),// "foobar" + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset: 32 + "0000000000000000000000000000000000000000000000000000000000000006" + // len: 6 + "666f6f6261720000000000000000000000000000000000000000000000000000"), // "foobar" }, { "string[]", - []string{"hello","foobar"}, + []string{"hello", "foobar"}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset array 32 - "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 - "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 - "666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1] + "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 + "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 + "666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1] }, { @@ -352,21 +351,20 @@ func TestPack(t *testing.T) { // '0x0200000000000000000000000000000000000000000000000000000000000000'],['0x0300000000000000000000000000000000000000000000000000000000000000', // '0x0400000000000000000000000000000000000000000000000000000000000000','0x0500000000000000000000000000000000000000000000000000000000000000']]); "bytes32[][]", - [][]common.Hash{{{1}, {2}},{{3}, {4},{5}}}, + [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020" + // offset array 32 - "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 - "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0 - "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i=1 - "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 - "0100000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0200000000000000000000000000000000000000000000000000000000000000" + // array[1] - "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 - "0300000000000000000000000000000000000000000000000000000000000000" + // array[0] - "0400000000000000000000000000000000000000000000000000000000000000" + // array[1] - "0500000000000000000000000000000000000000000000000000000000000000"), // array[2] + "0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2 + "0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i=0 + "00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i=1 + "0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2 + "0100000000000000000000000000000000000000000000000000000000000000" + // array[0] + "0200000000000000000000000000000000000000000000000000000000000000" + // array[1] + "0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3 + "0300000000000000000000000000000000000000000000000000000000000000" + // array[0] + "0400000000000000000000000000000000000000000000000000000000000000" + // array[1] + "0500000000000000000000000000000000000000000000000000000000000000"), // array[2] }, - } { typ, err := NewType(test.typ) if err != nil { @@ -379,7 +377,7 @@ func TestPack(t *testing.T) { } if !bytes.Equal(output, test.output) { - t.Errorf("input %d for typ: %v failed. Expected bytes: '%x' Got: '%x'", i,typ.String(), test.output, output) + t.Errorf("input %d for typ: %v failed. Expected bytes: '%x' Got: '%x'", i, typ.String(), test.output, output) } } }