Free tech learning for everyone, from basics to pro

BlockchainEmerging Technologies and SecurityIntermediate

Smart contracts: how they work and how they fail

In short: A smart contract is a program stored on a blockchain that runs exactly as written when someone calls it. That makes it transparent and automatic, but also unforgiving: bugs are public and often cannot be patched easily. Secure development, testing, independent audits and monitoring are essential.

Code that lives on the chain

A smart contract is a program stored at an address on a blockchain. Anyone can send it a transaction, and every node runs the same code to reach the same result. The name is a little misleading: it is not a legal contract, and it is only as smart as its code. The idea is simply that rules ("release the payment when both parties confirm") are enforced by software that no single party controls.

Ethereum made smart contracts popular. Its contracts run on the Ethereum Virtual Machine (EVM) and are usually written in Solidity. Every operation costs gas, a fee paid by the caller, which stops infinite loops and pays validators for computation. Many other chains also run EVM-compatible contracts, and permissioned platforms such as Hyperledger Fabric use similar ideas under the name "chaincode".

Smart contract lifecycleWritecode and testsAuditreview,analysisDeploystored onchainCallusers send txsMonitoralerts, pause
Figure 1. Once deployed a contract is hard to change, so most of the security work happens before deployment.

What they are used for

  • Tokens: rules for creating and transferring digital tokens, following standards such as ERC-20.
  • Escrow and multi-party approval: funds move only when agreed conditions are met.
  • Decentralised applications: exchanges, lending and voting systems that run without a central server.
  • Business workflows: in permissioned chains, sharing steps of a supply chain or trade process between companies.

Smart contracts cannot fetch outside data on their own. They depend on oracles, services that bring in data such as prices or delivery events. An oracle becomes a point of trust and a target.

How smart contracts fail

Common smart contract flawsReentrancycall back before updateAccess controlmissing owner checksOracle manipulationbad price dataInteger issuesoverflow, roundingLogic errorswrong business rulesUpgrade risksproxy admin abuse
Figure 2. Most smart contract losses come from a small set of recurring bug classes.

A well-known example is reentrancy. A contract sends funds to another contract before updating its own records. The receiving contract calls back in and asks again while the balance still looks unchanged. The classic defence is the checks-effects-interactions pattern: check the conditions, update your own state, and only then interact with outside contracts. Reentrancy guards add a lock so a function cannot be entered twice at once.

FlawWhy it mattersDefence
ReentrancyFunds drained by repeated callsChecks-effects-interactions, reentrancy guards
Missing access controlAnyone can call admin functionsExplicit role checks, well-reviewed libraries
Oracle manipulationFake prices trigger wrong payoutsMultiple data sources, time-averaged prices
Arithmetic errorsWrong balances or rounding lossesModern compiler checks, careful maths, tests
Upgrade mechanism abuseAn admin key can replace all the logicMultisig admin, timelocks, transparency

Secure development practices

  1. Use mature, widely reviewed libraries rather than writing token or access logic from scratch.
  2. Write unit and property-based tests, including attack scenarios, on a local test chain.
  3. Run static analysis tools and fix every finding you cannot justify.
  4. Get an independent audit for anything that will hold value, and publish the scope.
  5. Deploy to a public test network first and run a bug bounty.
  6. Plan for incidents: pause functions, monitoring, and protected upgrade keys.

Tip: "code is law" cuts both ways. If the code allows something, the chain will do it, even if it was never the developer's intention. Treat contract code like firmware that cannot easily be recalled.

Common mistakes include deploying unaudited code, keeping the upgrade key on a single laptop, and trusting one price source. For exams, remember that smart contracts are deterministic programs, that they need oracles for outside data, and that immutability makes secure design before deployment critical. Testing should always use local or public test networks, never other people's live contracts.

Practice

Why do smart contracts need oracles?
Contracts can only see data already on the chain; oracles bring in outside information such as prices or events, which makes them a point of trust.
What is the checks-effects-interactions pattern?
Check conditions first, update the contract's own state second, and only then call external contracts, which prevents reentrancy attacks.
Why is an upgradeable contract a security trade-off?
It allows bugs to be fixed, but whoever controls the upgrade key can change all the logic, so that key must be strongly protected, for example with multisig and timelocks.

Key takeaways

  • A smart contract is a program on a blockchain that runs exactly as written.
  • Ethereum contracts run on the EVM, are often written in Solidity and cost gas.
  • Oracles bring in outside data and become a trust point.
  • Common flaws: reentrancy, access control, oracle manipulation, upgrade abuse.
  • Test, analyse, audit and monitor, because deployed code is hard to fix.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.