4: As far as the protocol is concerned, it is completely at the miner's discretion which transactions to include. It is legal if the miner chooses to include no transactions at all (other than the coinbase transaction which is required), and some miners actually do this (it means that the block is small and can be relayed quickly, which slightly decreases the chance that it will be orphaned).
Most miners will include as many unconfirmed transactions as possible, up to the block size limit, preferring those which have the highest fees. They may automatically exclude those which transfer a very small value (so-called "dust") or whose fee is below some threshold.
5: The miner is expected to verify that every transaction in his block is valid (outputs not already spent, valid ECDSA signatures, etc). When he finds a nonce which attains the target hash and submits his block to the network, other nodes will attempt to similarly verify the transactions in it, and if they are not all valid the entire block will be rejected (i.e. other miners will not use this block's hash as the "previous block" in their own mining). However, mining is not necessary for this sort of verification, since non-mining nodes will also do this verification, and refuse to recognize any transactions that don't pass.
You can have the next few, I'm quitting for the day :-) – Nate Eldredge – 2017-10-21T18:04:21.700
A lot of subquestions. Have to put some of them into separate questions. Damn doesnt let me post that often :D – croraf – 2017-10-21T18:21:18.413
You say if a block with correct header is put at the end of the chain but with invalid transaction the block itself is invalid. Does this mean that then anyone can use the same block with correct transactions and throw it on the chain again? – croraf – 2017-10-21T18:29:35.897
It would not be the same block because the invalid transaction would not be there and likely the nonces would be different because removing or replacing the invalid transaction (or changing it in any way) will change the block hash. But yes, there can be another block at the same height which contains almost exactly the same transactions. – Andrew Chow – 2017-10-21T18:31:24.063
So the solutions (you call them nonces?) depend on the transactions included in the block? – croraf – 2017-10-21T18:33:00.620
1The solutions are not nonces. Nonces are part of the data that is hashed and are what is commonly changed when trying to find a block hash (the solution) that is less than the target. The data that is hashed for the block hash includes the hash of all of the transactions in the block, so changing the transactions will also change the block hash. – Andrew Chow – 2017-10-21T18:34:40.090
Is it possible to add transaction to a block without it's descendants? I can't seem to find the answer to this. If it is possible, you would have to consider the packages with only the ancestors, and the packages with both the ancestors and the descendants, when selecting the transactions. – gijswijs – 2018-01-15T08:21:00.480
It is possible to add a transaction without it's descendants. There is nothing that prevents a miner from doing so. It is not possible to add a transaction without it's ancestors. Either the ancestors must already be in a previous block, or they must be included in the same block and come before the transaction in question. – Andrew Chow – 2018-01-15T17:26:41.803