mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-01-30 23:02:15 +00:00
Simplify FetchBlock by updating it to the latest APIs.
This commit is contained in:
parent
22f8034de8
commit
707e3a0989
@ -19,40 +19,28 @@ package com.google.bitcoin.examples;
|
|||||||
import com.google.bitcoin.core.*;
|
import com.google.bitcoin.core.*;
|
||||||
import com.google.bitcoin.store.BlockStore;
|
import com.google.bitcoin.store.BlockStore;
|
||||||
import com.google.bitcoin.store.MemoryBlockStore;
|
import com.google.bitcoin.store.MemoryBlockStore;
|
||||||
|
import com.google.bitcoin.utils.BriefLogFormatter;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
import org.jboss.netty.channel.ChannelFuture;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the block given a block hash from the localhost node and prints it out.
|
* Downloads the block given a block hash from the localhost node and prints it out.
|
||||||
*/
|
*/
|
||||||
public class FetchBlock {
|
public class FetchBlock {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
BriefLogFormatter.init();
|
||||||
System.out.println("Connecting to node");
|
System.out.println("Connecting to node");
|
||||||
final NetworkParameters params = NetworkParameters.testNet();
|
final NetworkParameters params = NetworkParameters.testNet();
|
||||||
|
|
||||||
BlockStore blockStore = new MemoryBlockStore(params);
|
BlockStore blockStore = new MemoryBlockStore(params);
|
||||||
BlockChain chain = new BlockChain(params, blockStore);
|
BlockChain chain = new BlockChain(params, blockStore);
|
||||||
PeerGroup peerGroup = new PeerGroup(params, chain);
|
PeerGroup peerGroup = new PeerGroup(params, chain);
|
||||||
peerGroup.start();
|
peerGroup.startAndWait();
|
||||||
|
PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.port, 31000);
|
||||||
final CountDownLatch latch = new CountDownLatch(1);
|
peerGroup.addAddress(addr);
|
||||||
peerGroup.addEventListener(new AbstractPeerEventListener() {
|
peerGroup.waitForPeers(1).get();
|
||||||
@Override
|
Peer peer = peerGroup.getConnectedPeers().get(0);
|
||||||
public void onPeerConnected(Peer peer, int peerCount) {
|
|
||||||
latch.countDown();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
ChannelFuture channelFuture =
|
|
||||||
peerGroup.connectTo(new InetSocketAddress(InetAddress.getLocalHost(), params.port));
|
|
||||||
latch.await();
|
|
||||||
|
|
||||||
Peer peer = PeerGroup.peerFromChannelFuture(channelFuture);
|
|
||||||
|
|
||||||
Sha256Hash blockHash = new Sha256Hash(args[0]);
|
Sha256Hash blockHash = new Sha256Hash(args[0]);
|
||||||
Future<Block> future = peer.getBlock(blockHash);
|
Future<Block> future = peer.getBlock(blockHash);
|
||||||
|
Loading…
Reference in New Issue
Block a user