I have been reading about blockchain forks and the longest chain situations and I cannot really understand what is going to happend in the situation when the transacton were included in both accepted AND rejected blocks? If transactions in rejected block are going back to the mempool, does it means that there will be blocks with duplicated transactions? I have drawed something out for better understanding of this: https://imgur.com/a/3ViIu I guess I`m missing something fundamental here. Thanks in advance!
Firstly, there is not only a single mempool, every node maintains its own mempool based on what transactions and blocks it has seen. Since these mempools will generally match pretty well, this fact is sometimes abbreviated by talking about "the mempool" and meaning the general body of unconfirmed transactions most nodes are aware of. If we were able to guarantee that everyone had the same mempool, we wouldn't need a blockchain. The main purpose of the blockchain is to give a decision between different versions of the mempool.
Secondly, from the perspective of each chain tip, the other chain tip is invalid and is treated as if it doesn't exist. Adding the observation that each node has its own mempool and assuming that the network is well-connected, you can find out the state of each node in regard to the chain tip they follow by acting as if the other chain doesn't exist.
Regarding your image:
- A node whose chain tip is C has TX10 in the mempool while TX3 and TX4 are confirmed.
- A node whose chain tip is D considers TX4 and TX10 to be confirmed and has TX3 in the mempool.
When block G is discovered, all nodes will reorganize to the lower chain tip DFG. At that point they will roll back to B, add TX14, TX15, TX3, and TX4 to their mempool, then apply block D, removing TX4 and TX10 from the mempool, apply block F, removing TX9 and TX15 from the mempool, and then applying block G, removing TX21 and TX20 from the mempool. TX3 remains in the mempool, because it is not confirmed in the lower chain yet.
Tx message Rule# 8: https://en.bitcoin.it/wiki/Protocol_rules Reject if we already have matching tx in the pool, or in a block in the main branch.