Why are transactions grouped into blocks? (how are transactions put into blocks) Why are transactions grouped into blocks (PART 3)?


Lets say we have just put a block on the blockchain. Now miners start forming new blocks and try to guess new hash values. The miners are putting transactions into their formed block (with a hash guess)? Which of the unconfirmed transactions do they put into the block (all unconfirmed or a subset)? 
Is putting a transaction into a block a way of stating that transaction has been validated? How does this guarantee the transaction, miner put in the block, is valid?
Lets say we have just put a block on the blockchain and there are no blocks at the moment that miners mine. Now miners start forming new blocks and try to guess new hash values. The miners are putting transactions into their formed block (with a hash guess)? Which of the unconfirmed transactions do they put into the block (all unconfirmed or a subset)?
  


Miners will select transactions to include based upon transaction fees. In Bitcoin Core, this transaction selection is done as follows: Group up transactions into packages, a package consists of a transaction and its unconfirmed ancestors and descendants (some packages may just be 1 transaction, others more). Then calculate the total fee rate paid by a package and sort the packages by fee rate. Select transactions by going down the list of packages and choosing each package until the weight of all of the transactions (as defined by BIP 141) cannot allow any more transactions to be included (i.e. add packages until the weight exceeds the limit, then remove what was just added and add the next one, etc. until no transactions left to add).

There will often be more unconfirmed transactions than can fit in a block, so miners will only select a subset of unconfirmed transactions.


  
  Is putting a transaction into a block a way of stating that transaction has been validated? How does this guarantee the transaction miner put in the block is valid?
  


It is not. A block could contain invalid transactions and the block (and those invalid transactions) would be invalid. A full node must check that a block is valid. They do this by checking the block header is valid, the coinbase transaction is valid, and all transactions in the block are valid. Just because a transaction is found in a block does not mean that it is necessarily valid, and if it is not, the block is invalid and should be rejected.