@Test public void simpleIntBeforeShortList() { final RLPInput in = RLP.input(h("0x02c22c3b")); assertThat(in.isDone()).isFalse(); assertThat(in.readIntScalar()).isEqualTo(2); assertThat(in.enterList()).isEqualTo(2); assertThat(in.readByte()).isEqualTo((byte) 0x2c); assertThat(in.readByte()).isEqualTo((byte) 0x3b); in.leaveList(); assertThat(in.isDone()).isTrue(); }
private static Object decode(final RLPInput in) { if (!in.nextIsList()) { return in.readBytesValue(); } final int size = in.enterList(); final List<Object> l = new ArrayList<>(size); for (int i = 0; i < size; i++) l.add(decode(in)); in.leaveList(); return l; }
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(); }
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(); }
assertThat(nv.readAsRlp().raw()).isEqualTo(nv.raw()); nv.reset(); assertThat(nv.nextIsNull()).isTrue(); assertThat(nv.readAsRlp().nextIsNull()).isTrue(); assertThat(el.readAsRlp().raw()).isEqualTo(emptyList); el.reset(); assertThat(el.readAsRlp().enterList()).isEqualTo(0); el.reset(); assertThat(el.enterList()).isEqualTo(0); final RLPInput compare = nl.readAsRlp(); assertThat(compare.raw()).isEqualTo(nl.raw()); nl.reset(); nl.enterList(); nl.skipNext(); // 0x01 assertThat(nl.readAsRlp().raw()).isEqualTo(h("0x02")); assertThat(nl.readAsRlp().raw()).isEqualTo(h("0xc51112c22122")); nl.reset(); nl.enterList(); nl.skipNext(); nl.skipNext(); nl.enterList(); nl.skipNext(); nl.skipNext();
public static BlockBody readBody(final long num) throws IOException { final RLPInput input = new BytesValueRLPInput( BytesValue.wrap( Resources.toByteArray( EthHashTest.class.getResource(String.format("block_%d.blocks", num)))), false); input.enterList(); input.skipNext(); final List<Transaction> transactions = input.readList(Transaction::readFrom); final List<BlockHeader> ommers = input.readList(rlp -> BlockHeader.readFrom(rlp, MainnetBlockHashFunction::createHash)); return new BlockBody(transactions, ommers); }
@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)); } } }
@Test public void simpleListwithBytesValue() { final RLPInput in = RLP.input(h("0xc28180")); assertThat(in.isDone()).isFalse(); assertThat(in.enterList()).isEqualTo(1); assertThat(in.readBytesValue()).isEqualTo(h("0x80")); in.leaveList(); assertThat(in.isDone()).isTrue(); }
public Block getBlock() { final RLPInput input = new BytesValueRLPInput(rlp, false); input.enterList(); final BlockHeader header = BlockHeader.readFrom(input, MainnetBlockHashFunction::createHash); final BlockBody body = new BlockBody( input.readList(Transaction::readFrom), input.readList( rlp -> BlockHeader.readFrom(rlp, MainnetBlockHashFunction::createHash))); return new Block(header, body); } }
@Test public void reset() { final RLPInput in = RLP.input(h("0xc80102c51112c22122")); for (int i = 0; i < 100; i++) { assertThat(in.enterList()).isEqualTo(3); assertThat(in.readByte()).isEqualTo((byte) 0x01); assertThat(in.readByte()).isEqualTo((byte) 0x02); assertThat(in.enterList()).isEqualTo(3); assertThat(in.readByte()).isEqualTo((byte) 0x11); assertThat(in.readByte()).isEqualTo((byte) 0x12); assertThat(in.enterList()).isEqualTo(2); assertThat(in.readByte()).isEqualTo((byte) 0x21); assertThat(in.readByte()).isEqualTo((byte) 0x22); in.reset(); } }
@Test public void emptyByteString() { final RLPInput in = RLP.input(h("0x80")); assertThat(in.isDone()).isFalse(); assertThat(in.readBytesValue()).isEqualTo(BytesValue.EMPTY); assertThat(in.isDone()).isTrue(); }
public static BlockHeader readHeader(final long num) throws IOException { final RLPInput input = new BytesValueRLPInput( BytesValue.wrap( Resources.toByteArray( EthHashTest.class.getResource(String.format("block_%d.blocks", num)))), false); input.enterList(); return BlockHeader.readFrom(input, MainnetBlockHashFunction::createHash); }
@Test public void empty() { final RLPInput in = RLP.input(BytesValue.EMPTY); assertThat(in.isDone()).isTrue(); }
@Test public void raw() { final BytesValue initial = h("0xc80102c51112c22122"); final RLPInput in = RLP.input(initial); assertThat(in.raw()).isEqualTo(initial); }
buffer.compact().position(0); final RLPInput oneBlock = new BytesValueRLPInput(BytesValue.wrap(block), false); oneBlock.enterList(); oneBlock.skipNext(); bodies.add( oneBlock.readList(Transaction::readFrom), oneBlock.readList( rlp -> BlockHeader.readFrom(rlp, MainnetBlockHashFunction::createHash))));
@Test public void blockHeadersRoundTrip() throws IOException { final List<NewBlockHashesMessage.NewBlockHash> 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(); final BlockHeader header = BlockHeader.readFrom(oneBlock, MainnetBlockHashFunction::createHash); hashes.add(new NewBlockHashesMessage.NewBlockHash(header.getHash(), header.getNumber())); // We don't care about the bodies, just the header hashes oneBlock.skipNext(); oneBlock.skipNext(); } final MessageData initialMessage = NewBlockHashesMessage.create(hashes); final MessageData raw = new RawMessage(EthPV62.NEW_BLOCK_HASHES, initialMessage.getData()); final NewBlockHashesMessage message = NewBlockHashesMessage.readFrom(raw); final Iterator<NewBlockHashesMessage.NewBlockHash> readHeaders = message.getNewHashes(); for (int i = 0; i < 50; ++i) { Assertions.assertThat(readHeaders.next()).isEqualTo(hashes.get(i)); } } }
@Test public void leaveListEarly() { final RLPInput in = RLP.input(h("0xc80102c51112c22122")); assertThat(in.enterList()).isEqualTo(3); assertThat(in.readByte()).isEqualTo((byte) 0x01); assertThatThrownBy(() -> in.leaveList(false)) .isInstanceOf(RLPException.class) .hasMessageStartingWith("Not at the end of the current list"); }