Nightforce Learning Center

Designing and reviewing contracts for security

Sign in with Discord

Introduction

A program can follow its instructions perfectly even when those instructions leave out an important rule.

What you’ll learn

Identify the conditions a contract should enforce and how to check them defensively.

Lesson

Begin a review by writing down what must always be true. For the workshop, the confirmed count must stay within capacity, a person’s eligibility must be checked, and only an authorized role may reopen registration. These are invariants. They give reviewers specific behavior to verify.

Concern Defensive design question
Authorization Who can change capacity, pause registration, or assign roles? Where are those permissions enforced?
Numeric bounds Are counters and amounts checked at zero, at their upper bounds, and during conversions?
External interactions Is state already consistent when control passes to another component? What happens if that component fails?
Ordering and time Can outcomes safely tolerate concurrent requests, delayed inclusion, and the protocol’s timestamp rules?
Work limits Can a growing participant list make a required operation too large to finish?

On platforms with synchronous external callbacks, reentrancy is a concern when control returns to a contract while an operation is in progress. Checks–Effects–Interactions is a defensive ordering pattern: validate the conditions, update state consistently, then make external calls. Review related operations that share the state as well. Established libraries can help enforce standard controls where they fit the platform, and tests should check how the controls are configured in the actual app.

Worked example. The workshop separates the authority to pause registration from the authority to change capacity. Tests cover the last available seat, a full room, an ordinary participant attempting a restricted operation, and a downstream service failing. A review checks that the reservation record and seat count agree after each outcome.

Practice quiz

Select one answer per question.

Answer key and explanations
  1. B. Confirmed reservations never exceed capacity.
  2. A. Submit an ordinary participant’s request, verify it is refused, and confirm registration state is unchanged.
  3. C. Before handing over control to the external component, with failure handling appropriate to the platform.

Sign in with Discord to take this quiz and earn your badge.