Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit a03131f

Browse files
authored
Merge pull request #1187 from eth-events/fix/1184-no-distinction-between-uint-and-int
Issue-1184: no distinction between uint and int
2 parents 78b4b27 + 196e0a7 commit a03131f

File tree

3 files changed

+127
-20
lines changed

3 files changed

+127
-20
lines changed

ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public String getCanonicalName() {
5858
public static SolidityType getType(String typeName) {
5959
if (typeName.contains("[")) return ArrayType.getType(typeName);
6060
if ("bool".equals(typeName)) return new BoolType();
61-
if (typeName.startsWith("int") || typeName.startsWith("uint")) return new IntType(typeName);
61+
if (typeName.startsWith("int")) return new IntType(typeName);
62+
if (typeName.startsWith("uint")) return new UnsignedIntType(typeName);
6263
if ("address".equals(typeName)) return new AddressType();
6364
if ("string".equals(typeName)) return new StringType();
6465
if ("bytes".equals(typeName)) return new BytesType();
@@ -356,23 +357,14 @@ public Object decode(byte[] encoded, int offset) {
356357
return ByteUtil.bigIntegerToBytes(bi, 20);
357358
}
358359
}
359-
360-
public static class IntType extends SolidityType {
361-
public IntType(String name) {
360+
361+
public static abstract class NumericType extends SolidityType {
362+
public NumericType(String name) {
362363
super(name);
363364
}
364365

365-
@Override
366-
public String getCanonicalName() {
367-
if (getName().equals("int")) return "int256";
368-
if (getName().equals("uint")) return "uint256";
369-
return super.getCanonicalName();
370-
}
371-
372-
@Override
373-
public byte[] encode(Object value) {
366+
BigInteger encodeInternal(Object value) {
374367
BigInteger bigInt;
375-
376368
if (value instanceof String) {
377369
String s = ((String)value).toLowerCase().trim();
378370
int radix = 10;
@@ -393,14 +385,20 @@ public byte[] encode(Object value) {
393385
} else {
394386
throw new RuntimeException("Invalid value for type '" + this + "': " + value + " (" + value.getClass() + ")");
395387
}
396-
return encodeInt(bigInt);
388+
return bigInt;
389+
}
390+
}
391+
392+
public static class IntType extends NumericType {
393+
public IntType(String name) {
394+
super(name);
397395
}
398396

399397
@Override
400-
public Object decode(byte[] encoded, int offset) {
401-
return decodeInt(encoded, offset);
398+
public String getCanonicalName() {
399+
if (getName().equals("int")) return "int256";
400+
return super.getCanonicalName();
402401
}
403-
404402
public static BigInteger decodeInt(byte[] encoded, int offset) {
405403
return new BigInteger(Arrays.copyOfRange(encoded, offset, offset + 32));
406404
}
@@ -410,6 +408,48 @@ public static byte[] encodeInt(int i) {
410408
public static byte[] encodeInt(BigInteger bigInt) {
411409
return ByteUtil.bigIntegerToBytesSigned(bigInt, 32);
412410
}
411+
@Override
412+
public Object decode(byte[] encoded, int offset) {
413+
return decodeInt(encoded, offset);
414+
}
415+
@Override
416+
public byte[] encode(Object value) {
417+
BigInteger bigInt = encodeInternal(value);
418+
return encodeInt(bigInt);
419+
}
420+
}
421+
422+
public static class UnsignedIntType extends NumericType {
423+
public UnsignedIntType(String name) {
424+
super(name);
425+
}
426+
427+
@Override
428+
public String getCanonicalName() {
429+
if (getName().equals("uint")) return "uint256";
430+
return super.getCanonicalName();
431+
}
432+
public static BigInteger decodeInt(byte[] encoded, int offset) {
433+
return new BigInteger(1, Arrays.copyOfRange(encoded, offset, offset + 32));
434+
}
435+
public static byte[] encodeInt(int i) {
436+
return encodeInt(new BigInteger("" + i));
437+
}
438+
public static byte[] encodeInt(BigInteger bigInt) {
439+
if (bigInt.signum() == -1) {
440+
throw new RuntimeException("Wrong value for uint type: " + bigInt);
441+
}
442+
return ByteUtil.bigIntegerToBytes(bigInt, 32);
443+
}
444+
@Override
445+
public byte[] encode(Object value) {
446+
BigInteger bigInt = encodeInternal(value);
447+
return encodeInt(bigInt);
448+
}
449+
@Override
450+
public Object decode(byte[] encoded, int offset) {
451+
return decodeInt(encoded, offset);
452+
}
413453
}
414454

415455
public static class BoolType extends IntType {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) [2016] [ <ether.camp> ]
3+
* This file is part of the ethereumJ library.
4+
*
5+
* The ethereumJ library is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* The ethereumJ library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.ethereum.solidity;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
import java.math.BigInteger;
23+
24+
import org.junit.Test;
25+
import org.spongycastle.util.encoders.Hex;
26+
27+
/**
28+
* Created by Maximilian Schmidt on 25.09.2018.
29+
*/
30+
public class SolidityTypeTest {
31+
@Test
32+
public void ensureUnsignedInteger_isDecodedWithCorrectSignum() {
33+
byte[] bigNumberByteArray = { -13, -75, 19, 86, -119, 67, 112, -4, 118, -86, 98, -46, 103, -42, -126, 63, -60, -15, -87, 57, 43, 11, -17, -52,
34+
0, 3, -65, 14, -67, -40, 65, 119 };
35+
SolidityType testObject = new SolidityType.UnsignedIntType("uint256");
36+
Object decode = testObject.decode(bigNumberByteArray);
37+
assertEquals(decode.getClass(), BigInteger.class);
38+
BigInteger actualBigInteger = (BigInteger) decode;
39+
BigInteger expectedBigInteger = new BigInteger(Hex.toHexString(bigNumberByteArray), 16);
40+
assertEquals(expectedBigInteger, actualBigInteger);
41+
}
42+
43+
@Test
44+
public void ensureSignedInteger_isDecoded() {
45+
byte[] bigNumberByteArray = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 127, -1, -1, -1, -1, -1, -1, -1 };
46+
SolidityType testObject = new SolidityType.IntType("int256");
47+
Object decode = testObject.decode(bigNumberByteArray);
48+
assertEquals(decode.getClass(), BigInteger.class);
49+
BigInteger actualBigInteger = (BigInteger) decode;
50+
BigInteger expectedBigInteger = new BigInteger(bigNumberByteArray);
51+
assertEquals(expectedBigInteger, actualBigInteger);
52+
}
53+
}

ethereumj-core/src/test/java/org/ethereum/util/StandaloneBlockchainTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,28 @@ public void encodeTest1() {
115115
StandaloneBlockchain sb = new StandaloneBlockchain().withAutoblock(true);
116116
SolidityContract a = sb.submitNewContract(
117117
"contract A {" +
118-
" uint public a;" +
119-
" function f(uint a_) {a = a_;}" +
118+
" int public a;" +
119+
" function f(int a_) {a = a_;}" +
120120
"}");
121121
a.callFunction("f", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
122122
BigInteger r = (BigInteger) a.callConstFunction("a")[0];
123123
System.out.println(r.toString(16));
124124
Assert.assertEquals(new BigInteger(Hex.decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")), r);
125125
}
126+
127+
@Test
128+
public void encodeTest2() {
129+
StandaloneBlockchain sb = new StandaloneBlockchain().withAutoblock(true);
130+
SolidityContract a = sb.submitNewContract(
131+
"contract A {" +
132+
" uint public a;" +
133+
" function f(uint a_) {a = a_;}" +
134+
"}");
135+
a.callFunction("f", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
136+
BigInteger r = (BigInteger) a.callConstFunction("a")[0];
137+
System.out.println(r.toString(16));
138+
Assert.assertEquals(new BigInteger(1, Hex.decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")), r);
139+
}
126140

127141
@Test
128142
public void invalidTxTest() {

0 commit comments

Comments
 (0)