I'm trying to iterate over all blocks, incl orphans, via the rpc api.
First, these are my assumptions. Please tell if these are correct:
Orphaned blocks are valid blocks.
correct.
Orphaned blocks contains valid transactions.
correct.
The transactions in an orphaned blocks can only be found in that orphaned block.
Nope. A transaction from an orphaned block usually gets rebroadcasted by the client or directly included in the competing block by the other miner.
In order to iterate over all blocks in the longest chain you could use these commands:
getblockcount
getblockhash
getblock
But what about all the orphans? They are not part of the longest chain, right? You can get any block if you know the hash with "getblock". But how do you discover the hash of the orphans in the first place?
Is there a way to discover the orphans via rpc?
There is a list here[1] or start bitcoind with printblocktree. This will AFAIK only work on forks your client witnessed. I know of no RPC call, but maybe someone else does.
You can run bitcoin -printblocktree then look in debug.log to see any chain splits your client observed. My copy has seen three throughout its lifetime, all of them only a single block long.
[1]
https://blockchain.info/orphaned-blocks