From 89c6c5bb85ff24c152218f245fa366e733c951a7 Mon Sep 17 00:00:00 2001 From: Thomas Bocek Date: Thu, 2 Jun 2016 18:43:27 +0200 Subject: accounts/abi: Negative numbers not properly converted in ABI encoding When converting a negative number e.g., -2, the resulting ABI encoding should look as follows: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe. However, since the check of the type is for an uint instead of an int, it results in the following ABI encoding: 0101010101010101010101010101010101010101010101010101010101010102. The Ethereum ABI (https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI) says, that signed integers are stored in two's complement which should be of the form ffffff.... and not 01010101..... for e.g. -1. Thus, I removed the type check in numbers.go as well as the function S256 as I don't think they are correct. Or maybe I'm missing something? --- accounts/abi/numbers_test.go | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) (limited to 'accounts/abi/numbers_test.go') diff --git a/accounts/abi/numbers_test.go b/accounts/abi/numbers_test.go index d66a43258..f409aa60f 100644 --- a/accounts/abi/numbers_test.go +++ b/accounts/abi/numbers_test.go @@ -26,48 +26,28 @@ import ( func TestNumberTypes(t *testing.T) { ubytes := make([]byte, 32) ubytes[31] = 1 - sbytesmin := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} unsigned := U256(big.NewInt(1)) if !bytes.Equal(unsigned, ubytes) { t.Errorf("expected %x got %x", ubytes, unsigned) } - - signed := S256(big.NewInt(1)) - if !bytes.Equal(signed, ubytes) { - t.Errorf("expected %x got %x", ubytes, unsigned) - } - - signed = S256(big.NewInt(-1)) - if !bytes.Equal(signed, sbytesmin) { - t.Errorf("expected %x got %x", ubytes, unsigned) - } } func TestPackNumber(t *testing.T) { ubytes := make([]byte, 32) ubytes[31] = 1 - sbytesmin := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} maxunsigned := []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} - packed := packNum(reflect.ValueOf(1), IntTy) - if !bytes.Equal(packed, ubytes) { - t.Errorf("expected %x got %x", ubytes, packed) - } - packed = packNum(reflect.ValueOf(-1), IntTy) - if !bytes.Equal(packed, sbytesmin) { - t.Errorf("expected %x got %x", ubytes, packed) - } - packed = packNum(reflect.ValueOf(1), UintTy) + packed := packNum(reflect.ValueOf(1)) if !bytes.Equal(packed, ubytes) { t.Errorf("expected %x got %x", ubytes, packed) } - packed = packNum(reflect.ValueOf(-1), UintTy) + packed = packNum(reflect.ValueOf(-1)) if !bytes.Equal(packed, maxunsigned) { t.Errorf("expected %x got %x", maxunsigned, packed) } - packed = packNum(reflect.ValueOf("string"), UintTy) + packed = packNum(reflect.ValueOf("string")) if packed != nil { t.Errorf("expected 'string' to pack to nil. got %x instead", packed) } -- cgit v1.2.3