Java Code Examples for org.bitcoinj.core.StoredBlock#build()

The following examples show how to use org.bitcoinj.core.StoredBlock#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SPVBlockStoreTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void basics() throws Exception {
    SPVBlockStore store = new SPVBlockStore(UNITTEST, blockStoreFile);

    Address to = LegacyAddress.fromKey(UNITTEST, new ECKey());
    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(UNITTEST.getGenesisBlock(), genesis.getHeader());
    assertEquals(0, genesis.getHeight());

    // Build a new block.
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    store.close();

    // Check we can get it back out again if we rebuild the store object.
    store = new SPVBlockStore(UNITTEST, blockStoreFile);
    StoredBlock b2 = store.get(b1.getHeader().getHash());
    assertEquals(b1, b2);
    // Check the chain head was stored correctly also.
    StoredBlock chainHead = store.getChainHead();
    assertEquals(b1, chainHead);
}
 
Example 2
Source File: SPVBlockStoreTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void twoStores_sequentially_grow() throws Exception {
    Address to = LegacyAddress.fromKey(UNITTEST, new ECKey());
    SPVBlockStore store = new SPVBlockStore(UNITTEST, blockStoreFile, 10, true);
    final StoredBlock block0 = store.getChainHead();
    final StoredBlock block1 = block0.build(block0.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(block1);
    final StoredBlock block2 = block1.build(block1.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(block2);
    store.setChainHead(block2);
    store.close();

    store = new SPVBlockStore(UNITTEST, blockStoreFile, 20, true);
    final StoredBlock read2 = store.getChainHead();
    assertEquals(block2, read2);
    final StoredBlock read1 = read2.getPrev(store);
    assertEquals(block1, read1);
    final StoredBlock read0 = read1.getPrev(store);
    assertEquals(block0, read0);
    store.close();
    assertEquals(SPVBlockStore.getFileSize(20), blockStoreFile.length());
}
 
Example 3
Source File: LevelDBBlockStoreTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void basics() throws Exception {
    File f = File.createTempFile("leveldbblockstore", null);
    f.delete();

    Context context = new Context(UNITTEST);
    LevelDBBlockStore store = new LevelDBBlockStore(context, f);
    store.reset();

    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(UNITTEST.getGenesisBlock(), genesis.getHeader());
    assertEquals(0, genesis.getHeight());

    // Build a new block.
    Address to = LegacyAddress.fromBase58(UNITTEST, "mrj2K6txjo2QBcSmuAzHj4nD1oXSEJE1Qo");
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    store.close();

    // Check we can get it back out again if we rebuild the store object.
    store = new LevelDBBlockStore(context, f);
    try {
        StoredBlock b2 = store.get(b1.getHeader().getHash());
        assertEquals(b1, b2);
        // Check the chain head was stored correctly also.
        StoredBlock chainHead = store.getChainHead();
        assertEquals(b1, chainHead);
    } finally {
        store.close();
        store.destroy();
    }
}
 
Example 4
Source File: SPVBlockStoreTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void basics() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    File f = File.createTempFile("spvblockstore", null);
    f.delete();
    f.deleteOnExit();
    SPVBlockStore store = new SPVBlockStore(params, f);

    Address to = new ECKey().toAddress(params);
    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(params.getGenesisBlock(), genesis.getHeader());
    assertEquals(0, genesis.getHeight());


    // Build a new block.
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    store.close();

    // Check we can get it back out again if we rebuild the store object.
    store = new SPVBlockStore(params, f);
    StoredBlock b2 = store.get(b1.getHeader().getHash());
    assertEquals(b1, b2);
    // Check the chain head was stored correctly also.
    StoredBlock chainHead = store.getChainHead();
    assertEquals(b1, chainHead);
}
 
Example 5
Source File: SPVBlockStoreTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void basics() throws Exception {
    NetworkParameters params = UnitTestParams.get();
    File f = File.createTempFile("spvblockstore", null);
    f.delete();
    f.deleteOnExit();
    SPVBlockStore store = new SPVBlockStore(params, f);

    Address to = new ECKey().toAddress(params);
    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(params.getGenesisBlock(), genesis.getHeader());
    assertEquals(0, genesis.getHeight());


    // Build a new block.
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    store.close();

    // Check we can get it back out again if we rebuild the store object.
    store = new SPVBlockStore(params, f);
    StoredBlock b2 = store.get(b1.getHeader().getHash());
    assertEquals(b1, b2);
    // Check the chain head was stored correctly also.
    StoredBlock chainHead = store.getChainHead();
    assertEquals(b1, chainHead);
}
 
Example 6
Source File: MapBlockStore.java    From jelectrum with MIT License 5 votes vote down vote up
public void put(Block b)
    throws org.bitcoinj.store.BlockStoreException
{
  Sha256Hash hash = b.getHash();
  Block header = b.cloneAsHeader();

  Sha256Hash prev = b.getPrevBlockHash();
  StoredBlock prev_sb = file_db.getBlockStoreMap().get(prev);

  StoredBlock sb = prev_sb.build(header);

  file_db.getBlockStoreMap().put(hash, sb);

  setChainHead(sb);
}
 
Example 7
Source File: MapBlockStore.java    From jelectrum with MIT License 5 votes vote down vote up
public void putAll(List<Block> blks)
    throws org.bitcoinj.store.BlockStoreException
{
  HashMap<Sha256Hash, StoredBlock> insert_map = new HashMap<>();

  StoredBlock last = null;

  for(Block b : blks)
  {
    Sha256Hash hash = b.getHash();
    Sha256Hash prev = b.getPrevBlockHash();

    if (!hash.equals(jelly.getNetworkParameters().getGenesisBlock().getHash()))
    {
      StoredBlock prev_sb = insert_map.get(prev);
      if (prev_sb == null)
      {
        prev_sb = file_db.getBlockStoreMap().get(prev);
      }
      Assert.assertNotNull(prev_sb);

      Block header = b.cloneAsHeader();
      StoredBlock sb = prev_sb.build(header);

      last = sb;

      insert_map.put(hash, sb);
    }
  }


  file_db.getBlockStoreMap().putAll(insert_map);

  if (last != null) setChainHead(last);

}