I'm not sure I understood everything but all the clients always keep the longest chain, ok. So, what happens if an evil person injects bad transactions in a block and successfully mine it (given the difficulty, the probability is extremely low, but let's imagine), how is the network gonna answer?
The rule is that the longest* valid chain is considered the active one. If you build a forking branch with invalid transactions in it, the network (in particular, all full nodes - including miners) will simply ignore it. If it's invalid, it does not exist. Blocks are not relayed to peers before validating them entirely.
You may be able to temporarily fool lightweight clients that don't do full validation, but to build a chain with sufficient length to "confirm" these invalid transactions requires sustained high hashrate, assumed to be impossible without significant (close to or above 50%) mining power.
longest* = it's actually the branch with the highest estimated total number of hashes that is considered the active one (as a tie breaker), not the longest one. This only very occasionally makes a difference (in particular, when the fork crosses a retarget).
Ok thanks for your answer. When you say "the total number of hashes", you mean in the last found block or in the whole blockchain?– heyoJul 22, 2014 at 23:08
In the whole blockchain. Fun fact: that number recently surpassed 1 yottahash (a 1 with 24 zeroes). Jul 22, 2014 at 23:11
another question: when are the transactions checked? When the block is found, when the emitter of the transaction sends it to other nodes or the both?– heyoJul 22, 2014 at 23:11
Every node validates it before sending it to other nodes. Jul 22, 2014 at 23:12
1Adding the same transaction again in a block within a block chain that already has that transaction is a classic double spend attempt, and simply makes the block invalid. No node or wallet in the network will accept it. Aug 14, 2014 at 17:50