Smart-contract security begins with the rules a system is supposed to enforce. Which assets can move, who can authorize a change, and which external information must be trusted? An implementation can behave exactly as written and still expose funds if those rules are incomplete.
The most useful security work connects design, code, deployment, and operations. An audit is one part of that process; it is not a substitute for clear invariants or a plan for responding when an assumption fails.
Introduction and Context
A deployed contract’s code is generally fixed at its address, but a system can still change through proxy upgrades, privileged configuration, or external dependencies. “Immutable” should therefore describe a specific component, not serve as a blanket promise about the whole application.
This revision focuses on mechanisms and review questions rather than unsourced loss totals or a “current” compiler version that quickly becomes stale. The Solidity security guidance is a starting point for implementation risks; the application’s economic and operational assumptions require additional analysis.
Common Vulnerabilities in Smart Contracts
| Risk | What can go wrong | What to examine |
|---|---|---|
| Reentrancy | An external call re-enters code while assumptions about state are temporarily invalid | State updates before interactions, cross-function effects, callbacks, and a suitable reentrancy guard |
| Arithmetic and rounding | Overflow, truncation, inconsistent decimals, or rounding direction changes balances | Boundary values, units, casts, unchecked blocks, and value-conservation properties |
| Access control | An unauthorized caller acquires or exercises a privileged role | Initialization, role administrators, upgrade authority, ownership transfer, and revocation |
| Time-dependent logic | A deadline or randomness assumption does not match the chain’s timestamp rules | Chain-specific timing constraints, tolerances, and behavior around boundaries |
| Storage incompatibility | A proxy upgrade interprets existing storage incorrectly | Layout compatibility, initializer execution, and upgrades tested against populated state |
| Business-logic errors | Individually valid actions combine to violate an intended rule | Invariants across deposits, withdrawals, fees, liquidations, and multiple contracts |
| Oracle failures | Stale, manipulated, unavailable, or incorrectly scaled data drives a decision | Data freshness, units, market depth, source independence, and failure behavior |
| Denial of service | A required action becomes unexecutable | Unbounded loops, reverting recipients, gas limits, and dependency availability |
Since Solidity 0.8, ordinary arithmetic reverts on overflow and underflow by default; unchecked blocks opt out. That does not resolve decimal mistakes or all explicit-conversion issues. The 0.8.0 changes explain the default behavior. Adding an older SafeMath pattern is not a replacement for reviewing the arithmetic actually used.
Time also needs context. Replacing a timestamp with a block number is not a universal fix: block numbers represent ordering, not a reliable wall-clock duration across every chain. Neither value should be treated as a private source of unpredictable randomness.
Best Practices for Smart Contract Development
Specify the invariants
Write properties that must hold across valid sequences of actions. For a simplified vault, an invariant might concern the relationship between assets, shares, and outstanding claims. State which fees, rounding rules, and external transfers affect that relationship.
Then test adversarial sequences, not only successful individual calls. A deposit followed by a withdrawal is a different test from repeated deposits around an external price change. If the expected result cannot be stated clearly, the design is not yet ready for meaningful security testing.
Reduce authority and external assumptions
List every privileged operation and the identity that can invoke it. Separate routine operation from upgrades and emergency actions where the design permits. Test that a role can be removed without leaving another route to the same privilege.
OpenZeppelin’s access-control documentation explains ownership, roles, and administration patterns. The choice of mechanism must be paired with an operational decision about who holds keys, how changes are reviewed, and how recovery works.
For external calls, consider both failure and unexpected callbacks. Update state in an appropriate order, avoid assuming that a recipient behaves like an ordinary account, and account for interactions across functions and contracts. A guard on one function does not establish the safety of every related path.
Pin and reproduce the build
Choose a supported compiler version after reviewing the project’s requirements and known issues. Pin the toolchain, dependencies, optimizer settings, and target environment so the reviewed build can be reproduced. Review a compiler update as a change to the system rather than automatically selecting whichever version is newest.
Use maintained libraries where they fit, but inspect their assumptions and configuration. A well-reviewed component can still be integrated incorrectly.
Test the properties that matter
Combine ordinary tests with fuzzing, invariant testing, static analysis, and independent review appropriate to the system’s value and complexity. Formal verification can check specified properties under a model; it cannot supply an omitted business requirement.
A useful test suite covers failures as well as success: rejected roles, stale data, zero and boundary values, malicious callbacks, and unavailable dependencies. Keep the failing case that motivated a fix as a regression test.
Additional Considerations and Unexpected Details
Upgrades create their own security boundary
Proxy upgradeability can enable repairs, but it introduces authority over future behavior and a requirement to preserve state correctly. Review initialization, implementation ownership, storage layout, and the exact transaction sequence used to upgrade.
OpenZeppelin’s guide to upgradeable contracts explains initializer and storage constraints. Test upgrades using representative existing state. A successful deployment of the new implementation is not evidence that old user balances remain correctly interpreted.
Prepare for an incident before deployment
Decide what can be paused, who can pause it, and how users recover or exit. A pause mechanism may limit damage, but it also creates an authority that can interrupt legitimate activity. Document its scope and test the recovery path.
Governance deserves the same scrutiny as application code. Review proposal execution, voting snapshots, quorum, timelocks, cancellation, and control of the contracts that hold assets. OpenZeppelin’s governance documentation describes mechanisms that must be configured as a coherent system.
Monitoring and a bug-reporting process help surface problems after release. Alerts need an owner and a meaningful response, while a bug bounty supplements rather than replaces pre-deployment review.
Conclusion
A defensible contract system has explicit rules, limited authority, reproducible code, tested failure behavior, and a response process the team can execute. Review those properties together. A secure-looking function does little good if an administrator, upgrade, or external input can bypass its assumptions.