@Test public void calculateSize_nullByteString() { // Prefix indicates a payload of size 0, with a 1 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0x80")); assertThat(size).isEqualTo(1); }
@Test public void calculateSize_longByteStringWithMultiByteSize() { // Prefix indicates a payload of 258 bytes, with a 3 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0xB90102")); assertThat(size).isEqualTo(261); }
@Test public void calculateSize_emptyList() { int size = RLP.calculateSize(BytesValue.fromHexString("0xC0")); assertThat(size).isEqualTo(1); }
@Test public void calculateSize_smallByteString() { // Prefix indicates a payload of size 5, with a 1 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0x85")); assertThat(size).isEqualTo(6); }
@Test public void calculateSize_maxSmallByteString() { // Prefix indicates a payload of size 55, with a 1 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0xB7")); assertThat(size).isEqualTo(56); }
@Test public void calculateSize_minNonEmptyList() { int size = RLP.calculateSize(BytesValue.fromHexString("0xC1")); assertThat(size).isEqualTo(2); }
@Test public void calculateSize_longListWithMultiByteSize() { // Prefix indicates a payload of 258 bytes, with a 3 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0xF90102")); assertThat(size).isEqualTo(261); }
@Test public void calculateSize_singleByteValue() { int size = RLP.calculateSize(BytesValue.fromHexString("0x01")); assertThat(size).isEqualTo(1); }
@Test public void calculateSize_minSingleByteValue() { int size = RLP.calculateSize(BytesValue.fromHexString("0x00")); assertThat(size).isEqualTo(1); }
@Test public void calculateSize_longByteString() { // Prefix indicates a payload of 56 bytes, with a 2 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0xB838")); assertThat(size).isEqualTo(58); }
@Test public void calculateSize_maxRLPStringLength() { // Value represents a single item with an encoded payload size of MAX_VALUE - 5 and // 5 bytes of metadata (payload is not actually present) assertThat(RLP.calculateSize(h("0xBB7FFFFFFA"))).isEqualTo(Integer.MAX_VALUE); }
@Test public void calculateSize_maxSingleByteValue() { int size = RLP.calculateSize(BytesValue.fromHexString("0x7F")); assertThat(size).isEqualTo(1); }
@Test public void calculateSize_minNonNullSmallByteString() { // Prefix indicates a payload of size 1, with a 1 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0x81")); assertThat(size).isEqualTo(2); }
@Test public void calculateSize_shortList() { // Prefix indicates a payload of 5 bytes, with a 1 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0xC5")); assertThat(size).isEqualTo(6); }
@Test public void calculateSize_maxShortList() { int size = RLP.calculateSize(BytesValue.fromHexString("0xF7")); assertThat(size).isEqualTo(56); }
@Test public void calculateSize_longList() { // Prefix indicates a payload of 56 bytes, with a 2 byte prefix int size = RLP.calculateSize(BytesValue.fromHexString("0xF838")); assertThat(size).isEqualTo(58); }
@Test public void calculateSize_fuzz() { final Random random = new Random(1); for (int i = 0; i < 1000; ++i) { BytesValueRLPOutput out = RLPTestUtil.randomRLPValue(random.nextInt()); assertThat(RLP.calculateSize(out.encoded())).isEqualTo(out.encodedSize()); } }
@Test public void calculateSize_overflowMaxRLPStringLength() { // Value represents a single item with an encoded payload size of MAX_VALUE - 4 and // 5 bytes of metadata (payload is not actually present) assertThatThrownBy(() -> RLP.calculateSize(h("0xBB7FFFFFFB"))) .isInstanceOf(RLPException.class) .hasMessageContaining("RLP item exceeds max supported size of 2147483647: 2147483648"); }
@Test public void calculateSize_extremelyDeepNestedList() { final int MAX_DEPTH = 20000; final BytesValueRLPOutput out = new BytesValueRLPOutput(); int depth = 0; for (int i = 0; i < MAX_DEPTH; ++i) { out.startList(); depth += 1; } while (depth > 0) { out.endList(); --depth; } assertThat(RLP.calculateSize(out.encoded())).isEqualTo(out.encodedSize()); }
@Test public void getBlockBodiesRoundTrip() throws IOException { final List<Hash> hashes = new ArrayList<>(); final ByteBuffer buffer = ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks"))); for (int i = 0; i < 50; ++i) { final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer)); final byte[] block = new byte[blockSize]; buffer.get(block); buffer.compact().position(0); final RLPInput oneBlock = new BytesValueRLPInput(BytesValue.wrap(block), false); oneBlock.enterList(); hashes.add(BlockHeader.readFrom(oneBlock, MainnetBlockHashFunction::createHash).getHash()); // We don't care about the bodies, just the headers oneBlock.skipNext(); oneBlock.skipNext(); } final MessageData initialMessage = GetBlockBodiesMessage.create(hashes); final MessageData raw = new RawMessage(EthPV62.GET_BLOCK_BODIES, initialMessage.getData()); final GetBlockBodiesMessage message = GetBlockBodiesMessage.readFrom(raw); final Iterator<Hash> readHeaders = message.hashes().iterator(); for (int i = 0; i < 50; ++i) { Assertions.assertThat(readHeaders.next()).isEqualTo(hashes.get(i)); } } }