/** * Recursively decodes an RLP encoded value. Byte strings are assumed to be non-scalar (leading * zeros are allowed). * * @param value The RLP encoded value to decode. * @return The output of decoding {@code value}. It will be either directly a {@link BytesValue}, * or a list whose elements are either {@link BytesValue}, or similarly composed sub-lists. * @throws RLPException if {@code value} is not a properly formed RLP encoding. */ public static Object decode(final BytesValue value) { return decode(RLP.input(value)); }
@Test(expected = RLPException.class) public void deserializeDifferentPacketData() { final byte[] r = new byte[64]; new Random().nextBytes(r); final BytesValue target = BytesValue.wrap(r); final FindNeighborsPacketData packet = FindNeighborsPacketData.create(target); final BytesValue serialized = RLP.encode(packet::writeTo); assertNotNull(serialized); NeighborsPacketData.readFrom(RLP.input(serialized)); }
@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); }
"Unmatched logs hash. Generated logs: " + logs, expectedLogsHash, Hash.hash(RLP.encode(logs::writeTo)));
private void assertLongScalar(final long expected, final BytesValue toTest) { final RLPInput in = RLP.input(toTest); assertThat(in.isDone()).isFalse(); assertThat(in.readLongScalar()).isEqualTo(expected); assertThat(in.isDone()).isTrue(); }
@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); }
private void assertIntScalar(final int expected, final BytesValue toTest) { final RLPInput in = RLP.input(toTest); assertThat(in.isDone()).isFalse(); assertThat(in.readIntScalar()).isEqualTo(expected); assertThat(in.isDone()).isTrue(); }
@Test public void calculateSize_emptyList() { int size = RLP.calculateSize(BytesValue.fromHexString("0xC0")); assertThat(size).isEqualTo(1); }
@Test public void empty() { final RLPInput in = RLP.input(BytesValue.EMPTY); assertThat(in.isDone()).isTrue(); }
@Test public void serializeDeserializeFindNeighborsPacketData() { final byte[] r = new byte[64]; new Random().nextBytes(r); final BytesValue target = BytesValue.wrap(r); final FindNeighborsPacketData packet = FindNeighborsPacketData.create(target); final BytesValue serialized = RLP.encode(packet::writeTo); assertNotNull(serialized); final FindNeighborsPacketData deserialized = FindNeighborsPacketData.readFrom(RLP.input(serialized)); assertThat(deserialized.getTarget()).isEqualTo(target); // Fuzziness: allow a skew of 1.5 seconds between the time the message was generated until the // assertion. assertThat(deserialized.getExpiration()) .isCloseTo( System.currentTimeMillis() + PacketData.DEFAULT_EXPIRATION_PERIOD_MS, offset(1500L)); }
@Test public void calculateSize_singleByteValue() { int size = RLP.calculateSize(BytesValue.fromHexString("0x01")); assertThat(size).isEqualTo(1); }
@Test public void raw() { final BytesValue initial = h("0xc80102c51112c22122"); final RLPInput in = RLP.input(initial); assertThat(in.raw()).isEqualTo(initial); }
@Test public void neighborsPacketData() { final List<DiscoveryPeer> peers = helper.createDiscoveryPeers(5); final NeighborsPacketData packet = NeighborsPacketData.create(peers); final BytesValue serialized = RLP.encode(packet::writeTo); assertNotNull(serialized); final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(serialized)); assertThat(deserialized.getNodes()).isEqualTo(peers); // Fuzziness: allow a skew of 1.5 seconds between the time the message was generated until the // assertion. assertThat(deserialized.getExpiration()) .isCloseTo( System.currentTimeMillis() + PacketData.DEFAULT_EXPIRATION_PERIOD_MS, offset(1500L)); }
@Test public void calculateSize_minSingleByteValue() { int size = RLP.calculateSize(BytesValue.fromHexString("0x00")); assertThat(size).isEqualTo(1); }
@Test public void decodeValueWithLeadingZerosAsUnsignedInt() { RLPInput in = RLP.input(h("0x84000000D0")); assertThat(in.readUnsignedInt()).isEqualTo(208); }
public void milestone(final String milestone) { final TransactionTestCaseSpec.Expectation expected = spec.expectation(milestone); try { final BytesValue rlp = spec.getRlp(); // Test transaction deserialization (will throw an exception if it fails). final Transaction transaction = Transaction.readFrom(RLP.input(rlp)); if (!transactionValidator(milestone).validate(transaction).isValid()) { throw new RuntimeException(String.format("Transaction is invalid %s", transaction)); } // Test rlp encoding final BytesValue actualRlp = RLP.encode(transaction::writeTo); Assert.assertTrue(expected.isSucceeds()); Assert.assertEquals(rlp, actualRlp); Assert.assertEquals(expected.getSender(), transaction.getSender()); Assert.assertEquals(expected.getHash(), transaction.hash()); } catch (final Exception e) { Assert.assertFalse(expected.isSucceeds()); } } }
@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 decodeValueWithLeadingZerosAsSignedInt() { RLPInput in = RLP.input(h("0x84000000D0")); assertThat(in.readInt()).isEqualTo(208); }