Bitcoin Forum
November 07, 2024, 04:41:35 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: getblock()->nextblockhash - how to avoid getting stuck in orphaned chains?  (Read 2142 times)
monsterer (OP)
Legendary
*
Offline Offline

Activity: 1008
Merit: 1007


View Profile
December 09, 2013, 09:24:01 AM
 #1

Hi guys,

I'm manually parsing the blockchain using bitcoind, calling getblock(), then processing, then moving to the nextblockhash. However, I can quite easily get stuck in a orphaned block, or chain of blocks.

What can I do to prevent this?

Cheers, Paul.
monsterer (OP)
Legendary
*
Offline Offline

Activity: 1008
Merit: 1007


View Profile
December 10, 2013, 09:36:04 AM
 #2

...could I use the number of confirmations on the block as an indicator of orphanhood?
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1026



View Profile
December 11, 2013, 05:04:40 AM
 #3

Dare I ask why you are doing this?

17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
monsterer (OP)
Legendary
*
Offline Offline

Activity: 1008
Merit: 1007


View Profile
December 11, 2013, 08:17:34 AM
 #4

Dare I ask why you are doing this?

I need to be able to tell when bitcoins arrive at a multi-sig address, so I have my own parser (using bitcoind), which steps over blocks as they come in, calls getrawtransaction on each one, then interrogates the output addresses.

I loop over blocks always moving to nextblockhash until it is null. But by doing this, I've become stuck on an orphaned block when my network went down. I want to know the best way to avoid getting stuck in the first place, or a neat way to become unstuck.

Cheers, Paul.
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1026



View Profile
December 11, 2013, 12:30:21 PM
 #5

If I were doing that, I would use -blocknotify to trigger my parser, and I'd keep track of the last X block hashes that I've seen.  This would never get stuck, and should be able to handle catching up after being offline for a while.  Just in case though, you could look for the new block's prevHash, and work backwards until you reach a block that you already know.

No matter what, you are going to need some means of dealing with orphans and reorgs, but this way it would be just for your totals.

17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
monsterer (OP)
Legendary
*
Offline Offline

Activity: 1008
Merit: 1007


View Profile
December 11, 2013, 01:41:14 PM
 #6

If I were doing that, I would use -blocknotify to trigger my parser, and I'd keep track of the last X block hashes that I've seen.  This would never get stuck, and should be able to handle catching up after being offline for a while.  Just in case though, you could look for the new block's prevHash, and work backwards until you reach a block that you already know.

No matter what, you are going to need some means of dealing with orphans and reorgs, but this way it would be just for your totals.

Blocknotify doesn't fit very well with my design pattern, because I would need to make sure bitcoind stopped immediately whenever my app stops, or I would get out of sync.

Is there nothing I can check on the block itself to determin whether its orphaned?

Cheers, Paul.
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1026



View Profile
December 11, 2013, 01:52:41 PM
 #7

If bitcoind knows blocks that your application doesn't, then when the next block comes in, track backwards until you find a block that you do know.

Trying to go forward is going to get you into trouble.  You've already discovered orphans.  You also have to deal with reorganizations.

17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
monsterer (OP)
Legendary
*
Offline Offline

Activity: 1008
Merit: 1007


View Profile
December 11, 2013, 09:31:29 PM
 #8

If bitcoind knows blocks that your application doesn't, then when the next block comes in, track backwards until you find a block that you do know.

Trying to go forward is going to get you into trouble.  You've already discovered orphans.  You also have to deal with reorganizations.

What about always going backwards from the most recent block getblockhash(getblockcount()), until you meet the last block you processed?
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!