0

I would like to download a full block using Java and bitcoinj and then parse it to an array to be able to work with it. Here is some (very basic) pseudocode:

connect to network;  
download latestBlock;  
write/convert block to readable array;  
read blockArray;  
   sout: blockArray[23][55] // this should get me eg. a txid, address, ...  
// or similar. I just want to be able to read values from the block 

Is there a way to do this? I found the following solution here (Using bitcoinj to discover peers and download the block chain):

public class DumpLastBlock {
public static void main(String args[]) throws Exception {
    WalletAppKit kit = new WalletAppKit(MainNetParams.get(), new java.io.File("."), "test");
    kit.startAndWait();
    BlockChain chain = kit.chain();
    BlockStore bs = chain.getBlockStore();
    Peer peer = kit.peerGroup().getDownloadPeer();
    Block b = peer.getBlock(bs.getChainHead().getHeader().getHash()).get();
    System.out.println(b);
    }
}

Unfortunately, kit.startAndWait() isn't available anymore.

Can someone help me to get the latest block and be able to read its values?

Thank you very much :)

1 Answer 1

0

Example in bitcoinj source code is exactly what you are looking for:

BriefLogFormatter.init();
// Parse command line arguments
OptionParser parser = new OptionParser();
OptionSet opts = null;
List<String> nonOpts = null;
try {
    parser.accepts("localhost", "Connect to the localhost node");
    parser.accepts("help", "Displays program options");
    opts = parser.parse(args);
    if (opts.has("help")) {
        System.out.println("usage: org.bitcoinj.examples.FetchBlock [--localhost] <blockHash>");
        parser.printHelpOn(System.out);
        return;
    }
    nonOpts = opts.nonOptionArguments();
    if (nonOpts.size() != 1) {
        throw new IllegalArgumentException("Incorrect number of block hash, please provide only one block hash.");
    }
} catch (OptionException | IllegalArgumentException e) {
    System.err.println(e.getMessage());
    System.err.println("usage: org.bitcoinj.examples.FetchBlock [--localhost] <blockHash>");
    parser.printHelpOn(System.err);
    return;
}

// Connect to testnet and find a peer
System.out.println("Connecting to node");
final NetworkParameters params = TestNet3Params.get();
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
if (!opts.has("localhost")) {
    peerGroup.addPeerDiscovery(new DnsDiscovery(params));
} else {
    PeerAddress addr = new PeerAddress(params, InetAddress.getLocalHost());
    peerGroup.addAddress(addr);
}
peerGroup.start();
peerGroup.waitForPeers(1).get();
Peer peer = peerGroup.getConnectedPeers().get(0);

// Retrieve a block through a peer
Sha256Hash blockHash = Sha256Hash.wrap(nonOpts.get(0));
Future<Block> future = peer.getBlock(blockHash);
System.out.println("Waiting for node to send us the requested block: " + blockHash);
Block block = future.get();
System.out.println(block);
peerGroup.stopAsync();
1
  • Please add the details of the link as it relates to the question in your answer. In the current answer, if the link ceases to work, future users will not be able to get any information from it. May 26, 2018 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.