Thank you for the detailed explanation!
Now, what exactly happens during mining? The computer generates random data, calculate its hash and if the hash is valid, the generated data is considered a valid block?
The computer selects a set of unconfirmed transactions.
A block header for that set of transactions is created
A double SHA-256 hash of the block header is calculated.
If the hash value is less than the target difficulty required by the protocol, the header and set of transactions are considered a valid block.
If the hash value is not less than the target difficulty, a number in the header called a "nonce" is incremented.
A double SHA-256 hash of the block header is calculated.
If the hash value is less than the target difficulty required by the protocol, the header and set of transactions are considered a valid block.
The nonce increment, double hash, and compare steps are repeated as fast as possible until either:
- A hash value less than the target difficulty is found
- A valid block is received from a peer
In either case the valid block (received or created) is added to the local copy of the blockchain and broadcast to all connected peers.
The process then begins all over again with a new set of unconfirmed transactions.