<p>OK, now I get it.</p>

<p>I can't.</p>

<p>Bitcoinj saves only the headers. And - what I did <strong>not</strong> know - you <strong><em>can't</em></strong> get the balance with just headers. </p>

<p>For more ingormation you can see the exact description what is in the headers here <a href="https://en.bitcoin.it/wiki/Block_hashing_algorithm" rel="nofollow">https://en.bitcoin.it/wiki/Block_hashing_algorithm</a></p>

<p>What I <strong>can</strong> do, however, is saving the wallet right afterwards. Which I will do.</p>

<hr>

<p>previous "answer"/thinking out loud:</p>

<p>I am poking around in source code.</p>

<p>It seems like the important part is when <code>sendTransactionsToWallet</code> is called in <code>BlockChain.java</code> - but it gets only called during <code>connectBlock</code> and nothing else.</p>

<p><code>connectBlock</code> is called only during <code>add</code>. And the only <code>add</code>ed blocks are the new blocks from the network, not the ones from BlockStore.</p>

<p>But I will look around the code a bit more.</p>

<hr>

<p>I am not sure, what terrible would happen, if I rewrote the constructor of <code>BlockChain.java</code> from:</p>

<pre><code>/**
 * Constructs a BlockChain connected to the given list of wallets and a store.
 */
public BlockChain(NetworkParameters params, List&lt;Wallet&gt; wallets,
                  BlockStore blockStore) throws BlockStoreException {
    this.blockStore = blockStore;
    chainHead = blockStore.getChainHead();
    log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader());
    this.params = params;
    this.wallets = new ArrayList&lt;Wallet&gt;(wallets);
}
</code></pre>

<p>to (writing from my head, getters/setters might not be right, etc)</p>

<pre><code>/**
 * Constructs a BlockChain connected to the given list of wallets and a store.
 */
public BlockChain(NetworkParameters params, List&lt;Wallet&gt; wallets,
                  BlockStore blockStore) throws BlockStoreException {
    this.blockStore = blockStore;
    chainHead = blockStore.getChainHead();
    log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader());
    this.params = params;
    this.wallets = new ArrayList&lt;Wallet&gt;(wallets);
    StoredBlock sBlock = blockStore.getChainHead();
    Sha256Hash prevHash = sBlock.header.prevBlockHash();
    while (prevHash!=Sha256Hash.ZERO_HASH) {
        for (Wallet wallet:wallets){
            sendTransactionsToWallet(sBlock, NewBlockType.SIDE_CHAIN, wallet, sBlock.transactions); 
        }
        sBlock = blockStore.get(hash);
        prevHash = sBlock.header.prevBlockHash();
    }
}
</code></pre>

<p>but again, I don't want to reinvent the wheel or anything.</p>

CC BY-SA 3.0
2012-08-19T13:05:39.490Z
2012-08-19T21:54:02.700Z
2012-08-19T21:54:02.700Z
Karel Bílek
<p>It is possible to do what you want by simply using the <a href="https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/com/google/bitcoin/store/PostgresFullPrunedBlockStore.java" rel="nofollow">PostgresFullPrunedBlockStore</a> class for storage and then you will just need to call <code>Address.calculateBalanceForAddress()</code> in order to get the balance of the desired address.</p>

CC BY-SA 3.0
2014-06-26T14:01:24.573Z
Unfortunately, it's kind of old question and I am no longer experimenting with bitcoinj so I cannot accept you, but I will let community decide by voting I guess
Karel Bílek
CC BY-SA 3.0
2014-06-26T14:26:14.837Z
No worries, I just felt to answer because there have been updates with bitcoinj recently and I thought others might want to know this.
Biteasy.com
CC BY-SA 3.0
2016-03-14T19:12:34.937Z
How big is postgress db going to be ? 100GB+ and growing fast right ?
Pavel Niedoba
CC BY-SA 3.0
2017-07-01T17:26:49.817Z
Heh, the answer is still hanging in there even in 2017... I have no idea how bitcoinj looks now.
Karel Bílek
CC BY-SA 3.0
2014-06-26T07:30:07.443Z
2014-06-26T07:30:07.443Z
Biteasy.com
<p>I thought I would mention that (mainly to save bandwidth) I have added a simple caching option into MultiBit where it simply saves the blocks received into a directory, each block is called [hash of block].block</p>

<p>You can use this to grab the transactions etc. as you want later.  Of course it takes up space!
It is in the MultiBit v0.5 branch. The main controlling class is CacheManager. 
It might be useful to you. </p>

<p>The bloom filter work Matt Corrallo is doing to filter the tx at the server side is IMHO the right way to manage the size of the block chain - this is just a stop gap until that gets into bitcoinj. </p>

CC BY-SA 3.0
2012-08-20T08:47:22.170Z
2012-08-20T08:47:22.170Z
jim618
<p>I am playing around with bitoinj and I can't seem to even get the most basic functionality running.</p>

<p>What I want to do: I have one wallet with one address. I want to dowload blockchain and from that, get the wallet balance.</p>

<p>What I did is following code (in scala, but it is more or less the same thing as Java), the wallet is in <code>address.walet</code>, I want the blockain to be saved at <code>saved.blockchain</code>:</p>

<pre><code>val params = NetworkParameters.prodNet
val walletFile = new java.io.File("address.wallet");
val wallet = Wallet.loadFromFile(walletFile)

val key = wallet.keychain.get(0)
val address:String = key.toAddress(params).toString
println("ADDRESS IS : "+address)

val blockStore = new BoundedOverheadBlockStore(params, new java.io.File("saved.blockchain"))
val chain = new BlockChain(params, wallet, blockStore)
val peerGroup = new PeerGroup(params, chain)
peerGroup.setUserAgent("BitToy", "0.1")

peerGroup.addPeerDiscovery(new DnsDiscovery(params))
peerGroup.addWallet(wallet)
println("START DOWNLOADING BLOCKCHAIN")

peerGroup.start()
peerGroup.downloadBlockChain()
peerGroup.stop()
println("DONE; BALANCE IS :")
println(wallet.getBalance)
</code></pre>

<p>I do have balance on the address in the wallet.</p>

<p>Now, when I run it for the first time, the blockchain (or, rather, the headers) gets downloaded and I see the correct balance. <em>However</em>, when I run it for second time, I just see:</p>

<pre><code>ADDRESS IS : [correct address here]
Aug 19, 2012 8:06:00 AM com.google.bitcoin.store.BoundedOverheadBlockStore load
INFO: Reading block store from saved.blockchain
Aug 19, 2012 8:06:00 AM com.google.bitcoin.store.BoundedOverheadBlockStore load
INFO: Read chain head from disk: 00000000000001b10c347b5ec05f9a1910d541905c5cd8827784a5c3e0695c95
Aug 19, 2012 8:06:00 AM com.google.bitcoin.core.BlockChain &lt;init&gt;
INFO: chain head is at height 194575:
v1 block: 
   previous block: 00000000000005ac50dc06021362cd4c284fafd4a1bdb7bad2849c978a875bb1
   merkle root: 158aedd0bc3386c7686f85ab933fd9e3ecb38e348b121ed0e61e2e776eda04a1
   time: [1345356028] Sun Aug 19 08:00:28 CEST 2012
   difficulty target (nBits): 436709470
   nonce: 4238997601

START DOWNLOADING BLOCKCHAIN
Aug 19, 2012 8:06:31 AM com.google.bitcoin.discovery.DnsDiscovery getPeers
INFO: DNS lookup for dnsseed.bitcoin.dashjr.org failed.
Aug 19, 2012 8:06:36 AM com.google.bitcoin.core.TCPNetworkConnection$NetworkHandler channelConnected
INFO: Announcing ourselves as: /BitCoinJ:0.6-SNAPSHOT/BitMessage:0.1/
Aug 19, 2012 8:06:41 AM com.google.bitcoin.core.Peer$PeerHandler exceptionCaught
INFO: com.google.bitcoin.core.Peer$PeerHandler@193f6e2 - No route to host
Aug 19, 2012 8:06:46 AM com.google.bitcoin.core.Peer$PeerHandler exceptionCaught
INFO: com.google.bitcoin.core.Peer$PeerHandler@61736e - No route to host
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.TCPNetworkConnection$NetworkHandler channelConnected
INFO: Announcing ourselves as: /BitCoinJ:0.6-SNAPSHOT/BitMessage:0.1/
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup handleNewPeer
INFO: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333): New peer
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup handleNewPeer
INFO:   starting block chain download
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup setDownloadPeer
INFO: Setting download peer: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333)
Downloading block chain of size 0. 
Done downloading block chain
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.Peer blockChainDownload
INFO: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333): blockChainDownload(0000000000000000000000000000000000000000000000000000000000000000) current head = 00000000000001b10c347b5ec05f9a1910d541905c5cd8827784a5c3e0695c95
DONE; BALANCE IS :
0
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup$PeerGroupThread run
INFO: shutdown start
</code></pre>

<p>and the balance is zero. If I delete the saved blockchain and let it re-download again, it works again, but only for one time.</p>

<p>What am I doing wrong?</p>

<bitcoinj>
Getting balance/downloading blockhain with bitcoinj
Karel Bílek
# Getting balance/downloading blockhain with bitcoinj

(Asked by: username_0 on 2012-08-19)

I am playing around with bitoinj and I can't seem to even get the most basic functionality running.

What I want to do: I have one wallet with one address. I want to dowload blockchain and from that, get the wallet balance.

What I did is following code (in scala, but it is more or less the same thing as Java), the wallet is in `address.walet`, I want the blockain to be saved at `saved.blockchain`:

```
val params = NetworkParameters.prodNet
val walletFile = new java.io.File("address.wallet");
val wallet = Wallet.loadFromFile(walletFile)

val key = wallet.keychain.get(0)
val address:String = key.toAddress(params).toString
println("ADDRESS IS : "+address)

val blockStore = new BoundedOverheadBlockStore(params, new java.io.File("saved.blockchain"))
val chain = new BlockChain(params, wallet, blockStore)
val peerGroup = new PeerGroup(params, chain)
peerGroup.setUserAgent("BitToy", "0.1")

peerGroup.addPeerDiscovery(new DnsDiscovery(params))
peerGroup.addWallet(wallet)
println("START DOWNLOADING BLOCKCHAIN")

peerGroup.start()
peerGroup.downloadBlockChain()
peerGroup.stop()
println("DONE; BALANCE IS :")
println(wallet.getBalance)

```

I do have balance on the address in the wallet.

Now, when I run it for the first time, the blockchain (or, rather, the headers) gets downloaded and I see the correct balance. *However*, when I run it for second time, I just see:

```
ADDRESS IS : [correct address here]
Aug 19, 2012 8:06:00 AM com.google.bitcoin.store.BoundedOverheadBlockStore load
INFO: Reading block store from saved.blockchain
Aug 19, 2012 8:06:00 AM com.google.bitcoin.store.BoundedOverheadBlockStore load
INFO: Read chain head from disk: 00000000000001b10c347b5ec05f9a1910d541905c5cd8827784a5c3e0695c95
Aug 19, 2012 8:06:00 AM com.google.bitcoin.core.BlockChain <init>
INFO: chain head is at height 194575:
v1 block: 
   previous block: 00000000000005ac50dc06021362cd4c284fafd4a1bdb7bad2849c978a875bb1
   merkle root: 158aedd0bc3386c7686f85ab933fd9e3ecb38e348b121ed0e61e2e776eda04a1
   time: [1345356028] Sun Aug 19 08:00:28 CEST 2012
   difficulty target (nBits): 436709470
   nonce: 4238997601

START DOWNLOADING BLOCKCHAIN
Aug 19, 2012 8:06:31 AM com.google.bitcoin.discovery.DnsDiscovery getPeers
INFO: DNS lookup for dnsseed.bitcoin.dashjr.org failed.
Aug 19, 2012 8:06:36 AM com.google.bitcoin.core.TCPNetworkConnection$NetworkHandler channelConnected
INFO: Announcing ourselves as: /BitCoinJ:0.6-SNAPSHOT/BitMessage:0.1/
Aug 19, 2012 8:06:41 AM com.google.bitcoin.core.Peer$PeerHandler exceptionCaught
INFO: com.google.bitcoin.core.Peer$PeerHandler@193f6e2 - No route to host
Aug 19, 2012 8:06:46 AM com.google.bitcoin.core.Peer$PeerHandler exceptionCaught
INFO: com.google.bitcoin.core.Peer$PeerHandler@61736e - No route to host
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.TCPNetworkConnection$NetworkHandler channelConnected
INFO: Announcing ourselves as: /BitCoinJ:0.6-SNAPSHOT/BitMessage:0.1/
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup handleNewPeer
INFO: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333): New peer
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup handleNewPeer
INFO:   starting block chain download
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup setDownloadPeer
INFO: Setting download peer: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333)
Downloading block chain of size 0. 
Done downloading block chain
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.Peer blockChainDownload
INFO: Peer(seed.bitcoin.sipa.be/50.130.4.89:8333): blockChainDownload(0000000000000000000000000000000000000000000000000000000000000000) current head = 00000000000001b10c347b5ec05f9a1910d541905c5cd8827784a5c3e0695c95
DONE; BALANCE IS :
0
Aug 19, 2012 8:06:52 AM com.google.bitcoin.core.PeerGroup$PeerGroupThread run
INFO: shutdown start

```

and the balance is zero. If I delete the saved blockchain and let it re-download again, it works again, but only for one time.

What am I doing wrong?

**Tags:** bitcoinj


---

## 3 answers

### Answer by username_0 on 2012-08-19. Score: 2 | Accepted answer

OK, now I get it.

I can't.

Bitcoinj saves only the headers. And - what I did **not** know - you ***can't*** get the balance with just headers.

For more ingormation you can see the exact description what is in the headers here <https://en.bitcoin.it/wiki/Block_hashing_algorithm>

What I **can** do, however, is saving the wallet right afterwards. Which I will do.

---

previous "answer"/thinking out loud:

I am poking around in source code.

It seems like the important part is when `sendTransactionsToWallet` is called in `BlockChain.java` - but it gets only called during `connectBlock` and nothing else.

`connectBlock` is called only during `add`. And the only `add`ed blocks are the new blocks from the network, not the ones from BlockStore.

But I will look around the code a bit more.

---

I am not sure, what terrible would happen, if I rewrote the constructor of `BlockChain.java` from:

```
/**
 * Constructs a BlockChain connected to the given list of wallets and a store.
 */
public BlockChain(NetworkParameters params, List<Wallet> wallets,
                  BlockStore blockStore) throws BlockStoreException {
    this.blockStore = blockStore;
    chainHead = blockStore.getChainHead();
    log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader());
    this.params = params;
    this.wallets = new ArrayList<Wallet>(wallets);
}

```

to (writing from my head, getters/setters might not be right, etc)

```
/**
 * Constructs a BlockChain connected to the given list of wallets and a store.
 */
public BlockChain(NetworkParameters params, List<Wallet> wallets,
                  BlockStore blockStore) throws BlockStoreException {
    this.blockStore = blockStore;
    chainHead = blockStore.getChainHead();
    log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader());
    this.params = params;
    this.wallets = new ArrayList<Wallet>(wallets);
    StoredBlock sBlock = blockStore.getChainHead();
    Sha256Hash prevHash = sBlock.header.prevBlockHash();
    while (prevHash!=Sha256Hash.ZERO_HASH) {
        for (Wallet wallet:wallets){
            sendTransactionsToWallet(sBlock, NewBlockType.SIDE_CHAIN, wallet, sBlock.transactions); 
        }
        sBlock = blockStore.get(hash);
        prevHash = sBlock.header.prevBlockHash();
    }
}

```

but again, I don't want to reinvent the wheel or anything.


---

### Answer by username_1 on 2014-06-26. Score: 2

It is possible to do what you want by simply using the [PostgresFullPrunedBlockStore](https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/com/google/bitcoin/store/PostgresFullPrunedBlockStore.java) class for storage and then you will just need to call `Address.calculateBalanceForAddress()` in order to get the balance of the desired address.

> **Comments:**
> **username_0**: Unfortunately, it's kind of old question and I am no longer experimenting with bitcoinj so I cannot accept you, but I will let community decide by voting I guess  
> **username_1**: No worries, I just felt to answer because there have been updates with bitcoinj recently and I thought others might want to know this.  
> **username_2**: How big is postgress db going to be ? 100GB+ and growing fast right ?  
> **username_0**: Heh, the answer is still hanging in there even in 2017... I have no idea how bitcoinj looks now.  

---

### Answer by username_3 on 2012-08-20. Score: 0

I thought I would mention that (mainly to save bandwidth) I have added a simple caching option into MultiBit where it simply saves the blocks received into a directory, each block is called [hash of block].block

You can use this to grab the transactions etc. as you want later. Of course it takes up space!
It is in the MultiBit v0.5 branch. The main controlling class is CacheManager.
It might be useful to you.

The bloom filter work Matt Corrallo is doing to filter the tx at the server side is IMHO the right way to manage the size of the block chain - this is just a stop gap until that gets into bitcoinj.


---

