Skip to content

Commit 4c1a405

Browse files
committed
client_transition: test new state
1 parent 7b3c1c9 commit 4c1a405

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

crates/consensus-logic/src/client_transition.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,29 @@ mod tests {
157157
)
158158
}
159159

160+
fn gen_l1_chain(len: usize) -> Vec<L1BlockManifest> {
161+
let mut blocks = vec![];
162+
for _ in 0..len {
163+
let block: L1BlockManifest = ArbitraryGenerator::new().generate();
164+
blocks.push(block);
165+
}
166+
blocks
167+
}
168+
160169
#[test]
161170
fn handle_l1_block() {
162171
let database = get_common_db();
163172
let params = get_params();
164173
let mut state = gen_client_state(&params);
165-
let l1_block_id = L1BlockId::from(Buf32::default());
174+
175+
assert!(!state.is_chain_active());
176+
let l1_chain = gen_l1_chain(15);
166177

167178
// before the genesis of L2 is reached
168179
{
169-
let event = SyncEvent::L1Block(1, l1_block_id);
180+
let height = 1;
181+
let l1_block_id = l1_chain[height as usize].block_hash().into();
182+
let event = SyncEvent::L1Block(height, l1_block_id);
170183

171184
let output = process_event(&state, &event, database.as_ref(), &params).unwrap();
172185

@@ -180,33 +193,45 @@ mod tests {
180193
assert_eq!(actions, expected_actions);
181194

182195
operation::apply_writes_to_state(&mut state, writes.iter().cloned());
196+
197+
assert!(!state.is_chain_active());
198+
assert_eq!(state.recent_l1_block(), Some(&l1_block_id));
199+
assert_eq!(state.buried_l1_height(), params.rollup.genesis_l1_height);
200+
assert_eq!(state.l1_view().local_unaccepted_blocks(), [l1_block_id]);
183201
}
184202

185203
// after the genesis of L2 is reached
186204
{
187205
let height = params.rollup.genesis_l1_height + 3;
206+
let l1_block_id = l1_chain[height as usize].block_hash().into();
188207
let event = SyncEvent::L1Block(height, l1_block_id);
189208

190209
let output = process_event(&state, &event, database.as_ref(), &params).unwrap();
191210

192-
let writes = output.writes();
193-
let actions = output.actions();
194-
195211
let expection_writes = [
196212
ClientStateWrite::AcceptL1Block(l1_block_id),
197213
ClientStateWrite::ActivateChain,
198214
];
199215
let expected_actions = [];
200216

201-
assert_eq!(writes, expection_writes);
202-
assert_eq!(actions, expected_actions);
217+
assert_eq!(output.writes(), expection_writes);
218+
assert_eq!(output.actions(), expected_actions);
203219

204-
operation::apply_writes_to_state(&mut state, writes.iter().cloned());
220+
operation::apply_writes_to_state(&mut state, output.writes().iter().cloned());
221+
222+
assert!(state.is_chain_active());
223+
assert_eq!(state.recent_l1_block(), Some(&l1_block_id));
224+
assert_eq!(state.buried_l1_height(), params.rollup.genesis_l1_height);
225+
assert_eq!(
226+
state.l1_view().local_unaccepted_blocks(),
227+
[l1_chain[1].block_hash().into(), l1_block_id,]
228+
);
205229
}
206230

207231
// after l1_reorg_depth is reached
208232
{
209233
let height = params.rollup.genesis_l1_height + params.rollup.l1_reorg_safe_depth as u64;
234+
let l1_block_id = l1_chain[height as usize].block_hash().into();
210235
let event = SyncEvent::L1Block(height, l1_block_id);
211236

212237
let output = process_event(&state, &event, database.as_ref(), &params).unwrap();
@@ -221,6 +246,17 @@ mod tests {
221246
assert_eq!(output.actions(), expected_actions);
222247

223248
operation::apply_writes_to_state(&mut state, output.writes().iter().cloned());
249+
250+
assert!(state.is_chain_active());
251+
assert_eq!(state.recent_l1_block(), Some(&l1_block_id));
252+
assert_eq!(
253+
state.buried_l1_height(),
254+
params.rollup.genesis_l1_height + 1
255+
);
256+
assert_eq!(
257+
state.l1_view().local_unaccepted_blocks(),
258+
[l1_chain[8].block_hash().into(), l1_block_id,]
259+
);
224260
}
225261
}
226262

0 commit comments

Comments
 (0)