headlong — new Contract ABI and RLP library (my project)

Java 1.7+, or Android API 10(ish)+


Previously only an RLP library, headlong now has Contract ABI features, an optimized Keccak implementation, and a faster (custom) hexadecimal codec.

The Function class:

Function signatures are automatically canonicalized. The *encodeCall* and *decodeCall* methods automatically validate the arguments against the function signature (e.g. *encodeCall()* throws when the *BigDecimal* argument for the *ufixed128x18* parameter has bit length > 128 or scale != 18). Supports up to 255-dimensional arrays (same as Java). Primitive arrays are supported: *byte[]*, *int[]*, *long[]*, *boolean[]*.

Example usage:

Function f = new Function(“baz(uint32,bool)”);

ByteBuffer encoded = f.encodeCall(69L, true); // arguments auto-boxed and wrapped in a Tuple
Tuple args = f.decodeCall(encoded.flip()); // Tuple is an immutable list of Objects, in this case { Long.valueOf(69L), Boolean.TRUE }

ByteBuffer buffer = ByteBuffer.allocate(f.lengthFor(args));

System.out.println(“equals = ” +
f.encodeCall(args, buffer)
System.out.println(“0x” + Function.hex(encoded.array()));


equals = true
0 0000000000000000000000000000000000000000000000000000000000000045
1 0000000000000000000000000000000000000000000000000000000000000001

The MonteCarloTest (single-threaded) tests random functions. It encodes and decodes one call (with randomly generated matching arguments) per function. When testing 100,000 functions on a laptop, the total time spent in the *decodeCall* method averages about 400ms. This gives a function call decode rate of 250,000 calls per second. Performance of *encodeCall()* is similar (~500ms, 200,000/s). What constitutes a random function is defined in MonteCarloTestCase (basically it has a random number of arguments between 0 and 5 (inclusive) with psuedorandomly selected types (each of which, with probability 0.25, can be a 1-dimensional array of a type, with between 0 and 33 elements (inclusive)).

View Reddit by AndDontCallMePammyView Source