Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parallel verification with Rayon for script groups. #4551

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ckb-logger = { path = "../util/logger", version = "= 0.117.0-pre", optional = tr
serde = { version = "1.0", features = ["derive"] }
ckb-error = { path = "../error", version = "= 0.117.0-pre" }
ckb-chain-spec = { path = "../spec", version = "= 0.117.0-pre" }
rayon = "1.10.0"

[dev-dependencies]
proptest = "1.0"
Expand Down
60 changes: 35 additions & 25 deletions script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use ckb_vm::{
snapshot::{resume, Snapshot},
DefaultMachineBuilder, Error as VMInternalError, SupportMachine, Syscalls,
};
use std::cell::RefCell;
use rayon::prelude::*;
use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -67,29 +67,38 @@ enum DataGuard {
Loaded(Bytes),
}

impl PartialEq for LazyData {
fn eq(&self, other: &Self) -> bool {
let self_guard = self.0.lock().unwrap();
let other_guard = other.0.lock().unwrap();
*self_guard == *other_guard
}
}
impl Eq for LazyData {}

/// LazyData wrapper make sure not-loaded data will be loaded only after one access
#[derive(Debug, PartialEq, Eq, Clone)]
struct LazyData(RefCell<DataGuard>);
#[derive(Debug, Clone)]
struct LazyData(Arc<Mutex<DataGuard>>);

impl LazyData {
fn from_cell_meta(cell_meta: &CellMeta) -> LazyData {
match &cell_meta.mem_cell_data {
Some(data) => LazyData(RefCell::new(DataGuard::Loaded(data.to_owned()))),
None => LazyData(RefCell::new(DataGuard::NotLoaded(
Some(data) => LazyData(Arc::new(Mutex::new(DataGuard::Loaded(data.to_owned())))),
None => LazyData(Arc::new(Mutex::new(DataGuard::NotLoaded(
cell_meta.out_point.clone(),
))),
)))),
}
}

fn access<DL: CellDataProvider>(&self, data_loader: &DL) -> Bytes {
let guard = self.0.borrow().to_owned();
match guard {
let mut guard = self.0.lock().expect("lazy data poisoned");
match &*guard {
DataGuard::Loaded(bytes) => bytes.clone(),
DataGuard::NotLoaded(out_point) => {
let data = data_loader.get_cell_data(&out_point).expect("cell data");
self.0.replace(DataGuard::Loaded(data.to_owned()));
let data = data_loader.get_cell_data(out_point).expect("cell data");
*guard = DataGuard::Loaded(data.clone());
data
}
DataGuard::Loaded(bytes) => bytes,
}
}
}
Expand Down Expand Up @@ -605,22 +614,23 @@ impl<DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + C
///
/// It returns the total consumed cycles on success, Otherwise it returns the verification error.
pub fn verify(&self, max_cycles: Cycle) -> Result<Cycle, Error> {
let mut cycles: Cycle = 0;

// Now run each script group
for (_hash, group) in self.groups() {
let cycles = std::sync::atomic::AtomicU64::new(0);
self.groups().par_bridge().try_for_each(|(_hash, group)| {
// max_cycles must reduce by each group exec
let used_cycles = self
.verify_script_group(group, max_cycles - cycles)
.map_err(|e| {
#[cfg(feature = "logging")]
logging::on_script_error(_hash, &self.hash(), &e);
e.source(group)
})?;
let used_cycles = self.verify_script_group(group, max_cycles).map_err(|e| {
#[cfg(feature = "logging")]
logging::on_script_error(_hash, &self.hash(), &e);
e.source(group)
})?;

cycles = wrapping_cycles_add(cycles, used_cycles, group)?;
}
Ok(cycles)
let old_sum_cycles = cycles.fetch_add(used_cycles, std::sync::atomic::Ordering::SeqCst);
let sum_cycles = cycles.load(std::sync::atomic::Ordering::SeqCst);
if sum_cycles > max_cycles {
return Err(ScriptError::CyclesOverflow(old_sum_cycles, sum_cycles).source(group));
}
Ok(())
})?;
Ok(cycles.load(std::sync::atomic::Ordering::SeqCst))
}

/// Performing a resumable verification on the transaction scripts.
Expand Down
Loading