Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: SgtSpike on July 08, 2011, 11:08:00 PM



Title: Any way to know about a blockchain reorganization?
Post by: SgtSpike on July 08, 2011, 11:08:00 PM
If the blockchain gets changed, is there any way to notify about it?  Reason being, I have a database being written to based off of every new block that arrives, so if one of the older blocks changes, I'd like to know about it.


Title: Re: Any way to know about a blockchain reorganization?
Post by: theymos on July 08, 2011, 11:11:34 PM
The way Bitcoin Block Explorer detects it is by getting some previous blocks that are already in the database and seeing if they've changed.


Title: Re: Any way to know about a blockchain reorganization?
Post by: SgtSpike on July 08, 2011, 11:51:21 PM
Thanks theymos.  If blockexplorer does it that way, I'll take it that's the only way to find out.  :)


Title: Re: Any way to know about a blockchain reorganization?
Post by: etotheipi on July 09, 2011, 02:23:10 PM
Can someone please explain why/how the blockchain would be "reorganized" ?  Are we talking about invalid blocks that were originally accepted but now have to be discarded?   



Title: Re: Any way to know about a blockchain reorganization?
Post by: vector76 on July 09, 2011, 02:33:55 PM
Previous blocks can change?

I thought reorganization happened when there was a fork in the block chain and the longest/best fork got surpassed by the other fork.  Then the transactions in the (now) shorter chain are undone and the transactions in the longer chain are applied.


Title: Re: Any way to know about a blockchain reorganization?
Post by: etotheipi on July 09, 2011, 03:05:31 PM
As far as I'm aware, this only happens in two cases:
1) The rare case of simultaneously transmitted blocks where 1 of those blocks will end up invalid and discarded
2) Someone pulls off a double-spending attack where they build a competing chain faster than the rest of the network, after someone has already accepted a huge payment from them in the original chain.  The attacker broadcasts the longer chain and all the miners switch to it because it's longer.

Are there other cases?



Title: Re: Any way to know about a blockchain reorganization?
Post by: kjj on July 10, 2011, 03:13:46 PM
If you watch the debug.log, you should see a "REORGANIZE" message come across too.


Title: Re: Any way to know about a blockchain reorganization?
Post by: bfever on July 10, 2011, 08:37:23 PM
If the block chain gets changed, is there any way to notify about it?  Reason being, I have a database being written to based off of every new block that arrives, so if one of the older blocks changes, I'd like to know about it.

The "older blocks" won't actually change. When a block chain forks, and you happen to be on the false part of it when one of the forks becomes the "longest", all your blocks from the fork up are simply no longer valid (and all transactions contained in it).

You don't get a direct notification of it, but you can "easily" know when there is/was a fork, because the new block you receive will NOT reference the latest block you have in your database.
In fact, there are 2 possibilities (current block height in your database is N):
  • The new block has a previous hash that you already have in your database (most probably references block N-1, the start of a fork). You can either choose to ignore this new block (as you already have a block chain that is longer or same length), OR you can drop all blocks up to the previous hash of the new block and append this new block as usual.
  • The new block has a previous hash "PH" that doesn't correspond to any of your current block hashes. This means there is/was a fork and your part of the bitcoin network did not get one or more blocks of the other branch. You will have to ask with a "getblock" message the block X with this hash "PH" and check if the previous hash in this block X is a hash you know of. If so, you can reconstruct this branch (and like in the first possibility decide what branch to keep), if not you'll have to do a "getblock" again (and loop until you get a known hash).