How does longest chain propagation take place in bitcoin?
An attack that attempts to outpace the mining power of the whole network by itself is called a majority-attack or &quot;51%-attack&quot;. Concretely, your example is a doublespend attack by means of a majority attack. When the attacker publishes their chain-tip, each node performs a chain-reorganization to switch to the best chain.
Concretely, the blocks B4 and O1 both share the hash of B3 as their prevHash. Once the attacker announces the blocks O1-&gt;O2-&gt;O3-&gt;O4, other nodes will notice that this chain-tip has more total work, find the last shared ancestor in the blockchain (B3), revert all blocks up to the shared ancestor, and then apply the new blocks to converge on the best chain-tip.
A blockchain-fork and chain reorganization can also happen benignly when two miners happen to find a block at the same moment. At that point the network's nodes are split in opinion what the best chain is, since each node considers the block it first saw to be the correct one. Once another block extends one of the two chain-tips, the network converges on the best chain as the nodes hear about the new block.