Search in sources :

Example 21 with Tuple

use of com.esaulpaugh.headlong.abi.Tuple in project headlong by esaulpaugh.

the class SuperSerialTest method testSuperSerial.

@Test
public void testSuperSerial() throws Throwable {
    // java -jar headlong-cli-0.2-SNAPSHOT.jar -e "(uint[],int[],uint32,(int32,uint8,(bool[],int8,int40,int64,int,int,int[]),bool,bool,int256[]),int,int)" "([  ], [ '' ], '80', ['7f', '3b', [ [  ], '', '', '30ffcc0009', '01', '02', [ '70' ] ], '', '01', ['0092030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2020']], '', '05')"
    TestUtils.assertThrown(IllegalArgumentException.class, "tuple index 0: signed val exceeds bit limit: 256 >= 256", () -> SuperSerial.deserialize(TupleType.parse("(int256)"), "('0092030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2020')", false));
    SuperSerial.deserialize(TupleType.parse("(uint256)"), "('0092030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2020')", false);
    TestUtils.assertThrown(IllegalArgumentException.class, "RLPList not allowed for this type: int8", () -> SuperSerial.deserialize(TupleType.of("int8"), "(['90', '80', '77'])", false));
    String sig = "(uint[],int[],uint32,(int32,uint8,(bool[],int8,int40,int64,int,int,int[]),bool,bool,int256[]),int,int)";
    Function f = new Function(sig);
    String vals = "([  ], [ '' ], '80', ['7f', '3b', [ [  ], '', '', '30ffcc0009', '01', '02', [ '70' ] ], '', '01', ['92030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2020']], '', '05')";
    Tuple decoded = SuperSerial.deserialize(f.getInputs(), vals, false);
    f.getInputs().validate(decoded);
    ByteBuffer bb = f.getInputs().encode(decoded);
    Tuple dd = f.getInputs().decode(bb);
    assertEquals(decoded, dd);
}
Also used : Function(com.esaulpaugh.headlong.abi.Function) ByteBuffer(java.nio.ByteBuffer) Tuple(com.esaulpaugh.headlong.abi.Tuple) Test(org.junit.jupiter.api.Test)

Example 22 with Tuple

use of com.esaulpaugh.headlong.abi.Tuple in project headlong by esaulpaugh.

the class SuperSerialTest method testParseArgs.

@Test
public void testParseArgs() throws Throwable {
    String boolArrStr = "['00', '01', '01']";
    TupleType tt = TupleType.parse("(bool[])");
    Tuple tuple = tt.parseArgument("(\n" + boolArrStr + "\n)");
    boolean[] arr0 = tuple.get(0);
    assertArrayEquals(new boolean[] { false, true, true }, arr0);
    boolean[] arr1 = (boolean[]) tt.get(0).parseArgument(boolArrStr);
    assertArrayEquals(arr0, arr1);
    IntType int32 = TupleType.parse("(int32)").get(0);
    assertEquals(Integer.MIN_VALUE, int32.parseArgument(Integer.toString(Integer.MIN_VALUE)));
    assertEquals(Integer.MAX_VALUE, int32.parseArgument(Integer.toString(Integer.MAX_VALUE)));
    IntType int8 = TupleType.parse("(int8)").get(0);
    TestUtils.assertThrown(IllegalArgumentException.class, "signed val exceeds bit limit: 8 >= 8", () -> int8.parseArgument("-129"));
    TestUtils.assertThrown(IllegalArgumentException.class, "signed val exceeds bit limit: 8 >= 8", () -> int8.parseArgument("128"));
    BooleanType bool = TupleType.parse("(bool)").get(0);
    Object bool2 = TypeFactory.create("bool");
    assertEquals(bool, bool2);
    TypeFactory.createNonCapturing("bool").encode(true);
    assertEquals(true, bool.parseArgument("true"));
    assertEquals(true, bool.parseArgument("TRUE"));
    assertEquals(true, bool.parseArgument("tRUe"));
    assertEquals(false, bool.parseArgument("false"));
    assertEquals(false, bool.parseArgument(""));
    assertEquals(false, bool.parseArgument("?\t*jjgHJge"));
    assertEquals(127, int8.parseArgument("127"));
    assertEquals(127, int8.parseArgument("0127"));
    assertEquals(127, int8.parseArgument("00127"));
}
Also used : TupleType(com.esaulpaugh.headlong.abi.TupleType) BooleanType(com.esaulpaugh.headlong.abi.BooleanType) Tuple(com.esaulpaugh.headlong.abi.Tuple) IntType(com.esaulpaugh.headlong.abi.IntType) Test(org.junit.jupiter.api.Test)

Example 23 with Tuple

use of com.esaulpaugh.headlong.abi.Tuple in project headlong by esaulpaugh.

the class SuperSerial method deserialize.

public static Tuple deserialize(TupleType tupleType, String str, boolean machine) {
    Tuple in = deserializeTuple(tupleType, machine ? Strings.decode(str) : RLPEncoder.encodeSequentially(Notation.parse(str)));
    tupleType.validate(in);
    return in;
}
Also used : Tuple(com.esaulpaugh.headlong.abi.Tuple)

Aggregations

Tuple (com.esaulpaugh.headlong.abi.Tuple)23 ArrayList (java.util.ArrayList)6 BigInteger (java.math.BigInteger)5 TupleType (com.esaulpaugh.headlong.abi.TupleType)4 Test (org.junit.jupiter.api.Test)4 Function (com.esaulpaugh.headlong.abi.Function)2 ByteString (com.google.protobuf.ByteString)2 Address (com.esaulpaugh.headlong.abi.Address)1 BooleanType (com.esaulpaugh.headlong.abi.BooleanType)1 IntType (com.esaulpaugh.headlong.abi.IntType)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MoreObjects (com.google.common.base.MoreObjects)1 HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)1 ActionableContractCall (com.hedera.services.bdd.spec.infrastructure.meta.ActionableContractCall)1 TrieSigMapGenerator.uniqueWithFullPrefixesFor (com.hedera.services.bdd.spec.keys.TrieSigMapGenerator.uniqueWithFullPrefixesFor)1 QueryVerbs.getTxnRecord (com.hedera.services.bdd.spec.queries.QueryVerbs.getTxnRecord)1 HapiTxnOp (com.hedera.services.bdd.spec.transactions.HapiTxnOp)1 TxnUtils (com.hedera.services.bdd.spec.transactions.TxnUtils)1 TxnUtils.extractTxnId (com.hedera.services.bdd.spec.transactions.TxnUtils.extractTxnId)1