Example #1
0
public class MemoryPoolTest {
  private NetworkParameters params = NetworkParameters.unitTests();
  private Transaction tx1, tx2;
  private PeerAddress address1, address2, address3;

  @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}));
  }

  @Test
  public void canonicalInstance() throws Exception {
    MemoryPool pool = new MemoryPool();
    // Check that if we repeatedly send it the same transaction but with different objects, we get
    // back the same
    // canonical instance with the confidences update.
    assertEquals(0, pool.numBroadcastPeers(tx1.getHash()));
    assertEquals(tx1, pool.seen(tx1, address1));
    assertEquals(1, tx1.getConfidence().numBroadcastPeers());
    assertEquals(1, pool.numBroadcastPeers(tx1.getHash()));
    assertEquals(tx1, pool.seen(tx2, address2));
    assertEquals(2, tx1.getConfidence().numBroadcastPeers());
    assertEquals(2, pool.numBroadcastPeers(tx1.getHash()));
    assertEquals(tx1, pool.get(tx1.getHash()));
  }

  @Test
  public void invAndDownload() throws Exception {
    MemoryPool pool = new MemoryPool();
    // Base case: we see a transaction announced twice and then download it. The count is in the
    // confidence object.
    assertEquals(0, pool.numBroadcastPeers(tx1.getHash()));
    pool.seen(tx1.getHash(), address1);
    assertEquals(1, pool.numBroadcastPeers(tx1.getHash()));
    assertTrue(pool.maybeWasSeen(tx1.getHash()));
    pool.seen(tx1.getHash(), address2);
    assertEquals(2, pool.numBroadcastPeers(tx1.getHash()));
    Transaction t = pool.seen(tx1, address1);
    assertEquals(2, t.getConfidence().numBroadcastPeers());
    // And now we see another inv.
    pool.seen(tx1.getHash(), address3);
    assertEquals(3, t.getConfidence().numBroadcastPeers());
    assertEquals(3, pool.numBroadcastPeers(tx1.getHash()));
  }
}
  @Test
  public void testStorage() throws Exception {
    File temp = File.createTempFile("bitcoinj-test", null, null);
    System.out.println(temp.getAbsolutePath());
    temp.deleteOnExit();

    NetworkParameters params = NetworkParameters.unitTests();
    Address to = new ECKey().toAddress(params);
    BoundedOverheadBlockStore store = new BoundedOverheadBlockStore(params, temp);
    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(params.genesisBlock, genesis.getHeader());

    // Build a new block.
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    // Check we can get it back out again if we rebuild the store object.
    store = new BoundedOverheadBlockStore(params, temp);
    StoredBlock b2 = store.get(b1.getHeader().getHash());
    assertEquals(b1, b2);
    // Check the chain head was stored correctly also.
    assertEquals(b1, store.getChainHead());
  }