Author

Topic: Any way to know about a blockchain reorganization? (Read 2365 times)

jr. member
Activity: 39
Merit: 1
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).
kjj
legendary
Activity: 1302
Merit: 1025
If you watch the debug.log, you should see a "REORGANIZE" message come across too.
legendary
Activity: 1428
Merit: 1093
Core Armory Developer
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?

member
Activity: 70
Merit: 18
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.
legendary
Activity: 1428
Merit: 1093
Core Armory Developer
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?   

legendary
Activity: 1400
Merit: 1005
Thanks theymos.  If blockexplorer does it that way, I'll take it that's the only way to find out.  Smiley
administrator
Activity: 5166
Merit: 12850
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.
legendary
Activity: 1400
Merit: 1005
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.
Jump to: