|
| 1 | +# Generating Saved States for Otter Tests |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Saved states allow tests to bootstrap from a previously generated network state rather than starting from genesis. This is useful for migration testing and scenarios where you want to start from a specific network configuration. |
| 6 | + |
| 7 | +## Generating a State |
| 8 | + |
| 9 | +Use the `GenerateStateTool` command-line tool to create a new saved state: |
| 10 | + |
| 11 | +```bash |
| 12 | +./gradlew :consensus-otter-tests:run --args="org.hiero.otter.fixtures.tools.GenerateStateTool" |
| 13 | +``` |
| 14 | + |
| 15 | +This tool: |
| 16 | +1. Creates a 4-node network using a deterministic seed |
| 17 | +2. Runs the network for 10 seconds |
| 18 | +3. Freezes the network to create a consistent snapshot |
| 19 | +4. Cleans up unnecessary files (keeping only the latest round and PCES directory) |
| 20 | +5. Moves the state to `platform-sdk/consensus-otter-tests/saved-states/previous-version-state` |
| 21 | + |
| 22 | +The generated state is a **freeze state**, making it safe for: |
| 23 | +- Migration tests that load a state from a previous software version |
| 24 | +- Tests that make roster changes (node additions/removals) |
| 25 | +- Any test requiring a known starting state with existing rounds |
| 26 | + |
| 27 | +## Using a Saved State in Tests |
| 28 | + |
| 29 | +Reference the saved state directory in your test: |
| 30 | + |
| 31 | +```java |
| 32 | +@OtterTest |
| 33 | +@OtterSpecs(randomNodeIds = false) |
| 34 | +void myTest(@NonNull final TestEnvironment env) { |
| 35 | + final Network network = env.network(); |
| 36 | + |
| 37 | + network.addNodes(numberOfNodes); |
| 38 | + network.savedStateDirectory(Path.of("previous-version-state")); |
| 39 | + network.version(targetVersion); |
| 40 | + network.start(); |
| 41 | + |
| 42 | + // ... test assertions |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +The network automatically handles: |
| 47 | +- Loading the saved state from `saved-states/previous-version-state` |
| 48 | +- Synchronizing FakeTime to the state's WALL_CLOCK_TIME + 1 hour if running in the Turtle environment to ensure time does not go backwards |
| 49 | +- Managing roster changes if the test uses different node counts |
| 50 | + |
| 51 | +## Git Management |
| 52 | + |
| 53 | +The `.gitignore` at the repository root ignores all `.swh` files: |
| 54 | + |
| 55 | +``` |
| 56 | +*.swh |
| 57 | +``` |
| 58 | + |
| 59 | +After generating a new state, **manually `git add` the `.swh` file** to include it in version control: |
| 60 | + |
| 61 | +```bash |
| 62 | +cd platform-sdk/consensus-otter-tests/saved-states/previous-version-state |
| 63 | +git add -f OtterApp/0/hiero/*/SignedState.swh |
| 64 | +``` |
| 65 | + |
| 66 | +The `-f` flag is required because the file matches the gitignore pattern. |
| 67 | + |
| 68 | +## Migration Testing |
| 69 | + |
| 70 | +> TODO: Document migration testing workflow when we have tests that load states generated from previous software versions. Include version handling and any specific considerations for testing state format compatibility. |
0 commit comments