Esempio n. 1
0
  @Before
  public void setup() throws Exception {
    BriefLogFormatter.init();
    tx1 = TestUtils.createFakeTx(params, Utils.toNanoCoins(1, 0), new ECKey().toAddress(params));
    tx2 = new Transaction(params, tx1.bitcoinSerialize());

    address1 = new PeerAddress(InetAddress.getByAddress(new byte[] {127, 0, 0, 1}));
    address2 = new PeerAddress(InetAddress.getByAddress(new byte[] {127, 0, 0, 2}));
    address3 = new PeerAddress(InetAddress.getByAddress(new byte[] {127, 0, 0, 3}));
  }
Esempio n. 2
0
  @Before
  public void setup() {
    BriefLogFormatter.init();
    Utils.setMockClock();
    group = new KeyChainGroup(params);
    group.setLookaheadSize(LOOKAHEAD_SIZE); // Don't want slow tests.
    group.getActiveKeyChain(); // Force create a chain.

    watchingAccountKey = DeterministicKey.deserializeB58(null, XPUB);
  }
Esempio n. 3
0
  public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();
    System.out.println("=== DNS ===");
    printDNS();
    System.out.println("=== Version/chain heights ===");

    ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
    for (InetSocketAddress peer : dnsPeers) addrs.add(peer.getAddress());
    System.out.println("Scanning " + addrs.size() + " peers:");

    final NetworkParameters params = MainNetParams.get();
    final Object lock = new Object();
    final long[] bestHeight = new long[1];

    List<ListenableFuture<TCPNetworkConnection>> futures = Lists.newArrayList();
    for (final InetAddress addr : addrs) {
      final ListenableFuture<TCPNetworkConnection> future =
          TCPNetworkConnection.connectTo(
              params, new InetSocketAddress(addr, params.getPort()), 1000 /* timeout */);
      futures.add(future);
      // Once the connection has completed version handshaking ...
      Futures.addCallback(
          future,
          new FutureCallback<TCPNetworkConnection>() {
            public void onSuccess(TCPNetworkConnection conn) {
              // Check the chain height it claims to have.
              VersionMessage ver = conn.getVersionMessage();
              long nodeHeight = ver.bestHeight;
              synchronized (lock) {
                long diff = bestHeight[0] - nodeHeight;
                if (diff > 0) {
                  System.out.println("Node is behind by " + diff + " blocks: " + addr);
                } else if (diff == 0) {
                  System.out.println("Node " + addr + " has " + nodeHeight + " blocks");
                  bestHeight[0] = nodeHeight;
                } else if (diff < 0) {
                  System.out.println("Node is ahead by " + Math.abs(diff) + " blocks: " + addr);
                  bestHeight[0] = nodeHeight;
                }
              }
              conn.close();
            }

            public void onFailure(Throwable throwable) {
              System.out.println("Failed to talk to " + addr + ": " + throwable.getMessage());
            }
          });
    }
    // Wait for every tried connection to finish.
    Futures.successfulAsList(futures).get();
  }
  public void setUp(BlockStore blockStore) throws Exception {
    BriefLogFormatter.init();

    unitTestParams = UnitTestParams.get();
    Wallet.SendRequest.DEFAULT_FEE_PER_KB = BigInteger.ZERO;
    this.blockStore = blockStore;
    wallet = new Wallet(unitTestParams);
    key = new ECKey();
    address = key.toAddress(unitTestParams);
    wallet.addKey(key);
    blockChain = new BlockChain(unitTestParams, wallet, blockStore);

    startPeerServers();
    if (clientType == ClientType.NIO_CLIENT_MANAGER
        || clientType == ClientType.BLOCKING_CLIENT_MANAGER) {
      channels.startAsync();
      channels.awaitRunning();
    }

    socketAddress = new InetSocketAddress("127.0.0.1", 1111);
  }
Esempio n. 5
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() + "'.");
  }