Documentation Index
Fetch the complete documentation index at: https://cowswap-mintlify-docs-audit-1775035615.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Registry and State Management
The Registry serves as the core state management system for the Watch Tower, maintaining a persistent record of all active programmatic orders, their owners, and associated metadata across restarts.What the Registry Stores
The Registry maintains several critical pieces of information:Owner and Order Mapping
The primary data structure maps owners to their programmatic orders:- Efficient lookup of all orders for a given owner
- Quick iteration over all owners and their orders
- Easy removal of orders when they expire or are cancelled
Block Tracking
The registry tracks the last processed block to ensure no events are missed:Storing the block hash allows the Watch Tower to detect blockchain reorganizations and handle them appropriately.
Error Tracking
The registry maintains a timestamp of the last error notification:Poll Results
Each programmatic order stores its most recent poll result:Registry Class Structure
Persistent Storage
The Watch Tower uses LevelDB for persistent storage, providing:- ACID guarantees: All writes are atomic
- Key-value simplicity: Fast lookups and updates
- Batch operations: Multiple writes in a single transaction
Database Schema
The following keys are stored in the database: CONDITIONAL_ORDER_REGISTRY Stores the serialized registry of all programmatic orders:Each key is prefixed with the network name to support multiple chains in the same database.
Loading and Saving
Loading the Registry
When the Watch Tower starts, it loads the registry from storage:- Retrieves the serialized owner orders from the database
- Checks the registry version and applies migrations if needed
- Loads the last processed block and error notification time
- Updates metrics with the current state
Writing the Registry
The registry is persisted after processing events or orders:Batch writes ensure that all registry data is updated atomically. If any part fails, the entire write is rolled back.
Order Tracking Lifecycle
Adding Orders
When a new programmatic order is detected:- Check if the owner already exists in the registry
- If the owner exists, add the order to their set (if not duplicate)
- If the owner is new, create a new entry with the order
- Update metrics for active orders and owners
Tracking Order State
As orders are polled and submitted:- Poll results are stored on the order
- Discrete order UIDs are mapped to their status (SUBMITTED or FILLED)
- The last execution timestamp and block number are recorded
Removing Orders
Orders are removed from the registry when:- They return
DONT_TRY_AGAINfrom polling - They expire based on their validity period
- They are explicitly cancelled
- A new merkle root is set for the owner (previous orders are flushed)
Order Expiration and Cleanup
The Watch Tower implements several cleanup mechanisms:Automatic Cleanup During Polling
While processing orders each block:Owner Cleanup
After processing all orders, owners with no remaining orders are removed:Merkle Root Updates
When a new merkle root is set, old orders are flushed:This cleanup ensures that only the latest set of orders from a merkle tree are tracked, preventing conflicts.
Serialization
The registry uses custom serialization to handle Map and Set objects:Metrics and Monitoring
The registry exposes metrics for monitoring:activeOwnersTotal: Current number of owners with active ordersactiveOrdersTotal: Current number of active programmatic orders