Skip to content

Commit 9b54bfc

Browse files
committed
add docs
1 parent 37f4a57 commit 9b54bfc

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

crates/batcher/src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,18 @@ pub struct Batcher {
9898
aggregator_fee_percentage_multiplier: u128,
9999
aggregator_gas_cost: u128,
100100

101-
// Shared state (Mutex)
102-
/// The general business rule is:
103-
/// - User processing can be done in parallel unless a batch creation is happening
104-
/// - Batch creation needs to be able to change all the states, so all processing
105-
/// needs to be stopped, and all user_states locks need to be taken
101+
// Shared state access:
102+
// Two kinds of threads interact with the shared state:
103+
// 1. User message processing threads (run in parallel)
104+
// 2. Batch creation thread (runs sequentially, includes failure recovery)
105+
//
106+
// Locking rules:
107+
// - To avoid deadlocks, always acquire `user_states` before `batch_state`.
108+
// - During failure recovery, restoring a valid state may require breaking this rule:
109+
// additional user locks might be acquired *after* the batch lock.
110+
// (See the `restore` algorithm in the `batch_queue` module.)
111+
//
112+
// Because of this exception, user message handling uses lock acquisition with timeouts.
106113
batch_state: Mutex<BatchState>,
107114

108115
user_states: Arc<RwLock<HashMap<Address, Arc<Mutex<UserState>>>>>,

0 commit comments

Comments
 (0)