Skip to content

Contract ABI

Evan Saulpaugh edited this page Nov 10, 2018 · 35 revisions

Contract ABI Codec

Description

Canonicalizes and parses function signatures, encodes and decodes contract ABI calls. Offers detailed error messages for illegal signature syntax and non-matching arguments.

Supports Java types of Integer, int[], Long, long[], BigInteger, BigDecimal, Boolean, boolean[], String, byte[], a custom Tuple type, and arrays of any of these. Arrays can have up to 255 dimensions (max allowed by Java) and tuples can be arbitrarily nested (tested to depth of 2000). Can handle roughly 225,000 function call encodes or decodes per second, single-threaded on a laptop.

Usage

Function f = new Function("baz(uint32,bool)");
Tuple argsTuple = new Tuple(69L, true);
ByteBuffer one = f.encodeCall(argsTuple);
ByteBuffer two = f.encodeCallForArgs(69L, true);

System.out.println(Function.formatABI(one.array()));

Tuple decoded = f.decodeCall((ByteBuffer) two.flip());

System.out.println(decoded.equals(argsTuple));

Tests

MonteCarloTest generates a random function signature, parses it, generates random matching arguments, encodes and decodes a function call, and tests for equality. Done N times successively. Randomness is derived from an inital seed, and is fully replayable. Customizable parameters for maximum tuple/array depth/length are used to constrain memory usage and running time.

Implementation Details

Before encoding, the output length is calculated so that a buffer of the correct length can be allocated. Encoding is done strictly left-to-right (no backtracking to insert offsets).

During decode, dynamic element offsets are read but their values are not necessary to complete the process as decoding is done strictly left-to-right.

A limited number of reflective calls are used to support the decoding of multidimensional arrays whose types are known only at runtime.

Build

Refer to https://github.com/esaulpaugh/headlong/wiki/Build

Clone this wiki locally