Skip to content

Commit

Permalink
add regression test for eth encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
loongy committed Aug 15, 2020
1 parent 43a9706 commit 391def5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
10 changes: 4 additions & 6 deletions compat/ethereumcompat/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func Encode(vals ...interface{}) []byte {

switch val := val.(type) {
case pack.Bytes:
ethval = val
println("BYTES")
ethval = []byte(val)
ty, err = abi.NewType("bytes", "", nil)
case pack.Bytes32:
ethval = val
ethval = [32]byte(val)
ty, err = abi.NewType("bytes32", "", nil)

case pack.U8:
ethval = big.NewInt(0).SetUint64(uint64(val.Uint8()))
ty, err = abi.NewType("uint256", "", nil)
Expand All @@ -53,11 +53,9 @@ func Encode(vals ...interface{}) []byte {
case pack.U256:
ethval = val.Int()
ty, err = abi.NewType("uint256", "", nil)

case Address:
ethval = val
ty, err = abi.NewType("bytes20", "", nil)

ty, err = abi.NewType("address", "", nil)
default:
panic(fmt.Errorf("non-exhaustive pattern: %T", val))
}
Expand Down
30 changes: 30 additions & 0 deletions compat/ethereumcompat/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,36 @@ var _ = Describe("Encoding", func() {
})
})

Context("when encoding complex calldata", func() {
It("should return the expected bytes", func() {
phashHex := "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
phashBytes, err := hex.DecodeString(phashHex)
Expect(err).ToNot(HaveOccurred())
var phash pack.Bytes32
copy(phash[:], phashBytes)
tokenHex := "0x675000eed287586fcb53d676f4ab1c15e8be314d"
tokenAddr, err := ethereumcompat.NewAddressFromHex(tokenHex)
Expect(err).ToNot(HaveOccurred())
toHex := "0xc7ddb84d0d8f70dbd8453f88a5967681cd3c9830"
toAddr, err := ethereumcompat.NewAddressFromHex(toHex)
Expect(err).ToNot(HaveOccurred())
nhashHex := "5f2515e44d1bf07d591b78f5c896c87b7dd7441fbf3395f3f0c88578ef7bdc04"
nhashBytes, err := hex.DecodeString(nhashHex)
Expect(err).ToNot(HaveOccurred())
var nhash pack.Bytes32
copy(nhash[:], nhashBytes)
args := []interface{}{
phash,
pack.NewU256FromU64(pack.NewU64(9711004)),
ethereumcompat.Address(tokenAddr),
ethereumcompat.Address(toAddr),
nhash,
}
result := ethereumcompat.Encode(args...)
Expect(hex.EncodeToString(result)).To(Equal("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4700000000000000000000000000000000000000000000000000000000000942d9c000000000000000000000000675000eed287586fcb53d676f4ab1c15e8be314d000000000000000000000000c7ddb84d0d8f70dbd8453f88a5967681cd3c98305f2515e44d1bf07d591b78f5c896c87b7dd7441fbf3395f3f0c88578ef7bdc04"))
})
})

type testCase struct {
addr string
amount uint64
Expand Down

0 comments on commit 391def5

Please sign in to comment.