Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Remove instance drop impl #1782

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/conductor_lib/src/holochain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ use holochain_core_types::{
use holochain_json_api::json::JsonString;

use holochain_core::{
state::StateWrapper,
state::State,
state_dump::{address_to_content_and_type, StateDump},
};
use holochain_persistence_api::cas::content::Address;
Expand Down Expand Up @@ -252,7 +252,7 @@ impl Holochain {
}

/// return
pub fn state(&self) -> Result<StateWrapper, HolochainInstanceError> {
pub fn state(&self) -> Result<State, HolochainInstanceError> {
self.check_instance()?;
Ok(self.instance.as_ref().unwrap().state().clone())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pub mod actions;
pub mod chain_store;
pub mod state;

use crate::state::StateWrapper;
use crate::state::State;
use holochain_core_types::{chain_header::ChainHeader, entry::Entry};

use holochain_persistence_api::cas::content::AddressableContent;

pub fn find_chain_header(entry: &Entry, state: &StateWrapper) -> Option<ChainHeader> {
pub fn find_chain_header(entry: &Entry, state: &State) -> Option<ChainHeader> {
let chain = state.agent().chain_store();
let top_header = state.agent().top_chain_header();
chain
Expand Down
15 changes: 7 additions & 8 deletions crates/core/src/agent/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
};
use holochain_persistence_api::cas::content::{Address, AddressableContent, Content};

use crate::state::StateWrapper;
use holochain_core_types::{
agent::AgentId,
chain_header::ChainHeader,
Expand Down Expand Up @@ -135,8 +134,8 @@ impl AgentStateSnapshot {
}
}

impl From<&StateWrapper> for AgentStateSnapshot {
fn from(state: &StateWrapper) -> Self {
impl From<&State> for AgentStateSnapshot {
fn from(state: &State) -> Self {
let agent = &*(state.agent());
let top_chain = agent.top_chain_header();
AgentStateSnapshot::new(top_chain)
Expand Down Expand Up @@ -175,14 +174,14 @@ pub enum ActionResponse {
pub fn create_new_chain_header(
entry: &Entry,
agent_state: &AgentState,
root_state: &StateWrapper,
root_state: &State,
crud_link: &Option<Address>,
provenances: &Vec<Provenance>,
) -> Result<ChainHeader, HolochainError> {
let agent_address = agent_state.get_agent_address()?;
let signature = Signature::from(
root_state
.conductor_api()
.conductor_api
.execute(entry.address().to_string(), CryptoMethod::Sign)?,
// Temporarily replaced by error handling for Holo hack signing.
// TODO: pull in the expect below after removing the Holo signing hack again
Expand Down Expand Up @@ -217,7 +216,7 @@ pub fn create_new_chain_header(
/// Since published headers are treated as entries, the header must also
/// have its own header!
pub fn create_entry_with_header_for_header(
root_state: &StateWrapper,
root_state: &State,
chain_header: ChainHeader,
) -> Result<EntryWithHeader, HolochainError> {
let entry = Entry::ChainHeader(chain_header);
Expand All @@ -241,7 +240,7 @@ fn reduce_commit_entry(
let result = create_new_chain_header(
&entry,
agent_state,
&StateWrapper::from(root_state.clone()),
&root_state,
&maybe_link_update_delete,
provenances,
)
Expand Down Expand Up @@ -435,7 +434,7 @@ pub mod tests {
let header = create_new_chain_header(
&test_entry(),
&agent_state,
&StateWrapper::from(state),
&state,
&None,
&vec![],
)
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use crossbeam_channel::{unbounded, Receiver, Sender};
use futures::{task::Poll, Future};

use crate::state::StateWrapper;
use crate::state::State;
use futures::task::noop_waker_ref;
use holochain_conductor_lib_api::ConductorApi;
use holochain_core_types::{
Expand Down Expand Up @@ -75,7 +75,7 @@ pub struct Context {
pub(crate) instance_name: String,
pub agent_id: AgentId,
pub persister: Arc<RwLock<dyn Persister>>,
state: Option<Arc<RwLock<StateWrapper>>>,
state: Option<Arc<RwLock<State>>>,
pub action_channel: Option<Sender<ActionWrapper>>,
pub observer_channel: Option<Sender<Observer>>,
pub chain_storage: Arc<RwLock<dyn ContentAddressableStorage>>,
Expand Down Expand Up @@ -185,19 +185,19 @@ impl Context {
self.instance_name.clone()
}

pub fn set_state(&mut self, state: Arc<RwLock<StateWrapper>>) {
pub fn set_state(&mut self, state: Arc<RwLock<State>>) {
self.state = Some(state);
}

pub fn state(&self) -> Option<RwLockReadGuard<StateWrapper>> {
pub fn state(&self) -> Option<RwLockReadGuard<State>> {
self.state.as_ref().map(|s| s.read().unwrap())
}

/// Try to acquire read-lock on the state.
/// Returns immediately either with the lock or with None if the lock
/// is occupied already.
/// Also returns None if the context was not initialized with a state.
pub fn try_state(&self) -> Option<RwLockReadGuard<StateWrapper>> {
pub fn try_state(&self) -> Option<RwLockReadGuard<State>> {
self.state.as_ref().map(|s| s.try_read()).unwrap_or(None)
}

Expand Down Expand Up @@ -465,7 +465,7 @@ pub mod tests {
false,
);

let global_state = Arc::new(RwLock::new(StateWrapper::new(Arc::new(context.clone()))));
let global_state = Arc::new(RwLock::new(State::new(Arc::new(context.clone()))));
context.set_state(global_state.clone());

{
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/dht/dht_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use holochain_persistence_api::{
};
use regex::Regex;

use crate::state::StateWrapper;
use crate::state::State;
use holochain_json_api::error::JsonResult;
use holochain_persistence_api::cas::content::Content;
use std::{
Expand Down Expand Up @@ -59,8 +59,8 @@ pub struct DhtStoreSnapshot {
pub holding_list: Vec<Address>,
}

impl From<&StateWrapper> for DhtStoreSnapshot {
fn from(state: &StateWrapper) -> Self {
impl From<&State> for DhtStoreSnapshot {
fn from(state: &State) -> Self {
DhtStoreSnapshot {
holding_list: state.dht().holding_list.clone(),
}
Expand Down
59 changes: 33 additions & 26 deletions crates/core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
persister::Persister,
scheduled_jobs,
signal::Signal,
state::{State, StateWrapper},
state::State,
workflows::application,
};
#[cfg(test)]
Expand Down Expand Up @@ -41,7 +41,7 @@ pub const RECV_DEFAULT_TIMEOUT_MS: Duration = Duration::from_millis(10000);
#[derive(Clone)]
pub struct Instance {
/// The object holding the state. Actions go through the store sequentially.
state: Arc<RwLock<StateWrapper>>,
state: Arc<RwLock<State>>,
action_channel: Option<Sender<ActionWrapper>>,
observer_channel: Option<Sender<Observer>>,
scheduler_handle: Option<Arc<ScheduleHandle>>,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl Instance {
) -> Vec<Observer> {
// Mutate state
{
let new_state: StateWrapper;
let new_state: State;

// Get write lock
let mut state = self
Expand Down Expand Up @@ -288,7 +288,7 @@ impl Instance {
/// Creates a new Instance with no channels set up.
pub fn new(context: Arc<Context>) -> Self {
Instance {
state: Arc::new(RwLock::new(StateWrapper::new(context.clone()))),
state: Arc::new(RwLock::new(State::new(context.clone()))),
action_channel: None,
observer_channel: None,
scheduler_handle: None,
Expand All @@ -300,7 +300,7 @@ impl Instance {

pub fn from_state(state: State, context: Arc<Context>) -> Self {
Instance {
state: Arc::new(RwLock::new(StateWrapper::from(state))),
state: Arc::new(RwLock::new(state)),
action_channel: None,
observer_channel: None,
scheduler_handle: None,
Expand All @@ -310,7 +310,7 @@ impl Instance {
}
}

pub fn state(&self) -> RwLockReadGuard<StateWrapper> {
pub fn state(&self) -> RwLockReadGuard<State> {
self.state
.read()
.expect("owners of the state RwLock shouldn't panic")
Expand All @@ -335,16 +335,6 @@ impl Instance {
}
}

impl Drop for Instance {
fn drop(&mut self) {
// TODO: this is already performed in Holochain::stop explicitly,
// can we get rid of one or the other?
let _ = self.shutdown_network();
self.stop_action_loop();
self.state.write().unwrap().drop_inner_state();
}
}

/*impl Default for Instance {
fn default(context:Context) -> Self {
Self::new(context)
Expand All @@ -361,7 +351,7 @@ pub fn dispatch_action_and_wait(context: Arc<Context>, action_wrapper: ActionWra
dispatch_action(context.action_channel(), action_wrapper.clone());

loop {
if context.state().unwrap().history().contains(&action_wrapper) {
if context.state().unwrap().history.contains(&action_wrapper) {
return;
} else {
let _ = tick_rx.recv_timeout(Duration::from_millis(10));
Expand Down Expand Up @@ -518,7 +508,7 @@ pub mod tests {
None,
false,
);
let global_state = Arc::new(RwLock::new(StateWrapper::new(Arc::new(context.clone()))));
let global_state = Arc::new(RwLock::new(State::new(Arc::new(context.clone()))));
context.set_state(global_state.clone());
Arc::new(context)
}
Expand Down Expand Up @@ -551,7 +541,7 @@ pub mod tests {
Some(chain_header),
context.agent_id.address(),
);
let state = StateWrapper::new_with_agent(Arc::new(context.clone()), agent_state);
let state = State::new_with_agent(Arc::new(context.clone()), agent_state);
let global_state = Arc::new(RwLock::new(state));
context.set_state(global_state.clone());
Arc::new(context)
Expand Down Expand Up @@ -605,7 +595,7 @@ pub mod tests {
// @see https://github.com/holochain/holochain-rust/issues/195
while instance
.state()
.history()
.history
.iter()
.find(|aw| match aw.action() {
Action::InitializeChain(_) => true,
Expand All @@ -619,7 +609,7 @@ pub mod tests {

while instance
.state()
.history()
.history
.iter()
.find(|aw| match aw.action() {
Action::Commit((entry, _, _)) => {
Expand All @@ -640,7 +630,7 @@ pub mod tests {

while instance
.state()
.history()
.history
.iter()
.find(|aw| match aw.action() {
Action::ReturnInitializationResult(_) => true,
Expand All @@ -663,6 +653,23 @@ pub mod tests {
test_instance(dna, None).expect("Blank instance could not be initialized!")
}

#[test]
pub fn can_clone_instance() {
let instance = test_instance_blank();
{
let _instance2 = instance.clone();
}
// wait for the action thread to receive kill signal, if applicable
std::thread::sleep(Duration::from_secs(2));
instance.action_channel().send(ActionWrapper::new(Action::Ping)).unwrap();
}

#[test]
pub fn can_ping_instance() {
let instance = test_instance_blank();
instance.action_channel().send(ActionWrapper::new(Action::Ping)).unwrap();
}

#[test]
/// This tests calling `process_action`
/// with an action that dispatches no new ones.
Expand Down Expand Up @@ -833,10 +840,10 @@ pub mod tests {
instance.process_action(&commit_action, state_observers, &rx_observer, &context);

// Check if AgentIdEntry is found
assert_eq!(1, instance.state().history().iter().count());
assert_eq!(1, instance.state().history.iter().count());
instance
.state()
.history()
.history
.iter()
.find(|aw| match aw.action() {
Action::Commit((entry, _, _)) => {
Expand Down Expand Up @@ -871,10 +878,10 @@ pub mod tests {
);

// Check if AgentIdEntry is found
assert_eq!(1, instance.state().history().iter().count());
assert_eq!(1, instance.state().history.iter().count());
instance
.state()
.history()
.history
.iter()
.find(|aw| match aw.action() {
Action::Commit((entry, _, _)) => {
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/link_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ pub mod tests {
let context = instance.initialize_context(context);
instance.process_action(&commit_action, state_observers, &rx_observer, &context);
// Check if LinkEntry is found
assert_eq!(1, instance.state().history().iter().count());
assert_eq!(1, instance.state().history.iter().count());
instance
.state()
.history()
.history
.iter()
.find(|aw| match aw.action() {
Action::Commit((entry, _, _)) => {
Expand Down Expand Up @@ -100,10 +100,10 @@ pub mod tests {
let context = instance.initialize_context(context);
instance.process_action(&commit_action, state_observers, &rx_observer, &context);
// Check if LinkEntry is found
assert_eq!(1, instance.state().history().iter().count());
assert_eq!(1, instance.state().history.iter().count());
instance
.state()
.history()
.history
.iter()
.find(|aw| match aw.action() {
Action::Commit((entry, _, _)) => {
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/network/actions/shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use holochain_core_types::{
sync::HcRwLock as RwLock,
};

use crate::state::StateWrapper;
use crate::state::State;
use std::{pin::Pin, sync::Arc};

/// Shutdown the network
/// This tells the network to untrack this instance and then stops the network thread
/// and sets the P2pNetwork instance in the state to None.
pub async fn shutdown(
state: Arc<RwLock<StateWrapper>>,
state: Arc<RwLock<State>>,
action_channel: Sender<ActionWrapper>,
) -> HcResult<()> {
if state.read().unwrap().network().initialized().is_ok() {
Expand All @@ -32,7 +32,7 @@ pub async fn shutdown(
}

pub struct ShutdownFuture {
state: Arc<RwLock<StateWrapper>>,
state: Arc<RwLock<State>>,
}

impl Future for ShutdownFuture {
Expand Down