Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 7a30b79

Browse files
authored
Fix/rws limit early stop (#852)
* fix: if rws reaches limit, stop early * upgrade mpt circuit
1 parent d2a241c commit 7a30b79

37 files changed

+183
-159
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bus-mapping/src/circuit_input_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'a> CircuitInputBuilder {
437437
call_id,
438438
CallContextField::TxId,
439439
Word::from(dummy_tx_id as u64),
440-
);
440+
)?;
441441
}
442442

443443
// increase the total rwc by 1
@@ -452,7 +452,7 @@ impl<'a> CircuitInputBuilder {
452452
dummy_tx_id,
453453
withdraw_root_before,
454454
),
455-
);
455+
)?;
456456

457457
let mut push_op = |step: &mut ExecStep, rwc: RWCounter, rw: RW, op: StartOp| {
458458
let op_ref = state.block.container.insert(Operation::new(rwc, rw, op));

bus-mapping/src/circuit_input_builder/input_state_ref.rs

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'a> CircuitInputStateRef<'a> {
124124
/// reference to the stored operation ([`OperationRef`]) inside the
125125
/// bus-mapping instance of the current [`ExecStep`]. Then increase the
126126
/// block_ctx [`RWCounter`](crate::operation::RWCounter) by one.
127-
pub fn push_op<T: Op>(&mut self, step: &mut ExecStep, rw: RW, op: T) {
127+
pub fn push_op<T: Op>(&mut self, step: &mut ExecStep, rw: RW, op: T) -> Result<(), Error> {
128128
if let OpEnum::Account(op) = op.clone().into_enum() {
129129
self.check_update_sdb_account(rw, &op)
130130
}
@@ -133,6 +133,21 @@ impl<'a> CircuitInputStateRef<'a> {
133133
.container
134134
.insert(Operation::new(self.block_ctx.rwc.inc_pre(), rw, op));
135135
step.bus_mapping_instance.push(op_ref);
136+
self.check_rw_num_limit()
137+
}
138+
139+
/// Check whether rws will overflow circuit limit.
140+
pub fn check_rw_num_limit(&self) -> Result<(), Error> {
141+
let max_rws = self.block.circuits_params.max_rws;
142+
if max_rws == 0 {
143+
return Ok(());
144+
}
145+
let rwc = self.block_ctx.rwc.0;
146+
if rwc > max_rws {
147+
log::error!("rwc > max_rws, rwc={}, max_rws={}", rwc, max_rws);
148+
return Err(Error::InternalError("rws not enough"));
149+
};
150+
Ok(())
136151
}
137152

138153
/// Push a read type [`CallContextOp`] into the
@@ -147,14 +162,14 @@ impl<'a> CircuitInputStateRef<'a> {
147162
call_id: usize,
148163
field: CallContextField,
149164
value: Word,
150-
) {
165+
) -> Result<(), Error> {
151166
let op = CallContextOp {
152167
call_id,
153168
field,
154169
value,
155170
};
156171

157-
self.push_op(step, RW::READ, op);
172+
self.push_op(step, RW::READ, op)
158173
}
159174

160175
/// Push a write type [`CallContextOp`] into the
@@ -169,14 +184,14 @@ impl<'a> CircuitInputStateRef<'a> {
169184
call_id: usize,
170185
field: CallContextField,
171186
value: Word,
172-
) {
187+
) -> Result<(), Error> {
173188
let op = CallContextOp {
174189
call_id,
175190
field,
176191
value,
177192
};
178193

179-
self.push_op(step, RW::WRITE, op);
194+
self.push_op(step, RW::WRITE, op)
180195
}
181196

182197
/// Push an [`Operation`](crate::operation::Operation) with reversible to be
@@ -213,7 +228,7 @@ impl<'a> CircuitInputStateRef<'a> {
213228
.push((self.tx.steps().len(), op_ref));
214229
}
215230

216-
Ok(())
231+
self.check_rw_num_limit()
217232
}
218233

219234
/// Push a read type [`MemoryOp`] into the
@@ -231,7 +246,7 @@ impl<'a> CircuitInputStateRef<'a> {
231246
let value = mem.read_word(address);
232247

233248
let call_id = self.call()?.call_id;
234-
self.push_op(step, RW::READ, MemoryOp::new(call_id, address, value));
249+
self.push_op(step, RW::READ, MemoryOp::new(call_id, address, value))?;
235250
Ok(value)
236251
}
237252

@@ -250,7 +265,7 @@ impl<'a> CircuitInputStateRef<'a> {
250265
let value = mem.read_word(address);
251266

252267
let caller_id = self.call()?.caller_id;
253-
self.push_op(step, RW::READ, MemoryOp::new(caller_id, address, value));
268+
self.push_op(step, RW::READ, MemoryOp::new(caller_id, address, value))?;
254269
Ok(value)
255270
}
256271

@@ -279,7 +294,7 @@ impl<'a> CircuitInputStateRef<'a> {
279294
step,
280295
RW::WRITE,
281296
MemoryOp::new_write(call_id, address, value, value_prev),
282-
);
297+
)?;
283298
Ok(value_prev_bytes.to_vec())
284299
}
285300

@@ -307,7 +322,7 @@ impl<'a> CircuitInputStateRef<'a> {
307322
step,
308323
RW::WRITE,
309324
MemoryOp::new_write(call_id, address, value, value_prev),
310-
);
325+
)?;
311326
Ok(value_prev_bytes.to_vec())
312327
}
313328

@@ -324,7 +339,7 @@ impl<'a> CircuitInputStateRef<'a> {
324339
value: Word,
325340
) -> Result<(), Error> {
326341
let call_id = self.call()?.call_id;
327-
self.push_op(step, RW::WRITE, StackOp::new(call_id, address, value));
342+
self.push_op(step, RW::WRITE, StackOp::new(call_id, address, value))?;
328343
Ok(())
329344
}
330345

@@ -341,7 +356,7 @@ impl<'a> CircuitInputStateRef<'a> {
341356
value: Word,
342357
) -> Result<(), Error> {
343358
let call_id = self.call()?.call_id;
344-
self.push_op(step, RW::READ, StackOp::new(call_id, address, value));
359+
self.push_op(step, RW::READ, StackOp::new(call_id, address, value))?;
345360
Ok(())
346361
}
347362

@@ -451,9 +466,9 @@ impl<'a> CircuitInputStateRef<'a> {
451466
address: Address,
452467
field: AccountField,
453468
value: Word,
454-
) {
469+
) -> Result<(), Error> {
455470
let op = AccountOp::new(address, field, value, value);
456-
self.push_op(step, RW::READ, op);
471+
self.push_op(step, RW::READ, op)
457472
}
458473

459474
/// Push a write type [`AccountOp`] into the
@@ -471,8 +486,7 @@ impl<'a> CircuitInputStateRef<'a> {
471486
value_prev: Word,
472487
) -> Result<(), Error> {
473488
let op = AccountOp::new(address, field, value, value_prev);
474-
self.push_op(step, RW::WRITE, op);
475-
Ok(())
489+
self.push_op(step, RW::WRITE, op)
476490
}
477491

478492
/// Push a write type [`TxLogOp`] into the
@@ -494,8 +508,7 @@ impl<'a> CircuitInputStateRef<'a> {
494508
step,
495509
RW::WRITE,
496510
TxLogOp::new(tx_id, log_id, field, index, value),
497-
);
498-
Ok(())
511+
)
499512
}
500513

501514
/// Push a read type [`TxReceiptOp`] into the
@@ -519,8 +532,7 @@ impl<'a> CircuitInputStateRef<'a> {
519532
field,
520533
value,
521534
},
522-
);
523-
Ok(())
535+
)
524536
}
525537

526538
/// Push a write type [`TxReceiptOp`] into the
@@ -544,8 +556,7 @@ impl<'a> CircuitInputStateRef<'a> {
544556
field,
545557
value,
546558
},
547-
);
548-
Ok(())
559+
)
549560
}
550561

551562
/// Add address to access list for the current transaction.
@@ -589,8 +600,7 @@ impl<'a> CircuitInputStateRef<'a> {
589600
is_warm,
590601
is_warm_prev,
591602
},
592-
);
593-
Ok(())
603+
)
594604
}
595605

596606
/// Push 2 reversible [`AccountOp`] to update `sender` and `receiver`'s
@@ -648,7 +658,7 @@ impl<'a> CircuitInputStateRef<'a> {
648658
value: sender_balance,
649659
value_prev: sender_balance_prev,
650660
},
651-
);
661+
)?;
652662
sender_balance_prev = sender_balance;
653663
}
654664
let sender_balance = sender_balance_prev - value;
@@ -692,7 +702,7 @@ impl<'a> CircuitInputStateRef<'a> {
692702
} else {
693703
CodeDB::empty_code_hash().to_word()
694704
};
695-
self.account_read(step, receiver, AccountField::CodeHash, prev_code_hash);
705+
self.account_read(step, receiver, AccountField::CodeHash, prev_code_hash)?;
696706
let write_op = AccountOp::new(
697707
receiver,
698708
AccountField::CodeHash,
@@ -702,7 +712,7 @@ impl<'a> CircuitInputStateRef<'a> {
702712
if reversible {
703713
self.push_op_reversible(step, write_op)?;
704714
} else {
705-
self.push_op(step, RW::WRITE, write_op);
715+
self.push_op(step, RW::WRITE, write_op)?;
706716
}
707717
#[cfg(feature = "scroll")]
708718
{
@@ -716,7 +726,7 @@ impl<'a> CircuitInputStateRef<'a> {
716726
receiver,
717727
AccountField::KeccakCodeHash,
718728
prev_keccak_code_hash,
719-
);
729+
)?;
720730
let write_op = AccountOp::new(
721731
receiver,
722732
AccountField::KeccakCodeHash,
@@ -726,7 +736,7 @@ impl<'a> CircuitInputStateRef<'a> {
726736
if reversible {
727737
self.push_op_reversible(step, write_op)?;
728738
} else {
729-
self.push_op(step, RW::WRITE, write_op);
739+
self.push_op(step, RW::WRITE, write_op)?;
730740
}
731741
// TODO: set code size to 0?
732742
}
@@ -754,7 +764,7 @@ impl<'a> CircuitInputStateRef<'a> {
754764
if reversible {
755765
self.push_op_reversible(step, write_op)?;
756766
} else {
757-
self.push_op(step, RW::WRITE, write_op);
767+
self.push_op(step, RW::WRITE, write_op)?;
758768
}
759769

760770
Ok(())
@@ -903,16 +913,21 @@ impl<'a> CircuitInputStateRef<'a> {
903913
Ok(address)
904914
}
905915

906-
pub(crate) fn reversion_info_read(&mut self, step: &mut ExecStep, call: &Call) {
916+
pub(crate) fn reversion_info_read(
917+
&mut self,
918+
step: &mut ExecStep,
919+
call: &Call,
920+
) -> Result<(), Error> {
907921
for (field, value) in [
908922
(
909923
CallContextField::RwCounterEndOfReversion,
910924
call.rw_counter_end_of_reversion.to_word(),
911925
),
912926
(CallContextField::IsPersistent, call.is_persistent.to_word()),
913927
] {
914-
self.call_context_read(step, call.call_id, field, value);
928+
self.call_context_read(step, call.call_id, field, value)?;
915929
}
930+
Ok(())
916931
}
917932

918933
/// Check if address is a precompiled or not.
@@ -1318,7 +1333,7 @@ impl<'a> CircuitInputStateRef<'a> {
13181333
call.call_id,
13191334
CallContextField::IsSuccess,
13201335
0u64.into(),
1321-
);
1336+
)?;
13221337

13231338
// Even call.rw_counter_end_of_reversion is zero for now, it will set in
13241339
// set_value_ops_call_context_rwc_eor later
@@ -1329,7 +1344,7 @@ impl<'a> CircuitInputStateRef<'a> {
13291344
call.call_id,
13301345
CallContextField::RwCounterEndOfReversion,
13311346
call.rw_counter_end_of_reversion.into(),
1332-
);
1347+
)?;
13331348

13341349
if call.is_root {
13351350
return Ok(());
@@ -1344,7 +1359,7 @@ impl<'a> CircuitInputStateRef<'a> {
13441359
call.call_id,
13451360
CallContextField::CallerId,
13461361
caller.call_id.into(),
1347-
);
1362+
)?;
13481363

13491364
let (last_callee_return_data_offset, last_callee_return_data_length) =
13501365
Self::get_return_data_offset_and_len(exec_step, geth_step, self.caller_ctx()?)?;
@@ -1407,7 +1422,7 @@ impl<'a> CircuitInputStateRef<'a> {
14071422
self.caller_ctx()?.reversible_write_counter.into(),
14081423
),
14091424
] {
1410-
self.call_context_read(exec_step, caller.call_id, field, value);
1425+
self.call_context_read(exec_step, caller.call_id, field, value)?;
14111426
}
14121427

14131428
// EIP-211: CREATE/CREATE2 call successful case should set RETURNDATASIZE = 0
@@ -1431,7 +1446,7 @@ impl<'a> CircuitInputStateRef<'a> {
14311446
},
14321447
),
14331448
] {
1434-
self.call_context_write(exec_step, caller.call_id, field, value);
1449+
self.call_context_write(exec_step, caller.call_id, field, value)?;
14351450
}
14361451

14371452
Ok(())
@@ -2035,7 +2050,7 @@ impl<'a> CircuitInputStateRef<'a> {
20352050
src_chunk_index.into(),
20362051
Word::from_big_endian(read_chunk),
20372052
),
2038-
);
2053+
)?;
20392054
trace!("read chunk: {last_callee_id} {src_chunk_index} {read_chunk:?}");
20402055
src_chunk_index += 32;
20412056

0 commit comments

Comments
 (0)