# How does a node decide whether a block is valid?

I'm a software developer, and I've been trying to figure out how a node decides whether a block is valid, I do understand the hashes in bitcoin, the merkle root, and all those aspects but my question is:

If someone edits a block, how does the network know that "X" node modified a block? are the nodes constantly in communication sending some sort of fingerprint that changes if information gets modified (or sending the whole blockchain)?

If a node (A) detects that Block #XHeight from Node (b) is not valid, how does the network know who is right and who is not, since The block is valid for Node B but not for node A, how do they solve such argument? does the network goes into an election process, and the network decides which hash is the valid one? : how does two nodes certify that one of them is actually the valid one? is there some sort of "democracy" system where nodes vote in favor or against one specific hash in order to decide who's right between node A and Node B?

I'd like to get technical answers, since I'm trying to understand blockchain and bitcoin at a code level.

Blocks cannot be modified because the next block contains a hash of the previous block's header. Any change to a block would result in a change to the header, and thus, change its hash, requiring all blocks which succeed it to also be modified because their hashes would no longer be valid. It isn't possible to rebuild all of the blocks because it requires work to compute valid hashes (they must have nBits leading zeroes in their merkle root).

Blocks are correct only if they are in the longest known chain with the most accumulated proof-of-work. Meaning any chain with less work than the longest known chain is invalid.

If a node sees a valid block `X` but then later, receives a new block `Y`, where `YHeight > XHeight`, and the chain of blocks up to #YHeight does not contain the block `X`, then `X` is discarded. Only the blocks in the longest chain are treated as valid blocks.

This is also true in a tie. If two blocks are mined by different miners at the same time, each participant assumes that the first block they received is the valid one, and they continue the Proof-of-Work process from this valid block. The next miner to create a valid block ends the tie by creating the longer chain, and any miner who was working on a block other than the one referred to in the new block's header will discard the work they have done so far on the shorter chain, and continue working on the longer chain.

• But let’s say I modify a block, how does the network know that I actually modified a block, that I have a invalid chain? Are the nodes constanly communicating with each other? If they are, what are they sending in order to let the network know about these modifications?
– Juan
Feb 10, 2019 at 22:53
• The only person you can deceive by attempting to modify a block is yourself. If your try to broadcast a modified block, then any other node on the network will reject the block, either because its hash does not match the expected hash of the block which is expected at that height, or because they have a longer valid chain which does not include that block. Feb 10, 2019 at 22:55
• Nodes do constantly communicate with each other when new blocks are created or new transactions are created. These are all broadcast, and you typically receive information from multiple different sources, and your own node determines which information is correct based on which one it validates to be the longest chain. Feb 10, 2019 at 22:57
• So If I broadcast my invalid block, and the network detects this, then the network would send me back the valid block. Right?
– Juan
Feb 10, 2019 at 23:01
• Other nodes will detect an invalid block and discard it, but it is not required that they inform you of the valid one. Your node will need to send a `getheaders` or `getblocks` command to its peers to request the most recent information, and the peers will respond with the newest blocks they know about. You can validate all of this information and determine its correctness. During normal operation, nodes will automatically send you information about new blocks with an `inv` command, without you requesting it (unless you opt out of this information in the initial `version` message to the peer). Feb 10, 2019 at 23:08