Midnight Blockchain Development - Intermediate › 1. Model robust contract state with sealed fields and state machines › Model robust contract state with sealed fields and state machines
Learning viewShow only the learning panels you need.
No video assigned to this section
The lesson can still contain reading, examples, practical work and a quiz.
Model robust contract state with sealed fields and state machines
1. Model robust contract state with sealed fields and state machines · 0 min · Midnight / Compact
Course path: Midnight Blockchain Development - Intermediate. This chapter is part of a progressive Beginner to Intermediate to Advanced Midnight developer pathway.
Model robust contract state with sealed fields and state machines
Intermediate development begins with stronger state modeling: immutable configuration, explicit states, and controlled transitions.
Learning objectives
Use sealed fields for immutable configuration.
Represent multi-step workflows with explicit states.
Reject illegal transitions in the contract.
Test the state machine independently from the UI.
Core lesson
The Compact security guide distinguishes ordinary ledger fields from sealed fields. A sealed field is useful for configuration that should not change after initialization.
A state machine turns an informal workflow into a finite set of states and allowed transitions. This matters in auctions, approvals, games, escrow, credential issuance, and other multi-step DApps.
The contract, not the front-end, must enforce transitions. A malicious client can call exported circuits directly even if the normal UI hides an action.
State tests should cover every legal path and attempt illegal calls in each state. This is one of the best ways to find missing authorization or lifecycle checks.
Recommended workflow
List every application state.
Define allowed transitions.
Mark configuration that should be sealed.
Encode transition assertions.
Write positive and negative transition tests.
Engineering and security note
State machines should reject unexpected transitions even when the UI never exposes those buttons.
Hands-on goal
Build a three-state approval workflow: DRAFT -> SUBMITTED -> APPROVED, with explicit rejection of invalid transitions.
Compatibility note: The version numbers in this package are a research snapshot from 2026-09-09. Before running commands, compare them with the current Midnight compatibility matrix and release notes.
Practical tasksLocal progress: 0%
MBI1T1 - Practical Task 1
Define the state enum.
MBI1T2 - Practical Task 2
Make one configuration field sealed.
MBI1T3 - Practical Task 3
Add submit and approve circuits.
MBI1T4 - Practical Task 4
Write a test that tries to approve from DRAFT.
Lab note: Confirm the exact enum and sealed-field syntax against the current Compact language reference before compiling.
Practice Lab - Model robust contract state with sealed fields and state machines: Use this editable lab to practise the configuration or code from the lesson. Confirm the exact enum and sealed-field syntax against the current Compact language reference before compiling.