Question: Node A receives a newest block with height `h1`, which it determines to be valid, and therefore stores the block on disk. A little later, it receives another block with the same height `h1` but a greater amount of proof of work than the previous one. What would node A do? 

Also I am confused about the order of processing a new block. The order in the source code:

```
              success                          success
Check(block)----------->store block on disk------------------>ReceivedBlockTransaction-------->ActivateBestChain

```

If `ReceivedBlockTransaction` or `ActivateBestChain` were to fail, would the disk be rolled back or not? If it would rollback, why not put the `receivedBlockTransaction` and `ActivateBestchain` before `store block on disk`?

Answer: 
First to clarify:  

Two competing blocks at the **same height** that build on the **same parent block** always have the **same amount of proof of work**, as that depends on the difficulty, which will be the same for both competitors.

Thus, a chain reorganization will only occur in practice if a competing chain tip pulls ahead of a previously accepted chain tip by accumulating a greater length.\*

Bitcoin Core stores competing blocks it receives as a sidebranch, if the headers would potentially make it the best chain. Otherwise it discards them.

When chain reorganization occurs, Bitcoin Core will undo each block from the tip one at a time, reverting back to the common ancestor with the new best chain, then will apply the blocks in the new best chain one at a time until it is at the new tip.

\* It is possible to construct a case around a difficulty change where competing chain tips have different difficulties at the same height. Then, the one with the higher difficulty would replace the one with lower difficulty at the same height. However, that would require a chain split of more than one block length.
When the client receives conflicting blockchains, the chain with the higher overall work will "win" and be the one that node A considers the true chain. If the two chains are equal (e.g. because two miners found a block at about the same time), it will hold on to both until it sees a new block on one of the chains make it the best chain. In the example you described, this is called a [chain reorganization](https://en.bitcoin.it/wiki/Chain_Reorganization).

I'm not familiar enough with the source code to tell you exactly *how* it does this, but it's apparent that it does. I would assume that it does remove an orphaned chain's blocks from the disk once it knows it's orphaned.