예제 #1
0
  public void run() {

    PeerDiscovery peerDiscovery = new DnsDiscovery(NetworkParameters.prodNet());
    try {
      while (true) {
        InetSocketAddress addresses[] = peerDiscovery.getPeers();
        // InetSocketAddress addresses[]={new InetSocketAddress("localhost",8333)};
        System.out.println("peers " + addresses.length);
        NetworkConnection conn;
        Peer peer;
        System.out.println("connecting to peers");

        for (InetSocketAddress a : addresses) {
          try {
            System.out.println("Trying " + a);
            conn =
                new NetworkConnection(
                    a.getAddress(),
                    Bitten.networkParameters,
                    blockChain.getChainHead().getHeight(),
                    3000);
            System.out.println("after network connection");
            peer = new Peer(Bitten.networkParameters, conn, blockChain);
            System.out.println("starting chain download");
            peer.start();
            peer.startBlockChainDownload();
            while (true) {
              if (!peer.running()) {
                System.out.println("peer stopped!");
                break;
              }
              sleep(1000);
            }
          } catch (IOException e) {
            System.out.println(e);
            continue;
          } catch (ProtocolException e) {
            continue;
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            continue;
          }
        }
      }

    } catch (PeerDiscoveryException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
예제 #2
0
  public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();

    // Sorted map of UNIX time of block to StoredBlock object.
    final TreeMap<Integer, StoredBlock> checkpoints = new TreeMap<Integer, StoredBlock>();

    // Configure bitcoinj to fetch only headers, not save them to disk, connect to a local fully
    // synced/validated
    // node and to save block headers that are on interval boundaries, as long as they are <1 month
    // old.
    final BlockStore store = new MemoryBlockStore(PARAMS);
    final BlockChain chain = new BlockChain(PARAMS, store);
    final PeerGroup peerGroup = new PeerGroup(PARAMS, chain);
    peerGroup.addAddress(InetAddress.getLocalHost());
    // peerGroup.addAddress(InetAddress.getByName("69.164.198.161"));
    long now = new Date().getTime() / 1000;
    peerGroup.setFastCatchupTimeSecs(now);

    final long oneMonthAgo = now - (86400 * 2);

    chain.addListener(
        new AbstractBlockChainListener() {
          @Override
          public void notifyNewBestBlock(StoredBlock block) throws VerificationException {
            int height = block.getHeight();

            if (height % CoinDefinition.getIntervalCheckpoints() == 0
                && block.getHeader().getTimeSeconds() <= oneMonthAgo) {

              //               if (height % PARAMS.getInterval() == 0 &&
              // block.getHeader().getTimeSeconds() <= oneMonthAgo) {

              System.out.println(
                  String.format(
                      "Checkpointing block %s at height %d",
                      block.getHeader().getHash(), block.getHeight()));
              checkpoints.put(height, block);
            }
          }
        },
        Threading.SAME_THREAD);

    peerGroup.startAndWait();
    peerGroup.downloadBlockChain();

    checkState(checkpoints.size() > 0);

    // Write checkpoint data out.
    final FileOutputStream fileOutputStream = new FileOutputStream(CHECKPOINTS_FILE, false);
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
    digestOutputStream.on(false);
    final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
    dataOutputStream.writeBytes("CHECKPOINTS 1");
    dataOutputStream.writeInt(0); // Number of signatures to read. Do this later.
    digestOutputStream.on(true);
    dataOutputStream.writeInt(checkpoints.size());
    ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
    for (StoredBlock block : checkpoints.values()) {
      block.serializeCompact(buffer);
      dataOutputStream.write(buffer.array());
      buffer.position(0);
    }
    dataOutputStream.close();
    Sha256Hash checkpointsHash = new Sha256Hash(digest.digest());
    System.out.println("Hash of checkpoints data is " + checkpointsHash);
    digestOutputStream.close();
    fileOutputStream.close();

    peerGroup.stopAndWait();
    store.close();

    // Sanity check the created file.
    CheckpointManager manager =
        new CheckpointManager(PARAMS, new FileInputStream(CHECKPOINTS_FILE));
    checkState(manager.numCheckpoints() == checkpoints.size());

    /*if (PARAMS.getId() == NetworkParameters.ID_MAINNET) {
        StoredBlock test = manager.getCheckpointBefore(1390500000); // Thu Jan 23 19:00:00 CET 2014
        checkState(test.getHeight() == 280224);
        checkState(test.getHeader().getHashAsString()
                .equals("00000000000000000b5d59a15f831e1c45cb688a4db6b0a60054d49a9997fa34"));
    } else if (PARAMS.getId() == NetworkParameters.ID_TESTNET) {
        StoredBlock test = manager.getCheckpointBefore(1390500000); // Thu Jan 23 19:00:00 CET 2014
        checkState(test.getHeight() == 167328);
        checkState(test.getHeader().getHashAsString()
                .equals("0000000000035ae7d5025c2538067fe7adb1cf5d5d9c31b024137d9090ed13a9"));
    }*/

    System.out.println("Checkpoints written to '" + CHECKPOINTS_FILE.getCanonicalPath() + "'.");
  }