Java Code Examples for org.bitcoinj.core.Block#getWork()

The following examples show how to use org.bitcoinj.core.Block#getWork() . 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: WalletProtobufSerializerTest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testAppearedAtChainHeightDepthAndWorkDone() throws Exception {
    // Test the TransactionConfidence appearedAtChainHeight, depth and workDone field are stored.

    BlockChain chain = new BlockChain(UNITTEST, myWallet, new MemoryBlockStore(UNITTEST));

    final ArrayList<Transaction> txns = new ArrayList<>(2);
    myWallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
        @Override
        public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
            txns.add(tx);
        }
    });

    // Start by building two blocks on top of the genesis block.
    Block b1 = UNITTEST.getGenesisBlock().createNextBlock(myAddress);
    BigInteger work1 = b1.getWork();
    assertTrue(work1.signum() > 0);

    Block b2 = b1.createNextBlock(myAddress);
    BigInteger work2 = b2.getWork();
    assertTrue(work2.signum() > 0);

    assertTrue(chain.add(b1));
    assertTrue(chain.add(b2));

    // We now have the following chain:
    //     genesis -> b1 -> b2

    // Check the transaction confidence levels are correct before wallet roundtrip.
    Threading.waitForUserCode();
    assertEquals(2, txns.size());

    TransactionConfidence confidence0 = txns.get(0).getConfidence();
    TransactionConfidence confidence1 = txns.get(1).getConfidence();

    assertEquals(1, confidence0.getAppearedAtChainHeight());
    assertEquals(2, confidence1.getAppearedAtChainHeight());

    assertEquals(2, confidence0.getDepthInBlocks());
    assertEquals(1, confidence1.getDepthInBlocks());

    // Roundtrip the wallet and check it has stored the depth and workDone.
    Wallet rebornWallet = roundTrip(myWallet);

    Set<Transaction> rebornTxns = rebornWallet.getTransactions(false);
    assertEquals(2, rebornTxns.size());

    // The transactions are not guaranteed to be in the same order so sort them to be in chain height order if required.
    Iterator<Transaction> it = rebornTxns.iterator();
    Transaction txA = it.next();
    Transaction txB = it.next();

    Transaction rebornTx0, rebornTx1;
    if (txA.getConfidence().getAppearedAtChainHeight() == 1) {
        rebornTx0 = txA;
        rebornTx1 = txB;
    } else {
        rebornTx0 = txB;
        rebornTx1 = txA;
    }

    TransactionConfidence rebornConfidence0 = rebornTx0.getConfidence();
    TransactionConfidence rebornConfidence1 = rebornTx1.getConfidence();

    assertEquals(1, rebornConfidence0.getAppearedAtChainHeight());
    assertEquals(2, rebornConfidence1.getAppearedAtChainHeight());

    assertEquals(2, rebornConfidence0.getDepthInBlocks());
    assertEquals(1, rebornConfidence1.getDepthInBlocks());
}
 
Example 2
Source File: MapBlockStore.java    From jelectrum with MIT License 3 votes vote down vote up
public MapBlockStore(Jelectrum jelly)
    throws org.bitcoinj.store.BlockStoreException
{
    this.jelly = jelly;

    this.file_db = jelly.getDB();
    NetworkParameters params = jelly.getNetworkParameters();
    


    genisis_hash = params.getGenesisBlock().getHash();

    Assert.assertNotNull(file_db);
    Assert.assertNotNull(file_db.getSpecialBlockStoreMap());


    if (!file_db.getSpecialBlockStoreMap().containsKey("head"))
    {
    
        System.out.println("Adding genesis");
        Block genesisHeader = params.getGenesisBlock().cloneAsHeader();
        StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0);
        put(storedGenesis);
        setChainHead(storedGenesis);
    }

}