From 59ab13680d6489a846e667cdc97cec8afab0fef4 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Tue, 15 Aug 2023 09:54:24 -0700 Subject: [PATCH] BlockFileLoader: make stream of `ByteBuffer` available * Inner class `BlockFileIterator` now iterates `ByteBuffer` * `stream()` method calls `MessageSerializer.makeBlock()` * `streamBuffers()` method makes raw `ByteBuffer` blocks available * Add integration test `streamEntireBitcoindBlockChainAsBuffers()` --- .../org/bitcoinj/utils/BlockFileLoader.java | 22 ++++++++++++------- .../util/BlockFileLoaderBitcoindTest.java | 10 +++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/utils/BlockFileLoader.java b/core/src/main/java/org/bitcoinj/utils/BlockFileLoader.java index bd1abe27f..7ccacd9d0 100644 --- a/core/src/main/java/org/bitcoinj/utils/BlockFileLoader.java +++ b/core/src/main/java/org/bitcoinj/utils/BlockFileLoader.java @@ -119,10 +119,10 @@ public class BlockFileLoader implements Iterable { /** * Iterates all the blocks in a single block file. */ - public class BlockFileIterator implements Iterator { + public class BlockFileIterator implements Iterator { private final File file; private final BufferedInputStream currentFileStream; - private org.bitcoinj.core.Block nextBlock = null; + private ByteBuffer nextBlock = null; public BlockFileIterator(File blockFile) throws FileNotFoundException { this.file = blockFile; @@ -137,10 +137,10 @@ public class BlockFileLoader implements Iterable { } @Override - public Block next() throws NoSuchElementException { + public ByteBuffer next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException(); - Block next = nextBlock; + ByteBuffer next = nextBlock; nextBlock = null; return next; } @@ -181,7 +181,7 @@ public class BlockFileLoader implements Iterable { return; } try { - nextBlock = serializer.makeBlock(ByteBuffer.wrap(dataBytes)); + nextBlock = ByteBuffer.wrap(dataBytes); } catch (ProtocolException e) { nextBlock = null; } catch (Exception e) { @@ -204,17 +204,23 @@ public class BlockFileLoader implements Iterable { } public Stream stream() { + return files.stream() + .flatMap(this::fileBlockStream) + .map(serializer::makeBlock); + } + + public Stream streamBuffers() { return files.stream() .flatMap(this::fileBlockStream); } - protected Stream fileBlockStream(File file) { + protected Stream fileBlockStream(File file) { return StreamSupport.stream(fileBlockSpliterator(file), false); } - protected Spliterator fileBlockSpliterator(File file) { + protected Spliterator fileBlockSpliterator(File file) { try { - Iterator iterator = new BlockFileIterator(file); + Iterator iterator = new BlockFileIterator(file); int characteristics = Spliterator.DISTINCT | Spliterator.ORDERED; return Spliterators.spliteratorUnknownSize(iterator, characteristics); } catch (FileNotFoundException e) { diff --git a/integration-test/src/test/java/org/bitcoinj/util/BlockFileLoaderBitcoindTest.java b/integration-test/src/test/java/org/bitcoinj/util/BlockFileLoaderBitcoindTest.java index 0c82726af..fbbc8ee13 100644 --- a/integration-test/src/test/java/org/bitcoinj/util/BlockFileLoaderBitcoindTest.java +++ b/integration-test/src/test/java/org/bitcoinj/util/BlockFileLoaderBitcoindTest.java @@ -84,4 +84,14 @@ public class BlockFileLoaderBitcoindTest { System.out.println("Final block height: " + (blockCount - 1)); assertTrue(blockCount > 1); } + + @Test + public void streamEntireBitcoindBlockchainAsBuffers() { + BlockFileLoader loader = new BlockFileLoader(BitcoinNetwork.MAINNET, BlockFileLoader.getReferenceClientBlockFileList()); + + long blockCount = loader.streamBuffers().count(); + System.out.println("Final block height: " + (blockCount - 1)); + assertTrue(blockCount > 1); + } + }