Newcomer's question : wallet dump shows always 0 BTC

241 views
Skip to first unread message

Alex Grinman

unread,
Mar 5, 2014, 3:07:58 PM3/5/14
to bitc...@googlegroups.com
Hello,

I use bitcoinj 0.11

Need to understand what I do wrong on conceptual level.
I have downloaded block store into SPVBlockStore. Each time I run my program block store is being updated with new blocks. So far so good.
I have created a wallet with several addresses and transferred some coins to one of them. They could be observed in any blockchain explorer, for example, here .
Now I want my app to show the balance of wallet by running wallet.toString()
I always see the following:

Wallet containing 0 BTC (0 satoshis) in:
  0 unspent transactions
  0 spent transactions
  0 pending transactions
  0 dead transactions
Last seen best block: -1 (time unknown): null

Keys:
  addr:mhZT2WDx9EMy4L8d9TyQvGxzZMECTes1gj pub:02a79154f0a9940d00a79bede3cbd7d231ee24c6e2705f7e2f308f6855151f1aa1 timestamp:1393880996


So the balance is 0. What exactly am I missing here? What should be done to make bitcoinj re-calculate wallet balance by passing over all block chains?
What do I have to do to get real wallet balance?

Thanks in advance. If this question was already alwered I would be happy to get a link.

Adam Mackler

unread,
Mar 5, 2014, 3:32:57 PM3/5/14
to bitc...@googlegroups.com
On Wednesday, March 5, 2014 3:07:58 PM UTC-5, Alex Grinman wrote:
So the balance is 0. What exactly am I missing here? What should be done to make bitcoinj re-calculate wallet balance by passing over all block chains?
What do I have to do to get real wallet balance?

You need to attach your Wallet to a BlockChain and PeerGroup before you start the block chain download.  That way BlockChain and PeerGroup will know what keys are in your Wallet, and will learn of the appropriate past Transactions during the download or as future transactions to your address occur while you're connected to peers.

You add the Wallet using BlockChain.addWallet(wallet), and PeerGroup.addWallet(wallet).  You can also pass the Wallet as a constructor argument to a BlockChain. It sounds like you already know how to start the download.  I think you might have to delete your store file if you've downloaded past the block that has the transaction you're interested in, but I'm not sure about that part.

I have found the source code for the WalletAppKit method startUp() to be very helpful in seeing how to connect these things.

--
Adam Mackler

 

Alex Grinman

unread,
Mar 6, 2014, 6:45:52 PM3/6/14
to bitc...@googlegroups.com


You need to attach your Wallet to a BlockChain and PeerGroup before you start the block chain download.  That way BlockChain and PeerGroup will know what keys are in your Wallet, and will learn of the appropriate past Transactions during the download or as future transactions to your address occur while you're connected to peers.

You add the Wallet using BlockChain.addWallet(wallet), and PeerGroup.addWallet(wallet).  You can also pass the Wallet as a constructor argument to a BlockChain. It sounds like you already know how to start the download.  I think you might have to delete your store file if you've downloaded past the block that has the transaction you're interested in, but I'm not sure about that part.

I have found the source code for the WalletAppKit method startUp() to be very helpful in seeing how to connect these things.

This is what I get now:

Wallet containing 21 BTC (2100000000 satoshis) in:
  2 unspent transactions

  0 spent transactions
  0 pending transactions
  0 dead transactions
Last seen best block: 203677 (Fri Mar 07 01:54:25 IST 2014): 00000000f48ac6895d10b658e11903d35d4215e284755c00a65979d9dd7c9d06

It works! I have indeed implemented what you advised and also passed over WalletAppKit.startUp() no learn the relationship those objects.
Thank you very much for your help, Adam.
 

Nitin Patil

unread,
Dec 11, 2015, 4:12:20 AM12/11/15
to bitcoinj
package org.sample.bitcoinj.explored.bitcoinj_explored;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.google.bitcoin.core.AbstractWalletEventListener;
import com.google.bitcoin.core.BlockChain;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.core.PeerAddress;
import com.google.bitcoin.core.PeerGroup;
import com.google.bitcoin.core.ScriptException;
import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.TransactionInput;
import com.google.bitcoin.core.Utils;
import com.google.bitcoin.core.Wallet;
import com.google.bitcoin.core.Wallet.SendResult;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.BlockStoreException;
import com.google.bitcoin.store.SPVBlockStore;
import com.google.bitcoin.store.UnreadableWalletException;

public class MyWallet {

public static void main(String[] args) {
boolean testNet = true; //args.length > 0 && args[0].equalsIgnoreCase("testnet");
        final NetworkParameters params = testNet ? NetworkParameters.testNet() : NetworkParameters.prodNet();
        String filePrefix = testNet ? "MyWallet-testnet" : "MyWallet-prodnet";

        // Try to read the wallet from storage, create a new one if not possible.
        Wallet wallet = null;
        final File walletFile = new File(filePrefix + ".wallet");
        
            try {
wallet = Wallet.loadFromFile(walletFile);
} catch (UnreadableWalletException e) {
wallet = new Wallet(params);
           wallet.addKey(new ECKey());
           try {
wallet.saveToFile(walletFile);
} catch (IOException e1) {
e1.printStackTrace();
}
}
        
        
        ECKey key = wallet.getKeys().get(0);

        System.out.println(wallet);

        // Load the block chain, if there is one stored locally.
        System.out.println("Reading block store from disk");
        File chainFile = new File("testnet.spvchain");
        BlockStore blockStore = null;
try {
blockStore = new SPVBlockStore(params, chainFile);
} catch (BlockStoreException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

        // Connect to the localhost node. One minute timeout since we won't try any other peers
        System.out.println("Connecting ...");
        BlockChain chain = null;
try {
chain = new BlockChain(params, wallet, blockStore);
} catch (BlockStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
chain.addWallet(wallet);
        final PeerGroup peerGroup = new PeerGroup(params, chain);
        peerGroup.addWallet(wallet);
        try {
peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost()));
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
        
        peerGroup.start();
        wallet.allowSpendingUnconfirmedTransactions();
        
        // We want to know when the balance changes.
        wallet.addEventListener(new AbstractWalletEventListener() {
            @Override
            public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
                // Running on a peer thread.
                //assert !newBalance.equals(BigInteger.ZERO);
                // It's impossible to pick one specific identity that you receive coins from in BitCoin as there
                // could be inputs from many addresses. So instead we just pick the first and assume they were all
                // owned by the same person.
                try {
                    TransactionInput input = tx.getInputs().get(0);
                    com.google.bitcoin.core.Address from = input.getFromAddress();
                    BigInteger value = tx.getValueSentToMe(w);
                    System.out.println("Received " + Utils.bitcoinValueToFriendlyString(value) + " from " + from.toString());
                    // Now send the coins back!
                    SendResult sendTx = null;
try {
sendTx = w.sendCoins(peerGroup, from, value);
} catch (InsufficientMoneyException e) {
e.printStackTrace();
}
                    System.out.println("Sent coins back! Transaction hash is " + sendTx.toString());
                    w.saveToFile(walletFile);
                } catch (ScriptException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        });
        try {
wallet.saveToFile(walletFile);
} catch (IOException e) {
e.printStackTrace();
}
        peerGroup.downloadBlockChain();
        System.out.println("Send coins to: " + key.toAddress(params).toString());
        System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");
}

}

Unable to send fake coins from http://tpfaucet.appspot.com to MyWallet-testnet.wallet file which is stored in my local hard disk. Always getting "Wallet containing 0 BTC (available: 0 BTC)".
Using below dependency in my pom.xml
<dependency>
      <groupId>com.google</groupId>
 <artifactId>bitcoinj</artifactId>
 <version>0.11.3</version>
      <scope>compile</scope>
    </dependency> 

Will you please suggest how to add fake btc coins in my local bitcoinj wallet. 
It is urgent. Will you please help to resolve balance issue.
Thanks in Advance.
Reply all
Reply to author
Forward
0 new messages