Search in sources :

Example 11 with RLPList

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

the class RLPTest method encodeDecodeShortListWithTwoByteArraysWithElementsLength56.

@Test
public void encodeDecodeShortListWithTwoByteArraysWithElementsLength56() {
    byte[] value1 = new byte[26];
    byte[] value2 = new byte[28];
    byte[] element1 = RLP.encodeElement(value1);
    byte[] element2 = RLP.encodeElement(value2);
    byte[] encoded = RLP.encodeList(element1, element2);
    Assert.assertNotNull(encoded);
    Assert.assertEquals(1 + 1 + 1 + 26 + 1 + 28, encoded.length);
    Assert.assertEquals((byte) (247 + 1), encoded[0]);
    Assert.assertEquals((byte) (56), 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 12 with RLPList

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

the class RemascState method create.

public static RemascState create(byte[] data) {
    RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
    Coin rlpRewardBalance = RLP.parseCoin(rlpList.get(0).getRLPData());
    Coin rlpBurnedBalance = RLP.parseCoin(rlpList.get(1).getRLPData());
    SortedMap<Long, List<Sibling>> rlpSiblings = RemascStorageProvider.getSiblingsFromBytes(rlpList.get(2).getRLPData());
    byte[] rlpBrokenSelectionRuleBytes = rlpList.get(3).getRLPData();
    Boolean rlpBrokenSelectionRule;
    if (rlpBrokenSelectionRuleBytes != null && rlpBrokenSelectionRuleBytes.length != 0 && rlpBrokenSelectionRuleBytes[0] != 0) {
        rlpBrokenSelectionRule = Boolean.TRUE;
    } else {
        rlpBrokenSelectionRule = Boolean.FALSE;
    }
    return new RemascState(rlpRewardBalance, rlpBurnedBalance, rlpSiblings, rlpBrokenSelectionRule);
}
Also used : Coin(co.rsk.core.Coin) List(java.util.List) RLPList(org.ethereum.util.RLPList) RLPList(org.ethereum.util.RLPList)

Example 13 with RLPList

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

the class RemascStorageProvider method getSiblingsFromBytes.

public static SortedMap<Long, List<Sibling>> getSiblingsFromBytes(byte[] bytes) {
    SortedMap<Long, List<Sibling>> siblings = new TreeMap<>();
    if (bytes == null || bytes.length == 0) {
        return siblings;
    }
    RLPList rlpList = (RLPList) RLP.decode2(bytes).get(0);
    int nentries = rlpList.size() / 2;
    for (int k = 0; k < nentries; k++) {
        byte[] bytesKey = rlpList.get(k * 2).getRLPData();
        byte[] bytesValue = rlpList.get(k * 2 + 1).getRLPData();
        long longKey = bytesKey == null ? 0 : BigIntegers.fromUnsignedByteArray(bytesKey).longValue();
        Long key = Long.valueOf(longKey);
        RLPList rlpSiblingList = (RLPList) RLP.decode2(bytesValue).get(0);
        int nsiblings = rlpSiblingList.size();
        List<Sibling> list = new ArrayList<>();
        for (int j = 0; j < nsiblings; j++) {
            byte[] bytesSibling = rlpSiblingList.get(j).getRLPData();
            Sibling sibling = Sibling.create(bytesSibling);
            list.add(sibling);
        }
        siblings.put(key, list);
    }
    return siblings;
}
Also used : RLPList(org.ethereum.util.RLPList) RLPList(org.ethereum.util.RLPList)

Example 14 with RLPList

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

the class Sibling method create.

public static Sibling create(byte[] data) {
    ArrayList<RLPElement> params = RLP.decode2(data);
    RLPList sibling = (RLPList) params.get(0);
    byte[] hash = sibling.get(0).getRLPData();
    RskAddress coinbase = RLP.parseRskAddress(sibling.get(1).getRLPData());
    RskAddress includedBlockCoinbase = RLP.parseRskAddress(sibling.get(2).getRLPData());
    Coin paidFees = RLP.parseCoin(sibling.get(3).getRLPData());
    byte[] bytesIncludedHeight = sibling.get(4).getRLPData();
    RLPElement uncleCountElement = sibling.get(5);
    byte[] bytesUncleCount = uncleCountElement != null ? uncleCountElement.getRLPData() : null;
    long includedHeight = bytesIncludedHeight == null ? 0 : BigIntegers.fromUnsignedByteArray(bytesIncludedHeight).longValue();
    int uncleCount = bytesUncleCount == null ? 0 : BigIntegers.fromUnsignedByteArray(bytesUncleCount).intValue();
    return new Sibling(hash, coinbase, includedBlockCoinbase, paidFees, includedHeight, uncleCount);
}
Also used : Coin(co.rsk.core.Coin) RLPElement(org.ethereum.util.RLPElement) RskAddress(co.rsk.core.RskAddress) RLPList(org.ethereum.util.RLPList)

Example 15 with RLPList

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

the class Block method parseRLP.

private void parseRLP() {
    ArrayList<RLPElement> params = RLP.decode2(rlpEncoded);
    RLPList block = (RLPList) params.get(0);
    // Parse Header
    RLPList header = (RLPList) block.get(0);
    this.header = new BlockHeader(header, this.sealed);
    // Parse Transactions
    RLPList txTransactions = (RLPList) block.get(1);
    this.transactionsList = parseTxs(txTransactions);
    byte[] calculatedRoot = getTxTrie(this.transactionsList).getHash().getBytes();
    this.checkExpectedRoot(this.header.getTxTrieRoot(), calculatedRoot);
    // Parse Uncles
    RLPList uncleBlocks = (RLPList) block.get(2);
    for (RLPElement rawUncle : uncleBlocks) {
        RLPList uncleHeader = (RLPList) rawUncle;
        BlockHeader blockData = new BlockHeader(uncleHeader, this.sealed);
        this.uncleList.add(blockData);
    }
    this.parsed = true;
}
Also used : RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList)

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