Search in sources :

Example 36 with RLPElement

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

the class RLPTest method encodeDecodeShortListWithTwoByteArraysWithThreeBytesLength.

@Test
public void encodeDecodeShortListWithTwoByteArraysWithThreeBytesLength() {
    byte[] value1 = new byte[128 * 256 - 3];
    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 + 3 + 3 + (128 * 256 - 3) + 3 + (128 * 256 - 3), encoded.length);
    Assert.assertEquals((byte) (247 + 3), encoded[0]);
    Assert.assertEquals((byte) (1), encoded[1]);
    Assert.assertEquals((byte) (0), encoded[2]);
    Assert.assertEquals((byte) (0), encoded[3]);
    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 37 with RLPElement

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

the class RLPTest method encodeDecodeShortListWithShortBytes.

@Test
public void encodeDecodeShortListWithShortBytes() {
    byte[] value1 = new byte[] { 0x01 };
    byte[] value2 = new byte[] { 0x02 };
    byte[] element1 = RLP.encodeElement(value1);
    byte[] element2 = RLP.encodeElement(value2);
    byte[] encoded = RLP.encodeList(element1, element2);
    Assert.assertNotNull(encoded);
    Assert.assertEquals(3, encoded.length);
    Assert.assertEquals((byte) (192 + 2), 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(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 38 with RLPElement

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

the class ContractDetailsImpl method decode.

@Override
public final void decode(byte[] rlpBytes) {
    ArrayList<RLPElement> rlpData = RLP.decode2(rlpBytes);
    RLPList rlpList = (RLPList) rlpData.get(0);
    RLPItem rlpAddress = (RLPItem) rlpList.get(0);
    RLPItem rlpIsExternalStorage = (RLPItem) rlpList.get(1);
    RLPItem rlpStorage = (RLPItem) rlpList.get(2);
    RLPElement rlpCode = rlpList.get(3);
    RLPList rlpKeys = (RLPList) rlpList.get(4);
    this.address = rlpAddress.getRLPData();
    this.externalStorage = rlpIsExternalStorage.getRLPData() != null;
    this.originalExternalStorage = this.externalStorage;
    if (this.externalStorage) {
        Keccak256 snapshotHash = new Keccak256(rlpStorage.getRLPData());
        this.trie = new TrieImpl(new TrieStoreImpl(levelDbByName(config, getDataSourceName())), true).getSnapshotTo(snapshotHash);
    } else {
        this.trie = TrieImpl.deserialize(rlpStorage.getRLPData());
    }
    this.code = (rlpCode.getRLPData() == null) ? EMPTY_BYTE_ARRAY : rlpCode.getRLPData();
    for (RLPElement key : rlpKeys) {
        addKey(key.getRLPData());
    }
    logger.trace("decoding contract details from bytes, hash {}, address {}, storage size {}, has external storage {}", this.getStorageHashAsString(), this.getAddressAsString(), this.getStorageSize(), this.hasExternalStorage());
}
Also used : RLPItem(org.ethereum.util.RLPItem) RLPElement(org.ethereum.util.RLPElement) Keccak256(co.rsk.crypto.Keccak256) RLPList(org.ethereum.util.RLPList)

Aggregations

RLPElement (org.ethereum.util.RLPElement)38 Test (org.junit.Test)29 RLPList (org.ethereum.util.RLPList)19 ArrayList (java.util.ArrayList)4 LogInfo (org.ethereum.vm.LogInfo)3 Coin (co.rsk.core.Coin)2 RskAddress (co.rsk.core.RskAddress)2 Keccak256 (co.rsk.crypto.Keccak256)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Script (co.rsk.bitcoinj.script.Script)1 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)1 BridgeConstants (co.rsk.config.BridgeConstants)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 Federation (co.rsk.peg.Federation)1 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)1 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)1 RemascTransaction (co.rsk.remasc.RemascTransaction)1 LinkedList (java.util.LinkedList)1 Block (org.ethereum.core.Block)1 ImmutableTransaction (org.ethereum.core.ImmutableTransaction)1