Sending bitcoins on the network is done by constructing a Bitcoin transaction (TX). Every transaction (except the very first one) is made of one or more digitally signed inputs. An input is an unspent output of a previous transaction.
Look at the following diagram to get an idea.
Let's say Bob has 50 BTC from his investments and wants to send 0.5 BTC to his friend Alice.
He will take his unspent output of 50 BTC, creates a new input that says how much is sent to which output(s), signs this input with his private key and broadcasts the transaction (TX A) on the network.
Because Bob doesn't want to spend the whole output to Alice, he spends 49.5 BTC to his "change address". If he didn't do it, this amount would go towards miners as a fee.
Think of the change address as a change you'd get back for paying $5 product with a $20 bill. You can't simply tear the bill in 2 parts. Instead, you hand over your $20 bill and get back the change of $15. .
Alice received Bob's 0.5 BTC as well as 0.1 BTC and 0.2 BTC from other people. These are her unspent outputs.
Alice then decides to spend all 3 outputs at once to her employee. She creates one input that references these not-yet-spent outputs, signs the input with her private keys and broadcasts the new transaction (TX B).
Confirm the Transaction
Once the TX is broadcasted on the network it hangs in a mempool (memory pool) for a while until it's picked up by miners.
Mempool is a place in system's memory that holds a list of all unconfirmed transactions that need to be processed by miners.
A miner is and individual or a company that competes with other miners by solving extremely difficult mathematical problems. This process is called mining and it's very resource intensive and expensive.
How Mining Works
The miner picks up transactions from the mempool based on his arbitrary rules and include them in a new block. This data along with the header of the previous block is used to create new block's header.
A nonce (an arbitrary unique number) is then added to the block header and hashed. This process is repeated by incrementing the nonce which produces a completely new hash, until it is equal or less than the mining target.
A mining target is a huge 256-bit string. The lower the value of the string is, the harder it gets to find the right hash.
Imagine you have a random number generator that generates fixed set of numbers from 0 to 100. Generating a number that is less than or equals to 100 is very simple with a probability of 100%. Any generated number would match. Lower this threshold and the probability of you meeting the requirement is way lower.
Because miners come and go and the rule of the network is to mine approximately 1 block every 10 minutes, the mining target needs to be periodically adjusted. This readjustment is set to happen every 2016 blocks or ~every 2 weeks.
The first miner to find the right hash broadcasts it on the network. He also gets to keep all the fees included in the block as well as the block reward (currently at 12.5 BTC per block).
Verify the New Block
After the newly minted block is broadcasted on the network, it gets verified by full nodes.
Every new block passes through full nodes which check whether included transactions and the block itself matches consensus rules. If it doesn't it absolutely get's rejected by the client.
This design makes sure miners are in check. If they don't follow the rules, their blocks will be invalid which means miners just burned a lot of electricity for no reason.
If you want to learn more about Bitcoin, read the original Bitcoin Whitepaper created by Satoshi.
We hope you like the post.