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); }
new RawBlockIterator( blocksFile.toPath(), rlp -> BlockHeader.readFrom(rlp, MainnetBlockHashFunction::createHash), initialCapacity);
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); } }
@BeforeClass public static void setupConstants() throws Exception { PROTOCOL_SCHEDULE = MainnetProtocolSchedule.create(); final URL blocksUrl = EthJsonRpcHttpBySpecTest.class .getClassLoader() .getResource("tech/pegasys/pantheon/ethereum/jsonrpc/jsonRpcTestBlockchain.blocks"); final URL genesisJsonUrl = EthJsonRpcHttpBySpecTest.class .getClassLoader() .getResource("tech/pegasys/pantheon/ethereum/jsonrpc/jsonRpcTestGenesis.json"); assertThat(blocksUrl).isNotNull(); assertThat(genesisJsonUrl).isNotNull(); BLOCKS = new ArrayList<>(); try (final RawBlockIterator iterator = new RawBlockIterator( Paths.get(blocksUrl.toURI()), rlp -> BlockHeader.readFrom(rlp, MainnetBlockHashFunction::createHash))) { while (iterator.hasNext()) { BLOCKS.add(iterator.next()); } } final String gensisjson = Resources.toString(genesisJsonUrl, Charsets.UTF_8); GENESIS_BLOCK = BLOCKS.get(0); GENESIS_CONFIG = GenesisState.fromJson(gensisjson, PROTOCOL_SCHEDULE); }
public static Block readBlock(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(); final BlockHeader header = BlockHeader.readFrom(input, MainnetBlockHashFunction::createHash); final List<Transaction> transactions = input.readList(Transaction::readFrom); final List<BlockHeader> ommers = input.readList(rlp -> BlockHeader.readFrom(rlp, MainnetBlockHashFunction::createHash)); final BlockBody body = new BlockBody(transactions, ommers); return new Block(header, body); } }
blocks, rlp -> BlockHeader.readFrom( rlp, ScheduleBasedBlockHashFunction.create(protocolSchedule)))) { BlockHeader previousHeader = null;
@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)); } } }
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 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)); } } }
oneBlock.readList(Transaction::readFrom), oneBlock.readList( rlp -> BlockHeader.readFrom(rlp, MainnetBlockHashFunction::createHash))));
/** * Verifies hashing against block 300005 of the public Ethereum chain. * * @throws Exception On Failure */ @Test public void hashimotoLight() throws Exception { final RLPInput input = new BytesValueRLPInput( BytesValue.wrap( Resources.toByteArray(EthHashTest.class.getResource("block_300005.blocks"))), false); input.enterList(); final BlockHeader header = BlockHeader.readFrom(input, MainnetBlockHashFunction::createHash); final long blockNumber = header.getNumber(); final long epoch = EthHash.epoch(blockNumber); final long datasetSize = EthHash.datasetSize(epoch); final long cacheSize = EthHash.cacheSize(epoch); Assertions.assertThat(datasetSize).isEqualTo(1157627776); Assertions.assertThat(cacheSize).isEqualTo(18087488); final int[] cache = EthHash.mkCache((int) cacheSize, blockNumber); Assertions.assertThat( Hash.wrap( Bytes32.wrap( Arrays.copyOf( EthHash.hashimotoLight( datasetSize, cache, EthHash.hashHeader(header), header.getNonce()), 32)))) .isEqualTo(header.getMixHash()); }
@Test @Ignore public void hashimotoFull() throws Exception { try (final EthHasher.Full hasher = new EthHasher.Full(folder.newFile().toPath())) { final RLPInput input = new BytesValueRLPInput( BytesValue.wrap( Resources.toByteArray(EthHashTest.class.getResource("block_300005.blocks"))), false); input.enterList(); final BlockHeader header = BlockHeader.readFrom(input, MainnetBlockHashFunction::createHash); final byte[] buffer = new byte[64]; hasher.hash(buffer, header.getNonce(), header.getNumber(), EthHash.hashHeader(header)); Assertions.assertThat( ByteArrayUtil.compare(buffer, 0, 32, header.getMixHash().extractArray(), 0, 32)) .isEqualTo(0); } } }
ScheduleBasedBlockHashFunction.create(protocolSchedule); try (final RawBlockIterator iterator = new RawBlockIterator(blocksPath, rlp -> BlockHeader.readFrom(rlp, blockHashFunction))) { while (iterator.hasNext()) { blocks.add(iterator.next());