I understand that each node has a transaction pool, which is used for adding transactions to the block. What happens to the transactions when a miner's block is declared invalid? Also, how are transactions removed from the local node's transaction pool once they are mined into the block?
[Ethereum] What happens to transactions in Uncles
miningtransactionstxpooluncle-blocks
Related Solutions
This is really an implementation detail and every miner could operate on a wide variety of mining strategies. That being said, AFAIK miner miners/pools run the standard algorithm included in Geth, which is:
- Discard anything below a threshold
- Gather all the executable transactions (i.e. one for each account)
- Pick the more expensive one
- Repeat from step 2. until no more transactions fit into the block
Few caveats:
- The algorithm is actually a bit smarter and does a fancy sort so that steps 2-3-4 can be done simply by iterating over a specially sorted array.
- Checking whether a transaction fits into a block or not uses the stated gas limit of the transaction, not the actual consumption (since the latter would require actually running the transaction first, which we don't want to).
- Based on the above, if the block is almost full, it can easily happen that a lower priced transaction gets included over a higher priced one if the higher one doesn't fit but the lower one does.
I'm trying to figure out how an invalid block would come into existence
A miner can create an invalid block in many ways (such as putting an invalid timestamp on it), including having invalid transaction in the block.
But the invalid block basically does not exist because all other miners and nodes will just ignore the block.
From what I understand, invalid transactions (like spending more ETH than the account balance) won't even make it into the block. So is it always fraud or chance
Correct, miners will not include invalid transactions in a block: they would not get any rewards from it. An invalid transaction is generally fraud or could be created by using buggy software. Generally, a valid transaction (that's not in a block yet) will always remain valid: if Alice writes a check to pay Bob, the check will always be valid: only Alice could try to invalidate the check by say sending all her money to Charlie using the same check number.
when a block does turn out to be invalid, are the valid transactions that were included returned to the transaction pool?
Each miner has their own transaction pool, and they each can decide which transactions to include a block. Since an invalid block is just ignored, miners and nodes will keep all transactions in the invalid block in their transaction pool (they don't even know that there were transactions in the invalid block, to remove them from the pool in the first place).
in the case of two miners publishing a block at the same time...are both including the same transactions, are they still in the pool until ..x.. confirmations have occured?
Generally, no, both miners are not including the same transactions. By chance, both may have included the same transactions, but each miner can decide what to include, and the order of the transactions. Each miner and node can have their own rule about when transactions will be removed from the pool, as this isn't part of the protocol.
I'm just a bit unclear on actual 'invalid block' reasons and how the system makes sure all valid transactions will still be included in another block
An invalid block is ignored, so any transactions in it do not affect the transaction pool. The transactions will remain in the pool and since miners would like to collect the gas/transaction fees, they are likely to include them in another block.
Best Answer
Answering your first question: If the transactions in a block that is declared invalid have not been already validated somewhere else in the main blockchain, they will be returned to the pool of unconfirmed transactions from which miners mine. There is no loss of data.