Lesson 4 of 4 Secure Smart Contract Development

Security Analysis and Audits

No single technique finds every bug, so secure projects layer several. This lesson explains static analysis, fuzzing and invariant testing, symbolic execution and formal verification, audits, bug bounties and monitoring, and how they fit into the development lifecycle.

Learning outcomes

After completing this lesson, you will be able to:

  • Compare the main security verification techniques.
  • Describe how static analysis findings are triaged.
  • Explain fuzz and invariant testing.
  • Describe the roles of audits, bug bounties and monitoring.

Layers of assurance

Each verification technique finds different kinds of problem. The following table compares them.

TechniqueHow it worksGood at findingExamples
Compiler warningsChecks during compilationUnused values, shadowing, unreachable codesolc
Unit testsDeveloper-written checks of expected behaviourLogic errors in known scenariosHardhat, Foundry
Static analysisScans source for known patterns without running itReentrancy, tx.origin, unchecked callsSlither, Remix static analysis
FuzzingRuns functions with many random inputsEdge cases developers did not think ofFoundry, Hardhat Solidity tests, Echidna
Invariant testingRandom call sequences checked against propertiesBroken accounting and state machinesFoundry invariants, Echidna
Symbolic executionReasons about all inputs mathematicallyPaths to unsafe statesMythril
Formal verificationProves properties against a specificationGuaranteed absence of specified bugsCertora, SMTChecker
Manual auditExpert review of design and codeLogic and economic flaws tools missIndependent audit firms
Match the pairs

Assurance techniques

Exam skills 3.3.1

Match each technique with what it is best at finding.

Log in to try this activity Interactive activities are free with an account.

Static analysis in practice

Slither, an open-source Python tool, runs dozens of detectors over a Hardhat or Foundry project in seconds and ranks findings by impact and confidence. Its output must be triaged: some findings are real bugs, others are acceptable by design or false positives. Remix's Solidity Analyzers plugin offers similar checks in the browser. Running static analysis in continuous integration catches regressions on every change.

Fuzzing and invariants

A fuzz test is a test function with parameters; the runner calls it with hundreds or thousands of generated values, including boundary values, and reports any input that breaks an assertion. An invariant is a property that must always hold, such as "the sum of all balances equals totalSupply" or "the contract's ether is at least the total owed"; an invariant test executes random sequences of calls by random actors and checks the property after each.

// A Hardhat or Foundry Solidity fuzz test
function testFuzz_DepositWithdraw(uint96 amount) public {
    vm.assume(amount > 0);
    vault.deposit{value: amount}();
    vault.withdraw(amount);
    assertEq(address(vault).balance, 0);
}

Audits, bug bounties and monitoring

A security audit is a time-boxed review by independent specialists, typically after the code is feature-complete and well tested. Auditors report findings by severity, the team fixes them, and the auditors review the fixes. An audit reduces risk but cannot prove the absence of bugs, and any change after the audit needs review again.

After deployment, a bug bounty programme rewards researchers who report vulnerabilities responsibly instead of exploiting them, and monitoring watches for unusual transactions, such as large withdrawals or calls to administrative functions, so the team can respond, for example by pausing.

Put in order

Assurance across a launch

Exam skills 3.3.1

Arrange the security activities of a launch in a sensible order.

Log in to try this activity Interactive activities are free with an account.
Worked example

Planning assurance for a launch

A team will launch a staking contract holding user funds in eight weeks. What security activities should the plan include, and in what order?

During development: unit tests for every function and failure path, static analysis in continuous integration, and fuzz and invariant tests for the accounting. When the code is frozen: an external audit, fixes and a fix review, then a deployment rehearsal on a fork and on Sepolia. At launch: verified source, administrative roles on a multisig, caps on deposits for the first weeks, monitoring and a bug bounty. The audit should not be the first time the code faces scrutiny.

Worked example

Triaging a tool finding

Slither reports "reentrancy-events" in a function that emits an event after an external call to a trusted, immutable token. The team asks whether this is a critical vulnerability.

The detector flags that an event is emitted after an external call, which could make event order misleading if the call re-entered. With a trusted, non-reentrant token, the impact is low, but moving the event before the call costs nothing and silences the finding. Each finding should be assessed for real impact, then fixed or documented as accepted, rather than dismissed or treated as automatically critical.

Check yourself

What is an invariant test?

Show answer

A test that runs random sequences of function calls and checks after each that a property always holds, such as the sum of balances equalling the total supply.

Exam Tips

Remember for the exam

  • Combine tests, static analysis, fuzzing, invariants, review and audits; each finds different bugs.
  • Slither and Remix analyzers detect known patterns; findings need triage.
  • Invariant tests check properties after random call sequences.
  • Audits reduce risk but are not guarantees; changed code needs re-review.
  • Bug bounties and monitoring continue security after deployment.