diff --git a/script/src/lib.rs b/script/src/lib.rs index e979f3e411..3158a281b4 100644 --- a/script/src/lib.rs +++ b/script/src/lib.rs @@ -12,7 +12,7 @@ pub use crate::error::{ScriptError, TransactionScriptError}; pub use crate::scheduler::{Scheduler, ROOT_VM_ID}; pub use crate::types::{ ChunkCommand, CoreMachine, DataPieceId, RunMode, ScriptGroup, ScriptGroupType, ScriptVersion, - TransactionSnapshot, TransactionState, TxData, VerifyResult, VmIsa, VmState, VmVersion, + TransactionState, TxData, VerifyResult, VmIsa, VmState, VmVersion, }; pub use crate::verify::{TransactionScriptsSyscallsGenerator, TransactionScriptsVerifier}; pub use crate::verify_env::TxVerifyEnv; diff --git a/script/src/types.rs b/script/src/types.rs index 728cb7aa55..f2c28b1491 100644 --- a/script/src/types.rs +++ b/script/src/types.rs @@ -1,4 +1,3 @@ -use ckb_error::Error; use ckb_types::{ core::{Cycle, ScriptHashType}, packed::{Byte32, Script}, @@ -186,20 +185,7 @@ impl fmt::Display for ScriptGroupType { } /// Struct specifies which script has verified so far. -/// Snapshot is lifetime free, but capture snapshot need heavy memory copy -pub struct TransactionSnapshot { - /// current suspended script index - pub current: usize, - /// vm snapshots - pub state: Option, - /// current consumed cycle - pub current_cycles: Cycle, - /// limit cycles when snapshot create - pub limit_cycles: Cycle, -} - -/// Struct specifies which script has verified so far. -/// State lifetime bound with vm machine. +/// State is lifetime free, but capture snapshot need heavy memory copy pub struct TransactionState { /// current suspended script index pub current: usize, @@ -240,41 +226,6 @@ impl TransactionState { } } -impl TransactionSnapshot { - /// Return next limit cycles according to max_cycles and step_cycles - pub fn next_limit_cycles(&self, step_cycles: Cycle, max_cycles: Cycle) -> (Cycle, bool) { - let remain = max_cycles - self.current_cycles; - let next_limit = self.limit_cycles + step_cycles; - - if next_limit < remain { - (next_limit, false) - } else { - (remain, true) - } - } -} - -impl TryFrom for TransactionSnapshot { - type Error = Error; - - fn try_from(state: TransactionState) -> Result { - let TransactionState { - current, - state, - current_cycles, - limit_cycles, - .. - } = state; - - Ok(TransactionSnapshot { - current, - state, - current_cycles, - limit_cycles, - }) - } -} - /// Enum represent resumable verify result #[allow(clippy::large_enum_variant)] #[derive(Debug)] @@ -285,16 +236,6 @@ pub enum VerifyResult { Suspended(TransactionState), } -impl std::fmt::Debug for TransactionSnapshot { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("TransactionSnapshot") - .field("current", &self.current) - .field("current_cycles", &self.current_cycles) - .field("limit_cycles", &self.limit_cycles) - .finish() - } -} - impl std::fmt::Debug for TransactionState { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> std::fmt::Result { f.debug_struct("TransactionState") diff --git a/script/src/verify.rs b/script/src/verify.rs index 141910fc25..6eac6981d6 100644 --- a/script/src/verify.rs +++ b/script/src/verify.rs @@ -15,7 +15,7 @@ use crate::{ type_id::TypeIdSystemScript, types::{ CoreMachine, DebugPrinter, Indices, ScriptGroup, ScriptGroupType, ScriptVersion, - TransactionSnapshot, TransactionState, VerifyResult, + TransactionState, VerifyResult, }, verify_env::TxVerifyEnv, }; @@ -730,7 +730,7 @@ where /// /// ## Params /// - /// * `snap` - Captured transaction verification snapshot. + /// * `snap` - Captured transaction verification state. /// /// * `limit_cycles` - Maximum allowed cycles to run the scripts. The verification quits early /// when the consumed cycles exceed the limit. @@ -741,7 +741,7 @@ where /// If verify is suspended, a borrowed state will returned. pub fn resume_from_snap( &self, - snap: &TransactionSnapshot, + snap: &TransactionState, limit_cycles: Cycle, ) -> Result { let mut cycles = snap.current_cycles; @@ -881,7 +881,7 @@ where /// /// ## Params /// - /// * `snap` - Captured transaction verification snapshot. + /// * `snap` - Captured transaction verification state. /// /// * `max_cycles` - Maximum allowed cycles to run the scripts. The verification quits early /// when the consumed cycles exceed the limit. @@ -889,7 +889,7 @@ where /// ## Returns /// /// It returns the total consumed cycles on completed, Otherwise it returns the verification error. - pub fn complete(&self, snap: &TransactionSnapshot, max_cycles: Cycle) -> Result { + pub fn complete(&self, snap: &TransactionState, max_cycles: Cycle) -> Result { let mut cycles = snap.current_cycles; let (_hash, current_group) = self.groups().nth(snap.current).ok_or_else(|| { diff --git a/script/src/verify/tests/ckb_latest/features_since_v2021.rs b/script/src/verify/tests/ckb_latest/features_since_v2021.rs index cb70fb0b4c..f18494a601 100644 --- a/script/src/verify/tests/ckb_latest/features_since_v2021.rs +++ b/script/src/verify/tests/ckb_latest/features_since_v2021.rs @@ -894,11 +894,11 @@ fn _check_typical_secp256k1_blake160_2_in_2_out_tx_with_snap(step_cycles: Cycle) let result = verifier.verify_map(script_version, &rtx, |verifier| { #[allow(unused_assignments)] - let mut init_snap: Option = None; + let mut init_snap: Option = None; let mut init_state: Option = None; match verifier.resumable_verify(step_cycles).unwrap() { - VerifyResult::Suspended(state) => init_snap = Some(state.try_into().unwrap()), + VerifyResult::Suspended(state) => init_snap = Some(state), VerifyResult::Completed(cycle) => return Ok(cycle), } @@ -911,7 +911,7 @@ fn _check_typical_secp256k1_blake160_2_in_2_out_tx_with_snap(step_cycles: Cycle) match verifier.resume_from_snap(&snap, limit_cycles).unwrap() { VerifyResult::Suspended(state) => { if count % 500 == 0 { - init_snap = Some(state.try_into().unwrap()); + init_snap = Some(state); } else { init_state = Some(state); } @@ -928,7 +928,7 @@ fn _check_typical_secp256k1_blake160_2_in_2_out_tx_with_snap(step_cycles: Cycle) match verifier.resume_from_state(state, limit_cycles).unwrap() { VerifyResult::Suspended(state) => { if count % 500 == 0 { - init_snap = Some(state.try_into().unwrap()); + init_snap = Some(state); } else { init_state = Some(state); } @@ -983,13 +983,13 @@ fn check_typical_secp256k1_blake160_2_in_2_out_tx_with_complete() { let mut cycles = 0; let verifier = TransactionScriptsVerifierWithEnv::new(); let result = verifier.verify_map(script_version, &rtx, |verifier| { - let mut init_snap: Option = None; + let mut init_snap: Option = None; if let VerifyResult::Suspended(state) = verifier .resumable_verify(TWO_IN_TWO_OUT_CYCLES / 10) .unwrap() { - init_snap = Some(state.try_into().unwrap()); + init_snap = Some(state); } for _ in 0..2 { @@ -997,7 +997,7 @@ fn check_typical_secp256k1_blake160_2_in_2_out_tx_with_complete() { let (limit_cycles, _last) = snap.next_limit_cycles(TWO_IN_TWO_OUT_CYCLES / 10, TWO_IN_TWO_OUT_CYCLES); match verifier.resume_from_snap(&snap, limit_cycles).unwrap() { - VerifyResult::Suspended(state) => init_snap = Some(state.try_into().unwrap()), + VerifyResult::Suspended(state) => init_snap = Some(state), VerifyResult::Completed(_) => { unreachable!() } @@ -1133,10 +1133,10 @@ fn load_code_with_snapshot() { let max_cycles = Cycle::MAX; let verifier = TransactionScriptsVerifierWithEnv::new(); let result = verifier.verify_map(script_version, &rtx, |verifier| { - let mut init_snap: Option = None; + let mut init_snap: Option = None; if let VerifyResult::Suspended(state) = verifier.resumable_verify(max_cycles).unwrap() { - init_snap = Some(state.try_into().unwrap()); + init_snap = Some(state); } let snap = init_snap.take().unwrap(); @@ -1232,10 +1232,10 @@ fn load_code_with_snapshot_more_times() { let verifier = TransactionScriptsVerifierWithEnv::new(); verifier.verify_map(script_version, &rtx, |verifier| { - let mut init_snap: Option = None; + let mut init_snap: Option = None; if let VerifyResult::Suspended(state) = verifier.resumable_verify(max_cycles).unwrap() { - init_snap = Some(state.try_into().unwrap()); + init_snap = Some(state); } loop { @@ -1244,7 +1244,7 @@ fn load_code_with_snapshot_more_times() { match result.unwrap() { VerifyResult::Suspended(state) => { - init_snap = Some(state.try_into().unwrap()); + init_snap = Some(state); } VerifyResult::Completed(cycle) => { cycles = cycle; diff --git a/script/src/verify/tests/ckb_latest/features_since_v2023.rs b/script/src/verify/tests/ckb_latest/features_since_v2023.rs index ddb0f0627d..7ab47dfc9a 100644 --- a/script/src/verify/tests/ckb_latest/features_since_v2023.rs +++ b/script/src/verify/tests/ckb_latest/features_since_v2023.rs @@ -597,8 +597,7 @@ fn check_spawn_state() { loop { times += 1; - let state: TransactionSnapshot = - init_state.take().unwrap().try_into().expect("no snapshot"); + let state: TransactionState = init_state.take().unwrap(); match verifier.resume_from_snap(&state, max_cycles).unwrap() { VerifyResult::Suspended(state) => { init_state = Some(state); diff --git a/script/src/verify/tests/utils.rs b/script/src/verify/tests/utils.rs index 760921f627..035b8c3da9 100644 --- a/script/src/verify/tests/utils.rs +++ b/script/src/verify/tests/utils.rs @@ -29,7 +29,7 @@ use ckb_types::{ }; use faster_hex::hex_encode; use std::sync::Arc; -use std::{convert::TryInto as _, fs::File, path::Path}; +use std::{fs::File, path::Path}; use tempfile::TempDir; use crate::verify::*; @@ -236,7 +236,7 @@ impl TransactionScriptsVerifierWithEnv { times += 1; let mut init_snap = match verifier.resumable_verify(max_cycles).unwrap() { - VerifyResult::Suspended(state) => Some(state.try_into().unwrap()), + VerifyResult::Suspended(state) => Some(state), VerifyResult::Completed(cycle) => { cycles = cycle; return Ok((cycles, times)); @@ -248,7 +248,7 @@ impl TransactionScriptsVerifierWithEnv { let snap = init_snap.take().unwrap(); match verifier.resume_from_snap(&snap, max_cycles) { Ok(VerifyResult::Suspended(state)) => { - init_snap = Some(state.try_into().unwrap()); + init_snap = Some(state); } Ok(VerifyResult::Completed(cycle)) => { cycles = cycle; diff --git a/tx-pool/src/chunk_process.rs b/tx-pool/src/chunk_process.rs index d5837cf1cc..0f101feab2 100644 --- a/tx-pool/src/chunk_process.rs +++ b/tx-pool/src/chunk_process.rs @@ -16,7 +16,7 @@ use ckb_verification::cache::TxVerificationCache; use ckb_verification::{ cache::{CacheEntry, Completed}, ContextualWithoutScriptTransactionVerifier, DaoScriptSizeVerifier, ScriptError, ScriptVerifier, - ScriptVerifyResult, ScriptVerifyState, TimeRelativeTransactionVerifier, TransactionSnapshot, + ScriptVerifyResult, ScriptVerifyState, TimeRelativeTransactionVerifier, TransactionState, TxVerifyEnv, }; use std::sync::Arc; @@ -37,7 +37,7 @@ pub(crate) enum ChunkCommand { enum State { Stopped, - Suspended(Arc), + Suspended(Arc), Completed(Cycle), } @@ -133,7 +133,7 @@ impl ChunkProcess { &mut self, rtx: Arc, data_loader: DL, - mut init_snap: Option>, + mut init_snap: Option>, max_cycles: Cycle, consensus: Arc, tx_env: Arc, diff --git a/verification/src/cache.rs b/verification/src/cache.rs index 88a14364c2..4a9948b072 100644 --- a/verification/src/cache.rs +++ b/verification/src/cache.rs @@ -1,6 +1,6 @@ //! TX verification cache -use ckb_script::TransactionSnapshot; +use ckb_script::TransactionState; use ckb_types::{ core::{Capacity, Cycle, EntryCompleted}, packed::Byte32, @@ -25,8 +25,8 @@ pub type CacheEntry = Completed; pub struct Suspended { /// Cached tx fee pub fee: Capacity, - /// Snapshot - pub snap: Arc, + /// State + pub state: Arc, } /// Completed entry diff --git a/verification/src/lib.rs b/verification/src/lib.rs index 7ae9bab362..2ed5df3a08 100644 --- a/verification/src/lib.rs +++ b/verification/src/lib.rs @@ -26,8 +26,8 @@ pub use crate::transaction_verifier::{ TimeRelativeTransactionVerifier, }; pub use ckb_script::{ - ScriptError, ScriptGroupType, TransactionSnapshot, TransactionState as ScriptVerifyState, - TxVerifyEnv, VerifyResult as ScriptVerifyResult, + ScriptError, ScriptGroupType, TransactionState as ScriptVerifyState, TxVerifyEnv, + VerifyResult as ScriptVerifyResult, }; /// Maximum amount of time that a block timestamp is allowed to exceed the diff --git a/verification/src/transaction_verifier.rs b/verification/src/transaction_verifier.rs index 62be30fcfb..0b90b897a9 100644 --- a/verification/src/transaction_verifier.rs +++ b/verification/src/transaction_verifier.rs @@ -7,7 +7,7 @@ use ckb_dao_utils::DaoError; use ckb_error::Error; #[cfg(not(target_family = "wasm"))] use ckb_script::ChunkCommand; -use ckb_script::{TransactionScriptsVerifier, TransactionSnapshot}; +use ckb_script::{TransactionScriptsVerifier, TransactionState}; use ckb_traits::{ CellDataProvider, EpochProvider, ExtensionProvider, HeaderFieldsProvider, HeaderProvider, }; @@ -201,7 +201,7 @@ where &self, max_cycles: Cycle, skip_script_verify: bool, - snapshot: &TransactionSnapshot, + state: &TransactionState, ) -> Result { self.compatible.verify()?; self.time_relative.verify()?; @@ -209,7 +209,7 @@ where let cycles = if skip_script_verify { 0 } else { - self.script.complete(snapshot, max_cycles)? + self.script.complete(state, max_cycles)? }; let fee = self.fee_calculator.transaction_fee()?; Ok(Completed { cycles, fee })