Exemple #1
0
  @Test
  public void fastCatchup() throws Exception {
    connect();

    // Check that blocks before the fast catchup point are retrieved using getheaders, and after
    // using getblocks.
    // This test is INCOMPLETE because it does not check we handle >2000 blocks correctly.
    Block b1 = createFakeBlock(blockStore).block;
    blockChain.add(b1);
    Utils.rollMockClock(60 * 10); // 10 minutes later.
    Block b2 = makeSolvedTestBlock(b1);
    Utils.rollMockClock(60 * 10); // 10 minutes later.
    Block b3 = makeSolvedTestBlock(b2);
    Utils.rollMockClock(60 * 10);
    Block b4 = makeSolvedTestBlock(b3);

    // Request headers until the last 2 blocks.
    peer.setDownloadParameters((Utils.now().getTime() / 1000) - (600 * 2) + 1, false);
    peer.startBlockChainDownload();
    GetHeadersMessage getheaders = (GetHeadersMessage) outbound(writeTarget);
    List<Sha256Hash> expectedLocator = new ArrayList<Sha256Hash>();
    expectedLocator.add(b1.getHash());
    expectedLocator.add(unitTestParams.getGenesisBlock().getHash());
    assertEquals(getheaders.getLocator(), expectedLocator);
    assertEquals(getheaders.getStopHash(), Sha256Hash.ZERO_HASH);
    // Now send all the headers.
    HeadersMessage headers =
        new HeadersMessage(
            unitTestParams, b2.cloneAsHeader(), b3.cloneAsHeader(), b4.cloneAsHeader());
    // We expect to be asked for b3 and b4 again, but this time, with a body.
    expectedLocator.clear();
    expectedLocator.add(b2.getHash());
    expectedLocator.add(b1.getHash());
    expectedLocator.add(unitTestParams.getGenesisBlock().getHash());
    inbound(writeTarget, headers);
    GetBlocksMessage getblocks = (GetBlocksMessage) outbound(writeTarget);
    assertEquals(expectedLocator, getblocks.getLocator());
    assertEquals(Sha256Hash.ZERO_HASH, getblocks.getStopHash());
    // We're supposed to get an inv here.
    InventoryMessage inv = new InventoryMessage(unitTestParams);
    inv.addItem(new InventoryItem(InventoryItem.Type.Block, b3.getHash()));
    inbound(writeTarget, inv);
    GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
    assertEquals(b3.getHash(), getdata.getItems().get(0).hash);
    // All done.
    inbound(writeTarget, b3);
    pingAndWait(writeTarget);
    closePeer(peer);
  }