Search in sources :

Example 6 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class RLPTestCase method checkRLPAgainstJson.

public void checkRLPAgainstJson(RLPElement element, Object in) {
    if (in instanceof List) {
        Object[] array = ((List) in).toArray();
        RLPList list = (RLPList) element;
        for (int i = 0; i < array.length; i++) {
            checkRLPAgainstJson(list.get(i), array[i]);
        }
    } else if (in instanceof Number) {
        int computed = ByteUtil.byteArrayToInt(element.getRLPData());
        this.computed.add(Integer.toString(computed));
        this.expected.add(in.toString());
    } else if (in instanceof String) {
        String s = in.toString();
        if (s.contains("#")) {
            s = s.substring(1);
            BigInteger expected = new BigInteger(s);
            byte[] payload = element.getRLPData();
            BigInteger computed = new BigInteger(1, payload);
            this.computed.add(computed.toString());
            this.expected.add(expected.toString());
        } else {
            String expected = new String(element.getRLPData() != null ? element.getRLPData() : new byte[0], StandardCharsets.UTF_8);
            this.expected.add(expected);
            this.computed.add(s);
        }
    } else {
        throw new RuntimeException("Unexpected type: " + in.getClass());
    }
}
Also used : BigInteger(java.math.BigInteger) List(java.util.List) RLPList(org.ethereum.util.RLPList) ArrayList(java.util.ArrayList) RLPList(org.ethereum.util.RLPList)

Example 7 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class RLPTest method encodeDecodeShortListWithTwoByteArraysWithTwoBytesLength.

@Test
public void encodeDecodeShortListWithTwoByteArraysWithTwoBytesLength() {
    byte[] value1 = new byte[126];
    byte[] value2 = new byte[126];
    byte[] element1 = RLP.encodeElement(value1);
    byte[] element2 = RLP.encodeElement(value2);
    byte[] encoded = RLP.encodeList(element1, element2);
    Assert.assertNotNull(encoded);
    Assert.assertEquals(1 + 2 + 2 + 126 + 2 + 126, encoded.length);
    Assert.assertEquals((byte) (247 + 2), encoded[0]);
    Assert.assertEquals((byte) (1), encoded[1]);
    Assert.assertEquals((byte) (0), encoded[2]);
    ArrayList<RLPElement> list = RLP.decode2(encoded);
    Assert.assertNotNull(list);
    Assert.assertEquals(1, list.size());
    RLPList list2 = (RLPList) list.get(0);
    Assert.assertNotNull(list2);
    Assert.assertEquals(2, list2.size());
    Assert.assertArrayEquals(value1, list2.get(0).getRLPData());
    Assert.assertArrayEquals(value2, list2.get(1).getRLPData());
}
Also used : RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList) Test(org.junit.Test)

Example 8 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class RLPTest method encodeDecodeEmptyList.

@Test
public void encodeDecodeEmptyList() {
    byte[] encoded = RLP.encodeList();
    Assert.assertNotNull(encoded);
    Assert.assertEquals(1, encoded.length);
    Assert.assertEquals((byte) 192, encoded[0]);
    ArrayList<RLPElement> list = RLP.decode2(encoded);
    Assert.assertNotNull(list);
    Assert.assertEquals(1, list.size());
    RLPList list2 = (RLPList) list.get(0);
    Assert.assertNotNull(list2);
    Assert.assertEquals(0, list2.size());
}
Also used : RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList) Test(org.junit.Test)

Example 9 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class RLPTest method encodeDecodeShortListWithTwoByteArraysWithOneByteLength.

@Test
public void encodeDecodeShortListWithTwoByteArraysWithOneByteLength() {
    byte[] value1 = new byte[125];
    byte[] value2 = new byte[126];
    byte[] element1 = RLP.encodeElement(value1);
    byte[] element2 = RLP.encodeElement(value2);
    byte[] encoded = RLP.encodeList(element1, element2);
    Assert.assertNotNull(encoded);
    Assert.assertEquals(1 + 1 + 2 + 125 + 2 + 126, encoded.length);
    Assert.assertEquals((byte) (247 + 1), encoded[0]);
    Assert.assertEquals((byte) (255), encoded[1]);
    ArrayList<RLPElement> list = RLP.decode2(encoded);
    Assert.assertNotNull(list);
    Assert.assertEquals(1, list.size());
    RLPList list2 = (RLPList) list.get(0);
    Assert.assertNotNull(list2);
    Assert.assertEquals(2, list2.size());
    Assert.assertArrayEquals(value1, list2.get(0).getRLPData());
    Assert.assertArrayEquals(value2, list2.get(1).getRLPData());
}
Also used : RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList) Test(org.junit.Test)

Example 10 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class RLPTest method encodeDecodeShortListWithTwoByteArraysWithTwoBytesLengthBorderCase.

@Test
public void encodeDecodeShortListWithTwoByteArraysWithTwoBytesLengthBorderCase() {
    byte[] value1 = new byte[128 * 256 - 3 - 1];
    byte[] value2 = new byte[128 * 256 - 3];
    byte[] element1 = RLP.encodeElement(value1);
    byte[] element2 = RLP.encodeElement(value2);
    byte[] encoded = RLP.encodeList(element1, element2);
    Assert.assertNotNull(encoded);
    Assert.assertEquals(1 + 2 + 3 + (128 * 256 - 3 - 1) + 3 + (128 * 256 - 3), encoded.length);
    Assert.assertEquals((byte) (247 + 2), encoded[0]);
    Assert.assertEquals((byte) (255), encoded[1]);
    Assert.assertEquals((byte) (255), encoded[2]);
    ArrayList<RLPElement> list = RLP.decode2(encoded);
    Assert.assertNotNull(list);
    Assert.assertEquals(1, list.size());
    RLPList list2 = (RLPList) list.get(0);
    Assert.assertNotNull(list2);
    Assert.assertEquals(2, list2.size());
    Assert.assertArrayEquals(value1, list2.get(0).getRLPData());
    Assert.assertArrayEquals(value2, list2.get(1).getRLPData());
}
Also used : RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList) Test(org.junit.Test)

Aggregations

RLPList (org.ethereum.util.RLPList)60 RLPElement (org.ethereum.util.RLPElement)19 Test (org.junit.Test)13 Keccak256 (co.rsk.crypto.Keccak256)8 RskAddress (co.rsk.core.RskAddress)7 BigInteger (java.math.BigInteger)5 RLPItem (org.ethereum.util.RLPItem)5 LogInfo (org.ethereum.vm.LogInfo)5 Script (co.rsk.bitcoinj.script.Script)3 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)3 Coin (co.rsk.core.Coin)3 RepositoryImpl (co.rsk.db.RepositoryImpl)3 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)3 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2