Skip to content

Commit b8f655a

Browse files
authored
Merge pull request #4782 from xxuejie/remove-transaction-snapshot
refactor: Remove TransactionSnapshot structure
2 parents 9f48908 + 16a0b42 commit b8f655a

File tree

10 files changed

+34
-94
lines changed

10 files changed

+34
-94
lines changed

script/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use crate::error::{ScriptError, TransactionScriptError};
1212
pub use crate::scheduler::{Scheduler, ROOT_VM_ID};
1313
pub use crate::types::{
1414
ChunkCommand, CoreMachine, DataPieceId, RunMode, ScriptGroup, ScriptGroupType, ScriptVersion,
15-
TransactionSnapshot, TransactionState, TxData, VerifyResult, VmIsa, VmState, VmVersion,
15+
TransactionState, TxData, VerifyResult, VmIsa, VmState, VmVersion,
1616
};
1717
pub use crate::verify::{TransactionScriptsSyscallsGenerator, TransactionScriptsVerifier};
1818
pub use crate::verify_env::TxVerifyEnv;

script/src/types.rs

+1-60
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use ckb_error::Error;
21
use ckb_types::{
32
core::{Cycle, ScriptHashType},
43
packed::{Byte32, Script},
@@ -186,20 +185,7 @@ impl fmt::Display for ScriptGroupType {
186185
}
187186

188187
/// Struct specifies which script has verified so far.
189-
/// Snapshot is lifetime free, but capture snapshot need heavy memory copy
190-
pub struct TransactionSnapshot {
191-
/// current suspended script index
192-
pub current: usize,
193-
/// vm snapshots
194-
pub state: Option<FullSuspendedState>,
195-
/// current consumed cycle
196-
pub current_cycles: Cycle,
197-
/// limit cycles when snapshot create
198-
pub limit_cycles: Cycle,
199-
}
200-
201-
/// Struct specifies which script has verified so far.
202-
/// State lifetime bound with vm machine.
188+
/// State is lifetime free, but capture snapshot need heavy memory copy
203189
pub struct TransactionState {
204190
/// current suspended script index
205191
pub current: usize,
@@ -240,41 +226,6 @@ impl TransactionState {
240226
}
241227
}
242228

243-
impl TransactionSnapshot {
244-
/// Return next limit cycles according to max_cycles and step_cycles
245-
pub fn next_limit_cycles(&self, step_cycles: Cycle, max_cycles: Cycle) -> (Cycle, bool) {
246-
let remain = max_cycles - self.current_cycles;
247-
let next_limit = self.limit_cycles + step_cycles;
248-
249-
if next_limit < remain {
250-
(next_limit, false)
251-
} else {
252-
(remain, true)
253-
}
254-
}
255-
}
256-
257-
impl TryFrom<TransactionState> for TransactionSnapshot {
258-
type Error = Error;
259-
260-
fn try_from(state: TransactionState) -> Result<Self, Self::Error> {
261-
let TransactionState {
262-
current,
263-
state,
264-
current_cycles,
265-
limit_cycles,
266-
..
267-
} = state;
268-
269-
Ok(TransactionSnapshot {
270-
current,
271-
state,
272-
current_cycles,
273-
limit_cycles,
274-
})
275-
}
276-
}
277-
278229
/// Enum represent resumable verify result
279230
#[allow(clippy::large_enum_variant)]
280231
#[derive(Debug)]
@@ -285,16 +236,6 @@ pub enum VerifyResult {
285236
Suspended(TransactionState),
286237
}
287238

288-
impl std::fmt::Debug for TransactionSnapshot {
289-
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> std::fmt::Result {
290-
f.debug_struct("TransactionSnapshot")
291-
.field("current", &self.current)
292-
.field("current_cycles", &self.current_cycles)
293-
.field("limit_cycles", &self.limit_cycles)
294-
.finish()
295-
}
296-
}
297-
298239
impl std::fmt::Debug for TransactionState {
299240
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> std::fmt::Result {
300241
f.debug_struct("TransactionState")

script/src/verify.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
type_id::TypeIdSystemScript,
1616
types::{
1717
CoreMachine, DebugPrinter, Indices, ScriptGroup, ScriptGroupType, ScriptVersion,
18-
TransactionSnapshot, TransactionState, VerifyResult,
18+
TransactionState, VerifyResult,
1919
},
2020
verify_env::TxVerifyEnv,
2121
};
@@ -730,7 +730,7 @@ where
730730
///
731731
/// ## Params
732732
///
733-
/// * `snap` - Captured transaction verification snapshot.
733+
/// * `snap` - Captured transaction verification state.
734734
///
735735
/// * `limit_cycles` - Maximum allowed cycles to run the scripts. The verification quits early
736736
/// when the consumed cycles exceed the limit.
@@ -741,7 +741,7 @@ where
741741
/// If verify is suspended, a borrowed state will returned.
742742
pub fn resume_from_snap(
743743
&self,
744-
snap: &TransactionSnapshot,
744+
snap: &TransactionState,
745745
limit_cycles: Cycle,
746746
) -> Result<VerifyResult, Error> {
747747
let mut cycles = snap.current_cycles;
@@ -881,15 +881,15 @@ where
881881
///
882882
/// ## Params
883883
///
884-
/// * `snap` - Captured transaction verification snapshot.
884+
/// * `snap` - Captured transaction verification state.
885885
///
886886
/// * `max_cycles` - Maximum allowed cycles to run the scripts. The verification quits early
887887
/// when the consumed cycles exceed the limit.
888888
///
889889
/// ## Returns
890890
///
891891
/// It returns the total consumed cycles on completed, Otherwise it returns the verification error.
892-
pub fn complete(&self, snap: &TransactionSnapshot, max_cycles: Cycle) -> Result<Cycle, Error> {
892+
pub fn complete(&self, snap: &TransactionState, max_cycles: Cycle) -> Result<Cycle, Error> {
893893
let mut cycles = snap.current_cycles;
894894

895895
let (_hash, current_group) = self.groups().nth(snap.current).ok_or_else(|| {

script/src/verify/tests/ckb_latest/features_since_v2021.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,11 @@ fn _check_typical_secp256k1_blake160_2_in_2_out_tx_with_snap(step_cycles: Cycle)
894894

895895
let result = verifier.verify_map(script_version, &rtx, |verifier| {
896896
#[allow(unused_assignments)]
897-
let mut init_snap: Option<TransactionSnapshot> = None;
897+
let mut init_snap: Option<TransactionState> = None;
898898
let mut init_state: Option<TransactionState> = None;
899899

900900
match verifier.resumable_verify(step_cycles).unwrap() {
901-
VerifyResult::Suspended(state) => init_snap = Some(state.try_into().unwrap()),
901+
VerifyResult::Suspended(state) => init_snap = Some(state),
902902
VerifyResult::Completed(cycle) => return Ok(cycle),
903903
}
904904

@@ -911,7 +911,7 @@ fn _check_typical_secp256k1_blake160_2_in_2_out_tx_with_snap(step_cycles: Cycle)
911911
match verifier.resume_from_snap(&snap, limit_cycles).unwrap() {
912912
VerifyResult::Suspended(state) => {
913913
if count % 500 == 0 {
914-
init_snap = Some(state.try_into().unwrap());
914+
init_snap = Some(state);
915915
} else {
916916
init_state = Some(state);
917917
}
@@ -928,7 +928,7 @@ fn _check_typical_secp256k1_blake160_2_in_2_out_tx_with_snap(step_cycles: Cycle)
928928
match verifier.resume_from_state(state, limit_cycles).unwrap() {
929929
VerifyResult::Suspended(state) => {
930930
if count % 500 == 0 {
931-
init_snap = Some(state.try_into().unwrap());
931+
init_snap = Some(state);
932932
} else {
933933
init_state = Some(state);
934934
}
@@ -983,21 +983,21 @@ fn check_typical_secp256k1_blake160_2_in_2_out_tx_with_complete() {
983983
let mut cycles = 0;
984984
let verifier = TransactionScriptsVerifierWithEnv::new();
985985
let result = verifier.verify_map(script_version, &rtx, |verifier| {
986-
let mut init_snap: Option<TransactionSnapshot> = None;
986+
let mut init_snap: Option<TransactionState> = None;
987987

988988
if let VerifyResult::Suspended(state) = verifier
989989
.resumable_verify(TWO_IN_TWO_OUT_CYCLES / 10)
990990
.unwrap()
991991
{
992-
init_snap = Some(state.try_into().unwrap());
992+
init_snap = Some(state);
993993
}
994994

995995
for _ in 0..2 {
996996
let snap = init_snap.take().unwrap();
997997
let (limit_cycles, _last) =
998998
snap.next_limit_cycles(TWO_IN_TWO_OUT_CYCLES / 10, TWO_IN_TWO_OUT_CYCLES);
999999
match verifier.resume_from_snap(&snap, limit_cycles).unwrap() {
1000-
VerifyResult::Suspended(state) => init_snap = Some(state.try_into().unwrap()),
1000+
VerifyResult::Suspended(state) => init_snap = Some(state),
10011001
VerifyResult::Completed(_) => {
10021002
unreachable!()
10031003
}
@@ -1133,10 +1133,10 @@ fn load_code_with_snapshot() {
11331133
let max_cycles = Cycle::MAX;
11341134
let verifier = TransactionScriptsVerifierWithEnv::new();
11351135
let result = verifier.verify_map(script_version, &rtx, |verifier| {
1136-
let mut init_snap: Option<TransactionSnapshot> = None;
1136+
let mut init_snap: Option<TransactionState> = None;
11371137

11381138
if let VerifyResult::Suspended(state) = verifier.resumable_verify(max_cycles).unwrap() {
1139-
init_snap = Some(state.try_into().unwrap());
1139+
init_snap = Some(state);
11401140
}
11411141

11421142
let snap = init_snap.take().unwrap();
@@ -1232,10 +1232,10 @@ fn load_code_with_snapshot_more_times() {
12321232
let verifier = TransactionScriptsVerifierWithEnv::new();
12331233

12341234
verifier.verify_map(script_version, &rtx, |verifier| {
1235-
let mut init_snap: Option<TransactionSnapshot> = None;
1235+
let mut init_snap: Option<TransactionState> = None;
12361236

12371237
if let VerifyResult::Suspended(state) = verifier.resumable_verify(max_cycles).unwrap() {
1238-
init_snap = Some(state.try_into().unwrap());
1238+
init_snap = Some(state);
12391239
}
12401240

12411241
loop {
@@ -1244,7 +1244,7 @@ fn load_code_with_snapshot_more_times() {
12441244

12451245
match result.unwrap() {
12461246
VerifyResult::Suspended(state) => {
1247-
init_snap = Some(state.try_into().unwrap());
1247+
init_snap = Some(state);
12481248
}
12491249
VerifyResult::Completed(cycle) => {
12501250
cycles = cycle;

script/src/verify/tests/ckb_latest/features_since_v2023.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,7 @@ fn check_spawn_state() {
597597

598598
loop {
599599
times += 1;
600-
let state: TransactionSnapshot =
601-
init_state.take().unwrap().try_into().expect("no snapshot");
600+
let state: TransactionState = init_state.take().unwrap();
602601
match verifier.resume_from_snap(&state, max_cycles).unwrap() {
603602
VerifyResult::Suspended(state) => {
604603
init_state = Some(state);

script/src/verify/tests/utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use ckb_types::{
2929
};
3030
use faster_hex::hex_encode;
3131
use std::sync::Arc;
32-
use std::{convert::TryInto as _, fs::File, path::Path};
32+
use std::{fs::File, path::Path};
3333
use tempfile::TempDir;
3434

3535
use crate::verify::*;
@@ -236,7 +236,7 @@ impl TransactionScriptsVerifierWithEnv {
236236
times += 1;
237237

238238
let mut init_snap = match verifier.resumable_verify(max_cycles).unwrap() {
239-
VerifyResult::Suspended(state) => Some(state.try_into().unwrap()),
239+
VerifyResult::Suspended(state) => Some(state),
240240
VerifyResult::Completed(cycle) => {
241241
cycles = cycle;
242242
return Ok((cycles, times));
@@ -248,7 +248,7 @@ impl TransactionScriptsVerifierWithEnv {
248248
let snap = init_snap.take().unwrap();
249249
match verifier.resume_from_snap(&snap, max_cycles) {
250250
Ok(VerifyResult::Suspended(state)) => {
251-
init_snap = Some(state.try_into().unwrap());
251+
init_snap = Some(state);
252252
}
253253
Ok(VerifyResult::Completed(cycle)) => {
254254
cycles = cycle;

tx-pool/src/chunk_process.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ckb_verification::cache::TxVerificationCache;
1616
use ckb_verification::{
1717
cache::{CacheEntry, Completed},
1818
ContextualWithoutScriptTransactionVerifier, DaoScriptSizeVerifier, ScriptError, ScriptVerifier,
19-
ScriptVerifyResult, ScriptVerifyState, TimeRelativeTransactionVerifier, TransactionSnapshot,
19+
ScriptVerifyResult, ScriptVerifyState, TimeRelativeTransactionVerifier, TransactionState,
2020
TxVerifyEnv,
2121
};
2222
use std::sync::Arc;
@@ -37,7 +37,7 @@ pub(crate) enum ChunkCommand {
3737

3838
enum State {
3939
Stopped,
40-
Suspended(Arc<TransactionSnapshot>),
40+
Suspended(Arc<TransactionState>),
4141
Completed(Cycle),
4242
}
4343

@@ -133,7 +133,7 @@ impl ChunkProcess {
133133
&mut self,
134134
rtx: Arc<ResolvedTransaction>,
135135
data_loader: DL,
136-
mut init_snap: Option<Arc<TransactionSnapshot>>,
136+
mut init_snap: Option<Arc<TransactionState>>,
137137
max_cycles: Cycle,
138138
consensus: Arc<Consensus>,
139139
tx_env: Arc<TxVerifyEnv>,

verification/src/cache.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! TX verification cache
22
3-
use ckb_script::TransactionSnapshot;
3+
use ckb_script::TransactionState;
44
use ckb_types::{
55
core::{Capacity, Cycle, EntryCompleted},
66
packed::Byte32,
@@ -25,8 +25,8 @@ pub type CacheEntry = Completed;
2525
pub struct Suspended {
2626
/// Cached tx fee
2727
pub fee: Capacity,
28-
/// Snapshot
29-
pub snap: Arc<TransactionSnapshot>,
28+
/// State
29+
pub state: Arc<TransactionState>,
3030
}
3131

3232
/// Completed entry

verification/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub use crate::transaction_verifier::{
2626
TimeRelativeTransactionVerifier,
2727
};
2828
pub use ckb_script::{
29-
ScriptError, ScriptGroupType, TransactionSnapshot, TransactionState as ScriptVerifyState,
30-
TxVerifyEnv, VerifyResult as ScriptVerifyResult,
29+
ScriptError, ScriptGroupType, TransactionState as ScriptVerifyState, TxVerifyEnv,
30+
VerifyResult as ScriptVerifyResult,
3131
};
3232

3333
/// Maximum amount of time that a block timestamp is allowed to exceed the

verification/src/transaction_verifier.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ckb_dao_utils::DaoError;
77
use ckb_error::Error;
88
#[cfg(not(target_family = "wasm"))]
99
use ckb_script::ChunkCommand;
10-
use ckb_script::{TransactionScriptsVerifier, TransactionSnapshot};
10+
use ckb_script::{TransactionScriptsVerifier, TransactionState};
1111
use ckb_traits::{
1212
CellDataProvider, EpochProvider, ExtensionProvider, HeaderFieldsProvider, HeaderProvider,
1313
};
@@ -201,15 +201,15 @@ where
201201
&self,
202202
max_cycles: Cycle,
203203
skip_script_verify: bool,
204-
snapshot: &TransactionSnapshot,
204+
state: &TransactionState,
205205
) -> Result<Completed, Error> {
206206
self.compatible.verify()?;
207207
self.time_relative.verify()?;
208208
self.capacity.verify()?;
209209
let cycles = if skip_script_verify {
210210
0
211211
} else {
212-
self.script.complete(snapshot, max_cycles)?
212+
self.script.complete(state, max_cycles)?
213213
};
214214
let fee = self.fee_calculator.transaction_fee()?;
215215
Ok(Completed { cycles, fee })

0 commit comments

Comments
 (0)