I have my own bitcoind server (bitcoin core) and receive transaction data from the bitcoind via ZMQ. Then the transactions are stored into my database for data analytics or statics.
[bitcoind] --> (bitcoin transactions via ZMQ) --> [store into my DB]
Normally, it works well but sometimes blockchain will be re-organized when multiple chains are detected. At that time, I have to update my database as well. My question is how to detect the reorganization event from bitcoind server. Or do you have any good ideas for this.
FYI,
I'm listening new block generation notification via ZMQ.
The main reason why I have my own database is seeing Colored coin transactions and Counterparty transaction.
Update 1
It seems that I can subscribe only four channels; hashblock, hashtx, rawblock and rawtx. It may be impossible to know the reorganization via ZMQ. I may have to confirm when I receive newblock notification wether fork exists or not..
Update 2
I may have to call getchaintips RPC when every time I receive a new transaction notification.
How to detect a fork with bitcoin-cli?
Update 3
My understanding is that I need the following process to reorganize MY OWN Database.
getchaintips RPC's response
{
"height": 420561,
"hash": "000000000000000001cb02590846299c91794e9b9f422513cff4b9c1dd5c62a",
"branchlen": 0,
"status": "active"
},
{
"height": 419698,
"hash": "000000000000000004a3a78750438d0491b6335cbbe9c15099a6e55b6943e51a",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 418868,
"hash": "000000000000000000ba1d7d93ad1c7f04fb4a430fdee67c44fbdd3236f2b805",
"branchlen": 1,
"status": "valid-fork"
},
- Call getchaintips when I receive a new tx notification.
- Compare the two hashes. One is status is at "valid-fork" and another is stored hash at the same height in my own database. (As above response, height=418868, hash=000000000000000000ba1d7d93ad1c7f04fb4a430fdee67c44fbdd3236f2b805)
- If the two hashes are different, delete all records from valid-fork's height and fetch transactions from the hight to the height of active status(As above response, height=420561), then insert them into to my own database.
Update 4
It seems that Counterparty detects reorganizations in the following source code. I think I can see it as reference.