use of org.ethereum.util.RLPElement in project rskj by rsksmart.
the class RLPTest method encodeDecodeLongByteArrayWithThreeBytesLengthUsingEncode.
@Test
public void encodeDecodeLongByteArrayWithThreeBytesLengthUsingEncode() {
byte[] bytes = new byte[256 * 256];
byte[] encoded = RLP.encode(bytes);
Assert.assertNotNull(encoded);
Assert.assertEquals(4 + 256 * 256, encoded.length);
Assert.assertEquals((byte) (183 + 3), encoded[0]);
Assert.assertEquals((byte) 0x01, encoded[1]);
Assert.assertEquals((byte) 0x00, encoded[2]);
Assert.assertEquals((byte) 0x00, encoded[3]);
RLPElement element = RLP.decode2OneItem(encoded, 0);
Assert.assertNotNull(element);
byte[] decoded = element.getRLPData();
Assert.assertNotNull(decoded);
Assert.assertArrayEquals(bytes, decoded);
}
use of org.ethereum.util.RLPElement 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());
}
use of org.ethereum.util.RLPElement 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());
}
use of org.ethereum.util.RLPElement 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);
}
use of org.ethereum.util.RLPElement 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;
}
Aggregations