saifi009 / bitcoinj

Automatically exported from code.google.com/p/bitcoinj
0 stars 0 forks source link

How-To for missing transactions in your wallet #31

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This comes from reading the source, so please review it and think twice about 
it.

Here is the how-to:
- open your wallet / connect to the network
- Use a disk based block store
- send some coins to the wallet
- wait for the confirm log message on the bitcoinj app
- kill your java process :-)

What has happened?
- The chain head was updated in the blockstore
- The wallet "lost" the new transaction event
- The wallet will never ever see the event again.

I'd guess that this is the root of some of the lost-coins/lost-transaction 
complains. The blockstore is persisted before the wallet gets stored. And the 
head pointer is part of the block store, but it should be part of the wallet. 
(The key information should be "what revision has our wallet", not "what 
revision did we download last?").

Original issue reported on code.google.com by rtreffer@gmail.com on 26 Jun 2011 at 11:34

GoogleCodeExporter commented 9 years ago
Ok, I've looked into it a bit... It might be right that the blockstore keeps a 
pointer, but the wallet needs a second pointer.

Starting a BlockChain (the one that connects a blockstore and a wallet) should 
sync both pointers. A peer download should update the blockstore and currently 
connected wallets...

I'll try to build an experimental patch.

Original comment by rtreffer@gmail.com on 26 Jun 2011 at 11:43

GoogleCodeExporter commented 9 years ago
I think the actual issue here is that you are expected to flush the wallet to 
disk quite frequently and the code doesn't help you do that.

Generally, BitCoinJ is a very low level library and it assumes too much 
knowledge of how Bitcoin works. I'd like to see it get easier to use, eg, by 
changing how wallet persistence works so the wallet can save itself rather than 
relying on the API user.

Original comment by hearn@google.com on 27 Jun 2011 at 9:35

GoogleCodeExporter commented 9 years ago
Hmm, I don't think my lost coins problem is related to not saving the wallet.

I am saving just after the wallet is created the first time, onCoinsReceived(), 
onReorganize() and after sendCoins(). Is there any other case I need to cover?

Original comment by andreas....@gmail.com on 27 Jun 2011 at 12:25

GoogleCodeExporter commented 9 years ago
>Is there any other case I need to cover?
Yes. If your wallet receives a transaction that spends some of your BTC you 
will not get an onCoinsReceived(). In general we should have an 
OnWalletChanged() event, which would allow you to save the wallet, or we should 
have an automatic wallet save where the wallet owns an OutputStream factory so 
it can automatically save. Issue #38 has a brief discussion on when to save the 
wallet.

Original comment by jan.mol...@gmail.com on 26 Jul 2011 at 9:10

GoogleCodeExporter commented 9 years ago
> If your wallet receives a transaction that spends some of your BTC you will 
not get an onCoinsReceived().

Does this mean I always need to save the wallet when the application ends, 
because there could have been changes I did not notice?

This would be bad news, because there is no "onDestroy()" on Android 
applications.

Original comment by andreas....@gmail.com on 26 Jul 2011 at 11:49

GoogleCodeExporter commented 9 years ago
I believe that the wallet should store a pointer to the last block processed 
from the chain.  On startup, we should scan all blocks in the chain that are 
newer than that and also handle re-org if the last-processed block is no longer 
on the best chain.

Original comment by mi...@google.com on 14 Oct 2011 at 8:25

GoogleCodeExporter commented 9 years ago

Original comment by mi...@google.com on 14 Oct 2011 at 8:27

GoogleCodeExporter commented 9 years ago

Original comment by hearn@google.com on 8 Jan 2013 at 4:41

GoogleCodeExporter commented 9 years ago
Over time this has got a lot easier - the wallet autosaves itself these days, 
and the example apps show how to register a VM shutdown hook to do a save when 
Java is shutting down. You still have to do a little bit of setup yourself, but 
it's harder for transactions to go walkies due to the wallet not being saved at 
the right times.

Original comment by hearn@google.com on 31 Mar 2013 at 1:31