Skip to content

Commit

Permalink
add decodeSingletonReturn methods to Function;
Browse files Browse the repository at this point in the history
fix bug in test: extra i++ in gambleGamble loop;
update README;
bump version;
  • Loading branch information
esaulpaugh committed Nov 10, 2020
1 parent c05ce69 commit 9346980
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ABI spec: https://solidity.readthedocs.io/en/latest/abi-spec.html

RLP spec: https://github.com/ethereum/wiki/wiki/RLP

SHA-256 (headlong-4.0.0.jar): 60901358a33d78cb9893e4d839113e757a6d31a6842da37d16254019c07873eb
SHA-256 (headlong-4.0.1.jar): 56d02076dee49be090d8cd04296101316c75a1ae27b533a545068410954b7f0f

## Usage

Expand Down Expand Up @@ -51,6 +51,17 @@ Tuple decoded = foo.decodeReturn(
System.out.println(decoded.equals(new Tuple(new BigDecimal(BigInteger.valueOf(69L), 18), "w00t")));
```

```java
Function fooTwo = new Function("fooTwo()", "(uint8)");
Integer returned = fooTwo.decodeSingletonReturn(FastHex.decode("00000000000000000000000000000000000000000000000000000000000000FF", Integer.class));
```

#### Creating types directly

```java
BooleanType bool = (BooleanType) TypeFactory.create("bool", Boolean.class);
```

### RLP codec

```java
Expand Down Expand Up @@ -92,14 +103,14 @@ Or build locally:
Clone the project and install to your local maven repository using `gradle publishToMavenLocal` or `mvn install`, then declare it as a dependency:

```groovy
implementation 'com.esaulpaugh:headlong:4.0.1-SNAPSHOT'
implementation 'com.esaulpaugh:headlong:4.0.2-SNAPSHOT'
```

```xml
<dependency>
<groupId>com.esaulpaugh</groupId>
<artifactId>headlong</artifactId>
<version>4.0.1-SNAPSHOT</version>
<version>4.0.2-SNAPSHOT</version>
</dependency>
```
Alternatively:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {

group 'com.esaulpaugh'
archivesBaseName = "headlong"
version '4.0.1-SNAPSHOT'
version '4.0.2-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.esaulpaugh</groupId>
<artifactId>headlong</artifactId>
<version>4.0.1-SNAPSHOT</version>
<version>4.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>headlong</name>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/esaulpaugh/headlong/abi/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ public Tuple decodeReturn(byte[] returnVals) {
return outputTypes.decode(returnVals);
}

public Object decodeSingletonReturn(byte[] singleton) {
return decodeSingletonReturn(singleton, Object.class);
}

@SuppressWarnings("unchecked")
public <J> J decodeSingletonReturn(byte[] singleton, Class<J> classOfJ) {
return (J) outputTypes.get(0).decode(singleton);
}

public Tuple decodeReturn(ByteBuffer returnVals) {
return outputTypes.decode(returnVals);
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/com/esaulpaugh/headlong/abi/DecodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ public void testLenient() throws Throwable {

@Test
public void testUint() {
Function f = new Function("()", "(uint8)");
int val = (int) (Math.pow(2, 8)) - 1;
byte[] _byte = Strings.decode("00000000000000000000000000000000000000000000000000000000000000FF");
assertEquals((int) (Math.pow(2, 8)) - 1, new Function("()", "(uint8)").decodeReturn(_byte).get(0));

assertEquals(val, f.decodeReturn(_byte).get(0));
assertEquals(val, f.decodeSingletonReturn(_byte));
assertEquals(val, f.decodeSingletonReturn(_byte, Integer.class));

byte[] _int_ = Strings.decode("000000000000000000000000000000000000000000000000000000000000FFFF");
assertEquals((int) (Math.pow(2, 16)) - 1, new Function("()", "(uint16)").decodeReturn(_int_).get(0));
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public void fuzzSignatures() throws InterruptedException, TimeoutException {

final int size = map.size();
System.out.println("\nsize=" + map.size());
assertEquals(2 + (32 * 80), size);

List<String> list = new ArrayList<>();
for(Map.Entry<String, String> e : map.entrySet()) {
Expand All @@ -145,6 +144,8 @@ public void fuzzSignatures() throws InterruptedException, TimeoutException {
Collections.sort(list);

list.forEach(System.out::println);

assertEquals(2 + (32 * 80), size); // for prefix fixed or ufixed
}

private static final String ABI = "a71100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000004a00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230c5748dd399896196f734a17ecf80b2178adc067b361d5ba994ae6400000000b95ffb49ee209c0d760c3350ecaae6d4ad4ff7b62af4d91ef3b5ae4e00000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001816e178e3509b80a6a66a8827c8d5e62be37692efe0e22e59f936b8b000000000000000000000000000000000000000000000000000000000000000000000001a48b4c097589c15784ef0b97c17aa6dbb752f61fd1a272618d87a535000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033b6ae013f517ec3c2082aa77400d768168266af2c3c3680404075f300000000093ab24d3e36bb7194c68063c595ead7554b2e8ce3fa1e6498c0c1208000000009fd62e78a55909d9e882d50a8074384af470c9c13e618ccc761f09a700000000000000000000000000000000000000000000000000000000000000000000000182a731ded7c7b3dd45c528564df732e138a71dbccd14954c45b5c14e000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010daf12dcf419efe89f949a26f943bd7236aafbb8d6c4749fd27ed36f000000000000000000000000000000000000000000000000000000000000000000000003e6501ff6bc4074725885f5e65cc263b9ab0f098c14bc7cce3c2e4d2900000000f351c27189ce03746ce7dab3c560fb2bb430551b55a77a18380f066100000000e9753f8a5bb5e0da442d7e11d38d1eaafe5b6df463fd2256be199e380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000d05d841062e000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000001b052db5529dc0433400000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000013160000000000000000000000000000000000000000000000000000000000000001000000000051299437b715c73400c5e6e655c925eb10ee34669cf8f1ccf8febd";
Expand Down
48 changes: 28 additions & 20 deletions src/test/java/com/esaulpaugh/headlong/abi/MonteCarloTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,35 @@ private static int count(Object o) {
for(Object e : (Iterable<?>) o) {
x += count(e);
}
} else if (o instanceof byte[]) {
x += ((byte[]) o).length;
} else if(o instanceof BigInteger) {
x += ((BigInteger) o).toByteArray().length;
} else if(o instanceof BigDecimal) {
x += ((BigDecimal) o).unscaledValue().toByteArray().length;
} else if(o instanceof Integer) {
x += Integer.BYTES;
} else if(o instanceof Long) {
x += Long.BYTES;
} else if(o instanceof int[]) {
x += ((int[]) o).length * Integer.BYTES;
} else if(o instanceof long[]) {
x += ((long[]) o).length * Long.BYTES;
} else if(o instanceof Boolean) {
x += 1;
} else if(o instanceof String) {
x += Strings.decode((String) o, Strings.UTF_8).length;
} else if(o instanceof Number) {
if(o instanceof BigInteger) {
x += ((BigInteger) o).toByteArray().length;
} else if(o instanceof BigDecimal) {
x += ((BigDecimal) o).unscaledValue().toByteArray().length;
} else if(o instanceof Integer) {
x += Integer.BYTES;
} else if(o instanceof Long) {
x += Long.BYTES;
} else {
throw new Error("" + o.getClass());
}
} else {
throw new Error("" + o.getClass());
Class<?> c = o.getClass();
if (c == Boolean.class) {
x += 1;
} else if (c == boolean[].class) {
x += ((boolean[]) o).length;
} else if (c == int[].class) {
x += ((int[]) o).length * Integer.BYTES;
} else if (c == long[].class) {
x += ((long[]) o).length * Long.BYTES;
} else if (c == byte[].class) {
x += ((byte[]) o).length;
} else if (c == String.class) {
x += Strings.decode((String) o, Strings.UTF_8).length;
} else {
throw new Error("" + c);
}
}
return x;
}
Expand Down Expand Up @@ -157,7 +166,6 @@ static void doMonteCarlo(long threadSeed, int n) {
// .append("\t\t")
// .append(testCase.function.getCanonicalSignature().substring(testCase.function.getCanonicalSignature().indexOf('('))) // print function params
// .append('\n');
i++;
} catch (Throwable t) {
System.out.println(log.toString());
sleep();
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/esaulpaugh/headlong/abi/TupleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public void metaTest1() {

boolean[] bools = new boolean[pow];

@SuppressWarnings("unchecked")
UnitType<? extends Number> type = (UnitType<? extends Number>) TupleType.parse("(int" + bits + ")").get(0);
IntType type = (IntType) TypeFactory.create("int" + bits, Integer.class);

for (int i = 0; i < 1_579_919_999; i++) {
bools[(int) generateLong(r, type) & powMinus1] = true;
Expand Down

0 comments on commit 9346980

Please sign in to comment.