I have understood the sha256 algorithm example . But how does a node in a network decide if a transaction is valid or not .
Proof of work has nothing to do with transaction validity.
Transaction is valid when it meets certain simple rules:
- You do not spend more ether than you have.
- Index number(nonce) of the transaction follows previous transaction of your address.
All valid transactions go in a block. And that's where Proof of Work comes.
Imagine it like finding a needle in a haystack. All miners start to do it in the same time. The one who finds it becomes the creator of the block. It used to verify that miner used a lot of computing power. The reason for this is to prevent malicious users from creating long chains which may compromise whole blockchain.