@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 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 blockHeadersRoundTrip() throws IOException { final List<BlockHeader> headers = 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(); headers.add(BlockHeader.readFrom(oneBlock, MainnetBlockHashFunction::createHash)); // We don't care about the bodies, just the headers oneBlock.skipNext(); oneBlock.skipNext(); } final MessageData initialMessage = BlockHeadersMessage.create(headers); final MessageData raw = new RawMessage(EthPV62.BLOCK_HEADERS, initialMessage.getData()); final BlockHeadersMessage message = BlockHeadersMessage.readFrom(raw); final Iterator<BlockHeader> readHeaders = message.getHeaders( FixedDifficultyProtocolSchedule.create( GenesisConfigFile.development().getConfigOptions(), PrivacyParameters.noPrivacy())); for (int i = 0; i < 50; ++i) { Assertions.assertThat(readHeaders.next()).isEqualTo(headers.get(i)); } } }
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); }
oneBlock.enterList(); oneBlock.skipNext(); bodies.add(