I know orphan blocks can occur naturally when two miners produce blocks at similar times. in bitcore-node, How does this handled? for example, when orphan block occur in node, does bitcore change blockHeight in transactions table?
If there are multiple chaintips that match in total work, Bitcoin Core will consider the one it heard first about the best chain. Only if another chaintip accumulates more work, it'll switch to that chaintip. As only one block can be part of the best chain at the same height, the competing block is essentially irrelevant unless it happens to become part of the best chaintip.
After block 99, there were two competing blocks found at the same time. Our full node first hears about block
100A (the asterisk marks the block our full node currently considers the chaintip), and a peer announces chaintip
B with a different block at height 100. Bitcoin Core will not update its chaintip, but still consider
100A the best chaintip.
100A* 98 ← 99 ↢ 100B
It's very unlikely that both
100B get a successor block at the same time. Let's say that the chaintip
B is extended first.
100A* 98 ← 99 ↢ 100B ← 101B
Upon learning about the new best chain, our full node will first revert block
100A: all of the transactions that got confirmed in block
100A get added back to the mempool and marked unconfirmed.
100A 98 ← 99* ↢ 100B ← 101B
Then the full node follows the best chain by applying the two blocks
100A 98 ← 99 ↢ 100B ← 101B*
Any transactions that had been confirmed in
100A might have now been associated with
101B instead, or they may still be in the mempool.