Skip to content

Commit

Permalink
fix dead lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Apr 17, 2024
1 parent a379824 commit 71705dc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub static ROOCH_LOCAL_GENESIS: Lazy<RoochGenesis> = Lazy::new(|| {
RoochGenesis::build_with_option(
RoochChainID::LOCAL.genesis_ctx(mock_sequencer),
bitcoin_genesis_ctx,
BuildOption::Fresh,
BuildOption::Release,
)
.expect("build rooch genesis failed")
});
Expand Down
4 changes: 3 additions & 1 deletion crates/rooch-integration-test-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use rooch_genesis::{FrameworksGasParameters, RoochGenesis};
use rooch_types::function_arg::FunctionArg;
use std::path::PathBuf;
use std::{collections::BTreeMap, path::Path};
use tracing::debug;

pub struct MoveOSTestRunner<'a> {
compiled_state: CompiledState<'a>,
Expand Down Expand Up @@ -134,7 +135,7 @@ impl<'a> MoveOSTestAdapter<'a> for MoveOSTestRunner<'a> {
moveos,
root: ObjectEntity::root_object(state_root, size),
};

debug!("init moveos test adapter");
(adapter, None)
}

Expand All @@ -145,6 +146,7 @@ impl<'a> MoveOSTestAdapter<'a> for MoveOSTestRunner<'a> {
_gas_budget: Option<u64>,
_extra: Option<Self::ExtraPublishArgs>,
) -> anyhow::Result<(Option<String>, move_binary_format::CompiledModule)> {
debug!("publish module: {}", module.self_id());
let mut module_bytes = vec![];
module.serialize(&mut module_bytes)?;

Expand Down
40 changes: 28 additions & 12 deletions moveos/moveos/src/moveos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ impl MoveOS {
system_pre_execute_functions: Vec<FunctionCall>,
system_post_execute_functions: Vec<FunctionCall>,
) -> Result<Self> {
//TODO load the gas table from argument, and remove the cost_table lock.

let vm = MoveOSVM::new(natives, config.vm_config)?;
Ok(Self {
vm,
Expand Down Expand Up @@ -178,19 +180,33 @@ impl MoveOS {
}

fn load_cost_table(&self, root: &RootObjectEntity) -> VMResult<CostTable> {
if let Some(cost_table) = self.cost_table.read().as_ref() {
Ok(cost_table.clone())
} else {
let resolver = RootObjectResolver::new(root.clone(), &self.db);
let gas_entries = get_gas_schedule_entries(&resolver).map_err(|e| {
PartialVMError::new(StatusCode::STORAGE_ERROR)
.with_message(format!("Load gas schedule entries failed: {}", e))
.finish(Location::Undefined)
})?;
let cost_table = initial_cost_schedule(gas_entries);
self.cost_table.write().replace(cost_table.clone());
Ok(cost_table)
// We use a scoped lock here to avoid holding the lock for a long time.
{
let rlock = self.cost_table.read();
if let Some(cost_table) = rlock.as_ref() {
return Ok(cost_table.clone());
}
}

if log::log_enabled!(log::Level::Trace) {
log::trace!("load_cost_table from db");
}
let resolver = RootObjectResolver::new(root.clone(), &self.db);
let gas_entries = get_gas_schedule_entries(&resolver).map_err(|e| {
PartialVMError::new(StatusCode::STORAGE_ERROR)
.with_message(format!("Load gas schedule entries failed: {}", e))
.finish(Location::Undefined)
})?;
let cost_table = initial_cost_schedule(gas_entries);
match self.cost_table.try_write() {
Some(mut w) => {
w.replace(cost_table.clone());
}
None => {
log::warn!("load_cost_table try_write failed");
}
}
Ok(cost_table)
}

pub fn state(&self) -> &StateDBStore {
Expand Down

0 comments on commit 71705dc

Please sign in to comment.