Sorry for the long text. This is just my observation and was wondering if you could let me know if I am correct or not.
Let's say minerA and minerB both have the same transaction in their mempool since my transaction got propagated to the network before including it in the block.
Now, Let's say minerA was trying to mine a block and before it did, minerB solved it and broadcasted it to minerA.
Now, it's time that minerA has to remove the conflicting transactions that already happened on minerB's block. How will it remove those transactions from memory pool ? will minerA take a look at the minerB's block, get txId's from the transactions, and check if transactions with those txId is in its mempool and if it is, it removes them from it.
Let's say I have 1 BTC only. I send this 1BTC to address B on minerA, and I send this same BTC at the same time to minerB. minerB will include the transaction to its mempool and then move it to block and start mining on it. minerA will also include the transaction to its mempool, then move it to block and start mining on it.
let's say minerB solved its block quicker and broadcasted it. Now, what's gonna happen is minerA stops mining its block, since minerB was quicker. Now, minerA will look minerB's block, and seems legit. So now it will add it to its chain.
Right now, because minerB block got solved, minerA now needs to validate mempool again (it has to remove conflicting transactions as explained in case 1 and also has to check if after including minerB's block, if minerA's pool still has the valid transactions(balance is valid), in this case, since I only had 1BTC, it will check the transaction that i sent to address b and see that i don't have any BTC anymore. so it will remove it .
NOTE: miner will start validating mempool again before that miner includes transactions in a block and starts mining on it. right ?
I guess , miners remove transactions from mempool if
transaction is conflicting as explained in Case 1
transaction can't be included anymore because it's already invalid due to blocks mined at the same time as explained in Case 2.
What do you think I missed, do you agree to my observations ?