mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-04 09:59:34 +01:00
Add a tool that prints out an arbitrary block given its hash.
This commit is contained in:
parent
10fef0b480
commit
ff52cfd86b
1 changed files with 46 additions and 0 deletions
46
src/com/google/bitcoin/examples/FetchBlock.java
Normal file
46
src/com/google/bitcoin/examples/FetchBlock.java
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2011 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.bitcoin.examples;
|
||||||
|
|
||||||
|
import com.google.bitcoin.core.*;
|
||||||
|
import com.google.bitcoin.store.BlockStore;
|
||||||
|
import com.google.bitcoin.store.MemoryBlockStore;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads the block given a block hash from the localhost node and prints it out.
|
||||||
|
*/
|
||||||
|
public class FetchBlock {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
System.out.println("Connecting to node");
|
||||||
|
final NetworkParameters params = NetworkParameters.prodNet();
|
||||||
|
NetworkConnection conn = new NetworkConnection(InetAddress.getLocalHost(), params, 0, 60000);
|
||||||
|
BlockStore blockStore = new MemoryBlockStore(params);
|
||||||
|
BlockChain chain = new BlockChain(params, blockStore);
|
||||||
|
Peer peer = new Peer(params, conn, chain);
|
||||||
|
peer.start();
|
||||||
|
|
||||||
|
Sha256Hash blockHash = new Sha256Hash(args[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);
|
||||||
|
peer.disconnect();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue