I'm trying to understand Bitcoin a bit more and I'm struggling to find an answer for this scenario. Say for example I have access to a wallet and I have give a person on the other side of the world access to the same wallet (both using bitcoin core wallet and bitcoin-cli sendtoaddress). We are on our phones and we both have the exact same balance and are at the exact same block height. How does bitcoin prevent the scenario where I send money to one address and a person with access to the same wallets sends money to another address at approximately the same time? Does the first transaction to hit the network win and the later one just never get confirmed? How does the decentralized network decide?
-
1Does this answer your question? Bitcoin fork resolution - Fork losers download previous blocks 'til common block?– RedGrittyBrickMar 14, 2021 at 16:24
-
No. I'm trying to figure out why when I have two wallets on different devices in different locations and the wallet gets sent money, one wallet receives the transaction before the other.– JonathanMar 14, 2021 at 17:04
1 Answer
Does the first transaction to hit the network win and the later one just never get confirmed?
Almost however it's not as simple as the "first" transaction, but it is essentially a race.
When a new transaction is broadcast to other nodes it enters what is known as the mempool. Using the phrase "the mempool" can be confusing since there isn't a single mempool, every node has its own mempool and it may not be the same as another nodes mempool.
In your scenario, two identical wallets different sides of the world, the one that broadcasts a transaction with a higher fee may "win", or the one that broadcasts to nodes that are better connected for propagation across the network may "win".
Once the transaction is in the mempool and other nodes know about it, you will likely receive a "missing inputs" error when trying to broadcast inputs that have already been spent. The transaction does not have to be confirmed in a block in order for the double-spend to be noticed; it only needs to be in the mempool. Some transactions can sit in the mempool for hours or even days if network use is high and the transaction fee for that transaction are low making it important that transactions in the mempool are checked for double-spends.
In any event a transaction does not have any degree of permanence until it is confirmed in a block. Even with 1 confirmation a transaction may not be permanent in the event of competing longest chains, the losing chain will have orphaned blocks.
Back when bitcoin was younger many people would wait for 6 confirmations before considering a transaction permanent. Today a few confirmations is likely enough for most transactions to be considered permanent.