Hello Bitcointalk members, as it's apparent from the title, i need a reliable way to detect orphan blocks. I wrote a custom explorer, and it's running quite fast actually, but i need an explanation on how exactly i can detect orphans, and mark them as so.
In normal circumstances, orphan occurs when block A and B are produced at the same time and that a portion of the network is building on Block A and the other is building on Block B. The miners will compete with each other until either of the sides find a block. This further extends the network and thus one of the forks now have a larger accumulated proof of work. The longer fork will supersede the shorter one. The problem with this is that nodes won't register (AFAIK) block B after they've received block A. Hence, you would need to have a very good connection to the network and a modified client to see all of the orphans that is on the network.
What the longest chain fork due to orphan blocks is?
IIRC, the longest fork I've seen was during the 2013 hard fork and that lasted >30 blocks. Normally, the orphans should not go beyond 6 blocks.
If on every block sync, i check the last 1000 blocks, is that enough to assume that every block beyond these 1000 is NOT an orphan?
Is the -1 confirmations the only way to detect them?
The orphan blocks are stored within the Bitcoin database. Parse the database and you should be able to sieve them out.