Skip to content

Commit 26338f3

Browse files
committed
starknet_committer: extract commit plan to commit_or_load
1 parent de52d79 commit 26338f3

1 file changed

Lines changed: 60 additions & 40 deletions

File tree

crates/apollo_committer/src/committer.rs

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ struct CommitStateDiffOutput {
9191
pub global_root: GlobalRoot,
9292
}
9393

94+
enum CommitBlockHeightPlan {
95+
Historical { global_root: GlobalRoot },
96+
CommitTip { state_diff_commitment: StateDiffCommitment },
97+
}
98+
9499
/// Apollo committer. Maintains the Starknet state tries in persistent storage.
95100
pub struct Committer<S: Storage, ForestDB>
96101
where
@@ -122,39 +127,15 @@ where
122127
COMMITTER_OFFSET.set_lossy(offset.0);
123128
}
124129

125-
/// Commits a block to the forest.
126-
/// In the happy flow, the given height equals to the committer offset.
127-
pub async fn commit_block(
128-
&mut self,
129-
CommitBlockRequest { state_diff, state_diff_commitment, height }: CommitBlockRequest,
130-
) -> CommitterResult<CommitBlockResponse> {
131-
let result = self
132-
.commit_block_inner(CommitBlockRequest { state_diff, state_diff_commitment, height })
133-
.await;
134-
match &result {
135-
Ok(_) => {
136-
debug!("Committed block number {height} with state diff {state_diff_commitment:?}");
137-
}
138-
Err(err) => {
139-
error!("Failed to commit block number {height}: {err:?}");
140-
}
141-
};
142-
result
143-
}
144-
145-
/// Commits a block to the forest.
146-
/// In the happy flow, the given height equals to the committer offset.
147-
async fn commit_block_inner(
130+
/// Either load the committed global root for a past `height`, or validate inputs and return
131+
/// the state-diff commitment for committing the chain tip at `self.offset`.
132+
async fn commit_or_load(
148133
&mut self,
149-
CommitBlockRequest { state_diff, state_diff_commitment, height }: CommitBlockRequest,
150-
) -> CommitterResult<CommitBlockResponse> {
151-
info!(
152-
"Received request to commit block number {height} with state diff \
153-
{state_diff_commitment:?}"
154-
);
134+
state_diff: &ThinStateDiff,
135+
state_diff_commitment: Option<StateDiffCommitment>,
136+
height: BlockNumber,
137+
) -> CommitterResult<CommitBlockHeightPlan> {
155138
if height > self.offset {
156-
// Request to commit a future height.
157-
// Returns an error, indicating the committer has a hole in the state diff series.
158139
return Err(CommitterError::CommitHeightHole {
159140
input_height: height,
160141
committer_offset: self.offset,
@@ -164,7 +145,7 @@ where
164145
let state_diff_commitment = match state_diff_commitment {
165146
Some(commitment) => {
166147
if self.config.verify_state_diff_hash {
167-
let calculated_commitment = calculate_state_diff_hash(&state_diff);
148+
let calculated_commitment = calculate_state_diff_hash(state_diff);
168149
if commitment != calculated_commitment {
169150
return Err(CommitterError::StateDiffHashMismatch {
170151
provided_commitment: commitment,
@@ -175,32 +156,69 @@ where
175156
}
176157
commitment
177158
}
178-
None => calculate_state_diff_hash(&state_diff),
159+
None => calculate_state_diff_hash(state_diff),
179160
};
161+
180162
if height < self.offset {
181-
// Request to commit an old height.
182-
// Might be ok if the caller didn't get the results properly.
183163
warn!(
184164
"Received request to commit an old block number {height}. The committer offset is \
185165
{0}.",
186166
self.offset
187167
);
188168
let stored_state_diff_commitment = self.load_state_diff_commitment(height).await?;
189-
// Verify the input state diff matches the stored one by comparing the commitments.
190169
if state_diff_commitment != stored_state_diff_commitment {
191170
return Err(CommitterError::InvalidStateDiffCommitment {
192171
input_commitment: state_diff_commitment,
193172
stored_commitment: stored_state_diff_commitment,
194173
height,
195174
});
196175
}
197-
// Returns the precomputed global root.
198176
let db_global_root = self.load_global_root(height).await?;
199-
return Ok(CommitBlockResponse { global_root: db_global_root });
177+
return Ok(CommitBlockHeightPlan::Historical { global_root: db_global_root });
200178
}
201179

202-
// Happy flow. Commits the state diff and returns the computed global root.
203-
debug!("Committing block number {height} with state diff {state_diff_commitment:?}");
180+
Ok(CommitBlockHeightPlan::CommitTip { state_diff_commitment })
181+
}
182+
183+
/// Commits a block to the forest.
184+
/// In the happy flow, the given height equals to the committer offset.
185+
pub async fn commit_block(
186+
&mut self,
187+
CommitBlockRequest { state_diff, state_diff_commitment, height }: CommitBlockRequest,
188+
) -> CommitterResult<CommitBlockResponse> {
189+
let result = self
190+
.commit_block_inner(CommitBlockRequest { state_diff, state_diff_commitment, height })
191+
.await;
192+
match &result {
193+
Ok(_) => {
194+
debug!("Committed block number {height} with state diff {state_diff_commitment:?}");
195+
}
196+
Err(err) => {
197+
error!("Failed to commit block number {height}: {err:?}");
198+
}
199+
};
200+
result
201+
}
202+
203+
/// Commits a block to the forest.
204+
/// In the happy flow, the given height equals to the committer offset.
205+
async fn commit_block_inner(
206+
&mut self,
207+
CommitBlockRequest { state_diff, state_diff_commitment, height }: CommitBlockRequest,
208+
) -> CommitterResult<CommitBlockResponse> {
209+
info!(
210+
"Received request to commit block number {height} with state diff \
211+
{state_diff_commitment:?}"
212+
);
213+
214+
match self.commit_or_load(&state_diff, state_diff_commitment, height).await? {
215+
CommitBlockHeightPlan::Historical { global_root } => {
216+
Ok(CommitBlockResponse { global_root })
217+
}
218+
CommitBlockHeightPlan::CommitTip { state_diff_commitment } => {
219+
debug!(
220+
"Committing block number {height} with state diff {state_diff_commitment:?}"
221+
);
204222
let mut block_measurements = SingleBlockMeasurements::default();
205223
block_measurements.start_measurement(Action::EndToEnd);
206224
let CommitStateDiffOutput { filled_forest, global_root, deleted_nodes } =
@@ -236,6 +254,8 @@ where
236254
update_metrics(height, &block_measurements.block_measurement);
237255
self.update_offset(next_offset);
238256
Ok(CommitBlockResponse { global_root })
257+
}
258+
}
239259
}
240260

241261
/// Applies the given state diff to revert the changes of the given height.

0 commit comments

Comments
 (0)